def handle(self, args): app = EnumerateApp(args.ini_file, args.l10n_base_dir, args.locales) app.reference = args.reference try: observer = compareApp(app, merge_stage=args.merge, clobber=args.clobber) except (OSError, IOError), exc: print "FAIL: " + str(exc) self.parser.exit(2)
def handle(self, args, options): if len(args) < 2: self.parser.error('Need to pass in list of languages') inipath, l10nbase = args[:2] locales = args[2:] app = EnumerateApp(inipath, l10nbase, locales) app.reference = options.reference try: observer = compareApp(app, merge_stage=options.merge, clobber=options.clobber) except (OSError, IOError), exc: print "FAIL: " + str(exc) self.parser.exit(2)
def test_app(self): 'Test parsing a App' app = EnumerateApp( mozpath.join(self.stage, 'comm', 'mail', 'locales', 'l10n.ini'), mozpath.join(self.stage, 'l10n-central')) self.assertListEqual(app.config.allLocales(), ['af', 'de', 'fr']) self.assertEqual(len(app.config.children), 1) projectconfig = app.asConfig() self.assertListEqual(projectconfig.locales, ['af', 'de', 'fr']) files = ProjectFiles('de', [projectconfig]) files = list(files) self.assertEqual(len(files), 3) l10nfile, reffile, mergefile, test = files[0] self.assertListEqual( mozpath.split(l10nfile)[-3:], ['de', 'mail', 'mail.ftl']) self.assertListEqual( mozpath.split(reffile)[-4:], ['mail', 'locales', 'en-US', 'mail.ftl']) self.assertIsNone(mergefile) self.assertSetEqual(test, set()) l10nfile, reffile, mergefile, test = files[1] self.assertListEqual( mozpath.split(l10nfile)[-3:], ['de', 'toolkit', 'localized.ftl']) self.assertListEqual( mozpath.split(reffile)[-6:], [ 'comm', 'mozilla', 'toolkit', 'locales', 'en-US', 'localized.ftl' ]) self.assertIsNone(mergefile) self.assertSetEqual(test, set()) l10nfile, reffile, mergefile, test = files[2] self.assertListEqual( mozpath.split(l10nfile)[-3:], ['de', 'toolkit', 'platform.ftl']) self.assertListEqual( mozpath.split(reffile)[-6:], ['comm', 'mozilla', 'toolkit', 'locales', 'en-US', 'platform.ftl']) self.assertIsNone(mergefile) self.assertSetEqual(test, set())
def test_app(self): 'Test parsing a App' app = EnumerateApp( mozpath.join(self.stage, 'comm', 'mail', 'locales', 'l10n.ini'), mozpath.join(self.stage, 'l10n-central')) self.assertListEqual(app.config.allLocales(), ['af', 'de', 'fr']) self.assertEqual(len(app.config.children), 1) projectconfig = app.asConfig() self.assertListEqual(projectconfig.locales, ['af', 'de', 'fr']) files = ProjectFiles('de', [projectconfig]) files = list(files) self.assertEqual(len(files), 3) l10nfile, reffile, mergefile, test = files[0] self.assertListEqual(mozpath.split(l10nfile)[-3:], ['de', 'mail', 'mail.ftl']) self.assertListEqual(mozpath.split(reffile)[-4:], ['mail', 'locales', 'en-US', 'mail.ftl']) self.assertIsNone(mergefile) self.assertSetEqual(test, set()) l10nfile, reffile, mergefile, test = files[1] self.assertListEqual(mozpath.split(l10nfile)[-3:], ['de', 'toolkit', 'localized.ftl']) self.assertListEqual( mozpath.split(reffile)[-6:], ['comm', 'mozilla', 'toolkit', 'locales', 'en-US', 'localized.ftl']) self.assertIsNone(mergefile) self.assertSetEqual(test, set()) l10nfile, reffile, mergefile, test = files[2] self.assertListEqual(mozpath.split(l10nfile)[-3:], ['de', 'toolkit', 'platform.ftl']) self.assertListEqual( mozpath.split(reffile)[-6:], ['comm', 'mozilla', 'toolkit', 'locales', 'en-US', 'platform.ftl']) self.assertIsNone(mergefile) self.assertSetEqual(test, set())
def compare(self, l10n_ini=None, l10n_base=None, merge_dir=None, locales=None): from compare_locales.paths import EnumerateApp from compare_locales.compare import compareApp # check if we're configured and use defaults from there # otherwise, error early try: self.substs # explicitly check if not l10n_ini: l10n_ini = mozpath.join( self.topsrcdir, self.substs['MOZ_BUILD_APP'], 'locales', 'l10n.ini' ) if not l10n_base: l10n_base = mozpath.join( self.topsrcdir, self.substs['L10NBASEDIR'] ) except Exception: if not l10n_ini or not l10n_base: print('Specify --l10n-ini and --l10n-base or run configure.') return 1 if not merge_dir: try: # self.substs is raising an Exception if we're not configured # don't merge if we're not merge_dir = mozpath.join( self.topobjdir, self.substs['MOZ_BUILD_APP'], 'locales', 'merge-{ab_CD}' ) except Exception: pass app = EnumerateApp(l10n_ini, l10n_base, locales) observer = compareApp(app, merge_stage=merge_dir, clobber=True) print(observer.serialize().encode('utf-8', 'replace'))
def handle(self, config_paths, l10n_base_dir, locales, merge=None, defines=None, unified=False, full=False, quiet=0, validate=False, clobber=False, data='text'): # using nargs multiple times in argparser totally screws things # up, repair that. # First files are configs, then the base dir, everything else is # locales all_args = config_paths + [l10n_base_dir] + locales config_paths = [] locales = [] if defines is None: defines = [] while all_args and not os.path.isdir(all_args[0]): config_paths.append(all_args.pop(0)) if not config_paths: self.parser.error('no configuration file given') for cf in config_paths: if not os.path.isfile(cf): self.parser.error('config file %s not found' % cf) if not all_args: self.parser.error('l10n-base-dir not found') l10n_base_dir = all_args.pop(0) if validate: # signal validation mode by setting locale list to [None] locales = [None] else: locales.extend(all_args) # when we compare disabled projects, we set our locales # on all subconfigs, so deep is True. locales_deep = full configs = [] config_env = {} for define in defines: var, _, value = define.partition('=') config_env[var] = value for config_path in config_paths: if config_path.endswith('.toml'): try: config = TOMLParser.parse(config_path, env=config_env) except ConfigNotFound as e: self.parser.exit('config file %s not found' % e.filename) config.add_global_environment(l10n_base=l10n_base_dir) if locales: config.set_locales(locales, deep=locales_deep) configs.append(config) else: app = EnumerateApp(config_path, l10n_base_dir, locales) configs.append(app.asConfig()) try: unified_observer = None if unified: unified_observer = Observer(quiet=quiet) observers = compareProjects(configs, quiet=quiet, stat_observer=unified_observer, merge_stage=merge, clobber_merge=clobber) except (OSError, IOError) as exc: print("FAIL: " + str(exc)) self.parser.exit(2) if unified: observers = [unified_observer] rv = 0 for observer in observers: print(observer.serialize(type=data)) # summary is a dict of lang-summary dicts # find out if any of our results has errors, return 1 if so if rv > 0: continue # we already have errors for loc, summary in observer.summary.items(): if summary.get('errors', 0) > 0: rv = 1 # no need to check further summaries, but # continue to run through observers break return rv
def handle( self, quiet=0, verbose=0, validate=False, merge=None, config_paths=[], l10n_base_dir=None, locales=[], defines=[], full=False, return_zero=False, clobber=False, json=None, ): """The instance part of the classmethod call. Using keyword arguments as that is what we need for mach commands in mozilla-central. """ # log as verbose or quiet as we want, warn by default logging_level = logging.WARNING - (verbose - quiet) * 10 logging.basicConfig() logging.getLogger().setLevel(logging_level) config_paths, l10n_base_dir, locales = self.extract_positionals( validate=validate, config_paths=config_paths, l10n_base_dir=l10n_base_dir, locales=locales, ) # when we compare disabled projects, we set our locales # on all subconfigs, so deep is True. locales_deep = full configs = [] config_env = { 'l10n_base': l10n_base_dir } for define in defines: var, _, value = define.partition('=') config_env[var] = value for config_path in config_paths: if config_path.endswith('.toml'): try: config = TOMLParser().parse(config_path, env=config_env) except ConfigNotFound as e: self.parser.exit('config file %s not found' % e.filename) if locales_deep: if not locales: # no explicit locales given, force all locales config.set_locales(config.all_locales, deep=True) else: config.set_locales(locales, deep=True) configs.append(config) else: app = EnumerateApp(config_path, l10n_base_dir) configs.append(app.asConfig()) try: observers = compareProjects( configs, locales, l10n_base_dir, quiet=quiet, merge_stage=merge, clobber_merge=clobber) except (OSError, IOError) as exc: print("FAIL: " + str(exc)) self.parser.exit(2) if json is None or json != '-': details = observers.serializeDetails() if details: print(details) if len(configs) > 1: if details: print('') print("Summaries for") for config_path in config_paths: print(" " + config_path) print(" and the union of these, counting each string once") print(observers.serializeSummaries()) if json is not None: data = [observer.toJSON() for observer in observers] stdout = json == '-' indent = 1 if stdout else None fh = sys.stdout if stdout else open(json, 'w') json_dump(data, fh, sort_keys=True, indent=indent) if stdout: fh.write('\n') fh.close() rv = 1 if not return_zero and observers.error else 0 return rv
def handle( self, quiet=0, verbose=0, validate=False, merge=None, config_paths=[], l10n_base_dir=None, locales=[], defines=[], full=False, return_zero=False, clobber=False, json=None, ): """The instance part of the classmethod call. Using keyword arguments as that is what we need for mach commands in mozilla-central. """ # log as verbose or quiet as we want, warn by default logging_level = logging.WARNING - (verbose - quiet) * 10 logging.basicConfig() logging.getLogger().setLevel(logging_level) config_paths, l10n_base_dir, locales = self.extract_positionals( validate=validate, config_paths=config_paths, l10n_base_dir=l10n_base_dir, locales=locales, ) # when we compare disabled projects, we set our locales # on all subconfigs, so deep is True. locales_deep = full configs = [] config_env = {'l10n_base': l10n_base_dir} for define in defines: var, _, value = define.partition('=') config_env[var] = value for config_path in config_paths: if config_path.endswith('.toml'): try: config = TOMLParser().parse(config_path, env=config_env) except ConfigNotFound as e: self.parser.exit('config file %s not found' % e.filename) if locales_deep: if not locales: # no explicit locales given, force all locales config.set_locales(config.all_locales, deep=True) else: config.set_locales(locales, deep=True) configs.append(config) else: app = EnumerateApp(config_path, l10n_base_dir) configs.append(app.asConfig()) try: observers = compareProjects(configs, locales, l10n_base_dir, quiet=quiet, merge_stage=merge, clobber_merge=clobber) except (OSError, IOError) as exc: print("FAIL: " + str(exc)) self.parser.exit(2) if json is None or json != '-': details = observers.serializeDetails() if details: print(details) if len(configs) > 1: if details: print('') print("Summaries for") for config_path in config_paths: print(" " + config_path) print(" and the union of these, counting each string once") print(observers.serializeSummaries()) if json is not None: data = [observer.toJSON() for observer in observers] stdout = json == '-' indent = 1 if stdout else None fh = sys.stdout if stdout else open(json, 'w') json_dump(data, fh, sort_keys=True, indent=indent) if stdout: fh.write('\n') fh.close() rv = 1 if not return_zero and observers.error else 0 return rv
def handle(self, config_paths, l10n_base_dir, locales, merge=None, defines=None, unified=False, full=False, quiet=0, clobber=False, data='text'): # using nargs multiple times in argparser totally screws things # up, repair that. # First files are configs, then the base dir, everything else is # locales all_args = config_paths + [l10n_base_dir] + locales config_paths = [] locales = [] if defines is None: defines = [] while all_args and not os.path.isdir(all_args[0]): config_paths.append(all_args.pop(0)) if not config_paths: self.parser.error('no configuration file given') for cf in config_paths: if not os.path.isfile(cf): self.parser.error('config file %s not found' % cf) if not all_args: self.parser.error('l10n-base-dir not found') l10n_base_dir = all_args.pop(0) locales.extend(all_args) # when we compare disabled projects, we set our locales # on all subconfigs, so deep is True. locales_deep = full configs = [] config_env = {} for define in defines: var, _, value = define.partition('=') config_env[var] = value for config_path in config_paths: if config_path.endswith('.toml'): try: config = TOMLParser.parse(config_path, env=config_env) except ConfigNotFound as e: self.parser.exit('config file %s not found' % e.filename) config.add_global_environment(l10n_base=l10n_base_dir) if locales: config.set_locales(locales, deep=locales_deep) configs.append(config) else: app = EnumerateApp(config_path, l10n_base_dir, locales) configs.append(app.asConfig()) try: unified_observer = None if unified: unified_observer = Observer(quiet=quiet) observers = compareProjects(configs, quiet=quiet, stat_observer=unified_observer, merge_stage=merge, clobber_merge=clobber) except (OSError, IOError), exc: print "FAIL: " + str(exc) self.parser.exit(2)