|
4 | 4 | import shutil |
5 | 5 | import subprocess |
6 | 6 | import sys |
| 7 | +import sysconfig |
7 | 8 | import zipfile |
8 | 9 | from importlib import resources |
9 | 10 |
|
@@ -93,15 +94,39 @@ def _extract_bundled_template(zip_name: str, destination: str) -> None: |
93 | 94 | Extract a bundled template zip into the destination directory. |
94 | 95 | Tries package resources first; falls back to repo root `templates/` at dev time. |
95 | 96 | """ |
96 | | - # Try to load from installed package resources first |
| 97 | + # Try to load from installed package resources first (if templates are packaged inside the module) |
97 | 98 | try: |
98 | | - pkg_templates = resources.files("pythonnative").joinpath("templates") |
99 | | - resource_path = str(pkg_templates.joinpath(zip_name)) |
100 | | - if os.path.exists(resource_path): |
101 | | - _extract_zip_to_destination(resource_path, destination) |
102 | | - return |
| 99 | + cand = resources.files("pythonnative").joinpath("templates").joinpath(zip_name) |
| 100 | + with resources.as_file(cand) as p: |
| 101 | + resource_path = str(p) |
| 102 | + if os.path.exists(resource_path): |
| 103 | + _extract_zip_to_destination(resource_path, destination) |
| 104 | + return |
| 105 | + except Exception: |
| 106 | + # Not packaged inside the module; try data-files installation locations next |
| 107 | + pass |
| 108 | + |
| 109 | + # Try sysconfig data dir (where data-files are typically installed) |
| 110 | + try: |
| 111 | + data_dir = sysconfig.get_paths().get("data") |
| 112 | + if data_dir: |
| 113 | + candidate = os.path.join(data_dir, "pythonnative", "templates", zip_name) |
| 114 | + if os.path.exists(candidate): |
| 115 | + _extract_zip_to_destination(candidate, destination) |
| 116 | + return |
| 117 | + except Exception: |
| 118 | + pass |
| 119 | + |
| 120 | + # Try site-packages purelib/platlib (some environments place data files here) |
| 121 | + try: |
| 122 | + purelib = sysconfig.get_paths().get("purelib") |
| 123 | + platlib = sysconfig.get_paths().get("platlib") |
| 124 | + for base in filter(None, [purelib, platlib]): |
| 125 | + candidate = os.path.join(base, "pythonnative", "templates", zip_name) |
| 126 | + if os.path.exists(candidate): |
| 127 | + _extract_zip_to_destination(candidate, destination) |
| 128 | + return |
103 | 129 | except Exception: |
104 | | - # Fall back to repo layout |
105 | 130 | pass |
106 | 131 |
|
107 | 132 | # Fallback: use repository-level templates directory |
@@ -143,6 +168,7 @@ def run_project(args: argparse.Namespace) -> None: |
143 | 168 | """ |
144 | 169 | # Determine the platform |
145 | 170 | platform: str = args.platform |
| 171 | + prepare_only: bool = getattr(args, "prepare_only", False) |
146 | 172 |
|
147 | 173 | # Define the build directory |
148 | 174 | build_dir: str = os.path.join(os.getcwd(), "build", platform) |
@@ -171,11 +197,17 @@ def run_project(args: argparse.Namespace) -> None: |
171 | 197 | shutil.copytree(src_dir, dest_dir, dirs_exist_ok=True) |
172 | 198 |
|
173 | 199 | # Install any necessary Python packages into the project environment |
174 | | - requirements_path = os.path.join(os.getcwd(), "requirements.txt") |
175 | | - if os.path.exists(requirements_path): |
176 | | - subprocess.run([sys.executable, "-m", "pip", "install", "-r", requirements_path], check=False) |
| 200 | + # Skip installation during prepare-only to avoid network access and speed up scaffolding |
| 201 | + if not prepare_only: |
| 202 | + requirements_path = os.path.join(os.getcwd(), "requirements.txt") |
| 203 | + if os.path.exists(requirements_path): |
| 204 | + subprocess.run([sys.executable, "-m", "pip", "install", "-r", requirements_path], check=False) |
177 | 205 |
|
178 | 206 | # Run the project |
| 207 | + if prepare_only: |
| 208 | + print("Prepared project in build/ without building (prepare-only).") |
| 209 | + return |
| 210 | + |
179 | 211 | if platform == "android": |
180 | 212 | # Change to the Android project directory |
181 | 213 | android_project_dir: str = os.path.join(build_dir, "android_template") |
@@ -261,6 +293,11 @@ def main() -> None: |
261 | 293 | # Create a new command 'run' that calls run_project |
262 | 294 | parser_run = subparsers.add_parser("run") |
263 | 295 | parser_run.add_argument("platform", choices=["android", "ios"]) |
| 296 | + parser_run.add_argument( |
| 297 | + "--prepare-only", |
| 298 | + action="store_true", |
| 299 | + help="Extract templates and stage app without building", |
| 300 | + ) |
264 | 301 | parser_run.set_defaults(func=run_project) |
265 | 302 |
|
266 | 303 | # Create a new command 'clean' that calls clean_project |
|
0 commit comments