Skip to content

Commit

Permalink
now no file is treated as broad "temporary", now it is hammer file, o…
Browse files Browse the repository at this point in the history
…r (yet) unzipped compiled package. also upgrade to CLI, now at prompt at the beginning, it will state all chosen options. not using temporary folders anymore. fixed some small issues, added comments
  • Loading branch information
shaperones0 committed Jun 26, 2021
1 parent b9c0f4b commit d14aa47
Showing 1 changed file with 64 additions and 56 deletions.
120 changes: 64 additions & 56 deletions compile_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import argparse

OPTIMISE = False
# path to a folder, where all useful hammer files will be created, if it stated that they need to be created
HAMMER_PATH = None


def clean_vmf(vmf_path):
Expand Down Expand Up @@ -107,6 +109,7 @@ def search_list_of_dirs(list_of_dirs, zip_path):

def build_package(package_path, pack_zip_path):
"""Build the packages in a given folder."""
global HAMMER_PATH

zip_file = ZipFile(
pack_zip_path,
Expand Down Expand Up @@ -141,8 +144,16 @@ def build_package(package_path, pack_zip_path):
# Skip music files..
hammer_path = None
else:
hammer_path = os.path.join('zips/hammer/', hammer_path)
os.makedirs(os.path.dirname(hammer_path), exist_ok=True)
# if this files can be added to HAMMER_PATH folder,
# check if this folder was specified (because if its not,
# then you should not create hammer files)
if HAMMER_PATH is not None:
hammer_path = os.path.join(HAMMER_PATH, hammer_path)
os.makedirs(os.path.dirname(hammer_path), exist_ok=True)
else:
hammer_path = None
# yea, if at this point hammer_path is None, then file we currently looking at do not
# need to be put into a HAMMER_PATH folder (including every case)

print('.', end='', flush=True)

Expand Down Expand Up @@ -186,13 +197,13 @@ def main():
parser.add_argument("-c", "--confirm", action="store_const", const=True, default=False,
help="Will skip a confirmation prompt.", dest="skip_confirm")
# will specify an output path. if not specified, args.output will be set to None, and nothing will be moved then
parser.add_argument("-o", "--output", default=None,
parser.add_argument("-o", "--output", default="zips",
help="Will specify an output folder, otherwise \"./zips\" will be used.", dest="output")
# since after moving output files to output destination (specified above), there remains lot of junk files
# that looks like was for debugging and its safe to delete them. specifying this option will prevent that.
parser.add_argument("-p", "--preserve-temp", action="store_const", const=True, default=False,
help="Will not delete temporary and hammer files (you probably won't need hammer files "
"if you are not planning to use hammer)", dest="preserve_temp")
# during packing files, it can create several useful hammer files, which will be placed in specified directory,
# or won't be created if not specified
parser.add_argument("-hp", "--hammer_path", default=None,
help="Will create some useful files, which you can use in hammer later. "
"All files will be stored in specified directory.", dest="hammer")
# if NOT specified, then args.zip will be set to None, if specified BUT NOT set to any string value, then
# args.zip will be set to "" (empty string). Later, if args.zip remains set to "", then no zip will be created
parser.add_argument("--zip", nargs="?", default=None, const="",
Expand All @@ -205,25 +216,28 @@ def main():
args = parser.parse_args()
inp_list = args.input
output = args.output
global OPTIMISE
global OPTIMISE, HAMMER_PATH
OPTIMISE = args.optimize
do_zip = args.zip
HAMMER_PATH = args.hammer
if not args.skip_confirm:
print("You specified these folders:")
print("\n".join(inp_list))
# shows list with all files specified (in case using CLI is not
# so intuitive), also tells if you have chosen to optimize output (below)
print("These will be optimized" if OPTIMISE else "These will NOT be optimized")
if not conv_bool(input("Continue? (y/n) ")):
# shows list with all files specified (in case using CLI is not so intuitive)
print("You specified these folders:\n-------------------------------")
for inp in inp_list:
print("|", inp)
print("-------------------------------")
print("Selected options:")
print("* Optimization: " + ("ON" if OPTIMISE else "OFF"))
print("* Output folder: \"" + output + "\"")
print("* Hammer folder: " + ("OFF" if (HAMMER_PATH is None) else ("\"" + HAMMER_PATH + "\"")))
print("* Final zip: " + ("skip prompt" if do_zip == "" else
("not skip prompt" if do_zip is None else
do_zip)))
print()
if not conv_bool(input("---> Continue? (y/n) ")):
sys.exit(0) # confirmation just in case

# will dump all temporary files to 'zips' folder. also, all compiled packages will be left in
# zips/sml or zips/lrg depending on whether you chose to optimize it or not
zip_path = os.path.join(
os.getcwd(),
'zips',
'sml' if OPTIMISE else 'lrg',
)
zip_path = output

# if there is no such dir, then create it. if there is, and its empty,
# then use it. if there is and its not empty - throw error (since whole directory will be erased)
Expand All @@ -233,9 +247,22 @@ def main():
else:
os.makedirs(zip_path, exist_ok=True)

# if hammer path was specified then
if HAMMER_PATH:
# if there is no such dir, then create it. if there is, and its empty,
# then use it. if there is and its not empty - throw error (why not)
if os.path.isdir(HAMMER_PATH):
if os.listdir(HAMMER_PATH):
raise ValueError("The hammer files directory is not empty")
else:
os.makedirs(HAMMER_PATH, exist_ok=True)

packages_list = [] # list with filenames of all packages
# yield will return found packages one by one, so it is easy to iterate them
# without the need to process everything at once
for package in search_list_of_dirs(inp_list, zip_path):
# package is a tuple (raw_package_directory, path_to_compiled_version)
packages_list.append(os.path.relpath(package[1], zip_path))
build_package(*package)
print('Done!')

Expand All @@ -247,48 +274,29 @@ def main():
pack_name = 'BEE{}_packages.zip'.format(input('Version: '))
else:
pack_name = do_zip # do_zip also can be blank
# if its not blank, and not ending with .zip, then add .zip to filename
if pack_name and pack_name[-4:] != ".zip":
pack_name += ".zip"
# if pack_name remained blank to this stage, then no zip will be created
if pack_name != "":
print('Building main zip...')
with ZipFile(os.path.join('zips', pack_name), 'w', compression=ZIP_DEFLATED) as zip_file:
for file in os.listdir(zip_path):
with ZipFile(os.path.join(zip_path, pack_name), 'w', compression=ZIP_DEFLATED) as zip_file:
for file in packages_list:
# previously it put in zip all contains of a folder, including itself (no clue why it
# haven't raised any error), also, since hammer files do not need to appear in zip,
# packages_list variable was created
zip_file.write(os.path.join(zip_path, file), os.path.join('packages/', file))
print('.', end='', flush=True)
print('Done!')
if output is not None:
print("Moving zip to output destination")
shutil.move(os.path.join('zips', pack_name), output)
print('Done!')
if not args.preserve_temp:
print("Deleting temporary and hammer files")
shutil.rmtree("zips")
print("Done!")
else:
if not args.preserve_temp:
print("Deleting temporary and hammer files")
shutil.rmtree("zips/hammer")
if OPTIMISE:
shutil.rmtree("zips/sml")
else:
shutil.rmtree("zips/lrg")
print("Done!")
print("Deleting unzipped files...")
# removes all files that are in packages_list var
for file in packages_list:
os.remove(os.path.join(zip_path, file))
print("Done!")
else:
# dont make zip
if output is not None:
print("Moving files to output destination")
if OPTIMISE:
shutil.move("zips/sml", output)
else:
shutil.move("zips/lrg", output)
if not args.preserve_temp:
print("Deleting temporary and hammer files")
shutil.rmtree("zips")
print("Done!")
else:
if not args.preserve_temp:
print("Deleting hammer files")
shutil.rmtree("zips/hammer")
print("Done!")
# we dont actually need to do anything else
pass


if __name__ == '__main__':
Expand Down

0 comments on commit d14aa47

Please sign in to comment.