197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655 | def execute(args: list = None) -> list:
"""
Execute program with provided list of arguments.
If no arguments are given then it defaults to using
sys.argv. This is the main entrypoint for the program
and command line interface.
Parameters
----------
args : list
Commandline arguments. default=None
Returns
-------
list
Depends on what the command line args were.
"""
toggle_debug_mode(False)
if not args:
if sys.argv[1:]:
args = sys.argv[1:]
else:
args = ["-h"]
parser = ArgumentParser(
"torrentfile",
usage="torrentfile <options>",
description=(
"Command line tool for creating, editing, validating, building "
"and interacting with all versions of Bittorrent files"),
prefix_chars="-",
formatter_class=TorrentFileHelpFormatter,
conflict_handler="resolve",
)
parser.add_argument(
"-q",
"--quiet",
help="Turn off all text output.",
dest="quiet",
action="store_true",
)
parser.add_argument(
"-V",
"--version",
action="version",
version=f"torrentfile v{version}",
help="show program version and exit",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
dest="debug",
help="output debug information",
)
parser.set_defaults(func=parser.print_help)
subparsers = parser.add_subparsers(
title="Commands",
dest="command",
metavar="create, edit, info, magnet, recheck, rebuild, rename\n",
)
create_parser = subparsers.add_parser(
"create",
help="Create a new Bittorrent file.",
prefix_chars="-",
aliases=["new"],
formatter_class=TorrentFileHelpFormatter,
)
create_parser.add_argument(
"-a",
"--announce",
"--tracker",
action="store",
dest="announce",
metavar="<url>",
nargs="+",
default=[],
help="one or more space-seperated tracker url(s)",
)
create_parser.add_argument(
"-p",
"--private",
action="store_true",
dest="private",
help="create private torrent",
)
create_parser.add_argument(
"-s",
"--source",
action="store",
dest="source",
metavar="<source>",
help="add source field to the metadata",
)
create_parser.add_argument(
"--config",
action="store_true",
dest="config",
help="""
Parse torrent information from a config file. Looks in the current
working directory, or the directory named .torrentfile in the users
home directory for a torrentfile.ini file. See --config-path option.
""",
)
create_parser.add_argument(
"--config-path",
action="store",
metavar="<path>",
dest="config_path",
help="use in combination with --config to provide config file path",
)
create_parser.add_argument(
"-m",
"--magnet",
action="store_true",
dest="magnet",
)
create_parser.add_argument(
"-c",
"--comment",
action="store",
dest="comment",
metavar="<comment>",
help="include a comment in the torrent file metadata",
)
create_parser.add_argument(
"-o",
"--out",
action="store",
dest="outfile",
metavar="<path>",
help="path to write torrent file",
)
create_parser.add_argument(
"--prog",
"--progress",
default="1",
action="store",
dest="progress",
metavar="<int>",
help="""
set the progress bar level
Options = 0, 1, 2
(0) = Do not display progress bar.
(1) = Display progress bar for each file.(default)
(2) = Display one progress bar for full torrent.
""",
)
create_parser.add_argument(
"--meta-version",
default="1",
choices=["1", "2", "3"],
action="store",
dest="meta_version",
metavar="<int>",
help="""
bittorrent metafile version
options = 1, 2, 3
(1) = Bittorrent v1 (Default)
(2) = Bittorrent v2
(3) = Bittorrent v1 & v2 hybrid
""",
)
create_parser.add_argument(
"--piece-length",
action="store",
dest="piece_length",
metavar="<int>",
help="""
(Default: auto calculated based on total size of content)
acceptable values include numbers 14-26
14 = 16KiB, 20 = 1MiB, 21 = 2MiB etc. Examples:[--piece-length 14]
""",
)
create_parser.add_argument(
"--web-seed",
action="store",
dest="url_list",
metavar="<url>",
nargs="+",
help="list of web addresses where torrent data exists (GetRight)",
)
create_parser.add_argument(
"--http-seed",
action="store",
dest="httpseeds",
metavar="<url>",
nargs="+",
help="list of URLs, addresses where content can be found (Hoffman)",
)
create_parser.add_argument(
"--align",
action="store_true",
help=("Align pieces to file boundaries. "
"This option is ignored when not used with V1 torrents."),
)
create_parser.add_argument(
"content",
action="store",
metavar="<content>",
nargs="?",
help="path to content file or directory",
)
create_parser.set_defaults(func=commands.create)
edit_parser = subparsers.add_parser(
"edit",
help="edit torrent file",
prefix_chars="-",
formatter_class=TorrentFileHelpFormatter,
)
edit_parser.add_argument(
"metafile",
action="store",
help="path to *.torrent file",
metavar="<*.torrent>",
)
edit_parser.add_argument(
"--tracker",
action="store",
dest="announce",
metavar="<url>",
nargs="+",
help="replace current trackers with one or more urls",
)
edit_parser.add_argument(
"--web-seed",
action="store",
dest="url_list",
metavar="<url>",
nargs="+",
help="replace current web-seed with one or more url(s)",
)
edit_parser.add_argument(
"--http-seed",
action="store",
dest="httpseeds",
metavar="<url>",
nargs="+",
help="replace current http-seed urls with new ones (Hoffman)",
)
edit_parser.add_argument(
"--private",
action="store_true",
help="make torrent private",
dest="private",
)
edit_parser.add_argument(
"--comment",
help="replaces any existing comment with <comment>",
metavar="<comment>",
dest="comment",
action="store",
)
edit_parser.add_argument(
"--source",
action="store",
dest="source",
metavar="<source>",
help="replaces current source with <source>",
)
edit_parser.set_defaults(func=commands.edit)
info_parser = subparsers.add_parser(
"info",
help="show detailed information about a torrent file",
prefix_chars="-",
formatter_class=TorrentFileHelpFormatter,
)
info_parser.add_argument(
"metafile",
action="store",
metavar="<*.torrent>",
help="path to torrent file",
)
info_parser.set_defaults(func=commands.info)
magnet_parser = subparsers.add_parser(
"magnet",
help="generate magnet url from an existing torrent file",
aliases=["m"],
prefix_chars="-",
formatter_class=TorrentFileHelpFormatter,
)
magnet_parser.add_argument(
"metafile",
action="store",
help="path to torrent file",
metavar="<*.torrent>",
)
magnet_parser.add_argument(
"--meta-version",
action="store",
choices=["0", "1", "2", "3"],
default="0",
help="""
This option is only relevant for hybrid torrent files.
Options = 0, 1, 2, 3
(0) = [default] version is determined automatically
(1) = create V1 magnet link only
(2) = create V2 magnet link only
(3) = create a hybrid magnet link
""",
dest="meta_version",
metavar="<int>",
)
magnet_parser.set_defaults(func=commands.get_magnet)
check_parser = subparsers.add_parser(
"recheck",
help="gives a detailed look at how much of the torrent is available",
aliases=["check"],
prefix_chars="-",
formatter_class=TorrentFileHelpFormatter,
)
check_parser.add_argument(
"metafile",
action="store",
metavar="<*.torrent>",
help="path to .torrent file.",
)
check_parser.add_argument(
"content",
action="store",
metavar="<content>",
help="path to content file or directory",
)
check_parser.set_defaults(func=commands.recheck)
rebuild_parser = subparsers.add_parser(
"rebuild",
help="""
Re-assemble files obtained from a bittorrent file into the
appropriate file structure for re-seeding. Read documentation
for more information, or use cases.
""",
formatter_class=TorrentFileHelpFormatter,
)
rebuild_parser.add_argument(
"-m",
"--metafiles",
action="store",
metavar="<*.torrent>",
nargs="+",
dest="metafiles",
required=True,
help="path(s) to .torrent file(s)/folder(s) containing .torrent files",
)
rebuild_parser.add_argument(
"-c"
"--contents",
action="store",
dest="contents",
nargs="+",
required=True,
metavar="<contents>",
help="folders that might contain the source contents needed to rebuld",
)
rebuild_parser.add_argument(
"-d",
"--destination",
action="store",
dest="destination",
required=True,
metavar="<destination>",
help="path to where torrents will be re-assembled",
)
rebuild_parser.set_defaults(func=commands.rebuild)
rename_parser = subparsers.add_parser(
"rename",
help="""Rename a torrent file to it's original name provided in the
metadata/the same name you see in your torrent client.""",
formatter_class=TorrentFileHelpFormatter,
)
rename_parser.add_argument(
"target",
action="store",
metavar="<target>",
help="path to torrent file",
)
rename_parser.set_defaults(func=commands.rename)
all_commands = [
"m",
"-h",
"-V",
"new",
"edit",
"info",
"check",
"create",
"magnet",
"rename",
"rebuild",
"recheck",
]
if not any(i for i in all_commands if i in args):
start = 0
while args[start] in ["-v", "-q"]:
start += 1
args.insert(start, "create")
args = parser.parse_args(args)
if args.quiet:
Config.activate_quiet()
elif args.debug:
Config.activate_logger()
if hasattr(args, "func"):
return args.func(args)
return args # pragma: nocover
|