Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
17e3747
rm api from cli.py
cadenmyers13 Oct 20, 2025
82f7394
add copy_examples api to packsmanager
cadenmyers13 Oct 20, 2025
d973c83
add tests for copy_examples
cadenmyers13 Oct 20, 2025
375606c
add additional dirs to temp structure
cadenmyers13 Oct 20, 2025
711c24e
rm old tests
cadenmyers13 Oct 20, 2025
987c3e3
add news
cadenmyers13 Oct 20, 2025
9e65a27
glob only files and not dirs
cadenmyers13 Oct 20, 2025
5354831
fix target dir location to be under cwd and change name for new test …
cadenmyers13 Oct 22, 2025
47d87fb
Add additional test case, fix how copy location is tested
cadenmyers13 Oct 22, 2025
0a490b9
copy_examples function
cadenmyers13 Oct 22, 2025
6e2695d
rm error message function
cadenmyers13 Oct 23, 2025
e96988f
add test for forced overwrite of copied files
cadenmyers13 Oct 23, 2025
abfba68
Add 'force' param to copy_examples
cadenmyers13 Oct 23, 2025
d4cddf5
fix copy_tree to handle warnings and errors properly
cadenmyers13 Oct 27, 2025
38881b8
Merge branch 'main' of github.com:diffpy/diffpy.cmi into copy-func-test
cadenmyers13 Oct 28, 2025
f4fe605
pin workflow to python 3.13
cadenmyers13 Oct 30, 2025
07cbcd7
add test for force and non-force to make sure files are copied correctly
cadenmyers13 Oct 31, 2025
078b13c
update copy_tree_to_target to handle different copying cases
cadenmyers13 Oct 31, 2025
e7444d7
capture warning output in print statement
cadenmyers13 Oct 31, 2025
c792c0b
add rich to conda.txt
cadenmyers13 Oct 31, 2025
7d7a2a3
add UC comments
cadenmyers13 Oct 31, 2025
52a6752
use ANSI color code for now
cadenmyers13 Oct 31, 2025
611bdf5
rm rich from reqs
cadenmyers13 Oct 31, 2025
d6f34e0
Rm colors from print
cadenmyers13 Oct 31, 2025
087851b
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] Oct 31, 2025
87c1f43
rm color from test
cadenmyers13 Nov 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update copy_tree_to_target to handle different copying cases
  • Loading branch information
cadenmyers13 committed Oct 31, 2025
commit 078b13c3ee1b7a343b87ddad2482fd95b6b4942f
66 changes: 54 additions & 12 deletions src/diffpy/cmi/packsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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_info function (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.

Screenshot 2025-10-31 at 2 44 08 PM

Copy link
Copy Markdown
Contributor

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!

@cadenmyers13 cadenmyers13 Oct 31, 2025 •

Copy link
Copy Markdown
Contributor Author

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:

from rich.console import Console

console=Console()

console.print("[bold green]This text prints in bold green[/]")

without rich:

print("\033[32m This text prints in green \033[0m")

There's probably a way to print in bold with ANSI code too. I think rich has 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?

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay done


from diffpy.cmi.installer import (
ParsedReq,
install_requirements,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -207,19 +209,59 @@ def _is_example_name(self, name):
return True
return False

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 rich that allows you to print console outputs in color which is nice. This will be good for pretty printing for cmi print info because that will output a lot of text. Color will help separate the content.

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.
Expand Down