updated matrix program

This commit is contained in:
Paul 2020-04-25 22:57:20 +02:00
parent 2e09efb8d9
commit 583ee40431

View File

@ -15,7 +15,7 @@ backup_file = ""
rep = "" rep = ""
parser = argparse.ArgumentParser(description='Parse arguments') parser = argparse.ArgumentParser(description='Parse arguments')
parser.add_argument('mode', metavar='mode', type=str, choices=['backup','restore'], help='Mode') parser.add_argument('mode', metavar='mode', type=str, choices=['backup', 'restore'], help='Mode')
parser.add_argument('backup_file', metavar='backup_file', type=str, help='Backup file') parser.add_argument('backup_file', metavar='backup_file', type=str, help='Backup file')
parser.add_argument('directory', metavar='directory', type=str, help='Directory') parser.add_argument('directory', metavar='directory', type=str, help='Directory')
@ -23,42 +23,50 @@ args = parser.parse_args()
mode = args.mode mode = args.mode
backup_file = args.backup_file backup_file = args.backup_file
directory = args.directory.rstrip("/") directory = args.directory
def backup(): def backup():
fd = open(backup_file,"w+") i = 0
result=[] with open(backup_file, "w+") as file_desc:
for rd,dr,fs in os.walk(directory): result = []
for d in dr: for root_dir, dirs, files in os.walk(directory):
res = os.path.join(rd, d) for d in dirs:
result.append(res) res = os.path.join(root_dir, d)
for f in fs: result.append(res)
res = os.path.join(rd, f) for f in files:
result.append(res) res = os.path.join(root_dir, f)
for f_name_tmp in result: result.append(res)
try: for f_name in result:
f_name = f_name_tmp.replace(directory,'').lstrip("/") try:
f_perm = str(oct(os.stat(f_name_tmp).st_mode)) i += 1
line = "%s|%s\n" % (f_name,f_perm) f_uid = os.stat(f_name).st_uid
fd.write(line) f_gid = os.stat(f_name).st_gid
except OSError: f_perm = str(oct(os.stat(f_name).st_mode))
pass line = "{}|{}|{}|{}\n".format(f_name, f_uid, f_gid, f_perm)
fd.close() file_desc.write(line)
except OSError:
pass
print("Backup {} files permissions, storing to {}".format(i, backup_file))
def restore(): def restore():
fd = open(backup_file,"r+") i = 0
for f in fd: with open(backup_file, "r+") as file_desc:
line = f.strip("\n") for f in file_desc:
f_name_tmp = line.split('|')[0] line = f.strip("\n")
f_name = "%s/%s" % (directory,f_name_tmp) f_name = line.split('|')[0]
f_perm = line.split('|')[1] f_uid = line.split('|')[1]
try: f_gid = line.split('|')[2]
os.chmod(f_name,int(f_perm,8)) f_perm = line.split('|')[3]
except OSError: try:
pass os.chmod(f_name, int(f_perm, 8))
os.chown(f_name, int(f_uid), int(f_gid))
except OSError:
pass
i += 1
print("Restored {} files permissions".format(i))
if mode == "backup": if mode == "backup":
backup() backup()
elif mode == "restore": elif mode == "restore":
restore() restore()