bug 1396766, add ProjectFiles.match, r=stas
authorAxel Hecht <axel@pike.org>
Tue, 05 Sep 2017 11:42:43 +0200
changeset 318 4e52816a3e210b93d9a6db0cf3dd4a56cf182cb9
parent 315 43bdbd3bcf193d056862f9531e302b784398b2ac
child 319 90d69a0aae60eef880a96f052f2a4bd38247e231
push id100
push useraxel@mozilla.com
push dateThu, 07 Sep 2017 16:50:41 +0000
reviewersstas
bugs1396766
bug 1396766, add ProjectFiles.match, r=stas This api only works on the paths, and doesn't check if the files would actually exist. This is useful when files move in VCS history, and we want to check if it had a match in our project configs or not. MozReview-Commit-ID: lwBuszU5Yl
compare_locales/paths.py
compare_locales/tests/test_paths.py
--- a/compare_locales/paths.py
+++ b/compare_locales/paths.py
@@ -263,17 +263,17 @@ class ProjectConfig(object):
             key = key[3:]
         else:
             key = re.escape(key) + '$'
         rule['key'] = re.compile(key)
         yield rule
 
 
 class ProjectFiles(object):
-    '''Iterator object to get all files and tests for a locale and a
+    '''Iterable object to get all files and tests for a locale and a
     list of ProjectConfigs.
     '''
     def __init__(self, locale, projects, mergebase=None):
         self.locale = locale
         self.matchers = []
         self.mergebase = mergebase
         configs = []
         for project in projects:
@@ -370,16 +370,41 @@ class ProjectFiles(object):
                 yield base
             return
         for d, dirs, files in os.walk(base):
             for f in files:
                 p = mozpath.join(d, f)
                 if matcher.match(p):
                     yield p
 
+    def match(self, path):
+        '''Return the tuple of l10n_path, reference, mergepath, tests
+        if the given path matches any config, otherwise None.
+
+        This routine doesn't check that the files actually exist.
+        '''
+        for matchers in self.matchers:
+            matcher = matchers['l10n']
+            if matcher.match(path):
+                ref = merge = None
+                if 'reference' in matchers:
+                    ref = matcher.sub(matchers['reference'], path)
+                if 'merge' in matchers:
+                    merge = matcher.sub(matchers['merge'], path)
+                return path, ref, merge, matchers.get('test')
+            if 'reference' not in matchers:
+                continue
+            matcher = matchers['reference']
+            if matcher.match(path):
+                merge = None
+                l10n = matcher.sub(matchers['l10n'], path)
+                if 'merge' in matchers:
+                    merge = matcher.sub(matchers['merge'], path)
+                return l10n, path, merge, matchers.get('test')
+
 
 class ConfigNotFound(EnvironmentError):
     def __init__(self, path):
         super(ConfigNotFound, self).__init__(
             errno.ENOENT,
             'Configuration file not found',
             path)
 
--- a/compare_locales/tests/test_paths.py
+++ b/compare_locales/tests/test_paths.py
@@ -317,20 +317,27 @@ class TestProjectPaths(unittest.TestCase
             '/tmp/fr/': [
                 'good.ftl',
                 'not/subdir/bad.ftl'
             ],
         }
         files = MockProjectFiles(mocks, 'de', [cfg])
         self.assertListEqual(
             list(files), [('/tmp/de/good.ftl', None, None, set())])
+        self.assertTupleEqual(
+            files.match('/tmp/de/something.ftl'),
+            ('/tmp/de/something.ftl', None, None, set()))
+        self.assertIsNone(files.match('/tmp/fr/something.ftl'))
         files = MockProjectFiles(mocks, 'de', [cfg], mergebase='merging')
         self.assertListEqual(
             list(files),
             [('/tmp/de/good.ftl', None, 'merging/de/good.ftl', set())])
+        self.assertTupleEqual(
+            files.match('/tmp/de/something.ftl'),
+            ('/tmp/de/something.ftl', None, 'merging/de/something.ftl', set()))
         # 'fr' is not in the locale list, should return no files
         files = MockProjectFiles(mocks, 'fr', [cfg])
         self.assertListEqual(list(files), [])
 
     def test_reference_path(self):
         cfg = ProjectConfig()
         cfg.locales.append('de')
         cfg.add_paths({
@@ -356,25 +363,47 @@ class TestProjectPaths(unittest.TestCase
         self.assertListEqual(
             list(files),
             [
                 ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl', None,
                  set()),
                 ('/tmp/l10n/de/ref.ftl', '/tmp/reference/ref.ftl', None,
                  set()),
             ])
+        self.assertTupleEqual(
+            files.match('/tmp/l10n/de/good.ftl'),
+            ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl', None,
+             set()),
+            )
+        self.assertTupleEqual(
+            files.match('/tmp/reference/good.ftl'),
+            ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl', None,
+             set()),
+            )
+        self.assertIsNone(files.match('/tmp/l10n/de/subdir/bad.ftl'))
+        self.assertIsNone(files.match('/tmp/reference/subdir/bad.ftl'))
         files = MockProjectFiles(mocks, 'de', [cfg], mergebase='merging')
         self.assertListEqual(
             list(files),
             [
                 ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl',
                  'merging/de/good.ftl', set()),
                 ('/tmp/l10n/de/ref.ftl', '/tmp/reference/ref.ftl',
                  'merging/de/ref.ftl', set()),
             ])
+        self.assertTupleEqual(
+            files.match('/tmp/l10n/de/good.ftl'),
+            ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl',
+             'merging/de/good.ftl', set()),
+            )
+        self.assertTupleEqual(
+            files.match('/tmp/reference/good.ftl'),
+            ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl',
+             'merging/de/good.ftl', set()),
+            )
         # 'fr' is not in the locale list, should return no files
         files = MockProjectFiles(mocks, 'fr', [cfg])
         self.assertListEqual(list(files), [])
 
     def test_partial_l10n(self):
         cfg = ProjectConfig()
         cfg.locales.extend(['de', 'fr'])
         cfg.add_paths({
@@ -401,23 +430,28 @@ class TestProjectPaths(unittest.TestCase
         }
         files = MockProjectFiles(mocks, 'de', [cfg])
         self.assertListEqual(
             list(files),
             [
                 ('/tmp/de/major/good.ftl', None, None, set()),
                 ('/tmp/de/minor/good.ftl', None, None, set()),
             ])
+        self.assertTupleEqual(
+            files.match('/tmp/de/major/some.ftl'),
+            ('/tmp/de/major/some.ftl', None, None, set()))
+        self.assertIsNone(files.match('/tmp/de/other/some.ftl'))
         # 'fr' is not in the locale list of minor, should only return major
         files = MockProjectFiles(mocks, 'fr', [cfg])
         self.assertListEqual(
             list(files),
             [
                 ('/tmp/fr/major/good.ftl', None, None, set()),
             ])
+        self.assertIsNone(files.match('/tmp/fr/minor/some.ftl'))
 
 
 class TestProjectConfig(unittest.TestCase):
     def test_expand_paths(self):
         pc = ProjectConfig()
         pc.add_environment(one="first_path")
         self.assertEqual(pc.expand('foo'), 'foo')
         self.assertEqual(pc.expand('foo{one}bar'), 'foofirst_pathbar')