|
8 | 8 |
|
9 | 9 | class LayerUpdate(SubCommand): |
10 | 10 | """Update layers.""" |
11 | | - subcommand = 'layer-update' |
| 11 | + |
| 12 | + subcommand = "layer-update" |
12 | 13 |
|
13 | 14 | @staticmethod |
14 | 15 | def add_args(parser): |
15 | 16 | parser.add_argument( |
16 | | - '--dry-run', action='store_true', default=False, |
17 | | - help='Only check for conflicts and roll back at the end.', |
| 17 | + "--dry-run", |
| 18 | + action="store_true", |
| 19 | + default=False, |
| 20 | + help="Only check for conflicts and roll back at the end.", |
18 | 21 | ) |
19 | 22 | parser.add_argument( |
20 | | - '--skip-errors', action='store_true', |
| 23 | + "--skip-errors", |
| 24 | + action="store_true", |
21 | 25 | help="Skip failed objects and continue", |
22 | | - default=False |
| 26 | + default=False, |
23 | 27 | ) |
24 | 28 | parser.add_argument( |
25 | | - '--message', '-m', type=str, default='zodbsync layer-update', |
| 29 | + "--message", |
| 30 | + "-m", |
| 31 | + type=str, |
| 32 | + default="zodbsync layer-update", |
26 | 33 | help="Commit message base", |
27 | 34 | ) |
28 | 35 | parser.add_argument( |
29 | | - 'ident', type=str, nargs='*', |
30 | | - help='Layer identifier(s). May be * for all', |
| 36 | + "ident", |
| 37 | + type=str, |
| 38 | + nargs="*", |
| 39 | + help="Layer identifier(s). May be * for all", |
31 | 40 | ) |
32 | 41 |
|
33 | 42 | def commit_all(self, target, msg): |
34 | 43 | """Commit all unstaged changes in target, returning the commit ID or |
35 | 44 | None if there is no change.""" |
36 | | - sp.run(['git', 'add', '.'], cwd=target) |
37 | | - if sp.run(['git', 'commit', '-m', msg], cwd=target).returncode == 0: |
38 | | - return sp.check_output(['git', 'rev-parse', 'HEAD'], |
39 | | - cwd=target, text=True).strip() |
| 45 | + sp.run(["git", "add", "."], cwd=target) |
| 46 | + if sp.run(["git", "commit", "-m", msg], cwd=target).returncode == 0: |
| 47 | + return sp.check_output( |
| 48 | + ["git", "rev-parse", "HEAD"], cwd=target, text=True |
| 49 | + ).strip() |
40 | 50 |
|
41 | 51 | def run_layer(self, layer): |
42 | 52 | """ |
43 | 53 | For given layer, commit any unstaged changes, update work_dir from |
44 | 54 | source, commit that and play back any changes. |
45 | 55 | """ |
46 | | - source = layer['source'] |
47 | | - target = layer['workdir'] |
| 56 | + source = layer["source"] |
| 57 | + target = layer["workdir"] |
48 | 58 | msg = self.args.message |
49 | | - precommit = self.commit_all(target, f'{msg} (pre)') |
| 59 | + precommit = self.commit_all(target, f"{msg} (pre)") |
50 | 60 | self.unpack_source(source, target) |
51 | 61 | changes = [ |
52 | | - line[3:] for line in sp.check_output( |
53 | | - ['git', 'status', '--porcelain', '-u', '--no-renames'], |
| 62 | + line[3:] |
| 63 | + for line in sp.check_output( |
| 64 | + ["git", "status", "--porcelain", "-u", "--no-renames"], |
54 | 65 | cwd=target, |
55 | 66 | text=True, |
56 | | - ).split('\n') |
| 67 | + ).split("\n") |
57 | 68 | if line |
58 | 69 | ] |
59 | 70 | commit = None |
60 | 71 | if changes: |
61 | 72 | commit = self.commit_all(target, msg) |
62 | | - self.restore[layer['ident']] = (precommit, commit) |
| 73 | + self.restore[layer["ident"]] = (precommit, commit) |
63 | 74 | return { |
64 | | - os.path.dirname(line[len('__root__'):]) |
| 75 | + os.path.dirname(line[len("__root__") :]) |
65 | 76 | for line in changes |
66 | | - if line.startswith('__root__/') |
| 77 | + if line.startswith("__root__/") |
67 | 78 | } |
68 | 79 |
|
69 | 80 | def restore_layer(self, layer): |
70 | 81 | """ |
71 | 82 | Restore layer for dry-run or in case of failure |
72 | 83 | """ |
73 | | - (precommit, commit) = self.restore[layer['ident']] |
74 | | - target = layer['workdir'] |
| 84 | + precommit, commit = self.restore[layer["ident"]] |
| 85 | + target = layer["workdir"] |
75 | 86 | if commit: |
76 | | - sp.run( |
77 | | - ['git', 'reset', '--hard', f'{commit}~'], |
78 | | - cwd=target, check=True |
79 | | - ) |
| 87 | + sp.run(["git", "reset", "--hard", f"{commit}~"], cwd=target, check=True) |
80 | 88 | if precommit: |
81 | | - sp.run( |
82 | | - ['git', '-reset', f'{precommit}~'], |
83 | | - cwd=target, check=True |
84 | | - ) |
| 89 | + sp.run(["git", "-reset", f"{precommit}~"], cwd=target, check=True) |
85 | 90 |
|
86 | 91 | @SubCommand.with_lock |
87 | 92 | def run(self): |
88 | 93 | "Process given layers" |
89 | 94 | self.restore = {} # Info for restoring for dry-run |
90 | 95 | paths = set() |
91 | | - layers = {layer['ident']: layer |
92 | | - for layer in self.sync.layers |
93 | | - if layer['ident']} |
| 96 | + layers = {layer["ident"]: layer for layer in self.sync.layers if layer["ident"]} |
94 | 97 | idents = self.args.ident |
95 | | - if idents == ['*']: |
| 98 | + if idents == ["*"]: |
96 | 99 | idents = layers.keys() |
97 | 100 | for ident in idents: |
98 | 101 | assert ident in layers, "Invalid ident" |
@@ -122,10 +125,9 @@ def run(self): |
122 | 125 | for ident in idents: |
123 | 126 | self.restore_layer(layers[ident]) |
124 | 127 | else: |
125 | | - self.sync.record(paths, recurse=False, skip_errors=True, |
126 | | - ignore_removed=True) |
| 128 | + self.sync.record( |
| 129 | + paths, recurse=False, skip_errors=True, ignore_removed=True |
| 130 | + ) |
127 | 131 | for path in paths: |
128 | | - if self.sync.fs_pathinfo(path)['layeridx'] == 0: |
129 | | - self.logger.warning( |
130 | | - 'Conflict with object in custom layer: ' + path |
131 | | - ) |
| 132 | + if self.sync.fs_pathinfo(path)["layeridx"] == 0: |
| 133 | + self.logger.warning("Conflict with object in custom layer: " + path) |
0 commit comments