devtools/devctl.py
branchtls-sprint
changeset 1770 8bd788149f85
parent 1769 fb91d2b8a441
child 1774 4f4e0c6682e5
equal deleted inserted replaced
1769:fb91d2b8a441 1770:8bd788149f85
   323         else:
   323         else:
   324             cubes = [DevCubeConfiguration.cube_dir(cube) for cube in DevCubeConfiguration.available_cubes()]
   324             cubes = [DevCubeConfiguration.cube_dir(cube) for cube in DevCubeConfiguration.available_cubes()]
   325             cubes = [cubepath for cubepath in cubes if exists(join(cubepath, 'i18n'))]
   325             cubes = [cubepath for cubepath in cubes if exists(join(cubepath, 'i18n'))]
   326         update_cubes_catalogs(cubes)
   326         update_cubes_catalogs(cubes)
   327 
   327 
       
   328 
   328 def update_cubes_catalogs(cubes):
   329 def update_cubes_catalogs(cubes):
       
   330     toedit = []
       
   331     for cubedir in cubes:
       
   332         if not isdir(cubedir):
       
   333             print 'not a directory', cubedir
       
   334             continue
       
   335         try:
       
   336             toedit += update_cube_catalogs(cubedir)
       
   337         except Exception:
       
   338             import traceback
       
   339             traceback.print_exc()
       
   340             print 'error while updating catalogs for', cubedir
       
   341     # instructions pour la suite
       
   342     print '*' * 72
       
   343     print 'you can now edit the following files:'
       
   344     print '* ' + '\n* '.join(toedit)
       
   345 
       
   346 
       
   347 def update_cube_catalogs(cubedir):
   329     import shutil
   348     import shutil
   330     from tempfile import mktemp
   349     from tempfile import mktemp
   331     from logilab.common.fileutils import ensure_fs_mode
   350     from logilab.common.fileutils import ensure_fs_mode
   332     from logilab.common.shellutils import find, rm
   351     from logilab.common.shellutils import find, rm
   333     from cubicweb.common.i18n import extract_from_tal, execute
   352     from cubicweb.common.i18n import extract_from_tal, execute
   334     toedit = []
   353     toedit = []
   335     for cubedir in cubes:
   354     cube = basename(normpath(cubedir))
   336         cube = basename(normpath(cubedir))
   355     tempdir = mktemp()
   337         if not isdir(cubedir):
   356     mkdir(tempdir)
   338             print 'unknown cube', cube
   357     print '*' * 72
   339             continue
   358     print 'updating %s cube...' % cube
   340         tempdir = mktemp()
   359     chdir(cubedir)
   341         mkdir(tempdir)
   360     potfiles = [join('i18n', scfile) for scfile in ('entities.pot',)
   342         print '*' * 72
   361                 if exists(join('i18n', scfile))]
   343         print 'updating %s cube...' % cube
   362     print '******** extract schema messages'
   344         chdir(cubedir)
   363     schemapot = join(tempdir, 'schema.pot')
   345         potfiles = [join('i18n', scfile) for scfile in ('entities.pot',)
   364     potfiles.append(schemapot)
   346                     if exists(join('i18n', scfile))]
   365     # explicit close necessary else the file may not be yet flushed when
   347         print '******** extract schema messages'
   366     # we'll using it below
   348         schemapot = join(tempdir, 'schema.pot')
   367     schemapotstream = file(schemapot, 'w')
   349         potfiles.append(schemapot)
   368     generate_schema_pot(schemapotstream.write, cubedir)
   350         # explicit close necessary else the file may not be yet flushed when
   369     schemapotstream.close()
   351         # we'll using it below
   370     print '******** extract TAL messages'
   352         schemapotstream = file(schemapot, 'w')
   371     tali18nfile = join(tempdir, 'tali18n.py')
   353         generate_schema_pot(schemapotstream.write, cubedir)
   372     extract_from_tal(find('.', ('.py', '.pt'), blacklist=STD_BLACKLIST+('test',)), tali18nfile)
   354         schemapotstream.close()
   373     print '******** extract Javascript messages'
   355         print '******** extract TAL messages'
   374     jsfiles =  [jsfile for jsfile in find('.', '.js') if basename(jsfile).startswith('cub')]
   356         tali18nfile = join(tempdir, 'tali18n.py')
   375     if jsfiles:
   357         extract_from_tal(find('.', ('.py', '.pt'), blacklist=STD_BLACKLIST+('test',)), tali18nfile)
   376         tmppotfile = join(tempdir, 'js.pot')
   358         print '******** extract Javascript messages'
   377         execute('xgettext --no-location --omit-header -k_ -L java --from-code=utf-8 -o %s %s'
   359         jsfiles =  [jsfile for jsfile in find('.', '.js') if basename(jsfile).startswith('cub')]
   378                 % (tmppotfile, ' '.join(jsfiles)))
   360         if jsfiles:
   379         # no pot file created if there are no string to translate
   361             tmppotfile = join(tempdir, 'js.pot')
   380         if exists(tmppotfile):
   362             execute('xgettext --no-location --omit-header -k_ -L java --from-code=utf-8 -o %s %s'
       
   363                     % (tmppotfile, ' '.join(jsfiles)))
       
   364             # no pot file created if there are no string to translate
       
   365             if exists(tmppotfile):
       
   366                 potfiles.append(tmppotfile)
       
   367         print '******** create cube specific catalog'
       
   368         tmppotfile = join(tempdir, 'generated.pot')
       
   369         cubefiles = find('.', '.py', blacklist=STD_BLACKLIST+('test',))
       
   370         cubefiles.append(tali18nfile)
       
   371         execute('xgettext --no-location --omit-header -k_ -o %s %s'
       
   372                 % (tmppotfile, ' '.join(cubefiles)))
       
   373         if exists(tmppotfile): # doesn't exists of no translation string found
       
   374             potfiles.append(tmppotfile)
   381             potfiles.append(tmppotfile)
   375         potfile = join(tempdir, 'cube.pot')
   382     print '******** create cube specific catalog'
   376         print '******** merging .pot files'
   383     tmppotfile = join(tempdir, 'generated.pot')
   377         execute('msgcat %s > %s' % (' '.join(potfiles), potfile))
   384     cubefiles = find('.', '.py', blacklist=STD_BLACKLIST+('test',))
   378         print '******** merging main pot file with existing translations'
   385     cubefiles.append(tali18nfile)
   379         chdir('i18n')
   386     execute('xgettext --no-location --omit-header -k_ -o %s %s'
   380         for lang in LANGS:
   387             % (tmppotfile, ' '.join(cubefiles)))
   381             print '****', lang
   388     if exists(tmppotfile): # doesn't exists of no translation string found
   382             cubepo = '%s.po' % lang
   389         potfiles.append(tmppotfile)
   383             if not exists(cubepo):
   390     potfile = join(tempdir, 'cube.pot')
   384                 shutil.copy(potfile, cubepo)
   391     print '******** merging .pot files'
   385             else:
   392     execute('msgcat %s > %s' % (' '.join(potfiles), potfile))
   386                 execute('msgmerge -N -s %s %s > %snew' % (cubepo, potfile, cubepo))
   393     print '******** merging main pot file with existing translations'
   387                 ensure_fs_mode(cubepo)
   394     chdir('i18n')
   388                 shutil.move('%snew' % cubepo, cubepo)
   395     for lang in LANGS:
   389             toedit.append(abspath(cubepo))
   396         print '****', lang
   390         # cleanup
   397         cubepo = '%s.po' % lang
   391         rm(tempdir)
   398         if not exists(cubepo):
   392     # instructions pour la suite
   399             shutil.copy(potfile, cubepo)
   393     print '*' * 72
   400         else:
   394     print 'you can now edit the following files:'
   401             execute('msgmerge -N -s %s %s > %snew' % (cubepo, potfile, cubepo))
   395     print '* ' + '\n* '.join(toedit)
   402             ensure_fs_mode(cubepo)
       
   403             shutil.move('%snew' % cubepo, cubepo)
       
   404         toedit.append(abspath(cubepo))
       
   405     # cleanup
       
   406     rm(tempdir)
       
   407     return toedit
   396 
   408 
   397 
   409 
   398 class LiveServerCommand(Command):
   410 class LiveServerCommand(Command):
   399     """Run a server from within a cube directory.
   411     """Run a server from within a cube directory.
   400     """
   412     """