Repository navigation
test: copy_examples test
#65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
17e3747
82f7394
d973c83
375606c
711c24e
987c3e3
9e65a27
5354831
47d87fb
0a490b9
6e2695d
e96988f
abfba68
d4cddf5
38881b8
f4fe605
07cbcd7
078b13c
e7444d7
c792c0b
7d7a2a3
52a6752
611bdf5
d6f34e0
087851b
87c1f43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,11 +13,12 @@ | |
| # | ||
| ############################################################################## | ||
| import shutil | ||
| import warnings | ||
| from importlib.resources import as_file | ||
| from pathlib import Path | ||
| from typing import List, Union | ||
|
|
||
| from rich.console import Console | ||
|
|
||
| from diffpy.cmi.installer import ( | ||
| ParsedReq, | ||
| install_requirements, | ||
|
|
@@ -80,6 +81,7 @@ class PacksManager: | |
| def __init__(self, root_path=None) -> None: | ||
| self.packs_dir = _installed_packs_dir(root_path) | ||
| self.examples_dir = self._get_examples_dir() | ||
| self.console = Console() | ||
|
|
||
| def _get_examples_dir(self) -> Path: | ||
| """Return the absolute path to the installed examples directory. | ||
|
|
@@ -207,19 +209,59 @@ def _is_example_name(self, name): | |
| return True | ||
| return False | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Main update on recent push is the content below here. I've also found this package called |
||
| def _copy_tree_to_target(self, pack_name, example_name, src_path): | ||
| """Helper to handle the actual filesystem copy.""" | ||
| dest_dir = self._target_dir / pack_name / example_name | ||
| dest_dir.parent.mkdir(parents=True, exist_ok=True) | ||
| if dest_dir.exists() and not self._force: | ||
| warn_message = ( | ||
| f"Example directory(ies): '{dest_dir.stem}' already exist. " | ||
| "Current versions of existing files have been left unchanged. " | ||
| "To overwrite, please rerun and specify --force." | ||
| def _copy_tree_to_target(self, pack_name, example_name, example_origin): | ||
| """Copy an example folder from source to the user's target | ||
| directory.""" | ||
| target_dir = self._target_dir / pack_name / example_name | ||
| target_dir.parent.mkdir(parents=True, exist_ok=True) | ||
| if target_dir.exists() and self._force: | ||
| self._overwrite_example( | ||
| example_origin, target_dir, pack_name, example_name | ||
| ) | ||
| return | ||
| if target_dir.exists(): | ||
| self._copy_missing_files(example_origin, target_dir) | ||
| print( | ||
| f"WARNING: Example '{pack_name}/{example_name}' " | ||
| "already exists at the specified target directory. " | ||
| "Existing files were left unchanged; " | ||
| "new or missing files were copied. To overwrite everything, " | ||
| "rerun with --force." | ||
| ) | ||
| warnings.warn(warn_message, UserWarning) | ||
| return | ||
| shutil.copytree(src_path, dest_dir, dirs_exist_ok=self._force) | ||
| self._copy_new_example( | ||
| example_origin, target_dir, pack_name, example_name | ||
| ) | ||
|
|
||
| def _overwrite_example( | ||
| self, example_origin, target, pack_name, example_name | ||
| ): | ||
| """Delete target and copy example.""" | ||
| shutil.rmtree(target) | ||
| shutil.copytree(example_origin, target) | ||
| self.console.print( | ||
| f"[green]Overwriting example '{pack_name}/{example_name}'.[/]" | ||
| ) | ||
|
|
||
| def _copy_missing_files(self, example_origin, target): | ||
| """Copy only files and directories that are missing in the | ||
| target.""" | ||
| for example_item in example_origin.rglob("*"): | ||
| rel_path = example_item.relative_to(example_origin) | ||
| target_item = target / rel_path | ||
| if example_item.is_dir(): | ||
| target_item.mkdir(parents=True, exist_ok=True) | ||
| elif example_item.is_file() and not target_item.exists(): | ||
| target_item.parent.mkdir(parents=True, exist_ok=True) | ||
| shutil.copy2(example_item, target_item) | ||
|
|
||
| def _copy_new_example( | ||
| self, example_origin, target, pack_name, example_name | ||
| ): | ||
| shutil.copytree(example_origin, target) | ||
| self.console.print( | ||
| f"[green]Copied example '{pack_name}/{example_name}'.[/]" | ||
| ) | ||
|
|
||
| def _resolve_pack_file(self, identifier: Union[str, Path]) -> Path: | ||
| """Resolve a pack identifier to an absolute .txt path. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems to be introducing a new dependency. Is it worth adding the bloat for this? Why not just print? If we can be convinced it is worth it we probably have to add rich to all the dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sbillinge I was planning on using it for the
print_infofunction (see image). it helps with separation. On second thought, I think we could also add it using ANSI color codes and get rid of the dependency. I'll do that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could use rich if it is a nice package, but it is a bit like using pathlib.Path, if we like it, we may want to kind of start using it as a group standard. We could even choose nice colors!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sbillinge I agree! here's examples of what the syntax would look like with and without rich.
with rich:
without rich:
There's probably a way to print in bold with ANSI code too. I think
richhas preset colors that are commonly used/are nice but we could probably add a config file to change these if we wanted. what do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind about the syntax, it is more whether we want to go there at all. Let's just do a print without any colors for now and merge the PR. We can make an issue to add rich formatting and have a discussion about it in the broader group.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay done