Debian Codegraph @

kernel-headers/update

raw
1 #!/usr/bin/env python3
2 # Copyright 2018 Mellanox Technologies Inc.
3 # Licensed under BSD (MIT variant) or GPLv2. See COPYING.
4 # PYTHON_ARGCOMPLETE_OK
5 """This script takes a commitish from a kernel tree and synchronizes the RDMA
6 headers we use with that tree.
7
8 During development, before commits are accepted to the official kernel git
9 tree, the --not-final option should be used. Once finalized the commit should
10 be revised using --amend, eg using the exec feature of 'git rebase'"""
11 import argparse
12 import subprocess
13 import tempfile
14 import os
15 import contextlib
16 import textwrap
17 import email.utils
18 import collections
19
20 def git_call(args):
21 """Run git and display the output to the terminal"""
22 return subprocess.check_call(['git',] + args);
23
24 def git_output(args,mode=None,input=None):
25 """Run git and return the output"""
26 o = subprocess.check_output(['git',] + args,input=input);
27 if mode == "raw":
28 return o;
29 return o.strip();
30
31 @contextlib.contextmanager
32 def in_directory(dir):
33 """Context manager that chdirs into a directory and restores the original
34 directory when closed."""
35 cdir = os.getcwd();
36 old_env = {};
37 try:
38 # git rebase invokes its exec with a bunch of git variables set that
39 # prevent us from invoking git in another tree, blow them away.
40 for k in list(os.environ.keys()):
41 if k.startswith("GIT"):
42 old_env[k] = os.environ[k];
43 del os.environ[k];
44
45 os.chdir(dir);
46 yield True;
47 finally:
48 os.chdir(cdir);
49 os.environ.update(old_env);
50
51 def copy_objects(args):
52 """Copy the uapi header objects from the kernel repo at the commit indicated
53 into our repo. This is done by having git copy the tree object and blobs from the
54 kernel tree into this tree and then revising our index. This is a simple way
55 to ensure they match exactly."""
56 with in_directory(args.KERNEL_GIT):
57 if args.not_final:
58 fmt = "--format=?? (\"%s\")";
59 else:
60 fmt = "--format=%h (\"%s\")";
61
62 kernel_desc = git_output(["log",
63 "--abbrev=12","-1",
64 fmt,
65 args.COMMIT]);
66 ntree = git_output(["rev-parse",
67 "%s:include/uapi/rdma"%(args.COMMIT)]);
68 pack = git_output(["pack-objects",
69 "-q","--revs","--stdout"],
70 mode="raw",
71 input=ntree);
72 git_output(["unpack-objects","-q"],input=pack);
73 return (ntree,kernel_desc);
74
75 def update_cmake(args,ntree):
76 """Create a new CMakeLists.txt that lists all of the kernel headers
77 for installation."""
78 # We need to expand to a publish_internal_headers for each directory
79 fns = git_output(["ls-tree","--name-only","--full-tree","-r",ntree]).splitlines();
80 groups = collections.defaultdict(list);
81 for I in fns:
82 d,p = os.path.split(os.path.join("rdma",I.decode()));
83 groups[d].append(p);
84
85 data = subprocess.check_output(['git',"cat-file","blob",
86 ":kernel-headers/CMakeLists.txt"]);
87 data = data.decode();
88
89 # Build a new CMakeLists.txt in a temporary file
90 with tempfile.NamedTemporaryFile("wt") as F:
91 # Emit the headers lists
92 for I,vals in sorted(groups.items()):
93 F.write("publish_internal_headers(%s\n"%(I));
94 for J in sorted(vals):
95 F.write(" %s\n"%(os.path.join(I,J)));
96 F.write(" )\n");
97 F.write("\n");
98
99 # Throw away the old header lists
100 cur = iter(data.splitlines());
101 for ln in cur:
102 if not ln:
103 continue;
104 if ln.startswith("publish_internal_headers(rdma"):
105 while not next(cur).startswith(" )"):
106 pass;
107 continue;
108 F.write(ln + '\n');
109 break;
110
111 # and copy the remaining lines
112 for ln in cur:
113 F.write(ln + '\n');
114
115 F.flush();
116 blob = git_output(["hash-object","-w",F.name]);
117
118 git_call(["update-index","--cacheinfo",
119 b"0644,%s,kernel-headers/CMakeLists.txt"%(blob)]);
120
121 def make_commit(args,ntree,kernel_desc):
122 """Make the rdma-core commit that syncs the kernel header directory."""
123 head_id = git_output(["rev-parse","HEAD"]);
124 old_tree_id = git_output(["rev-parse",b"%s^{tree}"%(head_id)]);
125
126 if args.amend:
127 subject = git_output(["log","-1","--format=%s"]).decode();
128 if subject != "Update kernel headers":
129 raise ValueError("In amend mode, but current HEAD does not seem to be a kernel update with subject %r"%(
130 subject));
131 parent = git_output(["rev-parse",head_id + b"^"]);
132 else:
133 parent = head_id;
134
135 emaila = email.utils.formataddr((git_output(["config","user.name"]).decode(),
136 git_output(["config","user.email"]).decode()));
137
138 # Build a new tree object that replaces the kernel headers directory
139 with tempfile.NamedTemporaryFile() as F:
140 os.environ["GIT_INDEX_FILE"] = F.name;
141 git_call(["read-tree",head_id]);
142 git_call(["rm","-r","--quiet","--cached",
143 "kernel-headers/rdma"]);
144 git_call(["read-tree","--prefix=kernel-headers/rdma",ntree]);
145 update_cmake(args,ntree);
146 all_tree = git_output(["write-tree"]);
147 del os.environ["GIT_INDEX_FILE"];
148
149 if not args.amend and old_tree_id == all_tree:
150 raise ValueError("Commit is empty, aborting");
151
152 # And now create the commit
153 msg="Update kernel headers\n\n";
154 p = textwrap.fill("To commit: %s."%(kernel_desc.decode()),
155 width=74)
156 msg = msg + p;
157 msg = msg + "\n\nSigned-off-by: %s\n"%(emaila);
158
159 commit = git_output(["commit-tree",all_tree,"-p",parent,
160 "-F","-"],
161 input=msg.encode());
162 return commit,head_id;
163
164 parser = argparse.ArgumentParser(description='Update kernel headers from the kernel tree')
165 parser.add_argument("--amend",
166 action="store_true",
167 default=False,
168 help="Replace the top commit with the the kernel header commit");
169 parser.add_argument("--not-final",
170 action="store_true",
171 default=False,
172 help="Use if the git commit given is not part of the official kernel git tree. This option should be used during development.");
173 parser.add_argument("KERNEL_GIT",
174 action="store",
175 help="Kernel git directory");
176 parser.add_argument("COMMIT",
177 action="store",
178 help="Kernel commitish to synchronize headers with");
179
180 try:
181 import argcomplete;
182 argcomplete.autocomplete(parser);
183 except ImportError:
184 pass;
185
186 args = parser.parse_args();
187
188 ntree,kernel_desc = copy_objects(args);
189 commit,head_id = make_commit(args,ntree,kernel_desc);
190
191 # Finalize
192 if args.amend:
193 print("Commit amended");
194 git_call(["--no-pager","diff","--stat",head_id,commit]);
195 git_call(["reset","--merge",commit]);
196 else:
197 git_call(["merge","--ff","--ff-only",commit]);