changeset 7925:c7d7d9936809

use zipfile module instead of an external call to jar for creating jar files from projects
author Doug Simon <doug.simon@oracle.com>
date Tue, 05 Mar 2013 15:20:35 +0100
parents b6a87711eca0
children 9e3c0d8bca65
files mx/commands.py mxtool/mx.py
diffstat 2 files changed, 34 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/mx/commands.py	Tue Mar 05 13:11:36 2013 +0100
+++ b/mx/commands.py	Tue Mar 05 15:20:35 2013 +0100
@@ -1096,20 +1096,6 @@
         mx.abort('jacocoreport takes only one argument : an output directory')
     mx.run_java(['-jar', jacocoreport.get_path(True), '-in', 'jacoco.exec', '-g', join(_graal_home, 'graal'), out])
 
-def jar(args):
-    parser = ArgumentParser(prog='mx jar');
-    parser.add_argument('projects', nargs=REMAINDER, metavar='projects...')
-    args = parser.parse_args(args)
-    
-    if not args.projects:
-        mx.abort('Please specify at least one project to jar.')
-    
-    for pname in args.projects:
-        p = mx.project(pname, fatalIfMissing=True)
-        outputDir = p.output_dir()
-        targetJar = join(p.dir, p.name + '.jar')
-        mx.jar(targetJar, [outputDir])
-
 def site(args):
     """create a website containing javadoc and the project dependency graph"""
 
@@ -1136,7 +1122,6 @@
         'dacapo': [dacapo, '[[n] benchmark] [VM options|@DaCapo options]'],
         'scaladacapo': [scaladacapo, '[[n] benchmark] [VM options|@Scala DaCapo options]'],
         'specjvm2008': [specjvm2008, '[VM options|@specjvm2008 options]'],
-        'jar': [jar, '[-options]'],
         #'example': [example, '[-v] example names...'],
         'gate' : [gate, '[-options]'],
         'gv' : [gv, ''],
--- a/mxtool/mx.py	Tue Mar 05 13:11:36 2013 +0100
+++ b/mxtool/mx.py	Tue Mar 05 15:20:35 2013 +0100
@@ -1575,7 +1575,7 @@
     return 0
 
 def processorjars():
-    projects = set([])
+    projects = set()
     
     for p in sorted_deps():
         if _needsEclipseJarBuild(p):
@@ -1584,50 +1584,39 @@
     if len(projects) <= 0:
         return
     
-    build(['--projects', ",".join(map(lambda p: p.name, projects))])
-
-    for p in projects:
-        targetJar = join(p.dir, p.name + '.jar')
-        jar(targetJar, [p.output_dir()])
-            
-
-def jar(destFileName, dirs):
-    latestMod = _latestModification(dirs)
+    pnames = [p.name for p in projects]
+    build(['--projects', ",".join(pnames)])
+    jarprojects(pnames)
+
+def jarprojects(args):
+    """create jar files for the output of one or more projects"""
+    parser = ArgumentParser(prog='mx jar');
+    parser.add_argument('-d', '--dest', help='single jar file to create')
+    parser.add_argument('projects', nargs=REMAINDER, metavar='projects...')
+    args = parser.parse_args(args)
     
-    if exists(destFileName):
-        mod = os.path.getmtime(destFileName)
-        if int(round(latestMod*1000)) == int(round(mod*1000)):
-            # nothing todo
-            return
-        
-    if latestMod is None and exists(destFileName):
-        return
-
-    jarCmd = [java().jar, 'cf', destFileName]
+    if not args.projects:
+        args.projects = [p.name for p in projects()]
+
+    if args.dest is not None:
+        zf = zipfile.ZipFile(args.dest, 'w')
     
-    for directory in dirs:
-        jarCmd += ['-C', directory, '.']
-    
-    subprocess.check_call(jarCmd)
-    log('Written jar file {0}'.format(destFileName))
-    
-    atime = os.path.getatime(destFileName)
-    os.utime(destFileName, (atime, latestMod))
-
-def _latestModification(directories):
-    latestMod = None
-    for directory in directories:
-        if not os.path.exists (directory):
-            continue
-        for root, _, files in os.walk(directory):
-                for names in files:
-                    filepath = os.path.join(root, names)
-                    mod = os.path.getmtime(filepath)
-                    if latestMod is None:
-                        latestMod = mod
-                    elif mod > latestMod:
-                        latestMod = mod
-    return latestMod
+    for pname in args.projects:
+        p = project(pname, fatalIfMissing=True)
+        if args.dest is None:
+            jar = join(p.dir, p.name + '.jar')
+            zf = zipfile.ZipFile(jar, 'w')
+        outputDir = p.output_dir()
+        for root, _, files in os.walk(outputDir):
+            for f in files:
+                relpath = root[len(outputDir) + 1:]
+                arcname = join(relpath, f).replace(os.sep, '/')
+                zf.write(join(root, f), arcname)
+        if args.dest is None:
+            zf.close()
+            
+    if args.dest is not None:
+        zf.close()
 
 def canonicalizeprojects(args):
     """process all project files to canonicalize the dependencies
@@ -2213,7 +2202,7 @@
 
 def _genEclipseJarBuild(p):
     builders = []
-    builders.append(_genEclipseLaunch(p, 'Jar.launch', ''.join(['jar ', p.name]), refresh = False, async = False))
+    builders.append(_genEclipseLaunch(p, 'Jar.launch', ''.join(['jarprojects ', p.name]), refresh = False, async = False))
     builders.append(_genEclipseLaunch(p, 'Refresh.launch', '', refresh = True, async = True))
     return builders
 
@@ -2967,6 +2956,7 @@
     'help': [help_, '[command]'],
     'ideclean': [ideclean, ''],
     'ideinit': [ideinit, ''],
+    'jarprojects': [jarprojects, '[options]'],
     'projectgraph': [projectgraph, ''],
     'javap': [javap, ''],
     'javadoc': [javadoc, '[options]'],