ownsource: try to honour .gitignore
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 4 Apr 2017 15:50:40 +0000 (16:50 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 4 Apr 2017 15:50:40 +0000 (16:50 +0100)
hippotatlib/ownsource.py

index 218661a..4369cb9 100644 (file)
@@ -16,10 +16,11 @@ class SourceShipmentPreparer():
     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 +58,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 +115,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