Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 2-Clause License

Copyright (c) 2021, Alex
Copyright (c) 2021, alexpdev
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
# Python Piece Length
# Piece Length

Calculate ideal piece length for .torrent files.

Piece Length(`piecelength`) is a simple package that sole purpose is to calculate
the ideal piece length for the Bittorrent protocol based on the total size of
the torrent contents. Results are returned in integer form, and will always
be a perfect power of 2.

## Install

The `piecelength` package can be installed from git:

```sh
git clone https://github.com/alexpdev/piecelength.git
cd piecelength
pip install .
```

It is also available on PyPi:

```sh
pip install piecelength
```

## Usage

To use the package as library:

```python
from piecelength import piece_length

size = 100000000 # some integer value
result = piece_length(size)
```

It can also be used from the command line:

```sh
piecelength 3456677434645
```

## License

Licensed with BSD 3 see the LICENSE file for more details.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"name": "piecelength",
"version": "1.1",
"version": "1.2",
"displayName": "piecelength",
"description": "'.torrent' metafile ideal piecelength for small file size calculator.",
"scripts": {
"test": "python ./tests/test_piecelength.py"
},
"email": "alexpdev@protonmail.com",
"readme": "./README.md",
"keywords": ["bittorrent","metafiles"],
Expand Down
32 changes: 32 additions & 0 deletions piecelength/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
#! /usr/bin/python3
# -*- coding: utf-8 -*-

###############################################################################
# BSD 2-Clause License
#
# Copyright (c) 2021, alexpdev
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
"""Package initializer for piecelength project."""

from piecelength.piecelength import piece_length


Expand Down
47 changes: 45 additions & 2 deletions piecelength/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
#! /usr/bin/python3
# -*- coding: utf-8 -*-

###############################################################################
# BSD 2-Clause License
#
# Copyright (c) 2021, alexpdev
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
"""Entry point for Command Line Interface."""

import sys
from piecelength import piece_length


def main():
"""Use as entrypoint for CLI."""
try:
arg = int(sys.argv[1])
print(piece_length(arg))
except TypeError:
print("The first argument must be an integer.")
except IndexError:
print("Missing size argument.")


if __name__ == '__main__':
args = sys.argv[1]
sys.stdout.write(piece_length(args))
main()
14 changes: 8 additions & 6 deletions piecelength/piecelength.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
###############################################################################
# BSD 2-Clause License
#
# Copyright (c) 2021, AlexpDev
# Copyright (c) 2021, alexpdev
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand All @@ -28,29 +28,31 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
"""Module containing the logic for the piecelength package."""

def piece_length(size):

def piece_length(size: int) -> int:
"""
Calculate the ideal piece length for bittorrent data.

Piece Length is required to be a power of 2, and must be larger than 16KiB.
Not all clients support the same max length so to be safe it is set at 8MiB.

Args
-----------
----
size: int
- total bits of all files incluided in .torrent file

Returns
-----------
-------
int:
- the ideal piece length calculated from the size arguement
"""
# Smallest supported piece length is 16KiB
exp = 14
# Find the largest piece size possible less than size / 10
# 8MiB largest possible piece size
while (2**exp)*10 < size and exp < 23:
# 16MiB largest possible piece size
while (2**exp)*10 < size and exp < 24:
exp += 1
# piece length must be a power of 2
return 2**exp
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################


"""Package information for piecelength prject."""

from setuptools import setup, find_packages
import json
Expand All @@ -52,6 +51,7 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Software Development :: Libraries :: Python Modules",
"License :: OSI Approved :: BSD License",
],
Expand All @@ -60,9 +60,8 @@
author_email=INFO["email"],
license=INFO["license"],
include_package_data=True,
entry_points={"piecelength":"name=piecelength:main"},
entry_points={"console_scripts": "piecelength=piecelength.__main__:main"},
setup_requires=["setuptools"],
packages=find_packages(),
zip_safe=False,
test_suite="complete",
zip_safe=False
)
31 changes: 31 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /usr/bin/python3
# -*- coding: utf-8 -*-

###############################################################################
# BSD 2-Clause License
#
# Copyright (c) 2021, alexpdev
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
"""Initialize the unittest suit for the piecelength package."""
Loading