def save(self): """write back change log""" # filetutils isn't importable in appengine, so import locally from logilab.common.fileutils import ensure_fs_mode ensure_fs_mode(self.file, S_IWRITE) self.write(codecs.open(self.file, "w", encoding="utf-8"))
def pkginfo_save(pkginfo, modifs): """save modifications in the <modifs> dictionary to a __pkginfo__ file (path given by the <pkginfo> string) """ tmpfile = tempfile.mktemp() stream = open(tmpfile, 'w') try: # replace existants values for line in open(pkginfo, 'r'): key = line.split('=')[0].strip() # FIXME: multilines value val = modifs.pop(key, None) if val is not None: stream.write('%s = %r\n' % (key, val)) else: stream.write(line) # add missing values for key, value in modifs.items(): stream.write('%s = %r\n' % (key, value)) stream.close() ensure_fs_mode(pkginfo, S_IWRITE) # FIXME: fix perms if necessary shutil.move(tmpfile, pkginfo) finally: os.remove(tmpfile) raise
def update_cube_catalogs(cubedir): cubedir = osp.abspath(osp.normpath(cubedir)) workdir = tempfile.mkdtemp() try: cubename = osp.basename(cubedir) if cubename.startswith('cubicweb_'): # new layout distname = cubename cubename = cubename[len('cubicweb_'):] else: distname = 'cubicweb_' + cubename print('cubedir', cubedir) extract_cls = I18nCubeMessageExtractor try: extract_cls = pkg_resources.load_entry_point( distname, 'cubicweb.i18ncube', cubename) except (pkg_resources.DistributionNotFound, ImportError): pass # no customization found print(underline_title('Updating i18n catalogs for cube %s' % cubename)) chdir(cubedir) extractor = extract_cls(workdir, cubedir) potfile = extractor.generate_pot_file() if potfile is None: print('no message catalog for cube', cubename, 'nothing to translate') return () print('-> merging main pot file with existing translations:', end=' ') chdir('i18n') toedit = [] for lang in CubicWebNoAppConfiguration.cw_languages(): print(lang, end=' ') cubepo = '%s.po' % lang if not osp.exists(cubepo): shutil.copy(potfile, cubepo) else: cmd = [ 'msgmerge', '-N', '-s', '-o', cubepo + 'new', cubepo, potfile ] execute2(cmd) ensure_fs_mode(cubepo) shutil.move('%snew' % cubepo, cubepo) toedit.append(osp.abspath(cubepo)) print() return toedit finally: # cleanup shutil.rmtree(workdir)
def compile_i18n_catalogs(sourcedirs, destdir, langs): """generate .mo files for a set of languages into the `destdir` i18n directory """ from subprocess import CalledProcessError from logilab.common.fileutils import ensure_fs_mode print('-> compiling message catalogs to %s' % destdir) errors = [] for lang in langs: langdir = join(destdir, lang, 'LC_MESSAGES') if not exists(langdir): create_dir(langdir) pofiles = [join(path, '%s.po' % lang) for path in sourcedirs] pofiles = [pof for pof in pofiles if exists(pof)] mergedpo = join(destdir, '%s_merged.po' % lang) try: # merge instance/cubes messages catalogs with the stdlib's one cmd = [ 'msgcat', '--use-first', '--sort-output', '--strict', '-o', mergedpo ] + pofiles execute2(cmd) # make sure the .mo file is writeable and compiles with *msgfmt* applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo') try: ensure_fs_mode(applmo) except OSError: pass # suppose not exists execute2(['msgfmt', mergedpo, '-o', applmo]) except CalledProcessError as exc: errors.append( u'while handling language %s:\ncmd:\n%s\nstdout:\n%s\nstderr:\n%s\n' % (lang, exc.cmd, repr(exc.data[0]), repr(exc.data[1]))) except Exception as exc: errors.append(u'while handling language %s: %s' % (lang, exc)) try: # clean everything os.unlink(mergedpo) except Exception: continue return errors
def run(self, args): """run the command with its specific arguments""" import shutil import tempfile import yams from logilab.common.fileutils import ensure_fs_mode from logilab.common.shellutils import globfind, find, rm from logilab.common.modutils import get_module_files from cubicweb.i18n import extract_from_tal, execute2 tempdir = tempfile.mkdtemp(prefix='cw-') cwi18ndir = WebConfiguration.i18n_lib_dir() print('-> extract messages:', end=' ') print('schema', end=' ') schemapot = osp.join(tempdir, 'schema.pot') potfiles = [schemapot] potfiles.append(schemapot) # explicit close necessary else the file may not be yet flushed when # we'll using it below schemapotstream = open(schemapot, 'w') generate_schema_pot(schemapotstream.write, cubedir=None) schemapotstream.close() print('TAL', end=' ') tali18nfile = osp.join(tempdir, 'tali18n.py') extract_from_tal(find(osp.join(BASEDIR, 'web'), ('.py', '.pt')), tali18nfile) print('-> generate .pot files.') pyfiles = get_module_files(BASEDIR) pyfiles += globfind(osp.join(BASEDIR, 'misc', 'migration'), '*.py') schemafiles = globfind(osp.join(BASEDIR, 'schemas'), '*.py') jsfiles = globfind(osp.join(BASEDIR, 'web'), 'cub*.js') for id, files, lang in [ ('pycubicweb', pyfiles, None), ('schemadescr', schemafiles, None), ('yams', get_module_files(yams.__path__[0]), None), ('tal', [tali18nfile], None), ('js', jsfiles, 'java'), ]: potfile = osp.join(tempdir, '%s.pot' % id) cmd = ['xgettext', '--no-location', '--omit-header', '-k_'] if lang is not None: cmd.extend(['-L', lang]) cmd.extend(['-o', potfile]) cmd.extend(files) execute2(cmd) if osp.exists(potfile): potfiles.append(potfile) else: print('-> WARNING: %s file was not generated' % potfile) print('-> merging %i .pot files' % len(potfiles)) cubicwebpot = osp.join(tempdir, 'cubicweb.pot') cmd = ['msgcat', '-o', cubicwebpot] + potfiles execute2(cmd) print('-> merging main pot file with existing translations.') chdir(cwi18ndir) toedit = [] for lang in CubicWebNoAppConfiguration.cw_languages(): target = '%s.po' % lang cmd = [ 'msgmerge', '-N', '--sort-output', '-o', target + 'new', target, cubicwebpot ] execute2(cmd) ensure_fs_mode(target) shutil.move('%snew' % target, target) toedit.append(osp.abspath(target)) # cleanup rm(tempdir) # instructions pour la suite print('-> regenerated CubicWeb\'s .po catalogs.') print('\nYou can now edit the following files:') print('* ' + '\n* '.join(toedit)) print('when you are done, run "cubicweb-ctl i18ncube yourcube".')