Beispiel #1
0
 def test_file_name_from_path_directory_trailing_slash(self):
     test = files.file_name_from_path('/home/user/')
     self.assertFalse(test)
Beispiel #2
0
 def test_file_name_from_path_root(self):
     test = files.file_name_from_path('/')
     self.assertFalse(test)
Beispiel #3
0
 def test_file_name_from_path_directory(self):
     test = files.file_name_from_path('/home/user')
     self.assertEqual(test, 'user')
Beispiel #4
0
 def test_file_name_from_path_file_unicode(self):
     test = files.file_name_from_path('/home/user/Test — Path')
     self.assertEqual(test, 'Test — Path')
Beispiel #5
0
 def test_file_name_from_path_file_space(self):
     test = files.file_name_from_path('/home/user/File Name.txt')
     self.assertEqual(test, 'File Name.txt')
Beispiel #6
0
 def test_file_name_from_path_file(self):
     test = files.file_name_from_path('/home/user/text.txt')
     self.assertEqual(test, 'text.txt')
Beispiel #7
0
 def test_file_name_from_path_empty(self):
     test = files.file_name_from_path('')
     self.assertFalse(test)
Beispiel #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)