Created
June 26, 2017 13:56
-
-
Save kawashirov/4ddc2821fcb49c1590c1326a7ef865c1 to your computer and use it in GitHub Desktop.
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 python3 | |
import argparse | |
import datetime | |
import os | |
import pprint | |
import shutil | |
import subprocess | |
import sys | |
import time | |
SUBDIR_WORKING = 'working' | |
SUBDIR_CLASSES_EXTRA = 'classes_extra' | |
SUBDIR_CLASSES_ENGINE = 'classes_engine' | |
SUBDIR_CLASSES_GAME = 'classes_game' | |
SUBDIR_LIGDX_LWJGL = 'lwjgl_backend' | |
SUBDIR_LWJGL_NATIVES = 'lwjgl/native/linux' | |
SUBDIR_LWJGL_JARS = 'lwjgl/jar' | |
JARS_LIBGDX = ['gdx-natives.jar', 'gdx-freetype-natives.jar'] | |
JARS_LWJGL = ['lwjgl.jar', 'lwjgl_test.jar', 'lwjgl_util.jar', 'jinput.jar'] | |
TRUE_STORAGE = 'Android/data/catstudio/mgc2' | |
def timeformat(time): | |
return datetime.datetime.utcfromtimestamp(time).strftime('%Y-%m-%d %H:%M:%S') | |
def soft_rmdir(dirpath): | |
try: | |
os.rmdir(dirpath) | |
return True | |
except OSError: | |
return False | |
def remove_old(origin_path, time_diff): | |
c_processed, c_rm = 0, 0 | |
time_now = time.time() | |
if not os.path.isdir(origin_path): | |
return | |
for dirpath, dirnames, filenames in os.walk(origin_path, topdown=False): | |
for filename in filenames: | |
c_processed += 1 | |
full_filename = os.path.join(dirpath, filename) | |
stat = os.stat(full_filename) | |
time_last = max(stat.st_mtime, stat.st_ctime) | |
if time_now - time_last >= time_diff: | |
try: | |
os.remove(full_filename) | |
c_rm += 1 | |
print('Removed file: %r (%r)' % (full_filename, timeformat(time_last))) | |
except Exception as e: | |
print('Can not remove file:', full_filename, ':', e) | |
for dirname in dirnames: | |
full_dirname = os.path.join(dirpath, dirname) | |
if soft_rmdir(full_dirname): | |
print('Removed empty dir:', full_dirname) | |
if soft_rmdir(origin_path): | |
print('Removed empty origin:', origin_path) | |
return c_processed, c_rm | |
def cleanup_storage(working_path): | |
mgc2_dir = os.path.join(working_path, 'Android/data/catstudio/mgc2') | |
c_processed, c_rm = 0, 0 | |
for mgc2_item in os.listdir(path=mgc2_dir): | |
if mgc2_item == 'assets': | |
continue | |
mgc2_item_j = os.path.join(mgc2_dir, mgc2_item) | |
if not os.path.isdir(mgc2_item_j): | |
continue | |
cc_proc, cc_rm = remove_old(mgc2_item_j, 7 * 24 * 60 * 60) | |
c_processed += cc_proc | |
c_rm += cc_rm | |
print('Processed %r files; Removed %r (%r%%) files;' % (c_processed, c_rm, 100 * c_rm // c_processed)) | |
# true_storage = os.path.join(working_path, TRUE_STORAGE) | |
# for item in os.listdir(path=true_storage): | |
# if item == 'assets': continue | |
# full = os.path.join(true_storage, item) | |
# print('Removing ' + repr(full) + '...') | |
# shutil.rmtree(full) | |
def select_classes(path): | |
print('Selecting classes from ' + repr(path) + '...') | |
classpath = list() | |
for item in os.listdir(path=path): | |
classpath.append(os.path.join(path, item)) | |
return classpath | |
def main(): | |
script_path = os.path.dirname(os.path.abspath(__file__)) | |
working_dir = os.path.join(script_path, SUBDIR_WORKING) | |
lwjgl_natives_dir = os.path.join(script_path, SUBDIR_LWJGL_NATIVES) | |
lwjgl_jars_dir = os.path.join(script_path, SUBDIR_LWJGL_JARS) | |
if not os.path.exists(working_dir): | |
os.makedirs(working_dir) | |
cleanup_storage(working_dir) | |
classpath = list() | |
classpath += select_classes(os.path.join(script_path, SUBDIR_CLASSES_EXTRA)) | |
classpath += select_classes(os.path.join(script_path, SUBDIR_CLASSES_GAME)) | |
classpath += select_classes(os.path.join(script_path, SUBDIR_CLASSES_ENGINE)) | |
classpath += [ | |
os.path.join(script_path, SUBDIR_LWJGL_NATIVES), | |
os.path.join(script_path, SUBDIR_LIGDX_LWJGL), | |
] | |
classpath += [os.path.join(script_path, jar) for jar in JARS_LIBGDX] | |
classpath += [os.path.join(lwjgl_jars_dir, jar) for jar in JARS_LWJGL] | |
cmdline = [ | |
'nice', '-n', '15', 'ionice', '-c', '3', '-t', | |
'java', | |
'-noverify', # Need to use some broken classes | |
'-classpath', ':'.join(classpath), | |
'-Djava.library.path=' + lwjgl_natives_dir, | |
'-Duser.home=' + working_dir, | |
# '-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel', | |
'com.catstudio.restaurant.MGC2Desktop_CN', | |
] | |
print(repr(cmdline)) | |
p = subprocess.Popen( | |
cmdline, | |
cwd=working_dir, | |
stderr=sys.stderr, | |
stdout=sys.stdout, | |
) | |
p.wait() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment