-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.py
executable file
·388 lines (334 loc) · 11.8 KB
/
repository.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python
import logging
import argparse
import dataclasses
import pathlib
import shutil
import subprocess
import yaml
ROOT_DIR = pathlib.Path(__file__).parent
EXTERNAL_LIB_DIR = ROOT_DIR / "libs" / "external" / "lib"
OPS_SUNBEAM_DIR = ROOT_DIR / "ops-sunbeam" / "ops_sunbeam"
BUILD_FILE = ".sunbeam-build.yaml"
UTILITY_FILES = [
ROOT_DIR / ".stestr.conf",
ROOT_DIR / ".jujuignore",
]
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
###############################################
# Utility functions
###############################################
@dataclasses.dataclass
class SunbeamBuild:
path: pathlib.Path
external_libraries: list[str]
internal_libraries: list[str]
templates: list[str]
@classmethod
def load(cls, path: pathlib.Path) -> "SunbeamBuild":
with path.open() as f:
data = yaml.safe_load(f)
return cls(
path=path.parent,
external_libraries=data.get("external-libraries", []),
internal_libraries=data.get("internal-libraries", []),
templates=data.get("templates", []),
)
def _library_to_path(library: str) -> pathlib.Path:
split = library.split(".")
if len(split) != 4:
raise ValueError(f"Invalid library: {library}")
return pathlib.Path("/".join(split) + ".py")
def validate_charm(
charm: str,
internal_libraries: dict[str, pathlib.Path],
external_libraries: dict[str, pathlib.Path],
templates: dict[str, pathlib.Path],
) -> SunbeamBuild:
"""Validate the charm."""
path = ROOT_DIR / "charms" / charm
if not path.exists():
raise ValueError(f"Charm {charm} does not exist.")
build_file = path / BUILD_FILE
if not build_file.exists():
raise ValueError(f"Charm {charm} does not have a build file.")
charm_build = load_charm(charm)
for library in charm_build.external_libraries:
if library not in external_libraries:
raise ValueError(
f"Charm {charm} has invalid external library: {library} not found."
)
for library in charm_build.internal_libraries:
if library not in internal_libraries:
raise ValueError(
f"Charm {charm} has invalid internal library: {library} not found."
)
for template in charm_build.templates:
if template not in templates:
raise ValueError(
f"Charm {charm} has invalid template: {template} not found."
)
return charm_build
def load_external_libraries() -> dict[str, pathlib.Path]:
"""Load the external libraries."""
path = EXTERNAL_LIB_DIR
return {
str(p.relative_to(path))[:-3].replace("/", "."): p
for p in path.glob("**/*.py")
}
def load_internal_libraries() -> dict[str, pathlib.Path]:
"""Load the internal libraries."""
charms = list((ROOT_DIR / "charms").iterdir())
libraries = {}
for charm in charms:
path = charm / "lib"
search_path = path / "charms" / charm.name.replace("-", "_")
libraries.update(
{
str(p.relative_to(path))[:-3].replace("/", "."): p
for p in search_path.glob("**/*.py")
}
)
return libraries
def load_templates() -> dict[str, pathlib.Path]:
"""Load the templates."""
path = ROOT_DIR / "templates"
return {str(p.relative_to(path)): p for p in path.glob("**/*")}
def list_charms() -> list[str]:
"""List the available charms."""
return [p.name for p in (ROOT_DIR / "charms").iterdir() if p.is_dir()]
def load_charm(charm: str) -> SunbeamBuild:
"""Load the charm build file."""
path = ROOT_DIR / "charms" / charm / BUILD_FILE
return SunbeamBuild.load(path)
def copy(src: pathlib.Path, dest: pathlib.Path):
"""Copy the src to dest.
Only supports files.
"""
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(src, dest)
def prepare_charm(
charm: SunbeamBuild,
internal_libraries: dict[str, pathlib.Path],
external_libraries: dict[str, pathlib.Path],
templates: dict[str, pathlib.Path],
dry_run: bool = False,
):
"""Copy the necessary files.
Will copy external libraries, ops sunbeam and templates.
"""
dest = charm.path / "lib" / "ops_sunbeam"
logger.debug(f"Copying ops sunbeam to {dest}")
if not dry_run:
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(OPS_SUNBEAM_DIR, dest)
for utility_file in UTILITY_FILES:
utility_path = utility_file.relative_to(ROOT_DIR)
dest = charm.path / utility_path
logger.debug(f"Copying {utility_file} to {dest}")
if not dry_run:
copy(utility_file, dest)
for library in charm.external_libraries:
path = external_libraries[library]
library_path = path.relative_to(EXTERNAL_LIB_DIR)
dest = charm.path / "lib" / library_path
if not dest.exists():
logger.debug(f"Copying {library} to {dest}")
if dry_run:
continue
copy(path, dest)
for library in charm.internal_libraries:
path = internal_libraries[library]
library_path = _library_to_path(library)
dest = charm.path / "lib" / library_path
if not dest.exists():
logger.debug(f"Copying {library} to {dest}")
if dry_run:
continue
copy(path, dest)
for template in charm.templates:
path = templates[template]
dest = charm.path / "src" / "templates" / template
if not dest.exists():
logger.debug(f"Copying {template} to {dest}")
if dry_run:
continue
copy(path, dest)
def clean_charm(
charm: SunbeamBuild,
dry_run: bool = False,
):
"""Clean charm directory.
Will remove the external libraries, ops sunbeam and templates.
"""
path = charm.path / "lib" / "ops_sunbeam"
if path.exists():
logger.debug(f"Removing {path}")
if not dry_run:
shutil.rmtree(path)
for utility_file in UTILITY_FILES:
utility_path = utility_file.relative_to(ROOT_DIR)
path = charm.path / utility_path
if path.exists():
logger.debug(f"Removing {path}")
if not dry_run:
path.unlink()
for library in charm.external_libraries + charm.internal_libraries:
# Remove the charm namespace
path = charm.path / "lib" / _library_to_path(library).parents[1]
if path.exists():
logger.debug(f"Removing {path}")
if dry_run:
continue
shutil.rmtree(path)
for template in charm.templates:
path = charm.path / "src" / "templates" / template
if path.exists():
logger.debug(f"Removing {path}")
if dry_run:
continue
path.unlink()
###############################################
# Cli Definitions
###############################################
def _add_charm_argument(parser: argparse.ArgumentParser):
parser.add_argument(
"charm", type=str, nargs="*", help="The charm to operate on."
)
def main_cli():
main_parser = argparse.ArgumentParser(
description="Sunbeam Repository utilities."
)
main_parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose logging."
)
subparsers = main_parser.add_subparsers(
required=True, help="sub-command help"
)
prepare_parser = subparsers.add_parser("prepare", help="Prepare charm(s).")
_add_charm_argument(prepare_parser)
prepare_parser.add_argument(
"--clean",
action="store_true",
default=False,
help="Clean the charm(s) first.",
)
prepare_parser.add_argument(
"--dry-run", action="store_true", default=False, help="Dry run."
)
prepare_parser.set_defaults(func=prepare_cli)
clean_parser = subparsers.add_parser("clean", help="Clean charm(s).")
_add_charm_argument(clean_parser)
clean_parser.add_argument(
"--dry-run", action="store_true", default=False, help="Dry run."
)
clean_parser.set_defaults(func=clean_cli)
validate_parser = subparsers.add_parser(
"validate", help="Validate charm(s)."
)
_add_charm_argument(validate_parser)
validate_parser.set_defaults(func=validate_cli)
pythonpath_parser = subparsers.add_parser(
"pythonpath", help="Print the pythonpath."
)
pythonpath_parser.set_defaults(func=pythonpath_cli)
fetch_lib_parser = subparsers.add_parser(
"fetch-lib", help="Fetch the external libraries."
)
fetch_lib_parser.add_argument(
"libraries", type=str, nargs="*", help="Libraries to fetch."
)
fetch_lib_parser.set_defaults(func=fetch_lib_cli)
args = main_parser.parse_args()
level = logging.INFO
if args.verbose:
level = logging.DEBUG
logger.setLevel(level)
context = vars(args)
context["internal_libraries"] = load_internal_libraries()
context["external_libraries"] = load_external_libraries()
context["templates"] = load_templates()
context["sunbeam_charms"] = list_charms()
if "charm" in context:
charms = context.pop("charm")
if not charms:
charms = context["sunbeam_charms"]
context["charms"] = [
validate_charm(
charm,
context["internal_libraries"],
context["external_libraries"],
context["templates"],
)
for charm in charms
]
args.func(**context)
def prepare_cli(
charms: list[SunbeamBuild],
internal_libraries: dict[str, pathlib.Path],
external_libraries: dict[str, pathlib.Path],
templates: dict[str, pathlib.Path],
clean: bool = False,
dry_run: bool = False,
**kwargs,
):
for charm in charms:
logger.info("Preparing the charm %s", charm.path.name)
if clean:
clean_charm(charm, dry_run=dry_run)
prepare_charm(
charm,
internal_libraries,
external_libraries,
templates,
dry_run=dry_run,
)
def clean_cli(
charms: list[SunbeamBuild],
internal_libraries: dict[str, pathlib.Path],
external_libraries: dict[str, pathlib.Path],
templates: dict[str, pathlib.Path],
dry_run: bool = False,
**kwargs,
):
for charm in charms:
logger.info("Cleaning the charm %s", charm.path.name)
clean_charm(charm, dry_run=dry_run)
def validate_cli(
charms: list[SunbeamBuild],
internal_libraries: dict[str, pathlib.Path],
external_libraries: dict[str, pathlib.Path],
templates: dict[str, pathlib.Path],
**kwargs,
):
"""No op because done in the main_cli."""
for charm in charms:
logging.info("Charm %s is valid.", charm.path.name)
def pythonpath_cli(internal_libraries: dict[str, pathlib.Path], **kwargs):
"""Print the pythonpath."""
parent_dirs = set()
for path in internal_libraries.values():
parent_dirs.add(path.parents[3])
parent_dirs.add(OPS_SUNBEAM_DIR.parent)
parent_dirs.add(EXTERNAL_LIB_DIR)
print(":".join(str(p) for p in parent_dirs))
def fetch_lib_cli(
libraries: list[str], external_libraries: dict[str, pathlib.Path], **kwargs
):
"""Fetch the external libraries."""
cwd = EXTERNAL_LIB_DIR.parent
libraries_set = set(libraries)
unknown_libraries = libraries_set - set(external_libraries.keys())
if unknown_libraries:
raise ValueError(f"Unknown libraries: {unknown_libraries}")
if not libraries_set:
libraries_set = set(external_libraries.keys())
for library in libraries_set:
logging.info(f"Fetching {library}")
# Fetch the library
subprocess.run(
["charmcraft", "fetch-lib", library], cwd=cwd, check=True
)
if __name__ == "__main__":
main_cli()