# HG changeset patch # User Doug Simon # Date 1358953591 -3600 # Node ID 4584ca2618d576632755809eb3f8b9508ee4ac49 # Parent 2232848be4380c1d1caf2ff096b958eacc493e46 made eclipseformat command use batching diff -r 2232848be438 -r 4584ca2618d5 mxtool/mx.py --- a/mxtool/mx.py Wed Jan 23 15:32:51 2013 +0100 +++ b/mxtool/mx.py Wed Jan 23 16:06:31 2013 +0100 @@ -1471,44 +1471,75 @@ parser = ArgumentParser(prog='mx eclipseformat') parser.add_argument('-e', '--eclipse-exe', help='location of the Eclipse executable') parser.add_argument('-C', '--no-backup', action='store_false', dest='backup', help='do not save backup of modified files') + parser.add_argument('--projects', action='store', help='comma separated projects to process (omit to process all projects)') args = parser.parse_args(args) if args.eclipse_exe is None: args.eclipse_exe = os.environ.get('ECLIPSE_EXE') if args.eclipse_exe is None: abort('Could not find Eclipse executable. Use -e option or ensure ECLIPSE_EXE environment variable is set.') - + eclipseinit([], buildProcessorJars=False) - modified = dict() - for p in sorted_deps(): + # build list of projects to be processed + projects = sorted_deps() + if args.projects is not None: + projects = [project(name) for name in args.projects.split(',')] + + class Batch: + def __init__(self, settingsFile): + self.path = settingsFile + self.javafiles = list() + + def settings(self): + with open(self.path) as fp: + return fp.read() + + class FileInfo: + def __init__(self, path): + self.path = path + with open(path) as fp: + self.content = fp.read() + self.times = (os.path.getatime(path), os.path.getmtime(path)) + + def update(self): + with open(self.path) as fp: + content = fp.read() + if self.content != content: + self.content = content + return True + os.utime(self.path, self.times) + + modified = list() + batches = dict() # all sources with the same formatting settings are formatted together + for p in projects: if p.native: continue sourceDirs = p.source_dirs() - prefsFile = join(p.dir, '.settings', 'org.eclipse.jdt.core.prefs') - - if not exists(prefsFile): - log('[no Eclipse Code Formatter preferences at {0} - skipping]'.format(prefsFile)) + + batch = Batch(join(p.dir, '.settings', 'org.eclipse.jdt.core.prefs')) + + if not exists(batch.path): + log('[no Eclipse Code Formatter preferences at {0} - skipping]'.format(batch.path)) continue for sourceDir in sourceDirs: - javafiles = dict() for root, _, files in os.walk(sourceDir): for f in [join(root, name) for name in files if name.endswith('.java') and name != 'package-info.java']: - with open(f) as fp: - content = fp.read() - javafiles[f] = content - if len(javafiles) == 0: - log('[no Java sources in {0} - skipping]'.format(sourceDir)) - continue - - run([args.eclipse_exe, '-nosplash', '-application', 'org.eclipse.jdt.core.JavaCodeFormatter', '-config', prefsFile] + javafiles.keys()) - - for f, oldContent in javafiles.iteritems(): - with open(f) as fp: - newContent = fp.read() - if oldContent != newContent: - modified[f] = oldContent + batch.javafiles.append(FileInfo(f)) + if len(batch.javafiles) == 0: + log('[no Java sources in {0} - skipping]'.format(p.name)) + continue + + res = batches.setdefault(batch.settings(), batch) + if res is not batch: + res.javafiles = res.javafiles + batch.javafiles + + for batch in batches.itervalues(): + run([args.eclipse_exe, '-nosplash', '-application', 'org.eclipse.jdt.core.JavaCodeFormatter', '-config', batch.path] + [f.path for f in batch.javafiles]) + for fi in batch.javafiles: + if fi.update(): + modified.append(fi) log('{0} files were modified'.format(len(modified))) if len(modified) != 0: @@ -1516,9 +1547,9 @@ backup = os.path.abspath('eclipseformat.backup.zip') arcbase = _mainSuite.dir zf = zipfile.ZipFile(backup, 'w', zipfile.ZIP_DEFLATED) - for f, content in modified.iteritems(): - arcname = os.path.relpath(f, arcbase).replace(os.sep, '/') - zf.writestr(arcname, content) + for fi in modified: + arcname = os.path.relpath(fi.path, arcbase).replace(os.sep, '/') + zf.writestr(arcname, fi.content) zf.close() log('Wrote backup of {0} modified files to {1}'.format(len(modified), backup)) return 1