Initial matrix.py commit
This commit is contained in:
parent
d9942ba5c0
commit
ace902d4db
64
matrix.py
Executable file
64
matrix.py
Executable file
@ -0,0 +1,64 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# Copyright (c) 2018, Paul Lecuq
|
||||||
|
# All rights reserved.
|
||||||
|
# BSD Licensed
|
||||||
|
|
||||||
|
# Matrix is a small program that saves permissions on directory to a file and can restore it
|
||||||
|
# on another directory (or another machine)
|
||||||
|
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
mode = ""
|
||||||
|
backup_file = ""
|
||||||
|
rep = ""
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description='Parse arguments')
|
||||||
|
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('directory', metavar='directory', type=str, help='Directory')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
mode = args.mode
|
||||||
|
backup_file = args.backup_file
|
||||||
|
directory = args.directory
|
||||||
|
|
||||||
|
def backup():
|
||||||
|
fd = open(backup_file,"w+")
|
||||||
|
result=[]
|
||||||
|
for rd,dr,fs in os.walk(directory):
|
||||||
|
for d in dr:
|
||||||
|
res = os.path.join(rd, d)
|
||||||
|
result.append(res)
|
||||||
|
for f in fs:
|
||||||
|
res = os.path.join(rd, f)
|
||||||
|
result.append(res)
|
||||||
|
for f_name_tmp in result:
|
||||||
|
try:
|
||||||
|
f_name = f_name_tmp.lstrip(directory)
|
||||||
|
f_perm = str(oct(os.stat(f_name_tmp).st_mode))
|
||||||
|
line = "%s|%s\n" % (f_name,f_perm)
|
||||||
|
fd.write(line)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
fd.close()
|
||||||
|
|
||||||
|
def restore():
|
||||||
|
fd = open(backup_file,"r+")
|
||||||
|
for f in fd:
|
||||||
|
line = f.strip("\n")
|
||||||
|
f_name_tmp = line.split('|')[0]
|
||||||
|
f_name = "%s/%s" % (directory,f_name_tmp)
|
||||||
|
f_perm = line.split('|')[1]
|
||||||
|
try:
|
||||||
|
os.chmod(f_name,int(f_perm,8))
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if mode == "backup":
|
||||||
|
backup()
|
||||||
|
elif mode == "restore":
|
||||||
|
restore()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user