Ejemplo n.º 1
0
 def test_info_dictionary_empty_payload_dir(self):
     temp_dir = tempfile.mkdtemp()
     empty_test_file = open(temp_dir + os.sep + 'file', 'w')
     empty_test_file.write('')
     empty_test_file.close()
     with self.assertRaises(SystemExit):
         metainfo.InfoDictionary(empty_test_file.name)
     rmtree(temp_dir)
Ejemplo n.º 2
0
 def test_info_dictionary_simple_payload_dir(self):
     temp_dir = tempfile.mkdtemp()
     test_file = open(temp_dir + os.sep + 'file', 'w')
     test_file.write('Hello Testers')
     test_file.close()
     test = metainfo.InfoDictionary(temp_dir).get_bencoded()
     rmtree(temp_dir)
     self.assertEqual(
         test,
         b'd5:filesld6:lengthi13e4:pathl4:fileeee12:piece lengthi16384e6:pieces20:\xc5\x1e-\x8c\xd4\xb3\xabz\xbf\xaaA_\xad=O6\x94}\xdc\x13e'
     )  # noqa
Ejemplo n.º 3
0
 def test_meta_dictionary_comment(self):
     temp_dir = tempfile.mkdtemp()
     test_file = open(temp_dir + os.sep + 'file', 'w')
     test_file.write('Hello Testers')
     test_file.close()
     meta_dict = metainfo.MetaDictionary()
     meta_dict.info = metainfo.InfoDictionary(temp_dir)
     meta_dict.comment = 'Such a good test comment!'
     test = meta_dict.get_bencoded()
     rmtree(temp_dir)
     self.assertEqual(
         test,
         b'd7:comment25:Such a good test comment!4:infod5:filesld6:lengthi13e4:pathl4:fileeee12:piece lengthi16384e6:pieces20:\xc5\x1e-\x8c\xd4\xb3\xabz\xbf\xaaA_\xad=O6\x94}\xdc\x13ee'
     )  # noqa
Ejemplo n.º 4
0
 def test_meta_dictionary_add_node(self):
     temp_dir = tempfile.mkdtemp()
     test_file = open(temp_dir + os.sep + 'file', 'w')
     test_file.write('Hello Testers')
     test_file.close()
     meta_dict = metainfo.MetaDictionary()
     meta_dict.info = metainfo.InfoDictionary(temp_dir)
     meta_dict.add_node('example.com:6881')
     test = meta_dict.get_bencoded()
     rmtree(temp_dir)
     self.assertEqual(
         test,
         b'd4:infod5:filesld6:lengthi13e4:pathl4:fileeee12:piece lengthi16384e6:pieces20:\xc5\x1e-\x8c\xd4\xb3\xabz\xbf\xaaA_\xad=O6\x94}\xdc\x13e5:nodesll11:example.comi6881eeee'
     )  # noqa
Ejemplo n.º 5
0
 def test_meta_dictionary_created_by(self):
     temp_dir = tempfile.mkdtemp()
     test_file = open(temp_dir + os.sep + 'file', 'w')
     test_file.write('Hello Testers')
     test_file.close()
     meta_dict = metainfo.MetaDictionary()
     meta_dict.info = metainfo.InfoDictionary(temp_dir)
     meta_dict.created_by = 'Test God/2.4'
     test = meta_dict.get_bencoded()
     rmtree(temp_dir)
     self.assertEqual(
         test,
         b'd10:created by12:Test God/2.44:infod5:filesld6:lengthi13e4:pathl4:fileeee12:piece lengthi16384e6:pieces20:\xc5\x1e-\x8c\xd4\xb3\xabz\xbf\xaaA_\xad=O6\x94}\xdc\x13ee'
     )  # noqa
Ejemplo n.º 6
0
 def test_info_dictionary_include_dotfile(self):
     temp_dir = tempfile.mkdtemp()
     test_file = open(temp_dir + os.sep + 'file', 'w')
     test_file.write('Hello Testers')
     test_file.close()
     test_dotfile = open(temp_dir + os.sep + '.file', 'w')
     test_dotfile.write('Hidden')
     test_dotfile.close()
     test = metainfo.InfoDictionary(temp_dir,
                                    include_dotfiles=True).get_bencoded()
     rmtree(temp_dir)
     self.assertEqual(
         test,
         b'd5:filesld6:lengthi6e4:pathl5:.fileeed6:lengthi13e4:pathl4:fileeee12:piece lengthi16384e6:pieces20:\x05\x136\xb2_\x98\xd9\xd3T\x97\x1a\xd3\x1ea\xdd{\x1dS!0e'
     )  # noqa
Ejemplo n.º 7
0
 def test_meta_dictionary_add_announces(self):
     temp_dir = tempfile.mkdtemp()
     test_file = open(temp_dir + os.sep + 'file', 'w')
     test_file.write('Hello Testers')
     test_file.close()
     meta_dict = metainfo.MetaDictionary()
     meta_dict.info = metainfo.InfoDictionary(temp_dir)
     meta_dict.add_announce('example.com:6881')
     meta_dict.add_announce('203.0.113.29:8618')
     meta_dict.add_announce('203.0.113.79:5432')
     test = meta_dict.get_bencoded()
     rmtree(temp_dir)
     self.assertEqual(
         test,
         b'd8:announce16:example.com:688113:announce-listl16:example.com:688117:203.0.113.29:861817:203.0.113.79:5432e4:infod5:filesld6:lengthi13e4:pathl4:fileeee12:piece lengthi16384e6:pieces20:\xc5\x1e-\x8c\xd4\xb3\xabz\xbf\xaaA_\xad=O6\x94}\xdc\x13ee'
     )  # noqa
Ejemplo n.º 8
0
def main():
    user_arguments = parse_user_arguments()
    if user_arguments and user_arguments.paths:
        for in_path in user_arguments.paths:
            in_path = files.check_basename_path(in_path)
            if in_path:
                in_path = files.check_basename_path(in_path)
                basename = files.file_name_from_path(in_path)
                meta_dict = metainfo.MetaDictionary()
                if user_arguments.announces:
                    for announce in user_arguments.announces:
                        meta_dict.add_announce(str(announce))
                if user_arguments.comment:
                    meta_dict.comment = str(user_arguments.comment)
                if user_arguments.creator:
                    meta_dict.created_by = str(user_arguments.creator)
                if user_arguments.date:
                    meta_dict.creation_date = int(user_arguments.date)
                if user_arguments.nodes:
                    for node in user_arguments.nodes:
                        meta_dict.add_node(str(node))
                if user_arguments.website:
                    meta_dict.website = str(user_arguments.website)
                meta_dict.info = metainfo.InfoDictionary(
                    in_path,
                    max_piece_length=user_arguments.max_piece_length,
                    include_dotfiles=user_arguments.include_dotfiles)
                meta_dict.info.name = basename
                meta_dict.info.private = user_arguments.private
                if sys.stdout.isatty():
                    extra_print_destination = sys.stdout
                else:
                    extra_print_destination = sys.stderr
                if user_arguments.btih:
                    magnet_btih = bittorrent_info_hash(
                        meta_dict.info.get_bencoded())
                    print(magnet_btih, file=extra_print_destination)
                if user_arguments.magnet:
                    magnet_btih = bittorrent_info_hash(
                        meta_dict.info.get_bencoded())
                    magnet_size = int(meta_dict.info.length)
                    magnet_name = basename
                    print(magnet_uri(magnet_btih, magnet_size, magnet_name),
                          file=extra_print_destination)
                meta_file = meta_dict.get_bencoded()
                torrent_name = basename + '.torrent'
                if meta_file:
                    # Save to file or stdout if redirected and single
                    if not sys.stdout.isatty():
                        if len(user_arguments.paths) == 1:
                            sys.stdout.buffer.write(meta_file)
                            sys.exit(0)
                    torrent_path = os.getcwd() + os.sep + torrent_name
                    open(torrent_path, 'wb').write(meta_file)
                    print('Wrote torrent file "{0}" ({1} torrent file for \
                         {2} files).'.format(
                        torrent_name, files.file_length_hfmt(len(meta_file)),
                        files.file_length_hfmt(meta_dict.info.length)),
                          file=sys.stderr)
                else:
                    print('Torrent file "{0}" could not be bencoded because \
                        reasons. Ouch.'.format(torrent_name),
                          file=sys.stderr)
            else:
                print(
                    'There was a problem accessing the file path: {0}'.format(
                        in_path),
                    file=sys.stderr)
    else:
        user_arguments.print_usage()
        sys.exit(1)
    sys.exit(0)