Example #1
0
def updateTorrent(torrent, fetcher, backup_dir_path, client, save_customs_list, real_update_flag) :
	new_data = fetcher.fetchTorrent(torrent)
	tmp_torrent = tfile.Torrent()
	tmp_torrent.loadData(new_data)
	diff_tuple = tfile.diff(torrent, tmp_torrent)

	if real_update_flag :
		if not backup_dir_path is None :
			backup_file_path = os.path.join(backup_dir_path, "%s.%d.bak" % (os.path.basename(torrent.path()), time.time()))
			shutil.copyfile(torrent.path(), backup_file_path)

		if not client is None :
			if len(save_customs_list) != 0 :
				customs_dict = client.customs(torrent, save_customs_list)
			prefix = client.dataPrefix(torrent)
			client.removeTorrent(torrent)

		with open(torrent.path(), "w") as torrent_file :
			torrent_file.write(new_data)
		torrent.loadData(new_data, torrent.path())

		if not client is None :
			client.loadTorrent(torrent, prefix)
			if len(save_customs_list) != 0 :
				client.setCustoms(torrent, customs_dict)

	return diff_tuple
Example #2
0
def main() :
	(cli_parser, config_dict, argv_list) = config.partialParser(sys.argv[1:], description="Show the difference between two torrent files")
	config.addArguments(cli_parser,
		config.ARG_TIMEOUT,
		config.ARG_CLIENT,
		config.ARG_CLIENT_URL,
		config.ARG_NO_COLORS,
		config.ARG_USE_COLORS,
		config.ARG_FORCE_COLORS,
		config.ARG_NO_FORCE_COLORS,
	)
	cli_parser.add_argument("torrents_list", type=str, nargs=2, metavar="<path/hash>")
	cli_options = cli_parser.parse_args(argv_list)
	config.syncParsers(config.SECTION_RTDIFF, cli_options, config_dict)

	socket.setdefaulttimeout(cli_options.socket_timeout)

	client = None
	if not cli_options.client_name is None :
		client_class = clients.CLIENTS_MAP[cli_options.client_name]
		client = client_class(cli_options.client_url)

	hash_regexp = re.compile(r"[\da-fA-F]{40}")
	for count in xrange(2) :
		item = cli_options.torrents_list[count]
		if os.path.exists(item) :
			cli_options.torrents_list[count] = tfile.Torrent(item).files()
		elif hash_regexp.match(item) :
			if client is None :
				raise RuntimeError("Required client for hash: %s" % (item))
			cli_options.torrents_list[count] = client.files(item)
		else :
			raise RuntimeError("Invalid file or hash: %s" % (item))

	tfile.printDiff(tfile.diff(*cli_options.torrents_list), " ",
		use_colors_flag=(not cli_options.no_colors_flag),
		force_colors_flag=cli_options.force_colors_flag,
	)