comparison mxtool/mx.py @ 7528:4584ca2618d5

made eclipseformat command use batching
author Doug Simon <doug.simon@oracle.com>
date Wed, 23 Jan 2013 16:06:31 +0100
parents bbaa734b3627
children 780269e56ddd
comparison
equal deleted inserted replaced
7527:2232848be438 7528:4584ca2618d5
1469 The exit code 1 denotes that at least one file was modified.""" 1469 The exit code 1 denotes that at least one file was modified."""
1470 1470
1471 parser = ArgumentParser(prog='mx eclipseformat') 1471 parser = ArgumentParser(prog='mx eclipseformat')
1472 parser.add_argument('-e', '--eclipse-exe', help='location of the Eclipse executable') 1472 parser.add_argument('-e', '--eclipse-exe', help='location of the Eclipse executable')
1473 parser.add_argument('-C', '--no-backup', action='store_false', dest='backup', help='do not save backup of modified files') 1473 parser.add_argument('-C', '--no-backup', action='store_false', dest='backup', help='do not save backup of modified files')
1474 parser.add_argument('--projects', action='store', help='comma separated projects to process (omit to process all projects)')
1474 1475
1475 args = parser.parse_args(args) 1476 args = parser.parse_args(args)
1476 if args.eclipse_exe is None: 1477 if args.eclipse_exe is None:
1477 args.eclipse_exe = os.environ.get('ECLIPSE_EXE') 1478 args.eclipse_exe = os.environ.get('ECLIPSE_EXE')
1478 if args.eclipse_exe is None: 1479 if args.eclipse_exe is None:
1479 abort('Could not find Eclipse executable. Use -e option or ensure ECLIPSE_EXE environment variable is set.') 1480 abort('Could not find Eclipse executable. Use -e option or ensure ECLIPSE_EXE environment variable is set.')
1480 1481
1481 eclipseinit([], buildProcessorJars=False) 1482 eclipseinit([], buildProcessorJars=False)
1482 1483
1483 modified = dict() 1484 # build list of projects to be processed
1484 for p in sorted_deps(): 1485 projects = sorted_deps()
1486 if args.projects is not None:
1487 projects = [project(name) for name in args.projects.split(',')]
1488
1489 class Batch:
1490 def __init__(self, settingsFile):
1491 self.path = settingsFile
1492 self.javafiles = list()
1493
1494 def settings(self):
1495 with open(self.path) as fp:
1496 return fp.read()
1497
1498 class FileInfo:
1499 def __init__(self, path):
1500 self.path = path
1501 with open(path) as fp:
1502 self.content = fp.read()
1503 self.times = (os.path.getatime(path), os.path.getmtime(path))
1504
1505 def update(self):
1506 with open(self.path) as fp:
1507 content = fp.read()
1508 if self.content != content:
1509 self.content = content
1510 return True
1511 os.utime(self.path, self.times)
1512
1513 modified = list()
1514 batches = dict() # all sources with the same formatting settings are formatted together
1515 for p in projects:
1485 if p.native: 1516 if p.native:
1486 continue 1517 continue
1487 sourceDirs = p.source_dirs() 1518 sourceDirs = p.source_dirs()
1488 prefsFile = join(p.dir, '.settings', 'org.eclipse.jdt.core.prefs') 1519
1489 1520 batch = Batch(join(p.dir, '.settings', 'org.eclipse.jdt.core.prefs'))
1490 if not exists(prefsFile): 1521
1491 log('[no Eclipse Code Formatter preferences at {0} - skipping]'.format(prefsFile)) 1522 if not exists(batch.path):
1523 log('[no Eclipse Code Formatter preferences at {0} - skipping]'.format(batch.path))
1492 continue 1524 continue
1493 1525
1494 for sourceDir in sourceDirs: 1526 for sourceDir in sourceDirs:
1495 javafiles = dict()
1496 for root, _, files in os.walk(sourceDir): 1527 for root, _, files in os.walk(sourceDir):
1497 for f in [join(root, name) for name in files if name.endswith('.java') and name != 'package-info.java']: 1528 for f in [join(root, name) for name in files if name.endswith('.java') and name != 'package-info.java']:
1498 with open(f) as fp: 1529 batch.javafiles.append(FileInfo(f))
1499 content = fp.read() 1530 if len(batch.javafiles) == 0:
1500 javafiles[f] = content 1531 log('[no Java sources in {0} - skipping]'.format(p.name))
1501 if len(javafiles) == 0: 1532 continue
1502 log('[no Java sources in {0} - skipping]'.format(sourceDir)) 1533
1503 continue 1534 res = batches.setdefault(batch.settings(), batch)
1504 1535 if res is not batch:
1505 run([args.eclipse_exe, '-nosplash', '-application', 'org.eclipse.jdt.core.JavaCodeFormatter', '-config', prefsFile] + javafiles.keys()) 1536 res.javafiles = res.javafiles + batch.javafiles
1506 1537
1507 for f, oldContent in javafiles.iteritems(): 1538 for batch in batches.itervalues():
1508 with open(f) as fp: 1539 run([args.eclipse_exe, '-nosplash', '-application', 'org.eclipse.jdt.core.JavaCodeFormatter', '-config', batch.path] + [f.path for f in batch.javafiles])
1509 newContent = fp.read() 1540 for fi in batch.javafiles:
1510 if oldContent != newContent: 1541 if fi.update():
1511 modified[f] = oldContent 1542 modified.append(fi)
1512 1543
1513 log('{0} files were modified'.format(len(modified))) 1544 log('{0} files were modified'.format(len(modified)))
1514 if len(modified) != 0: 1545 if len(modified) != 0:
1515 if args.backup: 1546 if args.backup:
1516 backup = os.path.abspath('eclipseformat.backup.zip') 1547 backup = os.path.abspath('eclipseformat.backup.zip')
1517 arcbase = _mainSuite.dir 1548 arcbase = _mainSuite.dir
1518 zf = zipfile.ZipFile(backup, 'w', zipfile.ZIP_DEFLATED) 1549 zf = zipfile.ZipFile(backup, 'w', zipfile.ZIP_DEFLATED)
1519 for f, content in modified.iteritems(): 1550 for fi in modified:
1520 arcname = os.path.relpath(f, arcbase).replace(os.sep, '/') 1551 arcname = os.path.relpath(fi.path, arcbase).replace(os.sep, '/')
1521 zf.writestr(arcname, content) 1552 zf.writestr(arcname, fi.content)
1522 zf.close() 1553 zf.close()
1523 log('Wrote backup of {0} modified files to {1}'.format(len(modified), backup)) 1554 log('Wrote backup of {0} modified files to {1}'.format(len(modified), backup))
1524 return 1 1555 return 1
1525 1556
1526 def processorjars(): 1557 def processorjars():