Last active
April 28, 2019 11:08
-
-
Save AngelicosPhosphoros/9e5fa42465f5909906ef9ccbcc191d82 to your computer and use it in GitHub Desktop.
This file contains 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/python3 | |
W_DIR = 'data/save' | |
BEGIN_PATTERN = 'region3' | |
ZIP_EXECUTABLE = 'C:/Program Files/7-Zip/7z.exe' | |
OUT_PUT_PATH = 'E:/Games/DF/Backups' | |
import os | |
import os.path | |
import subprocess | |
import sys | |
import shutil | |
def w_func(): | |
os.chdir(W_DIR) | |
ALL_OK = True | |
with open('compress_log.txt','w') as log_file: | |
def log(*args): | |
print(*args) | |
log_file.write(' '.join(map(str,args))) | |
log_file.write('\n') | |
log_file.flush() | |
def err_log(string): | |
string = '[ERROR!!!]\n' + string + '\n' | |
log_file.write(string) | |
sys.stderr.write(string) | |
log_file.flush() | |
save_dirs = filter(os.path.isdir, os.listdir('.')) | |
save_dirs = [x for x in save_dirs if x[:len(BEGIN_PATTERN)+1]==BEGIN_PATTERN+'-'] | |
log('Will compress:') | |
log(save_dirs) | |
log('\n') | |
for d in save_dirs: | |
log('Compressing %s...'%d) | |
# Building run command | |
out_archive = os.path.join(OUT_PUT_PATH, d+'.7z') | |
args = [ZIP_EXECUTABLE, 'a', '-t7z', '-mmt', out_archive, d] | |
result = subprocess.run(args) | |
if result.returncode!=0: | |
ALL_OK = False | |
err_log('Failed to compress %s!'%d) | |
break | |
if not ALL_OK: | |
err_log('Failed to compress!!! Aborting deletion.') | |
os.system('pause') | |
return 1 | |
log('Compressed all!') | |
log('==============') | |
log('Deletion began') | |
for d in save_dirs: | |
log('Deleting %s...'%d) | |
try: | |
shutil.rmtree(d) | |
except: | |
ALL_OK = False | |
err_log('Failed to delete %s! Aboring'%d) | |
os.system('pause') | |
return 1 | |
log('All OK') | |
return 0 | |
r = w_func() | |
exit(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment