|
| 1 | +from pathlib import Path |
| 2 | +import sys |
| 3 | +from selenium.common.exceptions import TimeoutException |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import json |
| 7 | +from typing import List, Dict |
| 8 | +from io import FileIO |
| 9 | + |
| 10 | +# pycharm complains that build_assets is an unresolved ref |
| 11 | +# don't worry about it, the script still runs |
| 12 | +from build_assets.selenium_runner.BuildSeleniumRunner import BuildSeleniumRunner |
| 13 | +from build_assets import filehandler, arg_getters, util, api_handler |
| 14 | + |
| 15 | +def main(): |
| 16 | + """ |
| 17 | + Build the icons using Icomoon. Also optimize the svgs. |
| 18 | + """ |
| 19 | + runner = None |
| 20 | + logfile = open("log.txt", "w") |
| 21 | + try: |
| 22 | + args = arg_getters.get_selenium_runner_args(has_token=False) |
| 23 | + new_icons = get_icons_for_building(args.icomoon_json_path, args.devicon_json_path, logfile) |
| 24 | + if len(new_icons) == 0: |
| 25 | + sys.exit("No files need to be uploaded. Ending script...") |
| 26 | + |
| 27 | + print(f"There are {len(new_icons)} icons to be build. Here are they:", *new_icons, sep = "\n", file=logfile) |
| 28 | + |
| 29 | + print("Begin optimizing files...", file=logfile) |
| 30 | + optimize_svgs(new_icons, args.icons_folder_path, logfile=logfile) |
| 31 | + |
| 32 | + print("Updating the icomoon json...", file=logfile) |
| 33 | + update_icomoon_json(new_icons, args.icomoon_json_path, logfile) |
| 34 | + |
| 35 | + print("Start the building icons process...", file=logfile) |
| 36 | + icon_svgs = filehandler.get_svgs_paths( |
| 37 | + new_icons, args.icons_folder_path, icon_versions_only=True) |
| 38 | + zip_name = "devicon-v1.0.zip" |
| 39 | + zip_path = Path(args.download_path, zip_name) |
| 40 | + screenshot_folder = filehandler.create_screenshot_folder("./") |
| 41 | + |
| 42 | + runner = BuildSeleniumRunner(args.download_path, |
| 43 | + args.geckodriver_path, args.headless, log_output=logfile) |
| 44 | + print("Building icons...", file=logfile) |
| 45 | + runner.build_icons(args.icomoon_json_path, zip_path, |
| 46 | + icon_svgs, screenshot_folder) |
| 47 | + |
| 48 | + print("Extracting files...", file=logfile) |
| 49 | + filehandler.extract_files(str(zip_path), args.download_path, logfile) |
| 50 | + print("Renaming extracted files...", file=logfile) |
| 51 | + filehandler.rename_extracted_files(args.download_path, logfile) |
| 52 | + |
| 53 | + print("Task completed!", file=logfile) |
| 54 | + except TimeoutException as e: |
| 55 | + util.exit_with_err(Exception("Selenium Time Out Error: \n" + str(e)), logfile) |
| 56 | + except Exception as e: |
| 57 | + util.exit_with_err(e, logfile) |
| 58 | + finally: |
| 59 | + print("Exiting", file=logfile) |
| 60 | + if runner is not None: |
| 61 | + runner.close() |
| 62 | + logfile.close() |
| 63 | + |
| 64 | + |
| 65 | +def get_icons_for_building(icomoon_json_path: str, devicon_json_path: str, logfile: FileIO): |
| 66 | + """ |
| 67 | + Get the icons for building. |
| 68 | + :param icomoon_json_path - the path to the `icomoon.json`. |
| 69 | + :param devicon_json_path - the path to the `devicon.json`. |
| 70 | + :param logfile. |
| 71 | + :return a list of dict containing info on the icons. These are |
| 72 | + from the `devicon.json`. |
| 73 | + """ |
| 74 | + |
| 75 | + new_icons = [] |
| 76 | + |
| 77 | + # get any icons that might not have been found by the API |
| 78 | + # sometimes happen due to the PR being opened before the latest build release |
| 79 | + new_icons_from_devicon_json = filehandler.find_new_icons_in_devicon_json( |
| 80 | + devicon_json_path, icomoon_json_path) |
| 81 | + |
| 82 | + for icon in new_icons_from_devicon_json: |
| 83 | + if icon not in new_icons: |
| 84 | + new_icons.append(icon) |
| 85 | + |
| 86 | + return new_icons |
| 87 | + |
| 88 | + |
| 89 | +def optimize_svgs(new_icons: List[str], icons_folder_path: str, logfile: FileIO): |
| 90 | + """ |
| 91 | + Optimize the newly added svgs. This is done in batches |
| 92 | + since the command line has a limit on characters allowed. |
| 93 | + :param new_icons - the new icons that need to be optimized. |
| 94 | + :param icons_folder_path - the path to the /icons folder. |
| 95 | + :param logfile - the file obj to store logging info in. |
| 96 | + """ |
| 97 | + svgs = filehandler.get_svgs_paths(new_icons, icons_folder_path, icon_versions_only=False) |
| 98 | + start = 0 |
| 99 | + step = 10 |
| 100 | + for i in range(start, len(svgs), step): |
| 101 | + batch = svgs[i:i + step] |
| 102 | + print(f"Optimizing these files\n{batch}", file=logfile) |
| 103 | + subprocess.run(["npm", "run", "optimize-svg", "--", f"--svgFiles={json.dumps(batch)}"], shell=True) |
| 104 | + |
| 105 | + |
| 106 | +def update_icomoon_json(new_icons: List[str], icomoon_json_path: str, logfile: FileIO): |
| 107 | + """ |
| 108 | + Update the `icomoon.json` if it contains any icons |
| 109 | + that needed to be updated. This will remove the icons |
| 110 | + from the `icomoon.json` so the build script will reupload |
| 111 | + it later. |
| 112 | + """ |
| 113 | + icomoon_json = filehandler.get_json_file_content(icomoon_json_path) |
| 114 | + cur_len = len(icomoon_json["icons"]) |
| 115 | + messages = [] |
| 116 | + |
| 117 | + wrapper_function = lambda icomoon_icon : find_icomoon_icon_not_in_new_icons( |
| 118 | + icomoon_icon, new_icons, messages) |
| 119 | + icons_to_keep = filter(wrapper_function, icomoon_json["icons"]) |
| 120 | + icomoon_json["icons"] = list(icons_to_keep) |
| 121 | + |
| 122 | + new_len = len(icomoon_json["icons"]) |
| 123 | + print(f"Update completed. Removed {cur_len - new_len} icons:", *messages, sep='\n', file=logfile) |
| 124 | + filehandler.write_to_file(icomoon_json_path, json.dumps(icomoon_json)) |
| 125 | + |
| 126 | + |
| 127 | +def find_icomoon_icon_not_in_new_icons(icomoon_icon: Dict, new_icons: List, messages: List): |
| 128 | + """ |
| 129 | + Find all the icomoon icons that are not listed in the new icons. |
| 130 | + This also add logging for which icons were removed. |
| 131 | + :param icomoon_icon - a dict object from the icomoon.json's `icons` attribute. |
| 132 | + :param new_icons - a list of new icons. Each element is an object from the `devicon.json`. |
| 133 | + :param messages - an empty list where the function can attach logging on which |
| 134 | + icon were removed. |
| 135 | + """ |
| 136 | + for new_icon in new_icons: |
| 137 | + pattern = re.compile(f"^{new_icon['name']}-") |
| 138 | + if pattern.search(icomoon_icon["properties"]["name"]): |
| 139 | + message = f"-'{icomoon_icon['properties']['name']}' cause it matches '{new_icon['name']}'" |
| 140 | + messages.append(message) |
| 141 | + return False |
| 142 | + return True |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + main() |
0 commit comments