예제 #1
0
 def test_main(self):
     """
     Runs generate.main() which should merge source files,
     then compile all sources in all configured languages.
     Validates output by checking all .mo files in all configured languages.
     .mo files should exist, and be recently created (modified
     after start of test suite)
     """
     generate.main(verbose=0, strict=False)
     for locale in CONFIGURATION.translated_locales:
         for (filename, num_headers) in zip(('django', 'djangojs'), (6, 4)):
             mofile = filename + '.mo'
             file_path = os.path.join(
                 CONFIGURATION.get_messages_dir(locale), mofile)
             exists = os.path.exists(file_path)
             self.assertTrue(exists,
                             msg='Missing file in locale %s: %s' %
                             (locale, mofile))
             self.assertTrue(datetime.fromtimestamp(
                 os.path.getmtime(file_path), UTC) >= self.start_time,
                             msg='File not recently modified: %s' %
                             file_path)
         # Assert merge headers look right
         file_path = os.path.join(CONFIGURATION.get_messages_dir(locale),
                                  filename + '.po')
         self.assert_merge_headers(file_path, num_headers)
예제 #2
0
def clean_locale(locale):
    """
    Strips out the warning from all of a locale's translated po files
    about being an English source file.
    Iterates over machine-generated files.
    """
    dirname = CONFIGURATION.get_messages_dir(locale)
    for filename in ('django-partial.po', 'djangojs-partial.po', 'mako.po'):
        clean_file(dirname.joinpath(filename))
예제 #3
0
def clean_locale(locale):
    """
    Strips out the warning from all of a locale's translated po files
    about being an English source file.
    Iterates over machine-generated files.
    """
    dirname = CONFIGURATION.get_messages_dir(locale)
    for filename in ('django-partial.po', 'djangojs-partial.po', 'mako.po'):
        clean_file(dirname.joinpath(filename))
예제 #4
0
def clean_conf_folder(locale):
    """Remove the configuration directory for `locale`"""
    dirname = CONFIGURATION.get_messages_dir(locale)
    command = "rm -rf {}".format(dirname)
    print(command)
    try:
        execute(command)
    except Exception as exc:
        print("Encountered error {}; continuing...".format(exc))
        return
예제 #5
0
def segment_pofiles(locale):
    """Segment all the pofiles for `locale`.

    Returns a set of filenames, all the segment files written.

    """
    files_written = set()
    for filename, segments in CONFIGURATION.segment.items():
        filename = CONFIGURATION.get_messages_dir(locale) / filename
        files_written.update(segment_pofile(filename, segments))
    return files_written
예제 #6
0
 def test_main(self):
     """
     Runs generate.main() which should merge source files,
     then compile all sources in all configured languages.
     Validates output by checking all .mo files in all configured languages.
     .mo files should exist, and be recently created (modified
     after start of test suite)
     """
     generate.main(verbose=0, strict=False)
     for locale in CONFIGURATION.translated_locales:
         for (filename, num_headers) in zip(('django', 'djangojs'), (6, 4)):
             mofile = filename + '.mo'
             file_path = os.path.join(CONFIGURATION.get_messages_dir(locale), mofile)
             exists = os.path.exists(file_path)
             self.assertTrue(exists, msg='Missing file in locale %s: %s' % (locale, mofile))
             self.assertTrue(
                 datetime.fromtimestamp(os.path.getmtime(file_path), UTC) >= self.start_time,
                 msg='File not recently modified: %s' % file_path
             )
         # Assert merge headers look right
         file_path = os.path.join(CONFIGURATION.get_messages_dir(locale), filename + '.po')
         self.assert_merge_headers(file_path, num_headers)
예제 #7
0
 def test_main(self):
     """
     Runs generate.main() which should merge source files,
     then compile all sources in all configured languages.
     Validates output by checking all .mo files in all configured languages.
     .mo files should exist, and be recently created (modified
     after start of test suite)
     """
     generate.main()
     for locale in CONFIGURATION.locales:
         for filename in ('django', 'djangojs'):
             mofile = filename+'.mo'
             path = os.path.join(CONFIGURATION.get_messages_dir(locale), mofile)
             exists = os.path.exists(path)
             self.assertTrue(exists, msg='Missing file in locale %s: %s' % (locale, mofile))
             self.assertTrue(datetime.fromtimestamp(os.path.getmtime(path), UTC) >= self.start_time,
                             msg='File not recently modified: %s' % path)
예제 #8
0
 def test_main(self):
     """
     Runs generate.main() which should merge source files,
     then compile all sources in all configured languages.
     Validates output by checking all .mo files in all configured languages.
     .mo files should exist, and be recently created (modified
     after start of test suite)
     """
     generate.main()
     for locale in CONFIGURATION.translated_locales:
         for filename in ('django', 'djangojs'):
             mofile = filename+'.mo'
             path = os.path.join(CONFIGURATION.get_messages_dir(locale), mofile)
             exists = os.path.exists(path)
             self.assertTrue(exists, msg='Missing file in locale %s: %s' % (locale, mofile))
             self.assertTrue(datetime.fromtimestamp(os.path.getmtime(path), UTC) >= self.start_time,
                             msg='File not recently modified: %s' % path)
예제 #9
0
    def assert_merge_headers(self, locale):
        """
        This is invoked by test_main to ensure that it runs after
        calling generate.main().

        There should be exactly three merge comment headers
        in our merged .po file. This counts them to be sure.
        A merge comment looks like this:
        # #-#-#-#-#  django-partial.po (0.1a)  #-#-#-#-#

        """
        path = os.path.join(CONFIGURATION.get_messages_dir(locale), "django.po")
        po = pofile(path)
        pattern = re.compile("^#-#-#-#-#", re.M)
        match = pattern.findall(po.header)
        self.assertEqual(
            len(match), 3, msg="Found %s (should be 3) merge comments in the header for %s" % (len(match), path)
        )
예제 #10
0
    def assert_merge_headers(self, locale):
        """
        This is invoked by test_main to ensure that it runs after
        calling generate.main().

        There should be exactly three merge comment headers
        in our merged .po file. This counts them to be sure.
        A merge comment looks like this:
        # #-#-#-#-#  django-partial.po (0.1a)  #-#-#-#-#

        """
        path = os.path.join(CONFIGURATION.get_messages_dir(locale), 'django.po')
        po = pofile(path)
        pattern = re.compile('^#-#-#-#-#', re.M)
        match = pattern.findall(po.header)
        self.assertEqual(len(match), 3,
                         msg="Found %s (should be 3) merge comments in the header for %s" % \
                         (len(match), path))
 def test_main(self):
     """
     Runs generate.main() which should merge source files,
     then compile all sources in all configured languages.
     Validates output by checking all .mo files in all configured languages.
     .mo files should exist, and be recently created (modified
     after start of test suite)
     """
     generate.main(verbosity=0, strict=False)
     for locale in CONFIGURATION.translated_locales:
         for filename in ("django", "djangojs"):
             mofile = filename + ".mo"
             path = os.path.join(CONFIGURATION.get_messages_dir(locale), mofile)
             exists = os.path.exists(path)
             self.assertTrue(exists, msg="Missing file in locale %s: %s" % (locale, mofile))
             self.assertGreaterEqual(
                 datetime.fromtimestamp(os.path.getmtime(path), UTC),
                 self.start_time,
                 msg="File not recently modified: %s" % path,
             )
예제 #12
0
def merge(locale, target='django.po', sources=('django-partial.po',), fail_if_missing=True):
    """
    For the given locale, merge the `sources` files to become the `target`
    file.  Note that the target file might also be one of the sources.

    If fail_if_missing is true, and the files to be merged are missing,
    throw an Exception, otherwise return silently.

    If fail_if_missing is false, and the files to be merged are missing,
    just return silently.

    """
    LOG.info('Merging {target} for locale {locale}'.format(target=target, locale=locale))
    locale_directory = CONFIGURATION.get_messages_dir(locale)
    try:
        validate_files(locale_directory, sources)
    except Exception, e:
        if not fail_if_missing:
            return
        raise
예제 #13
0
    def test_resolve_merge_conflicts(self, mock_log):
        django_po_path = os.path.join(CONFIGURATION.get_messages_dir('fake2'), 'django.po')
        # File ought to have been generated in test_main
        # if not os.path.exists(django_po_path):
        generate.main(verbose=0, strict=False)

        with open(django_po_path, 'r') as django_po_file:
            po_lines = django_po_file.read()

        # check that there are no merge conflicts present
        # "#-#-#-#-#  django-partial.po (edx-platform)  #-#-#-#-#\n"
        pattern = re.compile('\"#-#-#-#-#.*#-#-#-#-#', re.M)
        match = pattern.findall(po_lines)
        self.assertEqual(len(match), 0, msg="Error, found merge conflicts in django.po: %s" % match)
        # Validate that the appropriate log warnings were shown
        self.assertTrue(mock_log.warn.called)
        self.assertIn(
            " %s duplicates in %s, details in .dup file",
            # the first item of call_args is the call arguments themselves as a tuple
            mock_log.warn.call_args[0]
        )
예제 #14
0
def merge(locale, target="django.po", fail_if_missing=True):
    """
    For the given locale, merge django-partial.po, messages.po, mako.po -> django.po
    target is the resulting filename

    If fail_if_missing is true, and the files to be merged are missing,
    throw an Exception, otherwise return silently.

    If fail_if_missing is false, and the files to be merged are missing,
    just return silently.

    """
    LOG.info("Merging locale={0}".format(locale))
    locale_directory = CONFIGURATION.get_messages_dir(locale)
    files_to_merge = ("django-partial.po", "messages.po", "mako.po")
    try:
        validate_files(locale_directory, files_to_merge)
    except Exception, e:
        if not fail_if_missing:
            return
        raise e
예제 #15
0
    def test_resolve_merge_conflicts(self, mock_log):
        django_po_path = os.path.join(CONFIGURATION.get_messages_dir('fake2'),
                                      'django.po')
        # File ought to have been generated in test_main
        # if not os.path.exists(django_po_path):
        generate.main(verbose=0, strict=False)

        with open(django_po_path, 'r') as django_po_file:
            po_lines = django_po_file.read()

        # check that there are no merge conflicts present
        # "#-#-#-#-#  django-partial.po (edx-platform)  #-#-#-#-#\n"
        pattern = re.compile('\"#-#-#-#-#.*#-#-#-#-#', re.M)
        match = pattern.findall(po_lines)
        self.assertEqual(len(match),
                         0,
                         msg="Error, found merge conflicts in django.po: %s" %
                         match)
        # Validate that the appropriate log warnings were shown
        self.assertTrue(mock_log.warning.called)
        self.assertIn(
            " %s duplicates in %s, details in .dup file",
            # the first item of call_args is the call arguments themselves as a tuple
            mock_log.warning.call_args[0])
예제 #16
0
def merge(locale,
          target='django.po',
          sources=('django-partial.po', ),
          fail_if_missing=True):
    """
    For the given locale, merge the `sources` files to become the `target`
    file.  Note that the target file might also be one of the sources.

    If fail_if_missing is true, and the files to be merged are missing,
    throw an Exception, otherwise return silently.

    If fail_if_missing is false, and the files to be merged are missing,
    just return silently.

    """
    LOG.info('Merging {target} for locale {locale}'.format(target=target,
                                                           locale=locale))
    locale_directory = CONFIGURATION.get_messages_dir(locale)
    try:
        validate_files(locale_directory, sources)
    except Exception, e:
        if not fail_if_missing:
            return
        raise e
예제 #17
0
        converter.convert_msg(msg)

    # If any message has a plural, then the file needs plural information.
    # Apply declaration for English pluralization rules so that ngettext will
    # do something reasonable.
    if any(m.msgid_plural for m in pofile):
        pofile.metadata['Plural-Forms'] = 'nplurals=2; plural=(n != 1);'

    new_file = new_filename(file, locale)
    create_dir_if_necessary(new_file)
    pofile.save(new_file)


def new_filename(original_filename, new_locale):
    """Returns a filename derived from original_filename, using new_locale as the locale"""
    orig_dir = os.path.dirname(original_filename)
    msgs_dir = os.path.basename(orig_dir)
    orig_file = os.path.basename(original_filename)
    return os.path.abspath(os.path.join(orig_dir, '../..', new_locale, msgs_dir, orig_file))

if __name__ == '__main__':
    # required arg: file
    if len(sys.argv) < 2:
        raise Exception("missing file argument")
    # optional arg: locale
    if len(sys.argv) < 3:
        locale = CONFIGURATION.get_dummy_locale()
    else:
        locale = sys.argv[2]
    main(sys.argv[1], locale)