Skip to content

Commit

Permalink
Add hidden file detection routines
Browse files Browse the repository at this point in the history
  • Loading branch information
kz26 committed Dec 9, 2016
1 parent d46dad3 commit 32114be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions bin/dottorrent_cli
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ if __name__ == '__main__':
'--piece_size', '-s', type=int, help='piece size in bytes')
parser.add_argument(
'--private', '-p', action='store_true', help='set private flag')
parser.add_argument(
'--source', help='source string (useful for private trackers')
parser.add_argument('--comment', '-c',
help='free-text string for the torrent comment field')
help='string for the torrent comment field')
parser.add_argument('--date', '-d', default='now',
help='Torrent creation date. \
Valid values: unix timestamp/none/now (default: now)')
Expand All @@ -61,8 +63,6 @@ if __name__ == '__main__':
'output_path', help='Output path for created .torrent file. \
If a directory is provided, the filename will be automatically \
generated based on the input.')
parser.add_argument(
'--source', help='Set source string')
args = parser.parse_args()

if args.date:
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

1.6.0
-----
* Add support for source strings (-s)
* Exclude hidden dotfiles from being added
* Exclude hidden files on Windows (requires Python 3.5+)

1.5.3
-----
* Use relative paths instead of absolute paths for directory mode path generation
Expand Down
27 changes: 21 additions & 6 deletions dottorrent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,55 @@
MAX_PIECE_SIZE = 2 ** 22


if sys.version_info >= (3, 5) and os.name == 'nt':
import stat

def is_hidden_file(path):
fn = path.split(os.sep)[-1]
return fn.startswith('.') or \
bool(os.stat(path).st_file_attributes &
stat.FILE_ATTRIBUTE_HIDDEN)
else:
def is_hidden_file(path):
fn = path.split(os.sep)[-1]
return fn.startswith('.')


def print_err(v):
print(v, file=sys.stderr)


class Torrent(object):

def __init__(self, path, trackers=None, web_seeds=None,
piece_size=None, private=False, creation_date=None,
comment=None, created_by=None, include_md5=False, source=None):
piece_size=None, private=False, source=None,
creation_date=None, comment=None, created_by=None,
include_md5=False):
"""
:param path: path to a file or directory from which to create the torrent
:param trackers: list/iterable of tracker URLs
:param web_seeds: list/iterable of HTTP/FTP seed URLs
:param piece_size: Piece size in bytes. Must be >= 16 KB and a power of 2.
If None, ``get_info()`` will be used to automatically select a piece size.
:param private: The private flag. If True, DHT/PEX will be disabled.
:param source: An optional source string for the torrent.
:param creation_date: An optional datetime object representing the torrent creation date.
:param comment: An optional comment string for the torrent.
:param created_by: name/version of the program used to create the .torrent.
If None, defaults to the value of ``DEFAULT_CREATOR``.
:param include_md5: If True, also computes and stores MD5 hashes for each file.
:param source: An optional source string for the torrent.
"""

self.path = os.path.normpath(path)
self.trackers = trackers
self.web_seeds = web_seeds
self.piece_size = piece_size
self.private = private
self.source = source
self.creation_date = creation_date
self.comment = comment
self.created_by = created_by
self.include_md5 = include_md5
self.source = source

@property
def trackers(self):
Expand Down Expand Up @@ -150,7 +165,7 @@ def get_info(self):
for fn in x[2]:
fpath = os.path.normpath(os.path.join(x[0], fn))
fsize = os.path.getsize(fpath)
if fsize:
if fsize and not is_hidden_file(fpath):
total_size += fsize
total_files += 1
else:
Expand Down Expand Up @@ -188,7 +203,7 @@ def generate(self, callback=None):
for fn in x[2]:
fpath = os.path.normpath(os.path.join(x[0], fn))
fsize = os.path.getsize(fpath)
if fsize:
if fsize and not is_hidden_file(fpath):
files.append((fpath, fsize, {}))
else:
raise exceptions.InvalidInputException
Expand Down
2 changes: 1 addition & 1 deletion dottorrent/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.5.3'
__version__ = '1.6.0'

0 comments on commit 32114be

Please sign in to comment.