chiark / gitweb /
ownsource does dpkg stuff
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 4 Apr 2017 14:53:01 +0000 (15:53 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 4 Apr 2017 14:53:01 +0000 (15:53 +0100)
hippotatlib/ownsource.py

index 89bc43f..ea78664 100644 (file)
@@ -5,6 +5,7 @@ import sys
 import fnmatch
 import stat
 import subprocess
+import tempfile
 
 class SourceShipmentPreparer():
   def __init__(s, destdir):
@@ -12,7 +13,7 @@ class SourceShipmentPreparer():
     s.output_name = 'srcbomb.tar.gz'
     # defaults, caller can modify after creation
     s.src_filter = s.src_filter_glob
-    s.src_package_globs = [!'/usr/local/*', '/usr*']
+    s.src_package_globs = ['!/usr/local/*', '/usr*']
     s.src_filter_globs = ['!/etc/*']
     s.src_likeparent = s.src_likeparent_git
     s.report_from_packages = s.report_from_packages_debian
@@ -54,7 +55,7 @@ class SourceShipmentPreparer():
     return negate
 
   def src_filter_glob(s, src): # default s.src_filter
-    return s.thing_matches_globs(s, src, s.src_filter_globs)
+    return s.thing_matches_globs(src, s.src_filter_globs)
 
   def src_likeparent_git(s, src):
     try:
@@ -95,7 +96,7 @@ class SourceShipmentPreparer():
     # no .git found anywhere
     return src
 
-  def src_prenormaliser(s, d, infol): # callers may monkey-patch away
+  def path_prenormaliser(s, d, infol): # callers may monkey-patch away
     return os.path.join(s.cwd, os.path.abspath(d))
 
   def srcdir_find_rune(s, d):
@@ -109,6 +110,9 @@ class SourceShipmentPreparer():
   def manifest_append(s, name, infol):
     s._manifest.append({ 'file':name, 'info':' '.join(infol) })
 
+  def manifest_append_absentfile(s, name, infol):
+    s._manifest.append({ 'file_print':name, 'info':' '.join(infol) })
+
   def new_output_name(s, nametail, infol):
     s._outcounter += 1
     name = '%04d-%s' % (s._outcounter, nametail)
@@ -142,14 +146,15 @@ class SourceShipmentPreparer():
     fh.close()
 
   def src_indir(s, d, infol):
-    d = s.src_prenormaliser(d, infol)
+    d = s.path_prenormaliser(d, infol)
     if not s.src_filter(d): return
 
     d = s.src_parentfinder(d, infol)
-    s.dir(d, infol)
+    if d is None: return
+    s.src_dir(d, infol)
 
   def report_from_packages_debian(s, files):
-    dpkg_S_in = tempfile.TemporaryFile()
+    dpkg_S_in = tempfile.TemporaryFile(mode='w+')
     for (file, infols) in files.items():
       assert('\n' not in file)
       dpkg_S_in.write(file)
@@ -162,18 +167,19 @@ class SourceShipmentPreparer():
                               stdout=subprocess.PIPE,
                               stderr=sys.stderr,
                               close_fds=False)
-    dpkg_show_in = tempfile.TemporaryFile()
+    dpkg_show_in = tempfile.TemporaryFile(mode='w+')
     pkginfos = { }
-    for l in dpkgs.stdout:
+    for l in dpkg_S.stdout:
+      l = l.strip(b'\n').decode('utf-8')
       (pkgs, fname) = l.split(': ',1)
-      pkgs = pkgs.split(', ')
-      for p in pkgs:
-        pkginfos.setdefault(p,{'files':[]})['files'].append([fname, infol])
-        print(p, file=dpkg_show_in)
+      pks = pkgs.split(', ')
+      for pk in pks:
+        pkginfos.setdefault(pk,{'files':[]})['files'].append(fname)
+        print(pk, file=dpkg_show_in)
     assert(dpkg_S.wait() == 0)
     dpkg_show_in.seek(0)
     cmdl = ['xargs','-r','dpkg-query',
- r'-f${Package}\t${Architecture}\t${Version}\t${Source}\t${Source-Version}\n'
+            r'-f${binary:Package}\t${Package}\t${Architecture}\t${Version}\t${source:Package}\t${source:Version}\n',
             '--show','--']
     dpkg_show = subprocess.Popen(cmdl,
                                  cwd='/',
@@ -182,43 +188,46 @@ class SourceShipmentPreparer():
                                  stderr=sys.stderr,
                                  close_fds=False)
     for l in dpkg_show.stdout:
-      (p,a,v) = l.split('\t')
-      pkginfos[p]['arch'] = a
-      pkginfos[p]['version'] = v
-      pkginfos[p]['source'] = v
-      pkginfos[p]['sourceversion'] = v
+      l = l.strip(b'\n').decode('utf-8')
+      (pk,p,a,v,sp,sv) = l.split('\t')
+      pkginfos[pk]['binary'] = p
+      pkginfos[pk]['arch'] = a
+      pkginfos[pk]['version'] = v
+      pkginfos[pk]['source'] = sp
+      pkginfos[pk]['sourceversion'] = sv
     assert(dpkg_show.wait() == 0)
-    pl = pkginfos.keys()
-    pl.sort()
-    for p in pl:
-      pi = pkginfos[p]
-      debfname = '%s_%s_%s.deb' % (p, pi['version'], pi['arch'])
+    for pk in sorted(pkginfos.keys()):
+      pi = pkginfos[pk]
+      debfname = '%s_%s_%s.deb' % (pi['binary'], pi['version'], pi['arch'])
       dscfname = '%s_%s.dsc' % (pi['source'], pi['sourceversion'])
-      s._manifest.append({ 'file_print': dscfname, 'info': debfname })
-      for (fname, infol) in pi['files']:
-        s._manifest.append({ 'file_print': fname, 'info': ' '.join(infol) })
+      s.manifest_append_absentfile(dscfname, [debfname])
+      for fname in pi['files']:
+        infol = files[fname]
+        if s.show_pathnames: infol = infol + [fname]
+        s.manifest_append_absentfile(' \t' + debfname, infol)
 
   def thing_ought_packaged(s, fname):
     return s.thing_matches_globs(fname, s.src_package_globs)
 
-  def src_file_packaged(s, fname);
-    s._package_files.setdefault(fname,[]).append(infol)
+  def src_file_packaged(s, fname, infol):
+    s._package_files.setdefault(fname,[]).extend(infol)
 
   def src_file(s, fname, infol):
     def fngens():
-      yield fname
-      yield s.path_prenormaliser(fname)
-      yield os.path.realpath(fname)
-
-    for fn in fngens():
-      if s.thing_ought_packaged(fngen):
-        s.src_file_packaged(fname, infol)
+      yield (infol, fname)
+      infol_copy = infol.copy()
+      yield (infol_copy, s.path_prenormaliser(fname, infol_copy))
+      yield (infol, os.path.realpath(fname))
+
+    for (tinfol, tfname) in fngens():
+      if s.thing_ought_packaged(tfname):
+        s.src_file_packaged(tfname, tinfol)
         return
 
     s.src_indir(fname, infol)
 
   def src_argv0(s, program, infol):
-    s.src_file(s, program, infol)
+    s.src_file(program, infol)
 
   def src_syspath(s, fname, infol):
     s.src_indir(fname, infol)
@@ -231,7 +240,7 @@ class SourceShipmentPreparer():
     if s.thing_ought_packaged(fname):
       s.src_file_packaged(fname, infol)
     else:
-      s.src_indir(s, fname)
+      s.src_indir(fname, infol)
 
   def srcs_allitems(s, dirs=sys.path):
     s.src_argv0(sys.argv[0], ['argv[0]'])
@@ -239,7 +248,7 @@ class SourceShipmentPreparer():
       s.src_syspath(d, ['sys.path'])
     for m in sys.modules.values():
       s.src_module(m, ['sys.modules'])
-    s.report_from_packages(s, s._package_files)
+    s.report_from_packages(s._package_files)
 
   def mk_portmanteau(s):
     cmdl = s.rune_shell + [ s.rune_portmanteau, 'x',
@@ -248,7 +257,7 @@ class SourceShipmentPreparer():
     for me in s._manifest:
       try: fname = me['file']
       except KeyError: fname = me.get('file_print','')
-      else: cmdl.append(name)
+      else: cmdl.append(fname)
       print('%s\t%s' % (fname, me['info']), file=mfh)
     mfh.close()
     subprocess.run(cmdl,