|
| 1 | +#! python3 |
| 2 | +""" |
| 3 | +Original source: https://github.com/diffCheckOrg/diffCheck/blob/main/invokes/yakerize.py |
| 4 | +
|
| 5 | +Yakerize.py was originally developed as part of the DiffCheck plugin by |
| 6 | +Andrea Settimi, Damien Gilliard, Eleni Skevaki, Marirena Kladeftira (IBOIS, CRCL, EPFL) in 2024. |
| 7 | +It is distributed under the MIT License, provided this attribution is retained. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import argparse |
| 13 | +import shutil |
| 14 | + |
| 15 | + |
| 16 | +def main( |
| 17 | + gh_components_dir: str, |
| 18 | + build_dir: str, |
| 19 | + manifest_path: str, |
| 20 | + logo_path: str, |
| 21 | + readme_path: str, |
| 22 | + license_path: str, |
| 23 | +) -> bool: |
| 24 | + current_file_path = os.path.abspath(__file__) |
| 25 | + current_directory = os.path.dirname(current_file_path) |
| 26 | + build_dir = os.path.abspath(build_dir) |
| 27 | + |
| 28 | + ##################################################################### |
| 29 | + # Copy manifest, logo, misc folder (readme, license, etc) |
| 30 | + ##################################################################### |
| 31 | + # if not clean build dir, clean it |
| 32 | + if os.path.isdir(build_dir): |
| 33 | + for f in os.listdir(build_dir): |
| 34 | + file_path = os.path.join(build_dir, f) |
| 35 | + try: |
| 36 | + if os.path.isfile(file_path) or os.path.islink(file_path): |
| 37 | + os.unlink(file_path) |
| 38 | + elif os.path.isdir(file_path): |
| 39 | + shutil.rmtree(file_path) |
| 40 | + except Exception as e: |
| 41 | + print(f"Failed to delete {file_path}: {e}") |
| 42 | + return False |
| 43 | + |
| 44 | + shutil.copy(manifest_path, build_dir) |
| 45 | + shutil.copy(logo_path, build_dir) |
| 46 | + path_miscdir: str = os.path.join(build_dir, "misc") |
| 47 | + os.makedirs(path_miscdir, exist_ok=False) |
| 48 | + shutil.copy(readme_path, path_miscdir) |
| 49 | + shutil.copy(license_path, path_miscdir) |
| 50 | + |
| 51 | + for f in os.listdir(gh_components_dir): |
| 52 | + if f.endswith(".ghuser"): |
| 53 | + shutil.copy(os.path.join(gh_components_dir, f), build_dir) |
| 54 | + print( |
| 55 | + f"Copied the manifest, logo, readme, license, and ghuser files to the dir {build_dir}." |
| 56 | + ) |
| 57 | + |
| 58 | + ##################################################################### |
| 59 | + # Yak exe |
| 60 | + ##################################################################### |
| 61 | + yak_exe_path: str = os.path.join(current_directory, "yaker", "exec", "Yak.exe") |
| 62 | + yak_exe_path = os.path.abspath(yak_exe_path) |
| 63 | + if not os.path.isfile(yak_exe_path): |
| 64 | + print(f"Yak.exe not found at {yak_exe_path}.") |
| 65 | + return False |
| 66 | + path_current: str = os.getcwd() |
| 67 | + os.chdir(build_dir) |
| 68 | + os.system("cd") |
| 69 | + try: |
| 70 | + os.system(f"{yak_exe_path} build --platform win") |
| 71 | + except Exception as e: |
| 72 | + print(f"Failed to build the yak package: {e}") |
| 73 | + return False |
| 74 | + if not any([f.endswith(".yak") for f in os.listdir(build_dir)]): |
| 75 | + print("No .yak file was created in the build directory.") |
| 76 | + return False |
| 77 | + os.chdir(path_current) |
| 78 | + |
| 79 | + return True |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + parser = argparse.ArgumentParser( |
| 84 | + description="The invoke task to yakerize the ghcomponents into a yak package." |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + "--gh-components-dir", |
| 88 | + type=str, |
| 89 | + required=True, |
| 90 | + help="The path to the ghcomponents directory.", |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "--build-dir", |
| 94 | + type=str, |
| 95 | + required=True, |
| 96 | + default="./build/yak", |
| 97 | + help="The path where the yak will be built.", |
| 98 | + ) |
| 99 | + parser.add_argument( |
| 100 | + "--manifest-path", |
| 101 | + type=str, |
| 102 | + required=True, |
| 103 | + default="./manifest.yml", |
| 104 | + help="The path to the manifest file.", |
| 105 | + ) |
| 106 | + parser.add_argument( |
| 107 | + "--logo-path", |
| 108 | + type=str, |
| 109 | + required=True, |
| 110 | + default="./logo.png", |
| 111 | + help="The path to the logo file.", |
| 112 | + ) |
| 113 | + parser.add_argument( |
| 114 | + "--license-path", |
| 115 | + type=str, |
| 116 | + required=True, |
| 117 | + default="./LICENSE", |
| 118 | + help="The path to the license file.", |
| 119 | + ) |
| 120 | + parser.add_argument( |
| 121 | + "--readme-path", |
| 122 | + type=str, |
| 123 | + required=True, |
| 124 | + default="./README.md", |
| 125 | + help="The path to the readme file.", |
| 126 | + ) |
| 127 | + |
| 128 | + args = parser.parse_args() |
| 129 | + parse_errors = [] |
| 130 | + |
| 131 | + is_gh_components_dir_correct = True |
| 132 | + if not os.path.isdir(args.gh_components_dir): |
| 133 | + parse_errors.append( |
| 134 | + f"Path to ghcomponents directory is invalid: {args.gh_components_dir}" |
| 135 | + ) |
| 136 | + is_gh_components_dir_correct = False |
| 137 | + if not any([f.endswith(".ghuser") for f in os.listdir(args.gh_components_dir)]): |
| 138 | + parse_errors.append( |
| 139 | + f"No .ghuser files found in the ghcomponents directory: {args.gh_components_dir}" |
| 140 | + ) |
| 141 | + is_gh_components_dir_correct = False |
| 142 | + for f in os.listdir(args.gh_components_dir): |
| 143 | + if not f.endswith(".ghuser"): |
| 144 | + parse_errors.append( |
| 145 | + f"Found non .ghuser file in the ghcomponents directory: {f}" |
| 146 | + ) |
| 147 | + is_gh_components_dir_correct = False |
| 148 | + |
| 149 | + is_build_dir_correct = True |
| 150 | + if os.path.isdir(args.build_dir): |
| 151 | + for f in os.listdir(args.build_dir): |
| 152 | + file_path = os.path.join(args.build_dir, f) |
| 153 | + try: |
| 154 | + if os.path.isfile(file_path) or os.path.islink(file_path): |
| 155 | + os.unlink(file_path) |
| 156 | + elif os.path.isdir(file_path): |
| 157 | + shutil.rmtree(file_path) |
| 158 | + except Exception as e: |
| 159 | + print(f"Failed to delete {file_path}: {e}") |
| 160 | + is_build_dir_correct = False |
| 161 | + else: |
| 162 | + os.makedirs(args.build_dir, exist_ok=False) |
| 163 | + if not os.path.isdir(args.build_dir): |
| 164 | + parse_errors.append(f"Path to build directory is invalid: {args.build_dir}") |
| 165 | + is_build_dir_correct = False |
| 166 | + |
| 167 | + is_manifest_path_correct = True |
| 168 | + if not os.path.isfile(args.manifest_path): |
| 169 | + parse_errors.append(f"Path to manifest file is invalid: {args.manifest_path}") |
| 170 | + is_manifest_path_correct = False |
| 171 | + |
| 172 | + is_logo_path_correct = True |
| 173 | + if not os.path.isfile(args.logo_path): |
| 174 | + parse_errors.append(f"Path to logo file is invalid: {args.logo_path}") |
| 175 | + is_logo_path_correct = False |
| 176 | + |
| 177 | + is_license_path_correct = True |
| 178 | + if not os.path.isfile(args.license_path): |
| 179 | + parse_errors.append(f"Path to license file is invalid: {args.license_path}") |
| 180 | + is_license_path_correct = False |
| 181 | + |
| 182 | + is_readme_path_correct = True |
| 183 | + if not os.path.isfile(args.readme_path): |
| 184 | + parse_errors.append(f"Path to readme file is invalid: {args.readme_path}") |
| 185 | + is_readme_path_correct = False |
| 186 | + |
| 187 | + print("Yakerize check:") |
| 188 | + print( |
| 189 | + f"\t[{'x' if is_gh_components_dir_correct else ' '}] Ghcomponents directory: {args.gh_components_dir}" |
| 190 | + ) |
| 191 | + print( |
| 192 | + f"\t[{'x' if is_build_dir_correct else ' '}] Build directory: {args.build_dir}" |
| 193 | + ) |
| 194 | + print( |
| 195 | + f"\t[{'x' if is_manifest_path_correct else ' '}] Manifest file path: {args.manifest_path}" |
| 196 | + ) |
| 197 | + print( |
| 198 | + f"\t[{'x' if is_logo_path_correct else ' '}] Logo file path: {args.logo_path}" |
| 199 | + ) |
| 200 | + print( |
| 201 | + f"\t[{'x' if is_license_path_correct else ' '}] License file path: {args.license_path}" |
| 202 | + ) |
| 203 | + print( |
| 204 | + f"\t[{'x' if is_readme_path_correct else ' '}] Readme file path: {args.readme_path}" |
| 205 | + ) |
| 206 | + if parse_errors: |
| 207 | + for error in parse_errors: |
| 208 | + print(error) |
| 209 | + sys.exit(1) |
| 210 | + print("Starting the yakerize task...") |
| 211 | + |
| 212 | + res = main( |
| 213 | + gh_components_dir=args.gh_components_dir, |
| 214 | + build_dir=args.build_dir, |
| 215 | + manifest_path=args.manifest_path, |
| 216 | + logo_path=args.logo_path, |
| 217 | + readme_path=args.readme_path, |
| 218 | + license_path=args.license_path, |
| 219 | + ) |
| 220 | + if res: |
| 221 | + print("[x] Yakerize task completed.") |
| 222 | + else: |
| 223 | + print("[ ] Yakerize task failed.") |
| 224 | + sys.exit(1) |
0 commit comments