[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[snapshot/master] Add add-dump-to-git



---
 misc/dump-tools/add-dump-to-git |  109 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 109 insertions(+), 0 deletions(-)
 create mode 100755 misc/dump-tools/add-dump-to-git

diff --git a/misc/dump-tools/add-dump-to-git b/misc/dump-tools/add-dump-to-git
new file mode 100755
index 0000000..1475f02
--- /dev/null
+++ b/misc/dump-tools/add-dump-to-git
@@ -0,0 +1,109 @@
+#!/usr/bin/python
+
+# Add a single dump file to a git tree.
+
+
+# Copyright (c) 2010 Peter Palfrader
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+import sys
+import os
+import optparse
+import re
+import shutil
+import subprocess
+
+author = 'snapshot add-dump-to-git script <snapshot@debian.org>'
+
+parser = optparse.OptionParser()
+parser.set_usage("%prog [<options>] <dumpfile> [<dumpfile> ...]")
+parser.add_option("-b", "--backing", dest="backing_git", metavar="GITDIR",
+  default = 'backing-git',
+  help="Location of backing git working copy.")
+parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
+  help="Be verbose.")
+(options, args) = parser.parse_args()
+
+if len(args) == 0:
+    parser.print_help()
+    sys.exit(1)
+
+if not os.path.isdir(options.backing_git):
+    print >> sys.stderr, "Error: %s does not exist or is not a directory."%(backing)
+
+basedir = os.getcwd()
+for dumpfilename in args:
+    f = open(dumpfilename)
+    metadata = {}
+    commitmsgbody = []
+    for line in f:
+        line = line.rstrip()
+        if line == "Contents:": break
+        key, value = line.split(': ', 1)
+        metadata[key] = value
+
+        commitmsgbody.append(line)
+    f.close()
+    if not 'Dump-Format' in metadata or \
+        metadata['Dump-Format'] != "snapshot.debian.org 0.1":
+        print >> sys.stderr, "Warning: Unknown dump format in %s."%(dumpfilename)
+        continue
+    required_headers = ['Archive', 'Date']
+    all_present = True
+    for r in required_headers:
+        if not r in metadata:
+            print >> sys.stderr, "Warning: no %s header in %s."%(r, dumpfilename)
+            all_present = False
+    if not all_present: continue
+    if re.search("[^a-z0-9-]", metadata['Archive']):
+        print >> sys.stderr, \
+            "Warning: invalid archive name '%s' in %s."%(metadata['Archive'], dumpfilename)
+        continue
+
+    target = os.path.join(options.backing_git, metadata['Archive'])
+    shutil.copyfile(dumpfilename, target)
+
+    commitmsg = "Add dump %s (%s)\n\n"%(os.path.basename(dumpfilename), metadata['Archive'])
+    if options.verbose: print commitmsg.rstrip(); sys.stdout.flush()
+    commitmsg += '\n'.join(commitmsgbody)
+
+    os.chdir(options.backing_git)
+    try:
+        if options.verbose: print "# git add '%s'"%(metadata['Archive']); sys.stdout.flush()
+        subprocess.check_call(['git', 'add', metadata['Archive']])
+        if options.verbose: print "# committing"; sys.stdout.flush()
+        subprocess.check_call(['git', 'commit', '--author', author,
+            '-m', commitmsg, '--date', metadata['Date']])
+    except:
+        print >> sys.stderr, "Git operations failed during processing of %s."%(dumpfilename)
+        sys.exit(1)
+    finally:
+        os.chdir(basedir)
+
+
+os.chdir(options.backing_git)
+if options.verbose: print "# git gc"; sys.stdout.flush()
+subprocess.check_call(['git', 'gc'])
+
+
+
+# vim:set et:
+# vim:set ts=4:
+# vim:set shiftwidth=4:
-- 
1.7.2.3



Reply to: