|
7 | 7 |
|
8 | 8 | from gamuLogger import debug, info, error, critical, Logger, LEVELS |
9 | 9 |
|
10 | | -def parseModuleNames(module_name: str): |
11 | | - # GamuNetwork/ModuleName -> ModuleName |
12 | | - return module_name.replace("\\", "/").split("/")[-1] |
13 | | - |
14 | | -def createJson(module_name, module_version : Version, module_type : ModuleTypes, module_description, module_author, branch = "main"): |
15 | | - debug(f"Creating json file for module {module_name} version {module_version}") |
16 | | - module = { |
17 | | - "name": module_name, |
18 | | - "version": str(module_version), |
19 | | - "type": str(module_type), |
20 | | - "description": module_description, |
21 | | - "author": module_author, |
22 | | - "created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), |
23 | | - "repository": f"https://github.com/GamuNetwork/{module_name}", |
24 | | - "branch": branch |
25 | | - } |
26 | | - result = json.dumps(module, indent=4) |
27 | | - debug(f"Json file created: {result}") |
28 | | - return result |
29 | | - |
30 | | -def addFolderToZip(zip: ZipFile, arc_path, folder, parent_folder = ""): |
31 | | - debug(f"Adding folder {folder} to zip") |
32 | | - for file in os.listdir(folder): |
33 | | - file_path = os.path.join(folder, file) |
34 | | - if os.path.isfile(file_path): |
35 | | - debug(f"Adding file {file} to zip") |
36 | | - zip.write(file_path, os.path.join(arc_path, parent_folder, file)) |
37 | | - elif os.path.isdir(file_path): |
38 | | - addFolderToZip(zip, arc_path, file_path, os.path.join(parent_folder, file)) |
39 | | - |
40 | | -def createArchive(compiled_code_folder, module_name, module_version : Version, module_type : ModuleTypes, module_description, module_author, branch = "main"): |
41 | | - with ZipFile(f'{module_name}-{str(module_version)}.gamod', 'w') as zip: |
42 | | - addFolderToZip(zip, "build", compiled_code_folder) |
43 | | - debug(f"Compiled code folder added to archive") |
44 | | - zip.writestr('module.json', createJson(module_name, module_version, module_type, module_description, module_author, branch)) |
45 | | - debug(f"Json file added to archive") |
46 | | - |
47 | | - return f'{module_name}-{str(module_version)}.gamod' |
48 | | - |
49 | | -if __name__ == "__main__": |
50 | | - import argparse |
51 | | - |
52 | | - def handleParserError(message): |
53 | | - error(message) |
54 | | - raise ValueError("Invalid arguments") |
55 | | - |
56 | | - def createParser(): |
57 | | - parser = argparse.ArgumentParser(description="Create a module archive") |
58 | | - parser.error = handleParserError |
59 | | - parser.add_argument("compiled_code_folder", help="The compiled code folder (dist folder for javascript)") |
60 | | - parser.add_argument("module_name", help="The module name") |
61 | | - parser.add_argument("module_version", help="The module version") |
62 | | - parser.add_argument("module_type", help="The module type") |
63 | | - parser.add_argument("module_description", help="The module description") |
64 | | - parser.add_argument("module_author", help="The module author") |
65 | | - parser.add_argument("--branch", help="The branch of the repository", default="main") |
66 | | - parser.add_argument("--debug", "-d", help="Enable debug mode", action="store_true") |
67 | | - return parser |
68 | | - |
69 | | - info("Starting module creation") |
70 | | - |
71 | | - try: |
72 | | - parser = createParser() |
73 | | - args = parser.parse_args() |
| 10 | +from .utils import addFolderToZip |
| 11 | + |
| 12 | +class Archive: |
| 13 | + def __init__(self, compiled_code_folder, module_name, module_version : Version, module_type : ModuleTypes, module_description, module_author, branch = "main"): |
| 14 | + self.compiled_code_folder = compiled_code_folder |
| 15 | + self.module_name = module_name |
| 16 | + self.module_version = module_version |
| 17 | + self.module_type = module_type |
| 18 | + self.module_description = module_description |
| 19 | + self.module_author = module_author |
| 20 | + self.branch = branch |
74 | 21 |
|
75 | | - if args.debug: |
76 | | - Logger.setLevel("stdout", LEVELS.DEBUG) |
| 22 | + self.archiveName = f'{self.module_name}-{str(self.module_version)}.gamod' |
77 | 23 |
|
78 | | - module_version = Version.fromString(args.module_version) |
79 | | - module_type = ModuleTypes.fromString(args.module_type) |
80 | | - |
81 | | - info(f"Creating archive for module {args.module_name} version {module_version}") |
| 24 | + def create(self): |
| 25 | + self.zipFile = ZipFile(self.archiveName, 'w') |
| 26 | + self.__addCode() |
| 27 | + self.__createJson() |
82 | 28 |
|
83 | | - createArchive(args.compiled_code_folder, parseModuleNames(args.module_name), module_version, module_type, args.module_description, args.module_author, args.branch) |
84 | | - |
85 | | - except Exception as e: |
86 | | - critical(f"Error creating module: {str(e)}") |
87 | | - exit(1) |
88 | | - |
89 | | - info(f"Archive created: {args.module_name}-{str(module_version)}.gamod") |
90 | | - exit(0) |
| 29 | + def __addCode(self): |
| 30 | + debug(f"Adding code from {self.compiled_code_folder} to zip") |
| 31 | + addFolderToZip(self.zipFile, "build", self.compiled_code_folder) |
| 32 | + |
| 33 | + def __createJson(self): |
| 34 | + debug(f"Creating json file for module {self.module_name} version {self.module_version}") |
| 35 | + module = { |
| 36 | + "name": self.module_name, |
| 37 | + "version": str(self.module_version), |
| 38 | + "type": str(self.module_type), |
| 39 | + "description": self.module_description, |
| 40 | + "author": self.module_author, |
| 41 | + "created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), |
| 42 | + "repository": f"https://github.com/GamuNetwork/{self.module_name}", |
| 43 | + "branch": self.branch |
| 44 | + } |
| 45 | + result = json.dumps(module, indent=4) |
| 46 | + debug(f"Json file created: {result}") |
| 47 | + self.zipFile.writestr('module.json', result) |
| 48 | + |
| 49 | + def __str__(self): |
| 50 | + return self.archiveName |
0 commit comments