Commit | Line | Data |
---|---|---|
0256fc10 IJ |
1 | # -*- python -*- |
2 | # | |
3 | # Hippotat - Asinine IP Over HTTP program | |
4 | # hippotatlib/ownsource.py - Automatic source code provision (AGPL compliance) | |
5 | # | |
6 | # Copyright 2017 Ian Jackson | |
7 | # | |
8 | # This program is free software: you can redistribute it and/or modify | |
9 | # it under the terms of the GNU Affero General Public License as | |
10 | # published by the Free Software Foundation, either version 3 of the | |
11 | # License, or (at your option) any later version, with the "CAF Login | |
12 | # Exception" as published by Ian Jackson (version 2, or at your option | |
13 | # any later version) as an Additional Permission. | |
14 | # | |
15 | # This program is distributed in the hope that it will be useful, | |
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | # GNU Affero General Public License for more details. | |
19 | # | |
20 | # You should have received a copy of the GNU Affero General Public | |
21 | # License and the CAF Login Exception along with this program, in the | |
22 | # file AGPLv3+CAFv2. If not, email Ian Jackson | |
23 | # <ijackson@chiark.greenend.org.uk>. | |
24 | ||
4460af45 | 25 | |
b635cd93 | 26 | import os |
4460af45 IJ |
27 | import sys |
28 | import fnmatch | |
b635cd93 IJ |
29 | import stat |
30 | import subprocess | |
e68fca7c | 31 | import tempfile |
a7d05900 IJ |
32 | import shutil |
33 | ||
34 | try: import debian.deb822 | |
35 | except ImportError: pass | |
4460af45 IJ |
36 | |
37 | class SourceShipmentPreparer(): | |
38 | def __init__(s, destdir): | |
39 | # caller may modify, and should read after calling generate() | |
a7d05900 IJ |
40 | s.output_names = ['srcbomb.tar.gz', 'fullsrcbomb.tar'] |
41 | s.output_paths = [None,None] # alternatively caller may read this | |
4460af45 | 42 | # defaults, caller can modify after creation |
c2c0da38 | 43 | s.logger = lambda m: print('SourceShipmentPreparer',m) |
4460af45 | 44 | s.src_filter = s.src_filter_glob |
e68fca7c | 45 | s.src_package_globs = ['!/usr/local/*', '/usr*'] |
e98894bc | 46 | s.src_filter_globs = ['!/etc/*'] |
4460af45 | 47 | s.src_likeparent = s.src_likeparent_git |
28861486 | 48 | s.src_direxcludes = s.src_direxcludes_git |
e98894bc | 49 | s.report_from_packages = s.report_from_packages_debian |
4460af45 | 50 | s.cwd = os.getcwd() |
b635cd93 | 51 | s.find_rune_base = "find -type f -perm -004 \! -path '*/tmp/*'" |
28861486 | 52 | s.ignores = ['*~', '*.bak', '*.tmp', '#*#', '__pycache__', |
b635cd93 | 53 | '[0-9][0-9][0-9][0-9]-src.cpio'] |
4460af45 | 54 | s.rune_shell = ['/bin/bash', '-ec'] |
b635cd93 | 55 | s.show_pathnames = True |
a7d05900 | 56 | s.download_packages = True |
b635cd93 | 57 | s.rune_cpio = r''' |
4460af45 IJ |
58 | set -o pipefail |
59 | ( | |
60 | %s | |
61 | # ^ by default, is find ... -print0 | |
62 | ) | ( | |
63 | cpio -Hustar -o --quiet -0 -R 1000:1000 || \ | |
64 | cpio -Hustar -o --quiet -0 | |
65 | ) | |
66 | ''' | |
67 | s.rune_portmanteau = r''' | |
a7d05900 IJ |
68 | GZIP=-1 tar zcf - "$@" |
69 | ''' | |
70 | s.rune_portmanteau_uncompressed = r''' | |
71 | tar cf - "$@" | |
4460af45 IJ |
72 | ''' |
73 | s.manifest_name='0000-MANIFEST.txt' | |
74 | # private | |
75 | s._destdir = destdir | |
b635cd93 | 76 | s._outcounter = 0 |
4460af45 | 77 | s._manifest = [] |
2371516a | 78 | s._dirmap = { } |
e98894bc | 79 | s._package_files = { } # map filename => infol |
a7d05900 IJ |
80 | s._packages_path = os.path.join(s._destdir, 'packages') |
81 | s._package_sources = [] | |
4460af45 | 82 | |
e98894bc IJ |
83 | def thing_matches_globs(s, thing, globs): |
84 | for pat in globs: | |
4460af45 IJ |
85 | negate = pat.startswith('!') |
86 | if negate: pat = pat[1:] | |
e98894bc | 87 | if fnmatch.fnmatch(thing, pat): |
4460af45 IJ |
88 | return not negate |
89 | return negate | |
90 | ||
e98894bc | 91 | def src_filter_glob(s, src): # default s.src_filter |
e68fca7c | 92 | return s.thing_matches_globs(src, s.src_filter_globs) |
e98894bc | 93 | |
28861486 IJ |
94 | def src_direxcludes_git(s, d): |
95 | try: | |
96 | excl = open(os.path.join(d, '.gitignore')) | |
97 | except FileNotFoundError: | |
98 | return [] | |
99 | r = [] | |
100 | for l in excl: | |
101 | l.strip | |
102 | if l.startswith('#'): next | |
103 | if not len(l): next | |
104 | r += l | |
105 | return r | |
106 | ||
4460af45 IJ |
107 | def src_likeparent_git(s, src): |
108 | try: | |
b635cd93 | 109 | os.stat(os.path.join(src, '.git/.')) |
4460af45 IJ |
110 | except FileNotFoundError: |
111 | return False | |
112 | else: | |
113 | return True | |
114 | ||
115 | def src_parentfinder(s, src, infol): # callers may monkey-patch away | |
116 | for deref in (False,True): | |
117 | xinfo = [] | |
118 | ||
119 | search = src | |
120 | if deref: | |
121 | search = os.path.realpath(search) | |
122 | ||
123 | def ascend(): | |
b635cd93 | 124 | nonlocal search |
4460af45 IJ |
125 | xinfo.append(os.path.basename(search)) |
126 | search = os.path.dirname(search) | |
127 | ||
128 | try: | |
b635cd93 | 129 | stab = os.lstat(search) |
4460af45 IJ |
130 | except FileNotFoundError: |
131 | return | |
132 | if stat.S_ISREG(stab.st_mode): | |
133 | ascend() | |
134 | ||
135 | while not os.path.ismount(search): | |
136 | if s.src_likeparent(search): | |
137 | xinfo.reverse() | |
b635cd93 | 138 | if len(xinfo): infol.append('want=' + os.path.join(*xinfo)) |
4460af45 IJ |
139 | return search |
140 | ||
141 | ascend() | |
142 | ||
143 | # no .git found anywhere | |
b635cd93 | 144 | return src |
4460af45 | 145 | |
e68fca7c | 146 | def path_prenormaliser(s, d, infol): # callers may monkey-patch away |
4460af45 IJ |
147 | return os.path.join(s.cwd, os.path.abspath(d)) |
148 | ||
e98894bc | 149 | def srcdir_find_rune(s, d): |
b635cd93 | 150 | script = s.find_rune_base |
a7d05900 | 151 | ignores = s.ignores + s.output_names + [s.manifest_name] |
28861486 IJ |
152 | ignores += s.src_direxcludes(d) |
153 | for excl in ignores: | |
4460af45 | 154 | assert("'" not in excl) |
28861486 IJ |
155 | script += r" \! -name '%s'" % excl |
156 | script += r" \! -path '*/%s/*'" % excl | |
4460af45 | 157 | script += ' -print0' |
b635cd93 | 158 | return script |
4460af45 | 159 | |
2371516a | 160 | def manifest_append(s, name, infol): |
a53aba4d | 161 | s._manifest.append({ 'file':name, 'info':' '.join(infol) }) |
2371516a | 162 | |
e68fca7c IJ |
163 | def manifest_append_absentfile(s, name, infol): |
164 | s._manifest.append({ 'file_print':name, 'info':' '.join(infol) }) | |
165 | ||
4460af45 | 166 | def new_output_name(s, nametail, infol): |
b635cd93 IJ |
167 | s._outcounter += 1 |
168 | name = '%04d-%s' % (s._outcounter, nametail) | |
2371516a | 169 | s.manifest_append(name, infol) |
4460af45 IJ |
170 | return name |
171 | ||
172 | def open_output_fh(s, name, mode): | |
173 | return open(os.path.join(s._destdir, name), mode) | |
174 | ||
e98894bc | 175 | def src_dir(s, d, infol): |
2371516a IJ |
176 | try: name = s._dirmap[d] |
177 | except KeyError: pass | |
178 | else: | |
179 | s.manifest_append(name, infol) | |
180 | return | |
181 | ||
b635cd93 | 182 | if s.show_pathnames: infol.append(d) |
e98894bc | 183 | find_rune = s.srcdir_find_rune(d) |
4460af45 | 184 | total_rune = s.rune_cpio % find_rune |
2371516a IJ |
185 | |
186 | name = s.new_output_name('src.cpio', infol) | |
187 | s._dirmap[d] = name | |
188 | fh = s.open_output_fh(name, 'wb') | |
189 | ||
c2c0da38 IJ |
190 | s.logger('packing up into %s: %s (because %s)' % |
191 | (name, d, ' '.join(infol))) | |
192 | ||
4460af45 | 193 | subprocess.run(s.rune_shell + [total_rune], |
b635cd93 | 194 | cwd=d, |
4460af45 IJ |
195 | stdin=subprocess.DEVNULL, |
196 | stdout=fh, | |
b635cd93 IJ |
197 | restore_signals=True, |
198 | check=True) | |
4460af45 IJ |
199 | fh.close() |
200 | ||
e98894bc | 201 | def src_indir(s, d, infol): |
e68fca7c | 202 | d = s.path_prenormaliser(d, infol) |
4460af45 | 203 | if not s.src_filter(d): return |
e98894bc | 204 | |
4460af45 | 205 | d = s.src_parentfinder(d, infol) |
e68fca7c IJ |
206 | if d is None: return |
207 | s.src_dir(d, infol) | |
e98894bc IJ |
208 | |
209 | def report_from_packages_debian(s, files): | |
e68fca7c | 210 | dpkg_S_in = tempfile.TemporaryFile(mode='w+') |
e98894bc IJ |
211 | for (file, infols) in files.items(): |
212 | assert('\n' not in file) | |
213 | dpkg_S_in.write(file) | |
214 | dpkg_S_in.write('\0') | |
215 | dpkg_S_in.seek(0) | |
216 | cmdl = ['xargs','-0r','dpkg','-S','--'] | |
217 | dpkg_S = subprocess.Popen(cmdl, | |
a53aba4d IJ |
218 | cwd='/', |
219 | stdin=dpkg_S_in, | |
220 | stdout=subprocess.PIPE, | |
221 | stderr=sys.stderr, | |
222 | close_fds=False) | |
e68fca7c | 223 | dpkg_show_in = tempfile.TemporaryFile(mode='w+') |
e98894bc | 224 | pkginfos = { } |
e68fca7c IJ |
225 | for l in dpkg_S.stdout: |
226 | l = l.strip(b'\n').decode('utf-8') | |
e98894bc | 227 | (pkgs, fname) = l.split(': ',1) |
e68fca7c IJ |
228 | pks = pkgs.split(', ') |
229 | for pk in pks: | |
230 | pkginfos.setdefault(pk,{'files':[]})['files'].append(fname) | |
231 | print(pk, file=dpkg_show_in) | |
a53aba4d IJ |
232 | assert(dpkg_S.wait() == 0) |
233 | dpkg_show_in.seek(0) | |
234 | cmdl = ['xargs','-r','dpkg-query', | |
a7d05900 | 235 | r'-f${binary:Package}\t${Package}\t${Architecture}\t${Version}\t${source:Package}\t${source:Version}\t${source:Upstream-Version}\n', |
a53aba4d IJ |
236 | '--show','--'] |
237 | dpkg_show = subprocess.Popen(cmdl, | |
238 | cwd='/', | |
239 | stdin=dpkg_show_in, | |
240 | stdout=subprocess.PIPE, | |
241 | stderr=sys.stderr, | |
242 | close_fds=False) | |
243 | for l in dpkg_show.stdout: | |
e68fca7c | 244 | l = l.strip(b'\n').decode('utf-8') |
a7d05900 | 245 | (pk,p,a,v,sp,sv,suv) = l.split('\t') |
e68fca7c IJ |
246 | pkginfos[pk]['binary'] = p |
247 | pkginfos[pk]['arch'] = a | |
248 | pkginfos[pk]['version'] = v | |
249 | pkginfos[pk]['source'] = sp | |
250 | pkginfos[pk]['sourceversion'] = sv | |
a7d05900 | 251 | pkginfos[pk]['sourceupstreamversion'] = sv |
a53aba4d | 252 | assert(dpkg_show.wait() == 0) |
e68fca7c IJ |
253 | for pk in sorted(pkginfos.keys()): |
254 | pi = pkginfos[pk] | |
255 | debfname = '%s_%s_%s.deb' % (pi['binary'], pi['version'], pi['arch']) | |
a53aba4d | 256 | dscfname = '%s_%s.dsc' % (pi['source'], pi['sourceversion']) |
e68fca7c | 257 | s.manifest_append_absentfile(dscfname, [debfname]) |
c2c0da38 IJ |
258 | s.logger('mentioning %s and %s because %s' % |
259 | (dscfname, debfname, pi['files'][0])) | |
e68fca7c IJ |
260 | for fname in pi['files']: |
261 | infol = files[fname] | |
56ffddf8 | 262 | if s.show_pathnames: infol = infol + ['loaded='+fname] |
e68fca7c | 263 | s.manifest_append_absentfile(' \t' + debfname, infol) |
e98894bc | 264 | |
a7d05900 IJ |
265 | if s.download_packages: |
266 | try: os.mkdir(s._packages_path) | |
267 | except FileExistsError: pass | |
268 | ||
269 | cmdl = ['apt-get','--download-only','source', | |
270 | '%s=%s' % (pi['source'], pi['sourceversion'])] | |
271 | subprocess.run(cmdl, | |
272 | cwd=s._packages_path, | |
273 | stdin=subprocess.DEVNULL, | |
274 | stdout=sys.stdout, | |
275 | stderr=sys.stderr, | |
276 | restore_signals=True, | |
277 | check=True) | |
278 | ||
279 | s._package_sources.append(dscfname) | |
280 | dsc = debian.deb822.Dsc(open(s._packages_path + '/' + dscfname)) | |
281 | for indsc in dsc['Files']: | |
282 | s._package_sources.append(indsc['name']) | |
283 | ||
e98894bc IJ |
284 | def thing_ought_packaged(s, fname): |
285 | return s.thing_matches_globs(fname, s.src_package_globs) | |
286 | ||
e68fca7c IJ |
287 | def src_file_packaged(s, fname, infol): |
288 | s._package_files.setdefault(fname,[]).extend(infol) | |
e98894bc IJ |
289 | |
290 | def src_file(s, fname, infol): | |
291 | def fngens(): | |
e68fca7c IJ |
292 | yield (infol, fname) |
293 | infol_copy = infol.copy() | |
294 | yield (infol_copy, s.path_prenormaliser(fname, infol_copy)) | |
295 | yield (infol, os.path.realpath(fname)) | |
296 | ||
297 | for (tinfol, tfname) in fngens(): | |
298 | if s.thing_ought_packaged(tfname): | |
299 | s.src_file_packaged(tfname, tinfol) | |
e98894bc | 300 | return |
4460af45 | 301 | |
e98894bc IJ |
302 | s.src_indir(fname, infol) |
303 | ||
304 | def src_argv0(s, program, infol): | |
e68fca7c | 305 | s.src_file(program, infol) |
e98894bc IJ |
306 | |
307 | def src_syspath(s, fname, infol): | |
56ffddf8 | 308 | if s.thing_ought_packaged(fname): return |
e98894bc IJ |
309 | s.src_indir(fname, infol) |
310 | ||
311 | def src_module(s, m, infol): | |
312 | try: fname = m.__file__ | |
16374080 | 313 | except AttributeError: return |
56ffddf8 | 314 | infol.append('module='+m.__name__) |
16374080 | 315 | |
e98894bc IJ |
316 | if s.thing_ought_packaged(fname): |
317 | s.src_file_packaged(fname, infol) | |
318 | else: | |
e68fca7c | 319 | s.src_indir(fname, infol) |
e98894bc IJ |
320 | |
321 | def srcs_allitems(s, dirs=sys.path): | |
c2c0da38 | 322 | s.logger('allitems') |
e98894bc | 323 | s.src_argv0(sys.argv[0], ['argv[0]']) |
4460af45 | 324 | for d in sys.path: |
e98894bc | 325 | s.src_syspath(d, ['sys.path']) |
16374080 | 326 | for m in sys.modules.values(): |
e98894bc | 327 | s.src_module(m, ['sys.modules']) |
e68fca7c | 328 | s.report_from_packages(s._package_files) |
c2c0da38 | 329 | s.logger('allitems done') |
4460af45 | 330 | |
a7d05900 IJ |
331 | def _mk_portmanteau(s, ix, rune, cwd, files): |
332 | output_name = s.output_names[ix] | |
333 | s.logger('making portmanteau %s' % output_name) | |
334 | output_path = os.path.join(s._destdir, output_name) | |
335 | subprocess.run(s.rune_shell + [ rune, 'x' ] + files, | |
336 | cwd=cwd, | |
337 | stdin=subprocess.DEVNULL, | |
338 | stdout=open(output_path, 'wb'), | |
339 | restore_signals=True, | |
340 | check=True) | |
341 | s.output_paths[ix] = output_path | |
342 | ||
343 | def mk_inner_portmanteau(s): | |
344 | outputs = [s.manifest_name] | |
b635cd93 | 345 | mfh = s.open_output_fh(s.manifest_name,'w') |
a53aba4d IJ |
346 | for me in s._manifest: |
347 | try: fname = me['file'] | |
348 | except KeyError: fname = me.get('file_print','') | |
a7d05900 | 349 | else: outputs.append(fname) |
a53aba4d | 350 | print('%s\t%s' % (fname, me['info']), file=mfh) |
4460af45 | 351 | mfh.close() |
a7d05900 IJ |
352 | |
353 | s._mk_portmanteau(0, s.rune_portmanteau, | |
354 | s._destdir, outputs) | |
355 | ||
356 | def mk_packages_portmanteau(s): | |
357 | s._mk_portmanteau(1, s.rune_portmanteau_uncompressed, | |
358 | s._packages_path, s._package_sources) | |
4460af45 IJ |
359 | |
360 | def generate(s): | |
e98894bc | 361 | s.srcs_allitems() |
a7d05900 IJ |
362 | s.mk_inner_portmanteau() |
363 | s.mk_packages_portmanteau() | |
364 | s.logger('portmanteau ready in %s %s' % tuple(s.output_paths)) |