|
20 | 20 |
|
21 | 21 |
|
22 | 22 | def load_module(project_root, rel_path, filename, module_name): |
23 | | - """Loads a module directly from its path in src/.""" |
24 | | - path = os.path.join(project_root, "src", rel_path.replace(".", os.sep), filename) |
25 | | - if not os.path.exists(path): |
26 | | - raise RuntimeError(f"Could not find module at {path}") |
27 | | - |
28 | | - # Add src to sys.path so absolute imports work |
29 | | - src_path = os.path.abspath(os.path.join(project_root, "src")) |
30 | | - if src_path not in sys.path: |
31 | | - sys.path.insert(0, src_path) |
32 | | - |
33 | | - spec = importlib.util.spec_from_file_location(module_name, path) |
34 | | - if spec and spec.loader: |
35 | | - module = importlib.util.module_from_spec(spec) |
36 | | - # Set the package context to allow relative imports if any |
37 | | - module.__package__ = rel_path |
38 | | - sys.modules[module_name] = module |
39 | | - spec.loader.exec_module(module) |
40 | | - return module |
41 | | - raise RuntimeError(f"Could not load module from {path}") |
| 23 | + """Loads a module directly from its path in src/.""" |
| 24 | + path = os.path.join(project_root, "src", rel_path.replace(".", os.sep), filename) |
| 25 | + if not os.path.exists(path): |
| 26 | + raise RuntimeError(f"Could not find module at {path}") |
| 27 | + |
| 28 | + # Add src to sys.path so absolute imports work |
| 29 | + src_path = os.path.abspath(os.path.join(project_root, "src")) |
| 30 | + if src_path not in sys.path: |
| 31 | + sys.path.insert(0, src_path) |
| 32 | + |
| 33 | + spec = importlib.util.spec_from_file_location(module_name, path) |
| 34 | + if spec and spec.loader: |
| 35 | + module = importlib.util.module_from_spec(spec) |
| 36 | + # Set the package context to allow relative imports if any |
| 37 | + module.__package__ = rel_path |
| 38 | + sys.modules[module_name] = module |
| 39 | + spec.loader.exec_module(module) |
| 40 | + return module |
| 41 | + raise RuntimeError(f"Could not load module from {path}") |
42 | 42 |
|
43 | 43 |
|
44 | 44 | class PackSpecsBuildHook(BuildHookInterface): |
45 | 45 |
|
46 | | - def initialize(self, version, build_data): |
47 | | - project_root = self.root |
48 | | - |
49 | | - # Load constants and utils dynamically from src/ |
50 | | - schema_path = "a2ui.core.schema" |
51 | | - a2ui_constants = load_module( |
52 | | - project_root, schema_path, "constants.py", "_constants_load" |
53 | | - ) |
54 | | - a2ui_utils = load_module(project_root, schema_path, "utils.py", "_utils_load") |
55 | | - |
56 | | - basic_catalog_constants = load_module( |
57 | | - project_root, |
58 | | - "a2ui.basic_catalog", |
59 | | - "constants.py", |
60 | | - "_basic_catalog_constants_load", |
61 | | - ) |
62 | | - |
63 | | - spec_version_map = a2ui_constants.SPEC_VERSION_MAP |
64 | | - a2ui_asset_package = a2ui_constants.A2UI_ASSET_PACKAGE |
65 | | - specification_dir = a2ui_constants.SPECIFICATION_DIR |
66 | | - |
67 | | - # Dynamically find repo root by looking for specification_dir |
68 | | - repo_root = a2ui_utils.find_repo_root(project_root) |
69 | | - if not repo_root: |
70 | | - # Check for PKG-INFO which implies a packaged state (sdist). |
71 | | - # If PKG-INFO is present, trust the bundled assets. |
72 | | - if os.path.exists(os.path.join(project_root, "PKG-INFO")): |
73 | | - print("Repository root not found, but PKG-INFO present (sdist). Skipping copy.") |
74 | | - return |
75 | | - |
76 | | - raise RuntimeError( |
77 | | - f"Could not find repository root (looked for '{specification_dir}'" |
78 | | - " directory)." |
79 | | - ) |
80 | | - |
81 | | - # Target directory: src/a2ui/assets |
82 | | - target_base = os.path.join( |
83 | | - project_root, "src", a2ui_asset_package.replace(".", os.sep) |
84 | | - ) |
85 | | - |
86 | | - self._pack_schemas(repo_root, spec_version_map, target_base) |
87 | | - self._pack_basic_catalogs( |
88 | | - repo_root, basic_catalog_constants.BASIC_CATALOG_PATHS, target_base |
89 | | - ) |
90 | | - |
91 | | - def _pack_schemas(self, repo_root, spec_map, target_base): |
92 | | - for ver, schema_map in spec_map.items(): |
93 | | - target_dir = os.path.join(target_base, ver) |
94 | | - os.makedirs(target_dir, exist_ok=True) |
95 | | - |
96 | | - for _schema_key, source_rel_path in schema_map.items(): |
97 | | - self._copy_schema(repo_root, source_rel_path, target_dir) |
98 | | - |
99 | | - def _pack_basic_catalogs(self, repo_root, catalog_paths, target_base): |
100 | | - for ver, path_map in catalog_paths.items(): |
101 | | - target_dir = os.path.join(target_base, ver) |
102 | | - os.makedirs(target_dir, exist_ok=True) |
103 | | - |
104 | | - for _key, source_rel_path in path_map.items(): |
105 | | - self._copy_schema(repo_root, source_rel_path, target_dir) |
106 | | - |
107 | | - def _copy_schema(self, repo_root, source_rel_path, target_dir): |
108 | | - source_path = os.path.join(repo_root, source_rel_path) |
109 | | - |
110 | | - if not os.path.exists(source_path): |
111 | | - print( |
112 | | - f"WARNING: Source schema file not found at {source_path}. Build" |
113 | | - " might produce incomplete wheel if not running from monorepo" |
114 | | - " root." |
115 | | - ) |
116 | | - return |
117 | | - |
118 | | - filename = os.path.basename(source_path) |
119 | | - dst_file = os.path.join(target_dir, filename) |
120 | | - |
121 | | - print(f"Copying {source_path} -> {dst_file}") |
122 | | - shutil.copy2(source_path, dst_file) |
| 46 | + def initialize(self, version, build_data): |
| 47 | + project_root = self.root |
| 48 | + |
| 49 | + # Load constants and utils dynamically from src/ |
| 50 | + schema_path = "a2ui.core.schema" |
| 51 | + a2ui_constants = load_module( |
| 52 | + project_root, schema_path, "constants.py", "_constants_load" |
| 53 | + ) |
| 54 | + a2ui_utils = load_module(project_root, schema_path, "utils.py", "_utils_load") |
| 55 | + |
| 56 | + basic_catalog_constants = load_module( |
| 57 | + project_root, |
| 58 | + "a2ui.basic_catalog", |
| 59 | + "constants.py", |
| 60 | + "_basic_catalog_constants_load", |
| 61 | + ) |
| 62 | + |
| 63 | + spec_version_map = a2ui_constants.SPEC_VERSION_MAP |
| 64 | + a2ui_asset_package = a2ui_constants.A2UI_ASSET_PACKAGE |
| 65 | + specification_dir = a2ui_constants.SPECIFICATION_DIR |
| 66 | + |
| 67 | + # Dynamically find repo root by looking for specification_dir |
| 68 | + repo_root = a2ui_utils.find_repo_root(project_root) |
| 69 | + if not repo_root: |
| 70 | + # Check for PKG-INFO which implies a packaged state (sdist). |
| 71 | + # If PKG-INFO is present, trust the bundled assets. |
| 72 | + if os.path.exists(os.path.join(project_root, "PKG-INFO")): |
| 73 | + print( |
| 74 | + "Repository root not found, but PKG-INFO present (sdist). Skipping copy." |
| 75 | + ) |
| 76 | + return |
| 77 | + |
| 78 | + raise RuntimeError( |
| 79 | + f"Could not find repository root (looked for '{specification_dir}'" |
| 80 | + " directory)." |
| 81 | + ) |
| 82 | + |
| 83 | + # Target directory: src/a2ui/assets |
| 84 | + target_base = os.path.join( |
| 85 | + project_root, "src", a2ui_asset_package.replace(".", os.sep) |
| 86 | + ) |
| 87 | + |
| 88 | + self._pack_schemas(repo_root, spec_version_map, target_base) |
| 89 | + self._pack_basic_catalogs( |
| 90 | + repo_root, basic_catalog_constants.BASIC_CATALOG_PATHS, target_base |
| 91 | + ) |
| 92 | + |
| 93 | + def _pack_schemas(self, repo_root, spec_map, target_base): |
| 94 | + for ver, schema_map in spec_map.items(): |
| 95 | + target_dir = os.path.join(target_base, ver) |
| 96 | + os.makedirs(target_dir, exist_ok=True) |
| 97 | + |
| 98 | + for _schema_key, source_rel_path in schema_map.items(): |
| 99 | + self._copy_schema(repo_root, source_rel_path, target_dir) |
| 100 | + |
| 101 | + def _pack_basic_catalogs(self, repo_root, catalog_paths, target_base): |
| 102 | + for ver, path_map in catalog_paths.items(): |
| 103 | + target_dir = os.path.join(target_base, ver) |
| 104 | + os.makedirs(target_dir, exist_ok=True) |
| 105 | + |
| 106 | + for _key, source_rel_path in path_map.items(): |
| 107 | + self._copy_schema(repo_root, source_rel_path, target_dir) |
| 108 | + |
| 109 | + def _copy_schema(self, repo_root, source_rel_path, target_dir): |
| 110 | + source_path = os.path.join(repo_root, source_rel_path) |
| 111 | + |
| 112 | + if not os.path.exists(source_path): |
| 113 | + print( |
| 114 | + f"WARNING: Source schema file not found at {source_path}. Build" |
| 115 | + " might produce incomplete wheel if not running from monorepo" |
| 116 | + " root." |
| 117 | + ) |
| 118 | + return |
| 119 | + |
| 120 | + filename = os.path.basename(source_path) |
| 121 | + dst_file = os.path.join(target_dir, filename) |
| 122 | + |
| 123 | + print(f"Copying {source_path} -> {dst_file}") |
| 124 | + shutil.copy2(source_path, dst_file) |
0 commit comments