1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import
6from __future__ import print_function
7from lib.xul import collect_messages
8from lib.dtd import get_dtds
9from lib.utils import read_file, write_file
10from lib.migration import build_migration
11from lib.fluent import build_ftl
12import json
13import argparse
14import sys
15# import re
16
17# To run this, you'll need to have lxml installed:
18#   `pip install lxml`
19
20# default migration directions
21data = {
22    'migration': 'python/l10n/fluent_migrations',
23    'description': 'Migrate l10n strings',
24    'prefix': ''
25}
26
27
28def parse_inputs():
29    sys_args = sys.argv[1:]
30
31    parser = argparse.ArgumentParser()
32    parser.add_argument('bug_id', type=str,
33                        help='Id number for bug tracking')
34    parser.add_argument('xul', type=str,
35                        help='POSIX path from mozilla-central to the XML to be updated')
36    parser.add_argument('ftl', type=str,
37                        help='Case sensitive POSIX path from mozilla-central to desired ftl file')
38    parser.add_argument('mozilla_central', type=str,
39                        help='Case sensitive absolute POSIX path to current mozilla-central repo')
40    parser.add_argument('dtd', type=str,
41                        help='comma delimited list of case sensitive POSIX dtd file paths')
42    parser.add_argument('description', type=str,
43                        help='string enclosed in quotes')
44    parser.add_argument('--dry-run', action='store_true',
45                        help='Tell if running dry run or not')
46    parser.add_argument('--prefix', type=str,
47                        help='a keyword string to be added to all l10n ids')
48
49    parsed_args = parser.parse_args(sys_args)
50
51    data['description'] = parsed_args.description
52    data['bug_id'] = parsed_args.bug_id
53    data['xul'] = parsed_args.xul
54    data['ftl'] = parsed_args.ftl
55    data['mozilla-central'] = parsed_args.mozilla_central
56    data['dtd'] = parsed_args.dtd.split(',')
57    data['dry-run'] = parsed_args.dry_run
58    data['prefix'] = parsed_args.prefix
59    data['recipe'] = "bug_{}_{}.py".format(data['bug_id'],
60                                           data['xul'].split('/')[-1].split('.')[0])
61
62    main()
63
64
65def main():
66    dry_run = data['dry-run']
67    dtds = get_dtds(data['dtd'], data['mozilla-central'])
68
69    print('======== DTDs ========')
70    print(json.dumps(dtds, sort_keys=True, indent=2))
71
72    s = read_file(data['xul'], data['mozilla-central'])
73
74    print('======== INPUT ========')
75    print(s)
76
77    print('======== OUTPUT ========')
78    (new_xul, messages) = collect_messages(s, data['prefix'])
79    print(new_xul)
80    if not dry_run:
81        write_file(data['xul'], new_xul, data['mozilla-central'])
82
83    print('======== L10N ========')
84
85    print(json.dumps(messages, sort_keys=True, indent=2))
86
87    migration = build_migration(messages, dtds, data)
88
89    print('======== MIGRATION ========')
90    print(migration)
91    recipe_path = "{}/{}".format(data['migration'], data['recipe'])
92    if not dry_run:
93        write_file(recipe_path, migration, data['mozilla-central'])
94
95    ftl = build_ftl(messages, dtds, data)
96
97    print('======== Fluent ========')
98    print(ftl.encode("utf-8"))
99    if not dry_run:
100        write_file(data['ftl'], ftl.encode("utf-8"), data['mozilla-central'], append=True)
101
102
103if __name__ == '__main__':
104    parse_inputs()
105