Last active
May 20, 2018 08:42
-
-
Save veirus/2a6da05ca0eaff2fe5c08a97c82c6e4d to your computer and use it in GitHub Desktop.
Pack current project folder into zip excluding node_modules
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 os | |
import zipfile | |
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 0, | |
length = 100, fill = '█', span = ' '): | |
""" | |
Call in a loop to create terminal progress bar | |
@params: | |
iteration - Required : current iteration (Int) | |
total - Required : total iterations (Int) | |
prefix - Optional : prefix string (Str) | |
suffix - Optional : suffix string (Str) | |
decimals - Optional : positive number of decimals in percent complete (Int) | |
length - Optional : character length of bar (Int) | |
fill - Optional : bar fill character (Str) | |
---- | |
from: https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console | |
""" | |
propeller = '-\|/'[iteration%4] | |
percent = f'{100 * (iteration / float(total)):.{decimals}f}' | |
filledLength = int(length * iteration // total) | |
bar = fill * filledLength + span * (length - filledLength) | |
print(f'\b[ {propeller} ] {prefix} [{bar}] {percent}% {suffix}', end = '\r') | |
# Print New Line on Complete | |
if iteration == total: | |
print() | |
def zipdir(path, ziph): | |
for root, dirs, files in os.walk(path): | |
for fol in dirs: | |
if (fol in il) and (os.path.isdir(fol)): | |
print(f'*31* exclude folder: {fol}', flush=True) | |
dirs.remove(fol) | |
for i, fil in enumerate(files): | |
l = len(files) | |
if fil in il: | |
print(f'*36* exclude file #{i}:'.ljust(45), f'{fil}', flush=True) | |
files.remove(fil) | |
continue | |
# include only this folder and not that one above | |
absfn = os.path.join(root, fil) | |
relfn = absfn[len(path)+len(os.sep):] #XXX: relative path | |
ziph.write(absfn, relfn) | |
printProgressBar(i, l, prefix = f'Zipping * {fname} *', suffix = '', length = 16) | |
if __name__ == '__main__': | |
from datetime import datetime | |
ignorefile = '.gitignore' | |
#default ignorelist: | |
il = ['lol'] | |
if os.path.isfile(ignorefile): | |
with open(ignorefile, 'r') as ign: | |
tmp = ign.readlines() | |
it = [i.strip() for i in tmp if not(i.startswith('#')) and not(i.startswith('*')) and i!='\n'] | |
il.extend(it) | |
# check for file patterns too. Currently unused due to complications | |
# ifl = [i.strip() for i in tmp if i.startswith('*')] | |
# print(il, sep='\n') | |
cat = os.getcwd() | |
fname = f'{os.path.basename(cat)}_{datetime.today():%Y%m%d_%H.%M.%S}.zip' | |
il.append(fname) | |
with zipfile.ZipFile(fname, 'w', zipfile.ZIP_DEFLATED) as zipf: | |
zipdir(cat, zipf) | |
print('\n* Done! *') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment