Skip to content

Instantly share code, notes, and snippets.

@ihidchaos
Created November 7, 2019 17:25
Show Gist options
  • Save ihidchaos/2f462c7b75c8e8024047875df736b9c1 to your computer and use it in GitHub Desktop.
Save ihidchaos/2f462c7b75c8e8024047875df736b9c1 to your computer and use it in GitHub Desktop.
py2pyc
import compileall
import os
import shutil
import tarfile
from pathlib import Path
keyword = '.cpython-37'
temp = 'publish'
app = 'app'
def rm_files(_dir, suffix):
for prefix, dirs, files in os.walk(_dir):
for f in files:
if f != 'DataTransponder.py' and f.endswith(suffix):
filename = os.path.join(prefix, f)
os.remove(filename)
for d in dirs:
if d == '__pycache__':
shutil.rmtree(os.path.join(prefix, d))
def move(_dir, k):
os.chdir(_dir)
lt = os.listdir(_dir)
for f in lt:
if not os.path.isdir(f):
if k in f:
tmp = f.replace(k, '')
os.renames(f, tmp)
shutil.move(os.path.join(os.path.abspath('.'), tmp), os.path.join(os.path.abspath('..'), tmp))
else:
move(_dir + '\\' + f, k)
os.chdir('..')
def pack(output_filename, source_dir):
os.chdir(source_dir)
with tarfile.open(output_filename, "w:gz") as tar:
for name in os.listdir('.'):
tar.add(name)
tar.close()
f = Path(output_filename).resolve()
upper = Path.cwd().parent.joinpath(output_filename)
f.replace(upper)
if __name__ == '__main__':
rm_files(os.getcwd(), '.pyc')
if os.path.exists(temp):
shutil.rmtree(temp)
shutil.copytree(app, temp, ignore=shutil.ignore_patterns('test', '.idea', 'log'))
compileall.compile_dir(temp, quiet=1)
move(os.getcwd(), keyword)
rm_files(temp, '.py')
os.remove(os.path.join(os.path.abspath('.'), temp, 'DataTransponder.pyc'))
pack('publish.tar.gz', temp)
print("done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment