Last active
December 15, 2015 19:09
-
-
Save jnns/5308837 to your computer and use it in GitHub Desktop.
Decrypts a TVA result folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#! -*- coding: utf-8 -*- | |
__license__ = "GPL" | |
__version__ = "1.0" | |
import os | |
import re | |
import shutil | |
CWD = os.getcwd() | |
def get_recordfile_name(file): | |
return file[1:-1] + ".tvr" | |
def read_name_from_record(file): | |
record = open(os.path.join(CWD, file, get_recordfile_name(file)), "r") | |
WORDCHARS = re.compile("\w+") | |
name = "".join((record.readlines()[:2])) | |
return "".join(WORDCHARS.findall(name)).lower() | |
def rename_folder_and_files(file): | |
src = os.path.join(CWD, file) | |
dst = os.path.join(CWD, "decrypted", read_name_from_record(file)) | |
try: | |
shutil.copytree(src, dst) | |
except Exception as e: | |
print e | |
for f in os.listdir(dst): | |
print f | |
m = re.search("^(\w+)([\.\w]+)$", f) | |
try: | |
os.rename(os.path.join(dst, f), os.path.join(dst, | |
read_name_from_record(file) + m.group(2))) | |
except AttributeError: | |
print "Error occurred while renaming %s" % f | |
def main(): | |
files = [f for f in os.listdir(CWD) if f[0] == "{"] | |
for file in files: | |
try: | |
if "{%s}" % file is not read_name_from_record(file): | |
print "Renaming: %s" % file | |
rename_folder_and_files(file) | |
else: | |
print "Already renamed: %" % file | |
except IOError: | |
print "No record file for %s" % file | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment