Fix previous.
[cdist.git] / cdist
diff --git a/cdist b/cdist
index d1abfd8785d6b1974dd74afa6d1e9a01b01653c8..c583990bb3e5cbdc5f2f2ae76139a22ab04cf95c 100755 (executable)
--- a/cdist
+++ b/cdist
@@ -30,6 +30,12 @@ import inspect
 
 TEMPORARY_DIRECTORY = '/tmp'
 
+class Globals:
+    quiet = False
+    command = None
+
+globals = Globals()
+
 class Error(Exception):
     def __init__(self, value):
         self.value = value
@@ -74,7 +80,7 @@ class Config:
 
         try:
             f = open('%s/.config/cdist' % os.path.expanduser('~'), 'r')
-            while 1:
+            while True:
                 l = f.readline()
                 if l == '':
                     break
@@ -103,16 +109,25 @@ config = Config()
 # 
 
 def log(m):
-    if not args.quiet:
+    if not globals.quiet:
         print '\x1b[33m* %s\x1b[0m' % m
 
+def scp_escape(n):
+    s = n.split(':')
+    assert(len(s) == 1 or len(s) == 2)
+    if len(s) == 2:
+        s[1] = '"\'%s\'"' % s[1]
+        return '%s:%s' % (s[0], s[1])
+    else:
+        return n
+
 def copytree(a, b):
     log('copy %s -> %s' % (a, b))
-    shutil.copytree(a, b)
+    os.system('scp -r %s %s' % (scp_escape(a), scp_escape(b)))
 
 def copyfile(a, b):
     log('copy %s -> %s' % (a, b))
-    shutil.copyfile(a, b)
+    os.system('scp %s %s' % (scp_escape(a), scp_escape(b)))
 
 def rmdir(a):
     log('remove %s' % a)
@@ -136,7 +151,7 @@ def command_and_read(c):
 
 def read_wscript_variable(directory, variable):
     f = open('%s/wscript' % directory, 'r')
-    while 1:
+    while True:
         l = f.readline()
         if l == '':
             break
@@ -452,7 +467,7 @@ def target_factory(s, debug, work):
     elif s.startswith('osx-'):
         target = OSXSingleTarget(int(s.split('-')[1]), work)
     elif s == 'osx':
-        if args.command == 'build':
+        if globals.command == 'build':
             target = OSXSingleTarget(64, work)
         else:
             target = OSXUniversalTarget(work)
@@ -482,7 +497,7 @@ class Project(object):
     def checkout(self, target):
         flags = ''
         redirect = ''
-        if args.quiet:
+        if globals.quiet:
             flags = '-q'
             redirect = '>/dev/null'
         command('git clone %s %s/%s.git %s/src/%s' % (flags, config.get('git_prefix'), self.name, target.directory, self.name))
@@ -509,7 +524,7 @@ class Project(object):
 def set_version_in_wscript(version):
     f = open('wscript', 'rw')
     o = open('wscript.tmp', 'w')
-    while 1:
+    while True:
         l = f.readline()
         if l == '':
             break
@@ -559,23 +574,64 @@ def devel_to_git(project, filename):
 
 def main():
 
-    args.output = os.path.abspath(args.output)
+    commands = {
+        "build": "build project",
+        "package": "package and build project",
+        "release": "release a project using its next version number (changing wscript and tagging)",
+        "pot": "build the project's .pot files",
+        "changelog": "generate a simple HTML changelog",
+        "manual": "build the project's manual",
+        "doxygen": "build the project's Doxygen documentation",
+        "latest": "print out the latest version",
+        "test": "run the project's unit tests",
+        "shell": "build the project then start a shell in its chroot",
+        "revision": "print the head git revision number"
+    }
+
+    one_of = "Command is one of:\n"
+    summary = ""
+    for k, v in commands.iteritems():
+        one_of += "\t%s\t%s\n" % (k, v)
+        summary += k + " "
+
+    parser = argparse.ArgumentParser()
+    parser.add_argument('command', help=summary)
+    parser.add_argument('-p', '--project', help='project name')
+    parser.add_argument('-d', '--directory', help='directory within project repo', default='.')
+    parser.add_argument('--minor', help='minor version number bump', action='store_true')
+    parser.add_argument('--micro', help='micro version number bump', action='store_true')
+    parser.add_argument('--major', help='major version to return with latest', type=int)
+    parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
+    parser.add_argument('-o', '--output', help='output directory', default='.')
+    parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
+    parser.add_argument('-t', '--target', help='target')
+    parser.add_argument('-k', '--keep', help='keep working tree', action='store_true')
+    parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
+    parser.add_argument('-w', '--work', help='override default work directory')
+    args = parser.parse_args()
+
+    if args.output.find(':') == -1:
+        # This isn't of the form host:path so make it absolute
+        args.output = os.path.abspath(args.output) + '/'
+
+    # Now, args.output is 'host:' or 'path/'
+
     if args.work is not None:
         args.work = os.path.abspath(args.work)
 
     if args.project is None and args.command != 'shell':
         raise Error('you must specify -p or --project')
+        
+    globals.quiet = args.quiet
+    globals.command = args.command
 
     project = Project(args.project, args.directory, args.checkout)
 
-    commands = ['build', 'package', 'release', 'pot', 'changelog', 'manual', 'doxygen', 'latest', 'test', 'shell', 'revision']
-    if args.command not in commands:
-        e = 'command must be one of: '
-        for c in commands:
-            e += '%s ' % c
-        raise Error(e)
+    if not globals.command in commands:
+        e = 'command must be one of:\n' + one_of
+        raise Error('command must be one of:\n%s' % one_of)
 
-    if args.command == 'build':
+    if globals.command == 'build':
         if args.target is None:
             raise Error('you must specify -t or --target')
 
@@ -586,7 +642,7 @@ def main():
         if not args.keep:
             target.cleanup()
 
-    elif args.command == 'package':
+    elif globals.command == 'package':
         if args.target is None:
             raise Error('you must specify -t or --target')
 
@@ -597,7 +653,7 @@ def main():
             packages = [packages]
 
         if target.platform == 'linux':
-            out = '%s/%s-%s-%d' % (args.output, target.distro, target.version, target.bits)
+            out = '%s%s-%s-%d' % (args.output, target.distro, target.version, target.bits)
             try:
                 os.makedirs(out)
             except:
@@ -606,12 +662,12 @@ def main():
                 copyfile(p, '%s/%s' % (out, os.path.basename(devel_to_git(project, p))))
         else:
             for p in packages:
-                copyfile(p, '%s/%s' % (args.output, os.path.basename(devel_to_git(project, p))))
+                copyfile(p, '%s%s' % (args.output, os.path.basename(devel_to_git(project, p))))
 
         if not args.keep:
             target.cleanup()
 
-    elif args.command == 'release':
+    elif globals.command == 'release':
         if args.minor is False and args.micro is False:
             raise Error('you must specify --minor or --micro')
 
@@ -640,28 +696,28 @@ def main():
 
         target.cleanup()
 
-    elif args.command == 'pot':
+    elif globals.command == 'pot':
         target = SourceTarget()
         project.checkout(target)
 
         pots = project.cscript['make_pot'](target)
         for p in pots:
-            copyfile(p, '%s/%s' % (args.output, os.path.basename(p)))
+            copyfile(p, '%s%s' % (args.output, os.path.basename(p)))
 
         target.cleanup()
 
-    elif args.command == 'changelog':
+    elif globals.command == 'changelog':
         target = SourceTarget()
         project.checkout(target)
 
         text = open('ChangeLog', 'r')
-        html = open('%s/changelog.html' % args.output, 'w')
+        html = tempfile.NamedTemporaryFile()
         versions = 8
 
         last = None
         changes = []
 
-        while 1:
+        while True:
             l = text.readline()
             if l == '':
                 break
@@ -690,22 +746,24 @@ def main():
                         else:
                             changes[-1] += " " + c
 
+        copyfile(html.file, '%schangelog.html' % args.output)
+        html.close()
         target.cleanup()
 
-    elif args.command == 'manual':
+    elif globals.command == 'manual':
         target = SourceTarget()
         project.checkout(target)
 
         outs = project.cscript['make_manual'](target)
         for o in outs:
             if os.path.isfile(o):
-                copyfile(o, '%s/%s' % (args.output, os.path.basename(o)))
+                copyfile(o, '%s%s' % (args.output, os.path.basename(o)))
             else:
-                copytree(o, '%s/%s' % (args.output, os.path.basename(o)))
+                copytree(o, '%s%s' % (args.output, os.path.basename(o)))
 
         target.cleanup()
 
-    elif args.command == 'doxygen':
+    elif globals.command == 'doxygen':
         target = SourceTarget()
         project.checkout(target)
 
@@ -714,11 +772,11 @@ def main():
             dirs = [dirs]
 
         for d in dirs:
-            copytree(d, '%s/%s' % (args.output, 'doc'))
+            copytree(d, '%s%s' % (args.output, 'doc'))
 
         target.cleanup()
 
-    elif args.command == 'latest':
+    elif globals.command == 'latest':
         target = SourceTarget()
         project.checkout(target)
 
@@ -741,7 +799,7 @@ def main():
         print latest
         target.cleanup()
 
-    elif args.command == 'test':
+    elif globals.command == 'test':
         if args.target is None:
             raise Error('you must specify -t or --target')
 
@@ -757,14 +815,14 @@ def main():
         if target is not None:
             target.cleanup()
 
-    elif args.command == 'shell':
+    elif globals.command == 'shell':
         if args.target is None:
             raise Error('you must specify -t or --target')
 
         target = target_factory(args.target, args.debug, args.work)
         target.command('bash')
 
-    elif args.command == 'revision':
+    elif globals.command == 'revision':
 
         target = SourceTarget()
         project.checkout(target)
@@ -772,28 +830,7 @@ def main():
         target.cleanup()
 
     else:
-        raise Error('invalid command %s' % args.command)
-
-
-#
-# Command-line parser
-# 
-
-parser = argparse.ArgumentParser()
-parser.add_argument('command')
-parser.add_argument('-p', '--project', help='project name')
-parser.add_argument('-d', '--directory', help='directory within project repo', default='.')
-parser.add_argument('--minor', help='minor version number bump', action='store_true')
-parser.add_argument('--micro', help='micro version number bump', action='store_true')
-parser.add_argument('--major', help='major version to return with latest', type=int)
-parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
-parser.add_argument('-o', '--output', help='output directory', default='.')
-parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
-parser.add_argument('-t', '--target', help='target')
-parser.add_argument('-k', '--keep', help='keep working tree', action='store_true')
-parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
-parser.add_argument('-w', '--work', help='override default work directory')
-args = parser.parse_args()
+        raise Error('invalid command %s' % globals.command)
 
 try:
     main()