-
Notifications
You must be signed in to change notification settings - Fork 182
/
module_patch.py
177 lines (158 loc) · 6.36 KB
/
module_patch.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
from pathlib import Path
from ...components.nfcore_component import NFCoreComponent
from ..modules_differ import ModulesDiffer
def module_patch(module_lint_obj, module: NFCoreComponent):
"""
Lint a patch file found in a module
Checks that the file name is well formed, and that
the patch can be applied in reverse with the correct result.
"""
# Check if the module is patched
if not module.is_patched:
# Nothing to do
return
if not check_patch_valid(module, module.patch_path):
# Test failed, just exit
return
patch_reversible(module_lint_obj, module, module.patch_path)
def check_patch_valid(module, patch_path):
"""
Checks whether a patch is valid. Looks for lines like
--- <path>
+++ <path>
@@ n,n n,n @@
and make sure that the come in the right order and that
the reported paths exists. If the patch file performs
file creation or deletion we issue a lint warning.
Args:
module (NFCoreComponent): The module currently being linted
patch_path (Path): The absolute path to the patch file.
Returns:
(bool): False if any test failed, True otherwise
"""
with open(patch_path) as fh:
patch_lines = fh.readlines()
# Check that the file contains a patch for at least one file
# and that the file is in the correct directory
paths_in_patch = []
passed = True
it = iter(patch_lines)
try:
while True:
line = next(it)
if line.startswith("---"):
frompath = Path(line.split(" ")[1].strip("\n"))
line = next(it)
if not line.startswith("+++"):
module.failed.append(
(
"patch_valid",
"Patch file invalid. Line starting with '---' should always be followed by line starting with '+++'",
patch_path,
)
)
passed = False
continue
topath = Path(line.split(" ")[1].strip("\n"))
if frompath == Path("/dev/null"):
paths_in_patch.append((frompath, ModulesDiffer.DiffEnum.CREATED))
elif topath == Path("/dev/null"):
paths_in_patch.append((frompath, ModulesDiffer.DiffEnum.REMOVED))
elif frompath == topath:
paths_in_patch.append((frompath, ModulesDiffer.DiffEnum.CHANGED))
else:
module.failed.append(
(
"patch_valid",
f"Patch file invaldi. From file '{frompath}' mismatched with to path '{topath}'",
patch_path,
)
)
passed = False
# Check that the next line is hunk
line = next(it)
if not line.startswith("@@"):
module.failed.append(
(
"patch_valid",
"Patch file invalid. File declarations should be followed by hunk",
patch_path,
)
)
passed = False
except StopIteration:
pass
if not passed:
return False
if len(paths_in_patch) == 0:
module.failed.append(("patch_valid", "Patch file invalid. Found no patches", patch_path))
return False
# Go through the files and check that they exist
# Warn about any created or removed files
passed = True
for path, diff_status in paths_in_patch:
if diff_status == ModulesDiffer.DiffEnum.CHANGED:
if not Path(module.base_dir, path).exists():
module.failed.append(
(
"patch_valid",
f"Patch file invalid. Path '{path}' does not exist but is reported in patch file.",
patch_path,
)
)
passed = False
continue
elif diff_status == ModulesDiffer.DiffEnum.CREATED:
if not Path(module.base_dir, path).exists():
module.failed.append(
(
"patch_valid",
f"Patch file invalid. Path '{path}' does not exist but is reported in patch file.",
patch_path,
)
)
passed = False
continue
module.warned.append(
("patch", f"Patch file performs file creation of {path}. This is discouraged."), patch_path
)
elif diff_status == ModulesDiffer.DiffEnum.REMOVED:
if Path(module.base_dir, path).exists():
module.failed.append(
(
"patch_valid",
f"Patch file invalid. Path '{path}' is reported as deleted but exists.",
patch_path,
)
)
passed = False
continue
module.warned.append(
("patch", f"Patch file performs file deletion of {path}. This is discouraged.", patch_path)
)
if passed:
module.passed.append(("patch_valid", "Patch file is valid", patch_path))
return passed
def patch_reversible(module_lint_object, module, patch_path):
"""
Try applying a patch in reverse to see if it is up to date
Args:
module (NFCoreComponent): The module currently being linted
patch_path (Path): The absolute path to the patch file.
Returns:
(bool): False if any test failed, True otherwise
"""
try:
ModulesDiffer.try_apply_patch(
module.component_name,
module_lint_object.modules_repo.repo_path,
patch_path,
Path(module.component_dir).relative_to(module.base_dir),
reverse=True,
)
except LookupError:
# Patch failed. Save the patch file by moving to the install dir
module.failed.append(("patch_reversible", "Patch file is outdated or edited", patch_path))
return False
module.passed.append(("patch_reversible", "Patch agrees with module files", patch_path))
return True