Add windows versioning support.
[cdist.git] / cdist
diff --git a/cdist b/cdist
index 3bf9460a9690dcda316e339e2f8cecc6e568004f..cab4ddf25c2a29d46c8ca981bd6438f75854f283 100755 (executable)
--- a/cdist
+++ b/cdist
@@ -16,6 +16,7 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
+from __future__ import print_function
 import os
 import sys
 import shutil
@@ -136,7 +137,7 @@ config = Config()
 
 def log(m):
     if not globals.quiet:
-        print '\x1b[33m* %s\x1b[0m' % m
+        print('\x1b[33m* %s\x1b[0m' % m)
 
 def scp_escape(n):
     s = n.split(':')
@@ -206,10 +207,10 @@ def set_version_in_wscript(version):
 
         s = l.split()
         if len(s) == 3 and s[0] == "VERSION":
-            print "Writing %s" % version
-            print >>o,"VERSION = '%s'" % version
+            print("Writing %s" % version)
+            print("VERSION = '%s'" % version, file=o)
         else:
-            print >>o,l,
+            print(l, file=o, end="")
     f.close()
     o.close()
 
@@ -331,10 +332,11 @@ class Target(object):
         tree.build(tree)
         return tree.call('package', tree.version), tree.git_commit
 
-    def test(self, tree):
+    def test(self, tree, test):
+        """test is the test case to run, or None"""
         tree.build_dependencies()
         tree.build()
-        return tree.call('test')
+        return tree.call('test', test)
 
     def set(self, a, b):
         self.variables[a] = b
@@ -357,7 +359,7 @@ class Target(object):
 
     def variables_string(self, escaped_quotes=False):
         e = ''
-        for k, v in self.variables.iteritems():
+        for k, v in self.variables.items():
             if escaped_quotes:
                 v = v.replace('"', '\\"')
             e += '%s=%s ' % (k, v)
@@ -372,8 +374,9 @@ class Target(object):
 #
 
 class WindowsTarget(Target):
-    def __init__(self, bits, directory=None):
+    def __init__(self, version, bits, directory=None):
         super(WindowsTarget, self).__init__('windows', directory)
+        self.version = version
         self.bits = bits
 
         self.windows_prefix = '%s/%d' % (config.get('windows_environment_prefix'), self.bits)
@@ -421,7 +424,7 @@ class LinuxTarget(Target):
         self.set('CXXFLAGS', '-I%s/include' % self.directory)
         self.set('CPPFLAGS', '')
         self.set('LINKFLAGS', '-L%s/lib' % self.directory)
-        self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:/usr/local/lib/pkgconfig' % self.directory)
+        self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib64/pkgconfig:/usr/local/lib/pkgconfig' % (self.directory, self.directory))
         self.set('PATH', '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin')
 
 class ChrootTarget(LinuxTarget):
@@ -472,7 +475,7 @@ class OSXSingleTarget(OSXTarget):
         else:
             arch = 'x86_64'
 
-        flags = '-isysroot %s/MacOSX%s.sdk -arch %s' % (self.sdk_prefix, self.osx_sdk, arch)
+        flags = '-isysroot %s/MacOSX%s.sdk -arch %s' % (self.sdk_prefix, self.sdk, arch)
         enviro = '%s/%d' % (config.get('osx_environment_prefix'), bits)
 
         # Environment variables
@@ -536,25 +539,39 @@ class SourceTarget(Target):
 def target_factory(s, debug, work):
     target = None
     if s.startswith('windows-'):
-        target = WindowsTarget(int(s.split('-')[1]), work)
+        x = s.split('-')
+        if len(x) == 2:
+            target = WindowsTarget(None, int(x[1]), work)
+        elif len(x) == 3:
+            target = WindowsTarget(x[1], int(x[2]), work)
+        else:
+            raise Error("Bad Windows target name `%s'")
     elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-'):
         p = s.split('-')
         if len(p) != 3:
             raise Error("Bad Linux target name `%s'; must be something like ubuntu-12.04-32 (i.e. distro-version-bits)" % s)
         target = ChrootTarget(p[0], p[1], int(p[2]), work)
+    elif s.startswith('arch-'):
+        p = s.split('-')
+        if len(p) != 2:
+            raise Error("Bad Arch target name `%s'; must be arch-32 or arch-64")
+        target = ChrootTarget(p[0], None, p[1], work)
     elif s == 'raspbian':
         target = ChrootTarget(s, None, None, work)
     elif s == 'host':
+        if command_and_read('uname -m').read().strip() == 'x86_64':
+            bits = 64
+        else:
+            bits = 32
         try:
             f = open('/etc/fedora-release', 'r')
             l = f.readline().strip().split()
-            if command_and_read('uname -m').read().strip() == 'x86_64':
-                bits = 64
-            else:
-                bits = 32
             target = HostTarget("fedora", l[2], bits, work)
         except Exception as e:
-            raise Error("could not identify distribution for `host' target (%s)" % e)
+            if os.path.exists('/etc/arch-release'):
+                target = HostTarget("arch", None, bits, work)
+            else:
+                raise Error("could not identify distribution for `host' target (%s)" % e)
     elif s.startswith('osx-'):
         target = OSXSingleTarget(int(s.split('-')[1]), work)
     elif s == 'osx':
@@ -618,7 +635,7 @@ class Tree(object):
         proj = '%s/src/%s' % (target.directory, self.name)
 
         self.cscript = {}
-        execfile('%s/cscript' % proj, self.cscript)
+        exec(open('%s/cscript' % proj).read(), self.cscript)
 
         if os.path.exists('%s/wscript' % proj):
             v = read_wscript_variable(proj, "VERSION");
@@ -644,7 +661,7 @@ class Tree(object):
                 if 'option_defaults' in dep.cscript:
                     options = dep.cscript['option_defaults']()
                     if len(d) > 2:
-                        for k, v in d[2].iteritems():
+                        for k, v in d[2].items():
                             options[k] = v
 
                 dep.build(options)
@@ -686,7 +703,7 @@ def main():
 
     one_of = "Command is one of:\n"
     summary = ""
-    for k, v in commands.iteritems():
+    for k, v in commands.items():
         one_of += "\t%s\t%s\n" % (k, v)
         summary += k + " "
 
@@ -704,6 +721,7 @@ def main():
     parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
     parser.add_argument('-w', '--work', help='override default work directory')
     parser.add_argument('-g', '--git-prefix', help='override configured git prefix')
+    parser.add_argument('--test', help='name of test to run (with `test''), defaults to all')
     args = parser.parse_args()
 
     # Override configured stuff
@@ -834,11 +852,11 @@ def main():
                     v = Version(s[2])
                     if v.micro == 0:
                         if last is not None and len(changes) > 0:
-                            print >>html,"<h2>Changes between version %s and %s</h2>" % (s[2], last)
-                            print >>html,"<ul>"
+                            print("<h2>Changes between version %s and %s</h2>" % (s[2], last), file=html)
+                            print("<ul>", file=html)
                             for c in changes:
-                                print >>html,"<li>%s" % c
-                            print >>html,"</ul>"
+                                print("<li>%s" % c, file=html)
+                            print("</ul>", file=html)
                         last = s[2]
                         changes = []
                         versions -= 1
@@ -903,7 +921,7 @@ def main():
                             if args.major is None or v.major == args.major:
                                 latest = v
 
-        print latest
+        print(latest)
         target.cleanup()
 
     elif globals.command == 'test':
@@ -915,7 +933,7 @@ def main():
             target = target_factory(args.target, args.debug, args.work)
             tree = globals.trees.get(args.project, args.checkout, target)
             with TreeDirectory(tree):
-                target.test(tree)
+                target.test(tree, args.test)
         except Error as e:
             if target is not None:
                 target.cleanup()
@@ -936,7 +954,7 @@ def main():
         target = SourceTarget()
         tree = globals.trees.get(args.project, args.checkout, target)
         with TreeDirectory(tree):
-            print command_and_read('git rev-parse HEAD').readline().strip()[:7]
+            print(command_and_read('git rev-parse HEAD').readline().strip()[:7])
         target.cleanup()
 
     elif globals.command == 'checkout':
@@ -956,5 +974,5 @@ def main():
 try:
     main()
 except Error as e:
-    print >>sys.stderr,'cdist: %s' % str(e)
+    print('cdist: %s' % str(e), file=sys.stderr)
     sys.exit(1)