Skip to content

Commit d4d1195

Browse files
Bug 1991427 - Add support for post-patch-actions to moz.yaml files r=tjr
This patch adds a new `post-patch-actions` section to be added in a `moz.yaml` file. This is useful when you need to run scripts that depend on Mozilla-specific patches being applied first. Differential Revision: https://phabricator.services.mozilla.com/D266665
1 parent c11b86e commit d4d1195

File tree

4 files changed

+112
-64
lines changed

4 files changed

+112
-64
lines changed

python/mozbuild/mozbuild/vendor/docs/index.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,37 @@ In the presence of patches, two steps are needed:
6464

6565
In the absence of patches, a single step is needed, and no extra argument is
6666
required.
67+
68+
69+
Vendoring Actions
70+
=================
71+
72+
Vendoring actions in the ``moz.yaml`` file can be configured to run either before
73+
or after patches are applied using separate sections:
74+
75+
* Actions in ``update-actions`` run **before** patches are applied
76+
* Actions in ``post-patch-actions`` run **after** patches are applied
77+
78+
This separation is useful when you need to run scripts that depend on Mozilla-specific
79+
patches being applied first, such as:
80+
81+
* Code generation scripts that need patched configuration files
82+
* Build system updates that depend on patched build definitions
83+
* Processing steps that require Mozilla-specific modifications to be in place
84+
85+
Example:
86+
87+
.. code-block:: yaml
88+
89+
# Actions that run before patches are applied
90+
update-actions:
91+
- action: run-script
92+
script: '{yaml_dir}/pre_patch_script.sh'
93+
cwd: '{yaml_dir}'
94+
95+
# Actions that run after patches are applied
96+
post-patch-actions:
97+
- action: run-script
98+
script: '{yaml_dir}/post_patch_script.sh'
99+
cwd: '{yaml_dir}'
100+
args: ['{revision}']

python/mozbuild/mozbuild/vendor/docs/template.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ vendoring:
158158
# All three file/path parameters ("keep", "exclude", and "include") support
159159
# filenames, directory names, and globs/wildcards.
160160

161-
# Actions to take after updating. Applied in order.
161+
# Actions to take after updating but before applying patches. Applied in order.
162162
# The action subfield is required. It must be one of:
163163
# - copy-file
164164
# - move-file
@@ -231,6 +231,15 @@ vendoring:
231231
script: '{cwd}/generate_sources.sh'
232232
cwd: '{yaml_dir}'
233233

234+
# Actions to take after patches have been applied. Applied in order.
235+
# Uses the same action types as update-actions.
236+
# optional
237+
post-patch-actions:
238+
- action: run-script
239+
script: '{yaml_dir}/post_patch_script.py'
240+
cwd: '{yaml_dir}'
241+
args: ['{revision}']
242+
234243

235244
# Configuration for automatic updating system.
236245
# optional

python/mozbuild/mozbuild/vendor/moz_yaml.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,39 @@ def load_moz_yaml(filename, verify=True, require_license_file=True):
119119

120120
def _schema_1():
121121
"""Returns Voluptuous Schema object."""
122+
123+
actions_schema = All(
124+
VendoringActions(),
125+
[
126+
{
127+
Required("action"): In(
128+
[
129+
"copy-file",
130+
"move-file",
131+
"move-dir",
132+
"replace-in-file",
133+
"replace-in-file-regex",
134+
"run-script",
135+
"run-command",
136+
"delete-path",
137+
"vcs-add-remove-files",
138+
],
139+
msg="Invalid action specified in vendoring-actions",
140+
),
141+
"from": All(str, Length(min=1)),
142+
"to": All(str, Length(min=1)),
143+
"pattern": All(str, Length(min=1)),
144+
"with": All(str, Length(min=1)),
145+
"file": All(str, Length(min=1)),
146+
"script": All(str, Length(min=1)),
147+
"command": All(str, Length(min=1)),
148+
"args": All([All(str, Length(min=1))]),
149+
"cwd": All(str, Length(min=1)),
150+
"path": All(str, Length(min=1)),
151+
}
152+
],
153+
)
154+
122155
return Schema(
123156
{
124157
Required("schema"): "1",
@@ -201,37 +234,8 @@ def _schema_1():
201234
"individual-files-default-upstream": str,
202235
"individual-files-default-destination": All(str, Length(min=1)),
203236
"individual-files-list": Unique([str]),
204-
"update-actions": All(
205-
UpdateActions(),
206-
[
207-
{
208-
Required("action"): In(
209-
[
210-
"copy-file",
211-
"move-file",
212-
"move-dir",
213-
"replace-in-file",
214-
"replace-in-file-regex",
215-
"run-script",
216-
"run-command",
217-
"delete-path",
218-
"vcs-add-remove-files",
219-
],
220-
msg="Invalid action specified in update-actions",
221-
),
222-
"from": All(str, Length(min=1)),
223-
"to": All(str, Length(min=1)),
224-
"pattern": All(str, Length(min=1)),
225-
"with": All(str, Length(min=1)),
226-
"file": All(str, Length(min=1)),
227-
"script": All(str, Length(min=1)),
228-
"command": All(str, Length(min=1)),
229-
"args": All([All(str, Length(min=1))]),
230-
"cwd": All(str, Length(min=1)),
231-
"path": All(str, Length(min=1)),
232-
}
233-
],
234-
),
237+
"update-actions": actions_schema,
238+
"post-patch-actions": actions_schema,
235239
},
236240
}
237241
)
@@ -438,8 +442,8 @@ def _schema_1_transform(manifest):
438442
return manifest
439443

440444

441-
class UpdateActions:
442-
"""Voluptuous validator which verifies the update actions(s) are valid."""
445+
class VendoringActions:
446+
"""Voluptuous validator which verifies the vendoring actions(s) are valid."""
443447

444448
def __call__(self, values):
445449
for v in values:
@@ -497,7 +501,7 @@ def __call__(self, values):
497501
return values
498502

499503
def __repr__(self):
500-
return "UpdateActions"
504+
return "VendoringActions"
501505

502506

503507
class UpdatebotTasks:

python/mozbuild/mozbuild/vendor/vendor_manifest.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def vendor(
137137
os.path.dirname(self.yaml_file),
138138
self.manifest["vendoring"]["vendor-directory"],
139139
)
140+
self.run_vendoring_actions(revision, "post-patch-actions")
140141
return
141142

142143
# ==========================================================
@@ -272,7 +273,7 @@ def process_regular_or_individual(
272273
self.logInfo({}, "Skipping fetching upstream source.")
273274

274275
self.logInfo({}, "Checking for update actions")
275-
self.update_files(new_revision)
276+
self.run_vendoring_actions(new_revision, "update-actions")
276277

277278
if self.patch_mode == "check":
278279
self.import_local_patches(
@@ -698,14 +699,14 @@ def spurious_check(self, revision, ignore_modified):
698699
"Version '{rev}' has changed {num} files.",
699700
)
700701

701-
def update_files(self, revision):
702-
if "update-actions" not in self.manifest["vendoring"]:
702+
def run_vendoring_actions(self, revision, actions_type="update-actions"):
703+
if actions_type not in self.manifest["vendoring"]:
703704
return
704705

705-
for update in self.manifest["vendoring"]["update-actions"]:
706-
if update["action"] == "copy-file":
707-
src = self.get_full_path(update["from"])
708-
dst = self.get_full_path(update["to"])
706+
for action in self.manifest["vendoring"][actions_type]:
707+
if action["action"] == "copy-file":
708+
src = self.get_full_path(action["from"])
709+
dst = self.get_full_path(action["to"])
709710

710711
self.logInfo(
711712
{"s": src, "d": dst}, "action: copy-file src: {s} dst: {d}"
@@ -715,24 +716,24 @@ def update_files(self, revision):
715716
contents = f.read()
716717
with open(dst, "w") as f:
717718
f.write(contents)
718-
elif update["action"] == "vcs-add-remove-files":
719-
directory = self.get_full_path(update["path"])
719+
elif action["action"] == "vcs-add-remove-files":
720+
directory = self.get_full_path(action["path"])
720721

721722
self.logInfo({"d": directory}, "action: vcs-add-remove-files dir: {d}")
722723

723724
self.repository.add_remove_files(directory)
724-
elif update["action"] == "move-file":
725-
src = self.get_full_path(update["from"])
726-
dst = self.get_full_path(update["to"])
725+
elif action["action"] == "move-file":
726+
src = self.get_full_path(action["from"])
727+
dst = self.get_full_path(action["to"])
727728

728729
self.logInfo(
729730
{"s": src, "d": dst}, "action: move-file src: {s} dst: {d}"
730731
)
731732

732733
shutil.move(src, dst)
733-
elif update["action"] == "move-dir":
734-
src = self.get_full_path(update["from"])
735-
dst = self.get_full_path(update["to"])
734+
elif action["action"] == "move-dir":
735+
src = self.get_full_path(action["from"])
736+
dst = self.get_full_path(action["to"])
736737

737738
self.logInfo(
738739
{"src": src, "dst": dst}, "action: move-dir src: {src} dst: {dst}"
@@ -761,32 +762,32 @@ def copy_tree(src, dst):
761762
copy_tree(src, dst)
762763
shutil.rmtree(src)
763764

764-
elif update["action"] in ["replace-in-file", "replace-in-file-regex"]:
765-
file = self.get_full_path(update["file"])
765+
elif action["action"] in ["replace-in-file", "replace-in-file-regex"]:
766+
file = self.get_full_path(action["file"])
766767

767768
self.logInfo({"file": file}, "action: replace-in-file file: {file}")
768769

769-
replacement = update["with"].replace("{revision}", revision)
770+
replacement = action["with"].replace("{revision}", revision)
770771
_replace_in_file(
771772
file,
772-
update["pattern"],
773+
action["pattern"],
773774
replacement,
774-
regex=update["action"] == "replace-in-file-regex",
775+
regex=action["action"] == "replace-in-file-regex",
775776
)
776-
elif update["action"] == "delete-path":
777-
path = self.get_full_path(update["path"])
777+
elif action["action"] == "delete-path":
778+
path = self.get_full_path(action["path"])
778779
self.logInfo({"path": path}, "action: delete-path path: {path}")
779780
mozfile.remove(path)
780-
elif update["action"] in ["run-script", "run-command"]:
781-
if update["action"] == "run-script":
782-
command = self.get_full_path(update["script"], support_cwd=True)
781+
elif action["action"] in ["run-script", "run-command"]:
782+
if action["action"] == "run-script":
783+
command = self.get_full_path(action["script"], support_cwd=True)
783784
else:
784-
command = update["command"]
785+
command = action["command"]
785786

786-
run_dir = self.get_full_path(update["cwd"], support_cwd=True)
787+
run_dir = self.get_full_path(action["cwd"], support_cwd=True)
787788

788789
args = []
789-
for a in update.get("args", []):
790+
for a in action.get("args", []):
790791
if a == "{revision}":
791792
args.append(revision)
792793
elif any(
@@ -808,7 +809,7 @@ def copy_tree(src, dst):
808809
"command": command,
809810
"run_dir": run_dir,
810811
"args": args,
811-
"type": update["action"],
812+
"type": action["action"],
812813
},
813814
"action: {type} command: {command} working dir: {run_dir} args: {args}",
814815
)

0 commit comments

Comments
 (0)