Last active
March 9, 2022 21:46
-
-
Save kivS/1eb9f1f86877e4d4048b1b6ce33abfad to your computer and use it in GitHub Desktop.
script to generate a chrome extension zip package whilst ignoring certain files. Assumes chrome extension code is inside a directory called `src`
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 | |
''' Creates a zip file from src folder into a build directory ''' | |
import os | |
import zipfile | |
import glob | |
APP_NAME = 'Fluctus' | |
BUILD_DIR = 'build' # where zip package will be stored | |
FILE_EXT_TO_IGNORE = ('.ts',) # file extensions to keep out of the zip extension package | |
if not(os.path.exists(f'{BUILD_DIR}')): | |
print('Creating build directory..\n') | |
os.mkdir(f'./{BUILD_DIR}') | |
else: | |
# clean build folder | |
print('cleaning directory.. \n') | |
for file in glob.glob(f'{BUILD_DIR}/*'): | |
os.unlink(file) | |
# create zipfile | |
with zipfile.ZipFile(f'{BUILD_DIR}/{APP_NAME}.zip', 'w', zipfile.ZIP_DEFLATED) as zip: | |
# walk down subdirs of src and get the files that will go into the zip file | |
for (path, dirs, files) in os.walk('src'): | |
if(len(files) < 1): | |
continue | |
for file in files: | |
file_path = os.path.join(path, file) | |
print(f'File found: {file_path}') | |
# skip ignored files | |
# get file extension(eg: '.json') & check if it's in the ignore list | |
if(file[file.find('.'):] in FILE_EXT_TO_IGNORE): | |
print('ignoring file... \n') | |
continue | |
# name for file in zipfile | |
# remove 'src' folder from file so only contents of src will be in the final zip | |
zipped_file_name = file_path[4:] | |
print(f'zipping file as: {zipped_file_name} \n') | |
zip.write(file_path, arcname=zipped_file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment