2017-01-11 12:04:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import py_compile
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import platform
|
|
|
|
|
|
|
|
from . import colorconsole as cc
|
2017-11-24 18:39:52 +00:00
|
|
|
from . import utils
|
2017-01-11 12:04:11 +00:00
|
|
|
|
|
|
|
rm_file_every_level = ['.pyc', '.pyo']
|
|
|
|
|
|
|
|
PY_VER = platform.python_version_tuple()
|
|
|
|
cpython_mid_name = 'cpython-{}{}'.format(PY_VER[0], PY_VER[1])
|
|
|
|
|
2018-09-16 13:52:10 +00:00
|
|
|
|
2017-01-11 12:04:11 +00:00
|
|
|
def make(tmp_path):
|
|
|
|
cc.v('Remove all old .pyc/.pyo files...')
|
|
|
|
clean_folder(tmp_path)
|
|
|
|
time.sleep(0.5)
|
|
|
|
cc.v('Compile all .py into .pyo...')
|
|
|
|
compile_files(tmp_path)
|
|
|
|
time.sleep(0.5)
|
|
|
|
cc.v('Remove all .py files...')
|
|
|
|
fix_pyo(tmp_path)
|
|
|
|
time.sleep(0.5)
|
|
|
|
cc.v('Remove all `__pycache__` folders...')
|
|
|
|
remove_cache(tmp_path)
|
|
|
|
|
|
|
|
|
|
|
|
def clean_folder(path):
|
|
|
|
for parent, dir_list, file_list in os.walk(path):
|
|
|
|
for d in dir_list:
|
|
|
|
clean_folder(os.path.join(parent, d))
|
|
|
|
|
|
|
|
for filename in file_list:
|
|
|
|
_, ext = os.path.splitext(filename)
|
|
|
|
if ext in rm_file_every_level:
|
|
|
|
os.remove(os.path.join(parent, filename))
|
|
|
|
|
|
|
|
|
|
|
|
def remove_cache(path):
|
|
|
|
for parent, dir_list, file_list in os.walk(path):
|
|
|
|
for d in dir_list:
|
|
|
|
d = d.lower()
|
|
|
|
if d == '__pycache__':
|
2017-11-24 18:39:52 +00:00
|
|
|
utils.remove(os.path.join(parent, d))
|
2017-01-11 12:04:11 +00:00
|
|
|
continue
|
|
|
|
remove_cache(os.path.join(parent, d))
|
|
|
|
|
|
|
|
|
|
|
|
def compile_files(path):
|
|
|
|
for parent, dir_list, file_list in os.walk(path):
|
|
|
|
for d in dir_list:
|
|
|
|
compile_files(os.path.join(parent, d))
|
|
|
|
|
|
|
|
for filename in file_list:
|
2018-09-16 13:52:10 +00:00
|
|
|
n, ext = os.path.splitext(filename)
|
2017-01-11 12:04:11 +00:00
|
|
|
# fileNameSplitList = filename.split(".")
|
|
|
|
# ext = fileNameSplitList[len(fileNameSplitList) - 1].lower()
|
|
|
|
if ext == '.py':
|
2018-09-23 18:47:50 +00:00
|
|
|
py_compile.compile(os.path.join(parent, filename), os.path.join(parent, n)+'.pyc', optimize=2)
|
|
|
|
# py_compile.compile(os.path.join(parent, filename), optimize=2)
|
2017-01-11 12:04:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def fix_pyo(path):
|
|
|
|
for parent, dir_list, file_list in os.walk(path):
|
|
|
|
for d in dir_list:
|
|
|
|
fix_pyo(os.path.join(parent, d))
|
|
|
|
|
|
|
|
for filename in file_list:
|
2018-09-16 13:52:10 +00:00
|
|
|
_, ext = os.path.splitext(filename)
|
|
|
|
if ext.lower() == '.py':
|
2017-01-11 12:04:11 +00:00
|
|
|
os.remove(os.path.join(parent, filename))
|
2018-09-23 18:47:50 +00:00
|
|
|
|
|
|
|
# names = filename.split(".")
|
|
|
|
# ext = names[len(names) - 1].lower()
|
|
|
|
# if ext.lower() == '.py':
|
|
|
|
# os.remove(os.path.join(parent, filename))
|
2018-09-16 13:52:10 +00:00
|
|
|
# elif ext == 'pyo':
|
2018-09-23 18:47:50 +00:00
|
|
|
# cpython = names[len(names) - 2].lower()
|
2018-09-16 13:52:10 +00:00
|
|
|
# if cpython == cpython_mid_name:
|
2018-09-23 18:47:50 +00:00
|
|
|
# del names[len(names) - 2]
|
2018-09-16 13:52:10 +00:00
|
|
|
# else:
|
|
|
|
# continue
|
2018-09-23 18:47:50 +00:00
|
|
|
# t_name = os.path.abspath(os.path.join(parent, '..', '.'.join(names)))
|
2018-09-16 13:52:10 +00:00
|
|
|
# f_name = os.path.join(parent, filename)
|
|
|
|
# shutil.copy(f_name, t_name)
|
2017-01-11 12:04:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
make(sys.argv[1])
|