Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 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
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
1 change: 1 addition & 0 deletions .github/workflows/tests-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
project: diffpy.cmi
c_extension: false
headless: false
python_version: 3.13
run: |
set -Eeuo pipefail
echo "Test cmds"
Expand Down
23 changes: 23 additions & 0 deletions news/copy-func-test.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Add test for ``copy_examples``.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
18 changes: 1 addition & 17 deletions src/diffpy/cmi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
from pathlib import Path
from shutil import copytree
from typing import Dict, List, Optional, Tuple
from typing import List, Optional, Tuple

from diffpy.cmi import __version__
from diffpy.cmi.conda import env_info
Expand All @@ -25,22 +25,6 @@
from diffpy.cmi.profilesmanager import ProfilesManager


def copy_examples(
examples_dict: Dict[str, List[Tuple[str, Path]]], target_dir: Path = None
) -> None:
"""Copy an example into the the target or current working directory.

Parameters
----------
examples_dict : dict
Dictionary mapping pack name -> list of (example, path) tuples.
target_dir : pathlib.Path, optional
Target directory to copy examples into. Defaults to current
working directory.
"""
return


# Examples
def _get_examples_dir() -> Path:
"""Return the absolute path to the installed examples directory.
Expand Down
127 changes: 126 additions & 1 deletion src/diffpy/cmi/packsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See LICENSE.rst for license information.
#
##############################################################################

import shutil
from importlib.resources import as_file
from pathlib import Path
from typing import List, Union
Expand Down Expand Up @@ -133,6 +133,131 @@ def available_examples(self) -> dict[str, List[tuple[str, Path]]]:
)
return examples_dict

def copy_examples(
self,
examples_to_copy: List[str],
target_dir: Path = None,
force: bool = False,
) -> None:
"""Copy examples or packs into the target or current working
directory.

Parameters
----------
examples_to_copy : list of str
User-specified pack(s), example(s), or "all" to copy all.
target_dir : pathlib.Path, optional
Target directory to copy examples into. Defaults to current
working directory.
force : bool, optional
Defaults to ``False``. If ``True``, existing files are
overwritten and directories are merged
(extra files in the target are preserved).
"""
self._target_dir = target_dir.resolve() if target_dir else Path.cwd()
self._force = force

if "all" in examples_to_copy:
self._copy_all()
return

for item in examples_to_copy:
if item in self.available_examples():
self._copy_pack(item)
elif self._is_example_name(item):
self._copy_example(item)
else:
raise FileNotFoundError(
f"No examples or packs found for input: '{item}'"
)
del self._target_dir
del self._force
return

def _copy_all(self):
"""Copy all packs and examples."""
for pack_name in self.available_examples():
self._copy_pack(pack_name)

def _copy_pack(self, pack_name):
"""Copy all examples in a single pack."""
examples = self.available_examples().get(pack_name, [])
for ex_name, ex_path in examples:
self._copy_tree_to_target(pack_name, ex_name, ex_path)

def _copy_example(self, example_name):
"""Copy a single example by its name."""
example_found = False
for pack_name, examples in self.available_examples().items():
for ex_name, ex_path in examples:
if ex_name == example_name:
self._copy_tree_to_target(pack_name, ex_name, ex_path)
example_found = True
if not example_found:
raise FileNotFoundError(
f"No examples or packs found for input: '{example_name}'"
)

def _is_example_name(self, name):
"""Return True if the given name matches any known example."""
for pack_name, examples in self.available_examples().items():
for example_name, _ in examples:
if example_name == name:
return True
return False

Copy link
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, 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"\033[33mWARNING:\033[0m 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."
)
return
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)
print(
f"\033[32mOverwriting example '{pack_name}/{example_name}'.\033[0m"
)

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)
print(f"\033[32mCopied example '{pack_name}/{example_name}'.\033[0m")

def _resolve_pack_file(self, identifier: Union[str, Path]) -> Path:
"""Resolve a pack identifier to an absolute .txt path.

Expand Down
44 changes: 34 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ def tmp_examples(tmp_path_factory):
yield tmp_examples


@pytest.fixture(scope="session")
@pytest.fixture(scope="function")
def example_cases(tmp_path_factory):
"""Copy the entire examples tree into a temp directory once per test
session.

Returns the path to that copy.
"""
root_temp_dir = tmp_path_factory.mktemp("temp")
cwd = root_temp_dir / "cwd"
cwd.mkdir(parents=True, exist_ok=True)
existing_dir = cwd / "existing_target"
existing_dir.mkdir(parents=True, exist_ok=True)

# case 1: pack with no examples
case1ex_dir = root_temp_dir / "case1" / "docs" / "examples"
Expand All @@ -44,30 +48,35 @@ def example_cases(tmp_path_factory):
case2ex_dir / "full_pack" / "ex1" / "solution" / "diffpy-cmi"
) # full_pack, ex1
case2a.mkdir(parents=True, exist_ok=True)
(case2a / "script1.py").touch()
(case2a / "script1.py").write_text(f"# {case2a.name} script1\n")

case2b = (
case2ex_dir / "full_pack" / "ex2" / "random" / "path"
) # full_pack, ex2
case2b.mkdir(parents=True, exist_ok=True)
(case2b / "script1.py").touch()
(case2b / "script2.py").touch()
(case2b / "script1.py").write_text(f"# {case2b.name} script1\n")
(case2b / "script2.py").write_text(f"# {case2b.name} script2\n")

case2req_dir = root_temp_dir / "case2" / "requirements" / "packs"
case2req_dir.mkdir(parents=True, exist_ok=True)

# Case 3: multiple packs with multiple examples
case3ex_dir = root_temp_dir / "case3" / "docs" / "examples"
case3a = case3ex_dir / "packA" / "ex1" # packA, ex1
case3a.mkdir(parents=True, exist_ok=True)
(case3a / "script1.py").touch()
(case3a / "script1.py").write_text(f"# {case3a.name} script1\n")

case3b = case3ex_dir / "packA" / "ex2" / "solutions" # packA, ex2
case3b.mkdir(parents=True, exist_ok=True)
(case3b / "script2.py").touch()
(case3b / "script2.py").write_text(f"# {case3b.name} script2\n")

case3c = (
case3ex_dir / "packB" / "ex3" / "more" / "random" / "path"
) # packB, ex3
case3c.mkdir(parents=True, exist_ok=True)
(case3c / "script3.py").touch()
(case3c / "script4.py").touch()
(case3c / "script3.py").write_text(f"# {case3c.name} script3\n")
(case3c / "script4.py").write_text(f"# {case3c.name} script4\n")

case3req_dir = root_temp_dir / "case3" / "requirements" / "packs"
case3req_dir.mkdir(parents=True, exist_ok=True)

Expand All @@ -80,12 +89,27 @@ def example_cases(tmp_path_factory):

# Case 5: multiple packs with the same example names
case5ex_dir = root_temp_dir / "case5" / "docs" / "examples"

case5a = case5ex_dir / "packA" / "ex1" / "path1" # packA, ex1
case5a.mkdir(parents=True, exist_ok=True)
(case5a / "script1.py").touch()
(case5a / "script1.py").write_text(f"# {case5a.name} script1\n")

case5b = case5ex_dir / "packB" / "ex1" / "path2" # packB, ex1
case5b.mkdir(parents=True, exist_ok=True)
(case5b / "script2.py").touch()
(case5b / "script2.py").write_text(f"# {case5b.name} script2\n")

case5c = case5ex_dir / "packA" / "ex2" # packA, ex2
case5c.mkdir(parents=True, exist_ok=True)
(case5c / "script3.py").write_text(f"# {case5c.name} script3\n")

case5d = case5ex_dir / "packB" / "ex3"
case5d.mkdir(parents=True, exist_ok=True)
(case5d / "script4.py").write_text(f"# {case5d.name} script4\n")

case5e = case5ex_dir / "packB" / "ex4"
case5e.mkdir(parents=True, exist_ok=True)
(case5e / "script5.py").write_text(f"# {case5e.name} script5\n")

case5req_dir = root_temp_dir / "case5" / "requirements" / "packs"
case5req_dir.mkdir(parents=True, exist_ok=True)

Expand Down
65 changes: 0 additions & 65 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,65 +0,0 @@
import os
from pathlib import Path

import pytest

from diffpy.cmi import cli


def test_map_pack_to_examples_structure():
"""Test that map_pack_to_examples returns the right shape of
data."""
actual = cli.map_pack_to_examples()
assert isinstance(actual, dict)
for pack, exdirs in actual.items():
assert isinstance(pack, str)
assert isinstance(exdirs, list)
for ex in exdirs:
assert isinstance(ex, str)
# Check for known packs
assert "core" in actual.keys()
assert "pdf" in actual.keys()
# Check for known examples
assert ["linefit"] in actual.values()


@pytest.mark.parametrize(
"input_valid_str",
[
"core/linefit",
"pdf/ch03NiModelling",
],
)
def test_copy_example_success(tmp_path, input_valid_str):
"""Given a valid example format (<pack>/<ex>), test that its copied
to the temp dir."""
os.chdir(tmp_path)
actual = cli.copy_example(input_valid_str)
expected = tmp_path / Path(input_valid_str).name
assert expected.exists() and expected.is_dir()
assert actual == expected


def test_copy_example_fnferror():
"""Test that FileNotFoundError is raised when the example does not
exist."""
with pytest.raises(FileNotFoundError):
cli.copy_example("pack/example1")


@pytest.mark.parametrize(
"input_bad_str",
[
"", # empty string
"/", # missing pack and example
"corelinefit", # missing slash
"linefit", # missing pack and slash
"core/", # missing example
"/linefit", # missing pack
"core/linefit/extra", # too many slashes
],
)
def test_copy_example_valueerror(input_bad_str):
"""Test that ValueError is raised when the format is invalid."""
with pytest.raises(ValueError):
cli.copy_example(input_bad_str)
Loading
Loading