/
crowdin_sync.py
97 lines (67 loc) · 3.32 KB
/
crowdin_sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from crowdin import crowdin_download_translations, crowdin_upload_sources, fix_product_name_tokens, get_crowdin_file_info
from datetime import datetime
from file_manager import get_crowdin_file, get_eligible_files, get_local_file, get_root_folders, get_translation_path
import git
from github import get_milestone_map, get_milestone_numbers, init_issues, update_translation_issues
import logging
import os
import re
from repository import initial_dir
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
# Retrieve the list of all files we should translate and all updated files that
# we need to re-translate.
# Update local copies of translations, translation memory, and glossaries.
def get_repository_state(repository, source_language, target_language):
global git_root
git_root = repository.github.git_root
logging.info('cd %s' % git_root)
os.chdir(git_root)
file_info = get_crowdin_file_info(repository, target_language)
all_files = [
get_local_file(repository, crowdin_file)
for crowdin_file, metadata in file_info.items()
if 'id' in metadata and crowdin_file.find(repository.crowdin.dest_folder) == 0
]
lstree_output = git.ls_tree('-r', '--name-only', repository.github.branch, strip=False)
branch_files = get_eligible_files(repository, lstree_output, source_language)
all_files.extend(branch_files)
all_files = sorted(set(all_files))
return all_files, all_files, file_info
def check_file_lists(repository, source_language, target_language, new_files, all_files):
for file in all_files:
if not os.path.exists(file):
continue
with open(file, 'r') as f:
file_content = f.read()
if re.search('@[^@ ]*-[^@ ]*@', file_content) is None:
continue
target_file = get_translation_path(file, source_language, target_language)
if not os.path.exists(target_file):
continue
with open(target_file, 'r') as f:
file_content = f.read()
if file_content.find('@<') == -1 and file_content.find('>@') == -1:
continue
if file not in new_files:
new_files.append(file)
for file in set(all_files) - set(new_files):
target_file = get_translation_path(file, source_language, target_language)
if not os.path.isfile(target_file):
new_files.append(file)
def update_repository(repository, source_language, target_language):
step_number = 1
logging.info('step %d: get repository state for translation download' % step_number)
step_number = step_number + 1
new_files, all_files, file_info = get_repository_state(repository, source_language, target_language)
logging.info('step %d: check for existing translations of %d files' % (step_number, len(all_files)))
step_number = step_number + 1
check_file_lists(repository, source_language, target_language, new_files, all_files)
logging.info('step %d: check for translations of %d source files' % (step_number, len(new_files)))
step_number = step_number + 1
crowdin_download_translations(repository, source_language, target_language, all_files, file_info)
logging.info('cd -')
os.chdir(initial_dir)
return new_files, all_files, file_info