chiark / gitweb /
wip ownsource, now getting there
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 3 Apr 2017 15:07:49 +0000 (16:07 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 3 Apr 2017 15:07:49 +0000 (16:07 +0100)
.gitignore
hippotatlib/ownsource.py
srcbombtest.py [moved from t.py with 100% similarity]

index 1758a2f..297c4f4 100644 (file)
@@ -1,2 +1,3 @@
 data.dump.dbg
 [tuv]
+tmp
index be7bdcd..b434f32 100644 (file)
@@ -1,7 +1,10 @@
 # Automatic source code provision (AGPL compliance)
 
+import os
 import sys
 import fnmatch
+import stat
+import subprocess
 
 class SourceShipmentPreparer():
   def __init__(s, destdir):
@@ -12,9 +15,12 @@ class SourceShipmentPreparer():
     s.src_filter_globs = ['/usr/local/*', '!/usr*', '!/etc/*']
     s.src_likeparent = s.src_likeparent_git
     s.cwd = os.getcwd()
-    s.excludes = ['*~', '*.bak', '*.tmp', '#*#']
+    s.find_rune_base = "find -type f -perm -004 \! -path '*/tmp/*'"
+    s.excludes = ['*~', '*.bak', '*.tmp', '#*#',
+                  '[0-9][0-9][0-9][0-9]-src.cpio']
     s.rune_shell = ['/bin/bash', '-ec']
-    s.rune_cpio = r''''
+    s.show_pathnames = True
+    s.rune_cpio = r'''
             set -o pipefail
            (
             %s
@@ -32,7 +38,7 @@ class SourceShipmentPreparer():
     s.manifest_name='0000-MANIFEST.txt'
     # private
     s._destdir = destdir
-    s._outcounter = 1
+    s._outcounter = 0
     s._manifest = []
 
   def src_filter_glob(s, src): # default s.src_filter
@@ -45,7 +51,7 @@ class SourceShipmentPreparer():
 
   def src_likeparent_git(s, src):
     try:
-      stat(os.path.join(d, '.git/.'))
+      os.stat(os.path.join(src, '.git/.'))
     except FileNotFoundError:
       return False
     else:
@@ -60,11 +66,12 @@ class SourceShipmentPreparer():
         search = os.path.realpath(search)
 
       def ascend():
+        nonlocal search
         xinfo.append(os.path.basename(search))
         search = os.path.dirname(search)
 
       try:
-        stab = lstat(search)
+        stab = os.lstat(search)
       except FileNotFoundError:
         return
       if stat.S_ISREG(stab.st_mode):
@@ -73,45 +80,49 @@ class SourceShipmentPreparer():
       while not os.path.ismount(search):
         if s.src_likeparent(search):
           xinfo.reverse()
-          infol.append(os.path.join(*xinfo))
+          if len(xinfo): infol.append('want=' + os.path.join(*xinfo))
           return search
 
         ascend()
 
     # no .git found anywhere
-    return d
+    return src
 
-  def src_prenormaliser(s, d): # callers may monkey-patch away
+  def src_prenormaliser(s, d, infol): # callers may monkey-patch away
     return os.path.join(s.cwd, os.path.abspath(d))
 
   def src_find_rune(s, d):
-    script = 'find -type f -perm +004'
-    for excl in s.excludes:
+    script = s.find_rune_base
+    for excl in s.excludes + [s.output_name, s.manifest_name]:
       assert("'" not in excl)
       script += r" \! -name '%s'" % excl
     script += ' -print0'
+    return script
 
   def new_output_name(s, nametail, infol):
-    name = '%04d-%s' % (s._outcounter++, nametail)
-    s._manifest.append((name, infol.join(' '))
+    s._outcounter += 1
+    name = '%04d-%s' % (s._outcounter, nametail)
+    s._manifest.append((name, ' '.join(infol)))
     return name
 
+  def new_output_fh(s, nametail, infol):
+    name = s.new_output_name(nametail, infol)
+    return s.open_output_fh(name, 'wb')
+
   def open_output_fh(s, name, mode):
     return open(os.path.join(s._destdir, name), mode)
 
-  def new_output_fh(s, nametail, infol):
-    name = new_output_name(s, nametail, infol)
-    return open_output_fh(name, 'wb')
-
-  def mk_from_dir(s, d):
-    find_rune = s.src_find_rune(s, d)
+  def mk_from_dir(s, d, infol):
+    if s.show_pathnames: infol.append(d)
+    find_rune = s.src_find_rune(d)
     total_rune = s.rune_cpio % find_rune
-    fh = new_output_fh('src.cpio')
+    fh = s.new_output_fh('src.cpio', infol)
     subprocess.run(s.rune_shell + [total_rune],
-                   cwd=s._destdir,
+                   cwd=d,
                    stdin=subprocess.DEVNULL,
                    stdout=fh,
-                   restore_signals=True)
+                   restore_signals=True,
+                   check=True)
     fh.close()
 
   def mk_from_src(s, d, infol):
@@ -125,20 +136,21 @@ class SourceShipmentPreparer():
     for d in sys.path:
       s.mk_from_src(d, ['sys.path'])
 
-  def mk_portmanteau(s):]
+  def mk_portmanteau(s):
     cmdl = s.rune_shell + [ s.rune_portmanteau, 'x',
                             s.output_name, s.manifest_name ]
-    mfh = open_output_fh(s.manifest_name,'w')
+    mfh = s.open_output_fh(s.manifest_name,'w')
     for (name, info) in s._manifest:
       cmdl.append(name)
-      print('%s\t%s\n' % (name,info), file=mfh)
+      print('%s\t%s' % (name,info), file=mfh)
     mfh.close()
     subprocess.run(s.rune_shell + cmdl,
-                   cmd=s._destdir,
+                   cwd=s._destdir,
                    stdin=subprocess.DEVNULL,
                    stdout=sys.stderr,
-                   restore_signals=True)
+                   restore_signals=True,
+                   check=True)
 
   def generate(s):
-    s.mk_from_srcdirs()
+    s.mk_from_srcs()
     s.mk_portmanteau()
similarity index 100%
rename from t.py
rename to srcbombtest.py