hippotatd: add a startup message
[hippotat] / hippotatlib / ownsource.py
index 218661a..4269055 100644 (file)
@@ -1,4 +1,27 @@
-# Automatic source code provision (AGPL compliance)
+# -*- python -*-
+#
+# Hippotat - Asinine IP Over HTTP program
+# hippotatlib/ownsource.py - Automatic source code provision (AGPL compliance)
+#
+# Copyright 2017 Ian Jackson
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version, with the "CAF Login
+# Exception" as published by Ian Jackson (version 2, or at your option
+# any later version) as an Additional Permission.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License and the CAF Login Exception along with this program, in the
+# file AGPLv3+CAFv2.  If not, email Ian Jackson
+# <ijackson@chiark.greenend.org.uk>.
+
 
 import os
 import sys
@@ -11,15 +34,18 @@ class SourceShipmentPreparer():
   def __init__(s, destdir):
     # caller may modify, and should read after calling generate()
     s.output_name = 'srcbomb.tar.gz'
+    # s.output_path   alternatively caller may read this
     # defaults, caller can modify after creation
+    s.logger = lambda m: print('SourceShipmentPreparer',m)
     s.src_filter = s.src_filter_glob
     s.src_package_globs = ['!/usr/local/*', '/usr*']
     s.src_filter_globs = ['!/etc/*']
     s.src_likeparent = s.src_likeparent_git
+    s.src_direxcludes = s.src_direxcludes_git
     s.report_from_packages = s.report_from_packages_debian
     s.cwd = os.getcwd()
     s.find_rune_base = "find -type f -perm -004 \! -path '*/tmp/*'"
-    s.excludes = ['*~', '*.bak', '*.tmp', '#*#',
+    s.ignores = ['*~', '*.bak', '*.tmp', '#*#', '__pycache__',
                   '[0-9][0-9][0-9][0-9]-src.cpio']
     s.rune_shell = ['/bin/bash', '-ec']
     s.show_pathnames = True
@@ -57,6 +83,19 @@ class SourceShipmentPreparer():
   def src_filter_glob(s, src): # default s.src_filter
     return s.thing_matches_globs(src, s.src_filter_globs)
 
+  def src_direxcludes_git(s, d):
+    try:
+      excl = open(os.path.join(d, '.gitignore'))
+    except FileNotFoundError:
+      return []
+    r = []
+    for l in excl:
+      l.strip
+      if l.startswith('#'): next
+      if not len(l): next
+      r += l
+    return r
+
   def src_likeparent_git(s, src):
     try:
       os.stat(os.path.join(src, '.git/.'))
@@ -101,9 +140,12 @@ class SourceShipmentPreparer():
 
   def srcdir_find_rune(s, d):
     script = s.find_rune_base
-    for excl in s.excludes + [s.output_name, s.manifest_name]:
+    ignores = s.ignores + [s.output_name, s.manifest_name]
+    ignores += s.src_direxcludes(d)
+    for excl in ignores:
       assert("'" not in excl)
-      script += r" \! -name '%s'" % excl
+      script += r" \! -name '%s'"     % excl
+      script += r" \! -path '*/%s/*'" % excl
     script += ' -print0'
     return script
 
@@ -137,6 +179,9 @@ class SourceShipmentPreparer():
     s._dirmap[d] = name
     fh = s.open_output_fh(name, 'wb')
 
+    s.logger('packing up into %s: %s (because %s)' %
+             (name, d, ' '.join(infol)))
+
     subprocess.run(s.rune_shell + [total_rune],
                    cwd=d,
                    stdin=subprocess.DEVNULL,
@@ -201,6 +246,8 @@ class SourceShipmentPreparer():
       debfname = '%s_%s_%s.deb' % (pi['binary'], pi['version'], pi['arch'])
       dscfname = '%s_%s.dsc' % (pi['source'], pi['sourceversion'])
       s.manifest_append_absentfile(dscfname, [debfname])
+      s.logger('mentioning %s and %s because %s' %
+               (dscfname, debfname, pi['files'][0]))
       for fname in pi['files']:
         infol = files[fname]
         if s.show_pathnames: infol = infol + ['loaded='+fname]
@@ -244,14 +291,17 @@ class SourceShipmentPreparer():
       s.src_indir(fname, infol)
 
   def srcs_allitems(s, dirs=sys.path):
+    s.logger('allitems')
     s.src_argv0(sys.argv[0], ['argv[0]'])
     for d in sys.path:
       s.src_syspath(d, ['sys.path'])
     for m in sys.modules.values():
       s.src_module(m, ['sys.modules'])
     s.report_from_packages(s._package_files)
+    s.logger('allitems done')
 
   def mk_portmanteau(s):
+    s.logger('making portmanteau')
     cmdl = s.rune_shell + [ s.rune_portmanteau, 'x',
                             s.output_name, s.manifest_name ]
     mfh = s.open_output_fh(s.manifest_name,'w')
@@ -267,6 +317,8 @@ class SourceShipmentPreparer():
                    stdout=sys.stderr,
                    restore_signals=True,
                    check=True)
+    s.output_path = os.path.join(s._destdir, s.output_name)
+    s.logger('portmanteau ready in %s' % s.output_path)
 
   def generate(s):
     s.srcs_allitems()