Ejemplo n.º 1
0
Archivo: rsigh.py Proyecto: dryes/rsigh
def main():
	config = init_configparser()
	args = init_argparse(config)

	input = args['input']

	sqlite = init_sqlite(args['sqlite_db'])

	if args['search'] == True and input != '.':
		search(sqlite, input)
		sys.exit()

	if args['collect'] == True and input != '.':
		if collect(sqlite, input, nzbdir=os.path.expanduser(args['nzb_dir']), watchdir=os.path.expanduser(args['watch_dir'])) == True:
			print('%s.nzb.gz > %r' % (input, os.path.expanduser(args['watch_dir'])))
		else:
			print('%r not found.' % input)

		sys.exit()

	if args['dupecheck'] == True and input != '.':
		if dupecheck(sqlite, input) == True:
			sys.exit(0)
		else:
			sys.exit(1)

	if args['list'] == True:
		list(sqlite, limit=50)
		sys.exit()

	if args['list_old'] == True:
		listold(sqlite, retention=args['retention'], limit=50)
		sys.exit()

	if args['delete'] == True:
		delete(sqlite, input, nzbdir=os.path.expanduser(args['nzb_dir']), tchdir=os.path.expanduser(args['tch_dir']))
		sys.exit()

	if args['post_process'] == True and input != '.':
		if postproc(sqlite, args, input) == True:
			sys.exit(0)
		else:
			sys.exit(1)

	if args['sqlite_vacuum'] == True:
		sqlite.execute('''VACUUM''')
		sys.exit()

	input = os.path.normpath(input)
	if input == '.' or not os.path.exists(input):
		sys.exit()

	if dupecheck(sqlite, input) == False:
		sys.exit(1)

	global temp
	temp = os.path.normpath(os.path.expanduser(args['tmp_dir'])) + os.sep + 'rsigh-tmp-' + str(os.getpid()) + os.sep

	filename = str(uuid.uuid4())
	while volcheck(sqlite, filename) == False:
		filename = str(uuid.uuid4())
	filename=temp + filename

	if not os.path.isdir(temp):
		try:
			os.makedirs(temp)
		except:
			if len(str(sys.exc_info()[1])) > 0:
				print(sys.exc_info()[1])
			sys.exit(1)

	signal.signal(signal.SIGINT, handler)

	passwords = []
	tc = TrueCrypt(filename=filename + '.tc', binary=args['truecrypt_bin'])
	if tc == False:
		rmtmp()
		sys.exit(1)

	passwords.append(tc.create((((filesize(input)*1.10)+1310720)), password=args['tc_ov_password'], enc_algorithm=args['tc_encryption_algo'], \
		hash_algorithm=args['tc_hash_algo'], filesystem=args['tc_filesystem'], random_char_length=args['tc_randcharlen']))

	if args['tc_volume_type'] == 'hidden':
		passwords.append(tc.create((((filesize(input)*1.075)+655360)), password=args['tc_hv_password'], type='hidden', enc_algorithm=args['tc_encryption_algo'], \
			hash_algorithm=args['tc_hash_algo'], filesystem=args['tc_filesystem'], random_char_length=args['tc_randcharlen']))

	if args['tc_volume_type'] == 'hidden':
		tc.mount(passwords, hidden=True)
	else:
		tc.mount(passwords, hidden=False)

	if os.path.isfile(input):
		try:
			shutil.copy(input, os.path.join(temp, filename) + os.sep)
		except:
			if len(str(sys.exc_info()[1])) > 0:
				print(sys.exc_info()[1])
			rmtmp()
			sys.exit(1)
	elif os.path.isdir(input):
		try:
			shutil.copytree(input, os.path.join(temp, filename, os.path.basename(input)) + os.sep)
		except:
			if len(str(sys.exc_info()[1])) > 0:
				print(sys.exc_info()[1])
			rmtmp()
			sys.exit(1)

	if tc.dismount(force=True, unlink=True) == False:
		rmtmp()
		sys.exit(1)

	if tc.backup_headers(tofile=True, overwrite=True) == False:
		rmtmp()
		sys.exit(1)
	tchdir = os.path.expanduser(args['tch_dir'])
	if not os.path.isdir(os.path.join(tchdir, os.path.basename(filename)[0])):
		try:
			os.makedirs(os.path.join(tchdir, os.path.basename(filename)[0]))
		except:
			if len(str(sys.exc_info()[1])) > 0:
				print(sys.exc_info()[1])
			rmtmp()
			sys.exit(1)
	try:
		shutil.move(filename + '.tch', os.path.join(tchdir, os.path.basename(filename)[0]))
	except:
		if len(str(sys.exc_info()[1])) > 0:
			print(sys.exc_info()[1])
		rmtmp()
		sys.exit(1)

	rar_password = tc.random_string(64)
	rar = RAR(filename=os.path.basename(filename) + '.rar', password=rar_password, binaries=(args['rar_bin'], args['unrar_bin']))	
	if rar == False:
		rmtmp()
		sys.exit(1)
	if rar.create(temp) == False:
		rmtmp()
		sys.exit(1)

	if tc.destroy_volume(unlink=True) == False:
		rmtmp()
		sys.exit(1)

	if args['no_par2'] == False:
		par2 = PAR2(filename=os.path.basename(filename) + '.par2', binary=args['par2_bin'])
		if par2 == False:
			rmtmp()
			sys.exit(1)
		if par2.create(temp, opts='-r%s -n%s' % (args['par2_redundancy'], args['par2_number'])) == False:
			rmtmp()
			sys.exit(1)

	if args['no_post'] == True:
		sys.exit(0)

	cwd = os.getcwd()
	os.chdir(temp)
	sp = subprocess.Popen('%s %s -c %s -g %s -f %s %s' % (args['python2_bin'], args['nm_script'], args['nm_config'], args['nm_group'], \
		os.path.basename(filename), ','.join(os.listdir(temp)).replace(',', ' ')), shell=True, stdin=subprocess.PIPE)
	sp.communicate()
	os.chdir(cwd)
	if sp.returncode > 0:
		rmtmp()
		sys.exit(1)
	else:
		tuple = (os.path.basename(input), os.path.basename(filename), passwords[0], passwords[1], rar_password, str(time.time()).split('.')[0])
		sqlite.execute('''INSERT INTO rsigh (dirname, volume, ov_password, hv_password, rar_password, posted) VALUES (?, ?, ?, ?, ?, ?)''', tuple)

		nzbdir = os.path.expanduser(args['nzb_dir'])
		if not os.path.isdir(os.path.join(nzbdir, os.path.basename(filename)[0])):
			try:
				os.makedirs(os.path.join(nzbdir, os.path.basename(filename)[0]))
			except:
				if len(str(sys.exc_info()[1])) > 0:
					print(sys.exc_info()[1])
				rmtmp()
				sys.exit(1)
		try:
			shutil.move(os.path.join(temp, 'newsmangler_' + os.path.basename(filename) + '.nzb'), \
				os.path.join(nzbdir, os.path.basename(filename)[0], os.path.basename(filename) + '.nzb'))
		except:
			if len(str(sys.exc_info()[1])) > 0:
				print(sys.exc_info()[1])
			rmtmp()
			sys.exit(1)
		
		gzcompress(os.path.join(nzbdir, os.path.basename(filename)[0], os.path.basename(filename) + '.nzb'), unlink=True)
Ejemplo n.º 2
0
Archivo: rsigh.py Proyecto: dryes/rsigh
def postproc(sqlite, args, input):
	input = os.path.normpath(input) + os.sep
	tuple = (os.path.dirname(input)[8:],)
	res = sqlite.query('''SELECT * FROM rsigh WHERE dirname = ? LIMIT 1''', tuple)

	if len(res) == 0:
		return False

	if os.path.isfile(os.path.join(input, res[0][2] + '.par2')):
		par2 = PAR2(filename=os.path.join(input, res[0][2] + '.par2'), binary=args['par2_bin'])
		if par2.verify() == False:
			if par2.repair() == False:
				sys.exit(1)

	if os.path.isfile(os.path.join(input, res[0][2] + '.rar')):
		if res[0][5] is not None:
			rar_password = str(res[0][5])
		else:
			rar_password = None
		rar = RAR(filename=os.path.join(input, res[0][2] + '.rar'), password=rar_password, binaries=(args['rar_bin'], args['unrar_bin']))
		if rar.unrar() == False:
			sys.exit(1)

	if os.path.isfile(os.path.join(input, res[0][2] + '.tc')):
		tc = TrueCrypt(filename=os.path.join(input, res[0][2] + '.tc'), binary=args['truecrypt_bin'])
		volume_headers = tc.read_tch(filename=os.path.join(os.path.expanduser(args['tch_dir']), str(res[0][2])[0], res[0][2] + '.tch'))
		if len(volume_headers) == 0:
			sys.exit(1)

		tc.restore_headers(volume_headers)

		passwords = (res[0][3], res[0][4])

		if len(volume_headers) == 2:
			tc.mount(passwords, hidden=True)
		elif len(volume_headers) == 1:
			tc.mount(passwords, hidden=False)
		else:
			sys.exit(1)

		if os.path.isdir(os.path.join(input, res[0][2], res[0][1])):
			try:
				shutil.move(os.path.join(input, res[0][2], res[0][1]), os.path.join(input, os.pardir) + os.sep + (input)[8:])
			except:
				if len(str(sys.exc_info()[1])) > 0:
					print(sys.exc_info()[1])
				sys.exit(1)
		elif os.path.isfile(os.path.join(input, res[0][2], res[0][1])):
			try:
				shutil.move(os.path.join(input, res[0][2], res[0][1]), os.path.join(input, os.pardir))
			except:
				if len(str(sys.exc_info()[1])) > 0:
					print(sys.exc_info()[1])
				sys.exit(1)			

		if tc.dismount(force=True, unlink=True) == False:
			sys.exit(1)

		try:
			shutil.rmtree(input)
		except:
			if len(str(sys.exc_info()[1])) > 0:
				print(sys.exc_info()[1])
			sys.exit(1)

	return True