|
25 | 25 |
|
26 | 26 | from . import ( |
27 | 27 | utils, |
28 | | - templates, |
| 28 | + resources, |
29 | 29 | core, |
30 | 30 | constants, |
31 | 31 | plastex, |
@@ -248,7 +248,8 @@ def devscript(args: List[str]) -> None: |
248 | 248 | """ |
249 | 249 | PY_CMD = sys.executable |
250 | 250 | subprocess.run( |
251 | | - [PY_CMD, str(core.resources.path("pretext", "pretext"))] + list(args) |
| 251 | + [PY_CMD, str(resources.resource_base_path() / "core" / "pretext" / "pretext")] |
| 252 | + + list(args) |
252 | 253 | ) |
253 | 254 |
|
254 | 255 |
|
@@ -284,47 +285,35 @@ def new(template: str, directory: Path, url_template: str) -> None: |
284 | 285 | """ |
285 | 286 | directory_fullpath = Path(directory).resolve() |
286 | 287 | if utils.project_path(directory_fullpath) is not None: |
287 | | - log.warning( |
| 288 | + log.error( |
288 | 289 | f"A project already exists in `{utils.project_path(directory_fullpath)}`." |
289 | 290 | ) |
290 | | - log.warning("No new project will be generated.") |
| 291 | + log.error("No new project will be generated.") |
291 | 292 | return |
292 | | - log.info( |
293 | | - f"Generating new PreTeXt project in `{directory_fullpath}` using `{template}` template." |
294 | | - ) |
| 293 | + log.info(f"Generating new PreTeXt project in `{directory_fullpath}`") |
| 294 | + directory_fullpath.mkdir(exist_ok=True) |
295 | 295 | if url_template is not None: |
| 296 | + log.info(f"Using template at `{url_template}`") |
| 297 | + # get project and extract to directory |
296 | 298 | r = requests.get(url_template) |
297 | 299 | archive = zipfile.ZipFile(io.BytesIO(r.content)) |
| 300 | + with tempfile.TemporaryDirectory(prefix="pretext_") as tmpdirname: |
| 301 | + archive.extractall(tmpdirname) |
| 302 | + content_path = [Path(tmpdirname) / i for i in os.listdir(tmpdirname)][0] |
| 303 | + shutil.copytree(content_path, directory_fullpath, dirs_exist_ok=True) |
298 | 304 | else: |
299 | | - with templates.resource_path(f"{template}.zip") as template_path: |
300 | | - archive = zipfile.ZipFile(template_path) |
301 | | - # find (first) project.ptx to use as root of template |
302 | | - filenames = [Path(filepath).name for filepath in archive.namelist()] |
303 | | - project_ptx_index = filenames.index("project.ptx") |
304 | | - project_ptx_path = Path(archive.namelist()[project_ptx_index]) |
305 | | - project_dir_path = project_ptx_path.parent |
306 | | - with tempfile.TemporaryDirectory(prefix="pretext_") as tmpdirname: |
307 | | - temp_path = Path(tmpdirname) / "new-project" |
308 | | - temp_path.mkdir() |
309 | | - for filepath in [ |
310 | | - filepath |
311 | | - for filepath in archive.namelist() |
312 | | - if project_dir_path in Path(filepath).parents |
313 | | - ]: |
314 | | - archive.extract(filepath, path=temp_path) |
315 | | - tmpsubdirname = temp_path / project_dir_path |
316 | | - shutil.copytree(tmpsubdirname, directory_fullpath, dirs_exist_ok=True) |
317 | | - # generate remaining boilerplate like requirements.txt |
318 | | - project = Project.parse(directory_fullpath) |
319 | | - project.generate_boilerplate(update_requirements=True) |
320 | | - if len(project.targets) == 0: |
321 | | - log.warning("The generated project has no targets!") |
322 | | - else: |
323 | | - target = project.targets[0] |
324 | | - log.info(f"Success! Open `{target.source_abspath()}` to edit your document") |
325 | | - log.info( |
326 | | - f"Then try to `pretext build` and `pretext view` from within `{directory_fullpath}`." |
327 | | - ) |
| 305 | + log.info(f"Using `{template}` template.") |
| 306 | + # copy project from installed resources |
| 307 | + with resources.resource_base_path() / "templates" / f"{template}" as template_path: |
| 308 | + shutil.copytree(template_path, directory_fullpath, dirs_exist_ok=True) |
| 309 | + # generate missing boilerplate |
| 310 | + with utils.working_directory(directory_fullpath): |
| 311 | + project_path = utils.project_path() |
| 312 | + if project_path is None: |
| 313 | + project = Project() |
| 314 | + else: |
| 315 | + project = Project.parse(project_path) |
| 316 | + project.generate_boilerplate(update_requirements=True) |
328 | 317 |
|
329 | 318 |
|
330 | 319 | # pretext init |
|
0 commit comments