Skip to content

Commit 13c424c

Browse files
authored
Merge pull request #17 from ruffsl/valid
Rephrase package selection extensions
2 parents c8d4b3b + 1ecbfd9 commit 13c424c

File tree

10 files changed

+193
-130
lines changed

10 files changed

+193
-130
lines changed

Diff for: README.md

+37-15
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,66 @@ An extension for [colcon-core](https://github.com/colcon/colcon-core) to cache p
44

55
## Example usage
66

7+
Setup workspace:
78
```
8-
# setup workspace
99
mkdir -p ~/ws/src && cd ~/ws
1010
wget https://raw.githubusercontent.com/colcon/colcon.readthedocs.org/main/colcon.repos
1111
vcs import src < colcon.repos
12+
```
1213

13-
# lock cache of workspace source
14+
Lock workspace by generating `cache` lockfiles:
15+
```
1416
colcon cache lock
17+
```
1518

16-
# build and test workspace
19+
Build and test workspace:
20+
```
1721
colcon build
1822
colcon test
23+
```
1924

20-
# change package source
25+
Modify package source:
26+
```
2127
echo "#foo" >> src/colcon-cmake/setup.py
28+
```
2229

23-
# update cache lock
30+
Update `cache` lockfiles:
31+
```
2432
colcon cache lock
33+
```
2534

26-
# list changed packges by comparing lockfile checksums
27-
PKGS_CHANGED=$(colcon list --packages-select-lock-changed | xarg)
35+
List modified packges by comparing `cache` lockfile checksums
36+
```
37+
PKGS_MODIFIED=$(colcon list --packages-select-cache-modified | xarg)
38+
```
2839

29-
# rebuild only changed packages and above
30-
colcon build --packages-above $PKGS_CHANGED
40+
Rebuild only modified packages and above:
41+
```
42+
colcon build --packages-above $PKGS_MODIFIED
43+
```
3144

32-
# alter package source again
45+
Modify package source again:
46+
```
3347
echo "#bar" >> src/colcon-cmake/setup.py
3448
echo "#baz" >> src/colcon-package-information/setup.py
49+
```
3550

36-
# update cache lock again
51+
Update cache lockfiles again:
52+
```
3753
colcon cache lock
54+
```
3855

39-
# rebuild changed packages by comparing verb lockfiles
56+
Rebuild by skipping packages with valid `build` lockfiles:
57+
```
4058
colcon build --packages-skip-cache-valid
59+
```
4160

42-
# retest packages with any untested build changes
43-
colcon test --packages-skip-cache-valid
61+
Retest by skipping packages with valid `test` lockfiles:
62+
```
63+
colcon test --packages-skip-cache-valid
64+
```
4465

45-
# list generated lockfiles from each verb
66+
List generated lockfiles from each `verb`:
67+
```
4668
ls build/colcon-cmake/cache
4769
```

Diff for: colcon_cache/cache/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __eq__(self, other): # noqa: D105
2626
return False
2727
return self.current == other.current
2828

29-
def is_changed(self): # noqa: D10s
29+
def is_modified(self): # noqa: D10s
3030
return self.current != self.reference
3131

3232

@@ -67,8 +67,8 @@ def __eq__(self, other): # noqa: D105
6767
return False
6868
return self.checksums == other.checksums
6969

70-
def is_changed(self): # noqa: D10s
71-
return self.checksums.is_changed()
70+
def is_modified(self): # noqa: D10s
71+
return self.checksums.is_modified()
7272

7373
def update_dependencies(self, dep_lockfiles): # noqa: D10s
7474
self.dependencies.clear()

Diff for: colcon_cache/event_handler/store_lockfile.py renamed to colcon_cache/event_handler/lockfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from colcon_core.plugin_system import satisfies_version
1313

1414

15-
class StoreLockfileEventHandler(EventHandlerExtensionPoint):
15+
class LockfileEventHandler(EventHandlerExtensionPoint):
1616
"""
1717
Persist the lockfile of a job in a file in its build directory.
1818

Diff for: colcon_cache/package_selection/key.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019 Dirk Thomas
2+
# Copyright 2021 Ruffin White
3+
# Licensed under the Apache License, Version 2.0
4+
5+
from colcon_cache.verb_handler import get_verb_handler_extensions
6+
from colcon_core.package_selection import PackageSelectionExtensionPoint
7+
from colcon_core.plugin_system import satisfies_version
8+
9+
10+
class KeyPackageSelection(PackageSelectionExtensionPoint):
11+
"""Skip a set of packages based on lockfiles from previous caches."""
12+
13+
def __init__(self): # noqa: D107
14+
super().__init__()
15+
satisfies_version(
16+
PackageSelectionExtensionPoint.EXTENSION_POINT_VERSION, '^1.0')
17+
18+
def add_arguments(self, *, parser): # noqa: D102
19+
parser.add_argument(
20+
'--packages-select-cache-key',
21+
choices=get_verb_handler_extensions().keys(),
22+
default=None,
23+
help='Only process packages using considered cache key. '
24+
'Fallbacks using invoked verb handler if unspecified.')
25+
26+
def select_packages(self, args, decorators): # noqa: D102
27+
# Pass given added arguments here are considered elsewhere by other
28+
# package selection extensions in colcon_cache
29+
pass

Diff for: colcon_cache/package_selection/lock.py

-85
This file was deleted.

Diff for: colcon_cache/package_selection/modified.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright 2019 Dirk Thomas
2+
# Copyright 2021 Ruffin White
3+
# Licensed under the Apache License, Version 2.0
4+
5+
import os
6+
7+
from colcon_cache.verb_handler import get_verb_handler_extensions
8+
from colcon_core.package_selection import logger
9+
from colcon_core.package_selection import PackageSelectionExtensionPoint
10+
from colcon_core.plugin_system import satisfies_version
11+
12+
13+
class ModifiedPackageSelection(PackageSelectionExtensionPoint):
14+
"""Skip a set of packages based on lockfiles from current lock."""
15+
16+
def __init__(self): # noqa: D107
17+
super().__init__()
18+
satisfies_version(
19+
PackageSelectionExtensionPoint.EXTENSION_POINT_VERSION, '^1.0')
20+
21+
def add_arguments(self, *, parser): # noqa: D102
22+
group = parser.add_mutually_exclusive_group()
23+
group.add_argument(
24+
'--packages-select-cache-modified', action='store_true',
25+
help='Only process a subset of packages whose cache '
26+
'denote package modifications (packages without lockfiles '
27+
'are not considered as modified)')
28+
group.add_argument(
29+
'--packages-select-cache-unmodified', action='store_true',
30+
help='Only process a subset of packages whose cache '
31+
'denote no package modifications (packages without lockfiles '
32+
'are not considered as unmodified)')
33+
34+
def select_packages(self, args, decorators): # noqa: D102
35+
if not any((
36+
args.packages_select_cache_modified,
37+
args.packages_select_cache_unmodified,
38+
)):
39+
return
40+
41+
if not hasattr(args, 'build_base'):
42+
if args.packages_select_cache_modified:
43+
argument = '--packages-select-cache-modified'
44+
elif args.packages_select_cache_unmodified:
45+
argument = '--packages-select-cache-unmodified'
46+
else:
47+
assert False
48+
logger.warning(
49+
"Ignoring '{argument}' since the invoked verb doesn't have a "
50+
"'--build-base' argument and therefore can't access "
51+
'information about the previous state of a package'
52+
.format_map(locals()))
53+
return
54+
55+
verb_name = args.packages_select_cache_key
56+
if not verb_name:
57+
verb_name = args.verb_name
58+
verb_handler_extensions = get_verb_handler_extensions()
59+
60+
if verb_name in verb_handler_extensions:
61+
verb_handler_extension = verb_handler_extensions[verb_name]
62+
else:
63+
logger.warning(
64+
"Ignoring '{argument}' since the respective verb "
65+
"'{verb_name}' doesn't have a colcon cache verb "
66+
"handler extension and therefore can't access "
67+
'information about the relative state of a package'
68+
.format_map(locals()))
69+
return
70+
71+
for decorator in decorators:
72+
# skip packages which have already been ruled out
73+
if not decorator.selected:
74+
continue
75+
76+
pkg = decorator.descriptor
77+
78+
package_build_base = os.path.join(
79+
args.build_base, pkg.name)
80+
81+
verb_lockfile = verb_handler_extension\
82+
.get_current_lockfile(package_build_base)
83+
84+
package_kind = None
85+
if verb_lockfile is None:
86+
package_kind = ('without lockfile')
87+
else:
88+
if args.packages_select_cache_modified:
89+
if not verb_lockfile.is_modified():
90+
package_kind = ('unmodified')
91+
92+
if args.packages_select_cache_unmodified:
93+
if verb_lockfile.is_modified():
94+
package_kind = ('modified')
95+
96+
if package_kind:
97+
logger.info(
98+
"Skipping {package_kind} package '{pkg.name}' in "
99+
"'{pkg.path}'".format_map(locals()))
100+
decorator.selected = False

0 commit comments

Comments
 (0)