Bug 1581185 - Fix relative paths in migration script generation. r=bgrins
☠☠ backed out by 00e37dda872b ☠ ☠
authorZibi Braniecki <zbraniecki@mozilla.com>
Wed, 18 Sep 2019 16:25:17 +0000
changeset 493860 5bb71874a9f31ee103f92c379042af757ffadb72
parent 493859 f68c48130f67c7464367f6fa76a1f05d2a6ef4f5
child 493861 86f77b5cc70529fa17047d956c5d5b84d22d912c
push id95722
push userzbraniecki@mozilla.com
push dateWed, 18 Sep 2019 16:26:40 +0000
treeherderautoland@86f77b5cc705 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins
bugs1581185
milestone71.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1581185 - Fix relative paths in migration script generation. r=bgrins Differential Revision: https://phabricator.services.mozilla.com/D45880
python/l10n/convert_xul_to_fluent/convert.py
python/l10n/convert_xul_to_fluent/lib/migration.py
python/l10n/convert_xul_to_fluent/lib/utils.py
python/l10n/convert_xul_to_fluent/lib/xul.py
--- a/python/l10n/convert_xul_to_fluent/convert.py
+++ b/python/l10n/convert_xul_to_fluent/convert.py
@@ -88,13 +88,13 @@ def main():
     if not dry_run:
         write_file(recipe_path, migration, data['mozilla-central'])
 
     ftl = build_ftl(messages, dtds, data)
 
     print('======== Fluent ========')
     print(ftl.encode("utf-8"))
     if not dry_run:
-        write_file(data['ftl'], ftl.encode("utf-8"), data['mozilla-central'])
+        write_file(data['ftl'], ftl.encode("utf-8"), data['mozilla-central'], append=True)
 
 
 if __name__ == '__main__':
     parse_inputs()
--- a/python/l10n/convert_xul_to_fluent/lib/migration.py
+++ b/python/l10n/convert_xul_to_fluent/lib/migration.py
@@ -1,13 +1,15 @@
 from __future__ import absolute_import
 
 
 def to_chrome_path(path):
-    return path.replace('/locales/en-US', '')
+    path = path.replace('/locales/en-US', '')
+    if path.startswith("./"):
+        path = path[2:]
     return path
 
 
 def get_dtd_path(name, dtds):
     return dtds[name[1:-1]]['file']
 
 
 def get_entity_name(s):
@@ -34,17 +36,17 @@ def make_header():
     res += 'TERM_REFERENCE, VARIABLE_REFERENCE\n'
     res += 'from fluent.migrate import COPY, CONCAT, REPLACE\n'
 
     return res
 
 
 def build_migration(messages, dtds, data):
     res = make_header()
-    desc = 'Bug {0} - {1}, part {{index}}'.format(
+    desc = 'Bug {0} - {1}, part {{index}}.'.format(
         data['bug_id'], data['description'])
     res += '\n\ndef migrate(ctx):\n    """{0}"""\n\n'.format(desc)
 
     for dtd_path in data['dtd']:
         res += "{0}ctx.maybe_add_localization('{1}')\n".format(
             ind(1), to_chrome_path(dtd_path))
 
     res += '\n'
--- a/python/l10n/convert_xul_to_fluent/lib/utils.py
+++ b/python/l10n/convert_xul_to_fluent/lib/utils.py
@@ -5,14 +5,15 @@ import os
 def read_file(path, base_path=None):
     if base_path is not None:
         path = os.path.join(base_path, path)
         path = path.replace('\\', '/')
     with open(path) as fptr:
         return fptr.read()
 
 
-def write_file(path, text, base_path=None):
+def write_file(path, text, base_path=None, append=False):
     if base_path is not None:
         path = os.path.join(base_path, path)
         path = path.replace('\\', '/')
-    with open(path, "w") as text_file:
+    mode = "a" if append and os.path.exists(path) else "w"
+    with open(path, mode) as text_file:
         text_file.write(text)
--- a/python/l10n/convert_xul_to_fluent/lib/xul.py
+++ b/python/l10n/convert_xul_to_fluent/lib/xul.py
@@ -39,16 +39,25 @@ def construct_l10n_id(val, attrs):
     if prefix:
         id = "{}-{}".format(prefix, id)
     return id
 
 
 vector = 0
 is_l10n = False
 
+def get_indent(pre_tag):
+    if "\n" not in pre_tag:
+        return " "
+    last_bl = pre_tag.rindex("\n")
+    indent = 0
+    for ch in pre_tag[last_bl:]:
+        if ch == " ":
+            indent += 1
+    return "\n" + " " * indent
 
 def tagrepl(m):
     global vector
     global is_l10n
     vector = 0
 
     is_l10n = False
     l10n_val = None
@@ -72,19 +81,21 @@ def tagrepl(m):
 
     tag = re.sub(attr_re, attrrepl, m.group(0))
     if is_l10n:
         l10n_id = construct_l10n_id(l10n_val, l10n_attrs)
         messages[l10n_id] = {
             "value": l10n_val,
             "attrs": l10n_attrs
         }
+        indent = get_indent(tag[0:len(m.group(1)) + 1 - vector])
         tag = \
             tag[0:len(m.group(1)) + 1 - vector] + \
-            ' data-l10n-id="' + \
+            indent + \
+            'data-l10n-id="' + \
             l10n_id + \
             '"' + \
             m.group(2) + \
             (m.group(3) if not l10n_val else "") + \
             tag[len(m.group(1)) + 1 + len(m.group(2)) +
                 len(m.group(3)) - vector:]
     return tag