Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: cms/bin/translate.py

Issue 29329499: Issue 3246 - Strip empty locale files provided by Crowdin (Closed)
Patch Set: Created Oct. 29, 2015, 6:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import collections 18 import collections
19 import io 19 import io
20 import itertools 20 import itertools
21 import json 21 import json
22 import logging 22 import logging
23 import os 23 import os
24 import posixpath 24 import posixpath
25 import shutil 25 import re
26 import sys 26 import sys
27 import urllib 27 import urllib
28 import zipfile 28 import zipfile
29 29
30 import urllib3 30 import urllib3
31 31
32 import cms.utils 32 import cms.utils
33 from cms.sources import FileSource 33 from cms.sources import FileSource
34 34
35 logger = logging.getLogger("cms.bin.translate") 35 logger = logging.getLogger("cms.bin.translate")
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 with zipfile.ZipFile(io.BytesIO(response.data), "r") as archive: 245 with zipfile.ZipFile(io.BytesIO(response.data), "r") as archive:
246 locale_path = os.path.join(source_dir, "locales") 246 locale_path = os.path.join(source_dir, "locales")
247 # First clear existing translation files 247 # First clear existing translation files
248 for root, dirs, files in os.walk(locale_path, topdown=True): 248 for root, dirs, files in os.walk(locale_path, topdown=True):
249 if root == locale_path: 249 if root == locale_path:
250 dirs[:] = [d for d in dirs if d in required_locales] 250 dirs[:] = [d for d in dirs if d in required_locales]
251 for f in files: 251 for f in files:
252 if f.lower().endswith(".json"): 252 if f.lower().endswith(".json"):
253 os.remove(os.path.join(root, f)) 253 os.remove(os.path.join(root, f))
254 # Then extract the new ones in place 254 # Then extract the new ones in place
255 non_empty_file_regexp = re.compile(r"[^\s{}[\]]")
255 for member in archive.namelist(): 256 for member in archive.namelist():
256 path, file_name = posixpath.split(member) 257 path, file_name = posixpath.split(member)
257 ext = posixpath.splitext(file_name)[1] 258 ext = posixpath.splitext(file_name)[1]
258 path_parts = path.split(posixpath.sep) 259 path_parts = path.split(posixpath.sep)
259 locale, file_path = path_parts[0], path_parts[1:] 260 locale, file_path = path_parts[0], path_parts[1:]
260 if ext.lower() == ".json" and locale in inverted_required_locales: 261 if ext.lower() == ".json" and locale in inverted_required_locales:
261 output_path = os.path.join( 262 output_path = os.path.join(
262 locale_path, inverted_required_locales[locale], 263 locale_path, inverted_required_locales[locale],
263 *file_path + [file_name] 264 *file_path + [file_name]
264 ) 265 )
265 with archive.open(member) as source_file, \ 266 with archive.open(member) as source_file:
266 open(output_path, "wb") as target_file: 267 locale_file_contents = source_file.read()
267 shutil.copyfileobj(source_file, target_file) 268 if re.search(non_empty_file_regexp, locale_file_contents):
Wladimir Palant 2015/10/29 19:12:35 I don't think that using regular expressions on JS
kzar 2015/10/30 12:03:03 Done.
269 with open(output_path, "wb") as target_file:
270 target_file.write(locale_file_contents)
268 271
269 def crowdin_sync(source_dir, crowdin_api_key): 272 def crowdin_sync(source_dir, crowdin_api_key):
270 with FileSource(source_dir) as source: 273 with FileSource(source_dir) as source:
271 config = source.read_config() 274 config = source.read_config()
272 defaultlocale = config.get("general", "defaultlocale") 275 defaultlocale = config.get("general", "defaultlocale")
273 crowdin_project_name = config.get("general", "crowdin-project-name") 276 crowdin_project_name = config.get("general", "crowdin-project-name")
274 277
275 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name) 278 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name)
276 279
277 logger.info("Requesting project information...") 280 logger.info("Requesting project information...")
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 if __name__ == "__main__": 316 if __name__ == "__main__":
314 if len(sys.argv) < 3: 317 if len(sys.argv) < 3:
315 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]" 318 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]"
316 sys.exit(1) 319 sys.exit(1)
317 320
318 logging.basicConfig() 321 logging.basicConfig()
319 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) 322 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO)
320 323
321 source_dir, crowdin_api_key = sys.argv[1:3] 324 source_dir, crowdin_api_key = sys.argv[1:3]
322 crowdin_sync(source_dir, crowdin_api_key) 325 crowdin_sync(source_dir, crowdin_api_key)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld