Skip to content

Commit 6c9fe14

Browse files
committed
Create Script to Snapshot Compadre into Trilinos (#175)
Create script to snapshot the Compadre repository containing the script into the Trilinos pointed to on the command line, excluding the kokkos, kokkos-kernels, python, and scripts directories, utilizing the SnapshotDir utility from TriBITS under the hood.
1 parent 5d38aa3 commit 6c9fe14

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed

scripts/snapshot_into_trilinos.py

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Create a snapshot from the Compadre repository containing this script into the
4+
Trilinos repository pointed to, omitting the kokkos, kokkos-kernels, python,
5+
and scripts directories.
6+
7+
Note: This will assert that both repositories are in a clean state.
8+
9+
Warning: This will clean out any locally-ignored files in Compadre (e.g.,
10+
ignored via .git/info/exclude) to avoid copying them over and then
11+
committing them to Trilinos. Be sure you don't have any ignored files in
12+
Compadre that you want to keep before running this script.
13+
14+
To test this script to ensure that it's working correctly, simply use
15+
16+
pytest snapshot_into_trilinos.py
17+
"""
18+
import os
19+
import sys
20+
21+
22+
23+
24+
def parse_arguments(argv):
25+
"""
26+
Parse the command line arguments to the script.
27+
28+
Parameters:
29+
argv (list): The command line arguments to be parsed.
30+
31+
Returns:
32+
dict: A mapping from the options to their values.
33+
"""
34+
import argparse, textwrap
35+
width = 79
36+
description = __doc__
37+
description = ("[ Snapshot Compadre Into Trilinos ]".center(width, "-") +
38+
"\n" + description)
39+
examples = """
40+
Show what's going to happen without actually doing the snapshot::
41+
42+
./snapshot_into_trilinos.py \\
43+
--trilinos-dir /path/to/Trilinos \\
44+
--dry-run
45+
46+
Actually do the snapshot::
47+
48+
./snapshot_into_trilinos.py \\
49+
--trilinos-dir /path/to/Trilinos
50+
"""
51+
examples = textwrap.dedent(examples)
52+
examples = "[ Examples ]".center(width, "-") + "\n\n" + examples
53+
parser = argparse.ArgumentParser(
54+
description=description,
55+
epilog=examples,
56+
formatter_class=argparse.RawDescriptionHelpFormatter
57+
)
58+
parser.add_argument("-t", "--trilinos-dir", dest="trilinos_dir",
59+
action="store", required=True, default=None,
60+
help="The path (relative or absolute) to the root of "
61+
"the Trilinos repository you wish to snapshot "
62+
"Compadre into.")
63+
parser.add_argument("-d", "--dry-run", dest="dry_run", action="store_true",
64+
default=False, help="Show what will happen, but don't "
65+
"actually do it.")
66+
return parser.parse_args(argv)
67+
68+
69+
70+
def test_parse_arguments():
71+
"""
72+
Test that the parse_arguments() function works as intended.
73+
"""
74+
options = parse_arguments("--trilinos-dir /path/to/Trilinos --dry-run".
75+
split())
76+
assert (options.trilinos_dir == "/path/to/Trilinos" and
77+
options.dry_run == True)
78+
options = parse_arguments("-t /path/to/Trilinos -d".split())
79+
assert (options.trilinos_dir == "/path/to/Trilinos" and
80+
options.dry_run == True)
81+
options = parse_arguments("-t /some/other/dir".split())
82+
assert (options.trilinos_dir == "/some/other/dir" and
83+
options.dry_run == False)
84+
85+
86+
87+
def create_directory_variables(trilinos_dir, verbose=False):
88+
"""
89+
Use the given path to Trilinos to create a handful of other variables
90+
pointing to:
91+
* The python_utils directory, so we can import the SnapshotDir utility.
92+
* The Compadre repository containing this script.
93+
* The location to where Compadre will be shapshotted.
94+
95+
Parameters:
96+
trilinos_dir (str): The path to Trilinos given on the command line.
97+
verbose (bool): Whether or not to print out details.
98+
99+
Returns:
100+
tuple: The original Compadre directory, and the location to be
101+
snapshotted to.
102+
"""
103+
trilinos_dir = os.path.abspath(trilinos_dir)
104+
python_utils_dir = os.path.join(trilinos_dir, "cmake/tribits/python_utils")
105+
sys.path.append(python_utils_dir)
106+
script_dir = os.path.dirname(os.path.realpath(__file__))
107+
compadre_orig_dir = os.path.abspath(os.path.join(script_dir, ".."))
108+
compadre_dest_dir = os.path.join(trilinos_dir, "packages/compadre")
109+
if verbose:
110+
print(f"Trilinos repository root: {trilinos_dir}\n"
111+
f"Using snapshot-dir.py from: {python_utils_dir}\n"
112+
f"Snapshotting Compadre from: {compadre_orig_dir}\n"
113+
f" into: {compadre_dest_dir}")
114+
return (compadre_orig_dir, compadre_dest_dir)
115+
116+
117+
118+
def test_create_directory_variables(capfd):
119+
"""
120+
Test that the create_directory_variables() function works as intended.
121+
"""
122+
trilinos_dir = "/path/to/Trilinos"
123+
compadre_dir = os.path.abspath(os.path.join(os.path.dirname(
124+
os.path.realpath(__file__)), ".."))
125+
(orig_dir, dest_dir) = create_directory_variables(trilinos_dir)
126+
assert (orig_dir == compadre_dir and
127+
dest_dir == "/path/to/Trilinos/packages/compadre" and
128+
os.path.join(trilinos_dir, "cmake/tribits/python_utils") in
129+
sys.path)
130+
out, err = capfd.readouterr()
131+
assert out == ""
132+
create_directory_variables(trilinos_dir, verbose=True)
133+
out, err = capfd.readouterr()
134+
expected = (f"Trilinos repository root: {trilinos_dir}\n"
135+
f"Using snapshot-dir.py from: {trilinos_dir}/cmake/tribits/"
136+
"python_utils\n"
137+
f"Snapshotting Compadre from: {compadre_dir}\n"
138+
f" into: {trilinos_dir}/packages/"
139+
"compadre\n")
140+
assert out == expected
141+
142+
143+
144+
def create_snapshot_dir_args(orig_dir, dest_dir, dry_run=False):
145+
"""
146+
Create the arguments to pass to the SnapshotDir utility from TriBITS.
147+
148+
Parameters:
149+
orig_dir (str): The path to the Compadre repository to be snapshotted.
150+
dest_dir (str): The path to where Compadre will be snapshotted.
151+
dry_run (bool): Whether or not to show what will happen without
152+
actually doing it.
153+
154+
Returns:
155+
list: The arguments to be passed to the utility.
156+
"""
157+
args = (f"--orig-dir {orig_dir}/ "
158+
f"--dest-dir {dest_dir}/ "
159+
"--exclude kokkos kokkos-kernels python scripts "
160+
"--clean-ignored-files-orig-dir")
161+
if dry_run:
162+
args += " --show-defaults"
163+
return args.split(" ")
164+
165+
166+
167+
def test_create_snapshot_dir_args():
168+
"""
169+
Test that the create_snapshot_dir_args() function works as intended.
170+
"""
171+
orig = "from_here"
172+
dest = "to_there"
173+
args = create_snapshot_dir_args(orig, dest)
174+
expected = (f"--orig-dir {orig}/ --dest-dir {dest}/ --exclude kokkos "
175+
"kokkos-kernels python scripts --clean-ignored-files-orig-dir")
176+
assert args == expected.split()
177+
args = create_snapshot_dir_args(orig, dest, dry_run=True)
178+
expected += " --show-defaults"
179+
assert args == expected.split()
180+
181+
182+
183+
def snapshot(snapshot_dir_args):
184+
"""
185+
Perform the snapshot using the SnapshotDir utility from TriBITS.
186+
187+
Parameters:
188+
shapshot_dir_args (str): The arguments to pass to SnapshotDir.
189+
190+
Returns:
191+
bool: Whether or not the snapshot was successful.
192+
193+
Note:
194+
The SnapshotDir utility is already unit tested within TriBITS, so
195+
there's no need for an additional test here.
196+
"""
197+
import SnapshotDir
198+
return SnapshotDir.snapshotDirMainDriver(snapshot_dir_args)
199+
200+
201+
202+
if __name__ == "__main__":
203+
options = parse_arguments(sys.argv[1:])
204+
(orig_dir, dest_dir) = create_directory_variables(options.trilinos_dir,
205+
options.dry_run)
206+
success = snapshot(create_snapshot_dir_args(orig_dir, dest_dir,
207+
options.dry_run))
208+
return_code = 0 if success else 1
209+
sys.exit(return_code)

0 commit comments

Comments
 (0)