Debian Codegraph @

tools/generate_patches.py

raw
1 #!/usr/bin/env python3
2 # Copyright 2025 Google LLC
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5 #
6 # It is a pain to tell Bazel "check out this repo and build it with these
7 # other external files". We can't have something like @freetype use files
8 # from @freetype_config because the include paths don't work. We can't
9 # copy the files from @freetype_config into @freetype first via a genrule
10 # without having external clients need to do some very janky things to set
11 # up the dependencies correctly. A modified repo_rule didn't work either because
12 # the absolute file paths got trapped in the MODULE.bazel.lock and kjlubick@
13 # couldn't figure out how to get relative file paths in there to work.
14 #
15 # The least bad option is to combine any files we want to add as a "patch"
16 # and then apply the patch via the git_repository_rule when we check it out.
17 # This Python script takes in one or more pairs of "source file in Skia" and
18 # "dst path in other repo" and makes a patch with all those contents.
19 # This file will need to be re-run (see //bazel/Makefile:generate_third_party_patches)
20 # if we update those config files to keep things in sync, which is not ideal
21 # but it works with the way things are.
22
23 import sys
24
25 def create_single_file_patch(source_file, destination_path):
26 try:
27 with open(source_file, 'r') as f:
28 content = f.read()
29 except FileNotFoundError:
30 print(f"Error: Source file not found: {source_file}", file=sys.stderr)
31 return None
32
33 lines = content.splitlines()
34 line_count = len(lines)
35
36 # Construct the patch section with the new header lines
37 # of note, the bazel patch tool doesn't seem to care about the index
38 # (git would normally have the sha1 sum of the file contents plus a header)
39 # so we can just use a stub and it works fine.
40 patch_section = f"new file mode 100644\n" \
41 f"index 0000000..fffffff\n" \
42 f"--- /dev/null\n" \
43 f"+++ {destination_path}\n" \
44 f"@@ -0,0 +1,{line_count} @@\n"
45
46 for line in lines:
47 patch_section += "+" + line + "\n"
48 return patch_section
49
50
51 if __name__ == "__main__":
52 if (len(sys.argv) - 1) % 2 != 0 or len(sys.argv) < 3:
53 print("Usage: python3 generate_patch.py <source_file1> <destination_path1> [<source_file2> <destination_path2> ...]", file=sys.stderr)
54 sys.exit(1)
55
56 all_patch_content = []
57 # sys.argv[0] is the script name, so start from index 1 and step by 2
58 for i in range(1, len(sys.argv), 2):
59 source_file = sys.argv[i]
60 destination_path = sys.argv[i + 1]
61
62 patch_section = create_single_file_patch(source_file, destination_path)
63 if patch_section is None:
64 sys.exit(1)
65 all_patch_content.append(patch_section)
66
67 print("".join(all_patch_content))