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