Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Michael Bestas | 1ab959b | 2014-07-26 16:01:01 +0300 | [diff] [blame] | 3 | # crowdin_sync.py |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 4 | # |
| 5 | # Updates Crowdin source translations and pushes translations |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 6 | # directly to LineageOS' Gerrit. |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 7 | # |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 8 | # Copyright (C) 2014-2015 The CyanogenMod Project |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 9 | # |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | # you may not use this file except in compliance with the License. |
| 12 | # You may obtain a copy of the License at |
| 13 | # |
| 14 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | # |
| 16 | # Unless required by applicable law or agreed to in writing, software |
| 17 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | # See the License for the specific language governing permissions and |
| 20 | # limitations under the License. |
| 21 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 22 | # ################################# IMPORTS ################################## # |
| 23 | |
| 24 | from __future__ import print_function |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 25 | |
| 26 | import argparse |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 27 | import git |
| 28 | import os |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 29 | import subprocess |
| 30 | import sys |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 31 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 32 | from xml.dom import minidom |
| 33 | |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 34 | # ################################# GLOBALS ################################## # |
| 35 | |
| 36 | _DIR = os.path.dirname(os.path.realpath(__file__)) |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 37 | _COMMITS_CREATED = False |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 38 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 39 | # ################################ FUNCTIONS ################################# # |
| 40 | |
| 41 | |
| 42 | def run_subprocess(cmd, silent=False): |
| 43 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 44 | universal_newlines=True) |
| 45 | comm = p.communicate() |
| 46 | exit_code = p.returncode |
| 47 | if exit_code != 0 and not silent: |
| 48 | print("There was an error running the subprocess.\n" |
| 49 | "cmd: %s\n" |
| 50 | "exit code: %d\n" |
| 51 | "stdout: %s\n" |
| 52 | "stderr: %s" % (cmd, exit_code, comm[0], comm[1]), |
| 53 | file=sys.stderr) |
| 54 | return comm, exit_code |
| 55 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 56 | |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame^] | 57 | def push_as_commit(base_path, path, name, branch, username): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 58 | print('Committing %s on branch %s' % (name, branch)) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 59 | |
| 60 | # Get path |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 61 | path = os.path.join(base_path, path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 62 | if not path.endswith('.git'): |
| 63 | path = os.path.join(path, '.git') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 64 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 65 | # Create repo object |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 66 | repo = git.Repo(path) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 67 | |
| 68 | # Remove previously deleted files from Git |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 69 | files = repo.git.ls_files(d=True).split('\n') |
| 70 | if files and files[0]: |
| 71 | repo.git.rm(files) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 72 | |
| 73 | # Add all files to commit |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 74 | repo.git.add('-A') |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 75 | |
| 76 | # Create commit; if it fails, probably empty so skipping |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 77 | try: |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame^] | 78 | repo.git.commit(m='Automatic translation import') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 79 | except: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 80 | print('Failed to create commit for %s, probably empty: skipping' |
| 81 | % name, file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 82 | return |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 83 | |
| 84 | # Push commit |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 85 | try: |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 86 | repo.git.push('ssh://%s@review.lineageos.org:29418/%s' % (username, name), |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 87 | 'HEAD:refs/for/%s%%topic=translation' % branch) |
| 88 | print('Successfully pushed commit for %s' % name) |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 89 | except: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 90 | print('Failed to push commit for %s' % name, file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 91 | |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 92 | _COMMITS_CREATED = True |
| 93 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 94 | |
| 95 | def check_run(cmd): |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 96 | p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr) |
| 97 | ret = p.wait() |
| 98 | if ret != 0: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 99 | print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr) |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 100 | sys.exit(ret) |
| 101 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 102 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 103 | def find_xml(base_path): |
| 104 | for dp, dn, file_names in os.walk(base_path): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 105 | for f in file_names: |
| 106 | if os.path.splitext(f)[1] == '.xml': |
| 107 | yield os.path.join(dp, f) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 108 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 109 | # ############################################################################ # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 110 | |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 111 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 112 | def parse_args(): |
| 113 | parser = argparse.ArgumentParser( |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 114 | description="Synchronising LineageOS' translations with Crowdin") |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 115 | parser.add_argument('-u', '--username', help='Gerrit username') |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 116 | parser.add_argument('-b', '--branch', help='LineageOS branch', |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 117 | required=True) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 118 | parser.add_argument('-c', '--config', help='Custom yaml config') |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 119 | parser.add_argument('--upload-sources', action='store_true', |
| 120 | help='Upload sources to Crowdin') |
| 121 | parser.add_argument('--upload-translations', action='store_true', |
| 122 | help='Upload translations to Crowdin') |
| 123 | parser.add_argument('--download', action='store_true', |
| 124 | help='Download translations from Crowdin') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 125 | return parser.parse_args() |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 126 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 127 | # ################################# PREPARE ################################## # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 128 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 129 | |
| 130 | def check_dependencies(): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 131 | # Check for Ruby version of crowdin-cli |
| 132 | cmd = ['gem', 'list', 'crowdin-cli', '-i'] |
| 133 | if run_subprocess(cmd, silent=True)[1] != 0: |
| 134 | print('You have not installed crowdin-cli.', file=sys.stderr) |
| 135 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 136 | return True |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 137 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 138 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 139 | def load_xml(x): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 140 | try: |
| 141 | return minidom.parse(x) |
| 142 | except IOError: |
| 143 | print('You have no %s.' % x, file=sys.stderr) |
| 144 | return None |
| 145 | except Exception: |
| 146 | # TODO: minidom should not be used. |
| 147 | print('Malformed %s.' % x, file=sys.stderr) |
| 148 | return None |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 149 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 150 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 151 | def check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 152 | for f in files: |
| 153 | if not os.path.isfile(f): |
| 154 | print('You have no %s.' % f, file=sys.stderr) |
| 155 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 156 | return True |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 157 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 158 | # ################################### MAIN ################################### # |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 159 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 160 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 161 | def upload_sources_crowdin(branch, config): |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 162 | if config: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 163 | print('\nUploading sources to Crowdin (custom config)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 164 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 165 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 166 | 'upload', 'sources', '--branch=%s' % branch]) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 167 | else: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 168 | print('\nUploading sources to Crowdin (AOSP supported languages)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 169 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 170 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 171 | 'upload', 'sources', '--branch=%s' % branch]) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 172 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 173 | print('\nUploading sources to Crowdin (non-AOSP supported languages)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 174 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 175 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 176 | 'upload', 'sources', '--branch=%s' % branch]) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 177 | |
| 178 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 179 | def upload_translations_crowdin(branch, config): |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 180 | if config: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 181 | print('\nUploading translations to Crowdin (custom config)') |
| 182 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 183 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 184 | 'upload', 'translations', '--branch=%s' % branch, |
| 185 | '--no-import-duplicates', '--import-eq-suggestions', |
| 186 | '--auto-approve-imported']) |
| 187 | else: |
| 188 | print('\nUploading translations to Crowdin ' |
| 189 | '(AOSP supported languages)') |
| 190 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 191 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 192 | 'upload', 'translations', '--branch=%s' % branch, |
| 193 | '--no-import-duplicates', '--import-eq-suggestions', |
| 194 | '--auto-approve-imported']) |
| 195 | |
| 196 | print('\nUploading translations to Crowdin ' |
| 197 | '(non-AOSP supported languages)') |
| 198 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 199 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 200 | 'upload', 'translations', '--branch=%s' % branch, |
| 201 | '--no-import-duplicates', '--import-eq-suggestions', |
| 202 | '--auto-approve-imported']) |
| 203 | |
| 204 | |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame^] | 205 | def download_crowdin(base_path, branch, xml, username, config): |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 206 | if config: |
| 207 | print('\nDownloading translations from Crowdin (custom config)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 208 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 209 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 210 | 'download', '--branch=%s' % branch]) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 211 | else: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 212 | print('\nDownloading translations from Crowdin ' |
| 213 | '(AOSP supported languages)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 214 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 215 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 216 | 'download', '--branch=%s' % branch]) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 217 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 218 | print('\nDownloading translations from Crowdin ' |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 219 | '(non-AOSP supported languages)') |
| 220 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 221 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 222 | 'download', '--branch=%s' % branch]) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 223 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 224 | print('\nRemoving useless empty translation files') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 225 | empty_contents = { |
| 226 | '<resources/>', |
| 227 | '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>', |
| 228 | ('<resources xmlns:android=' |
| 229 | '"http://schemas.android.com/apk/res/android"/>'), |
| 230 | ('<resources xmlns:android="http://schemas.android.com/apk/res/android"' |
| 231 | ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'), |
| 232 | ('<resources xmlns:tools="http://schemas.android.com/tools"' |
| 233 | ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>') |
| 234 | } |
| 235 | xf = None |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 236 | for xml_file in find_xml(base_path): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 237 | xf = open(xml_file).read() |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 238 | for line in empty_contents: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 239 | if line in xf: |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 240 | print('Removing ' + xml_file) |
| 241 | os.remove(xml_file) |
| 242 | break |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 243 | del xf |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 244 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 245 | print('\nCreating a list of pushable translations') |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 246 | # Get all files that Crowdin pushed |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 247 | paths = [] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 248 | if config: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 249 | files = ['%s/config/%s' % (_DIR, config)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 250 | else: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 251 | files = ['%s/config/%s.yaml' % (_DIR, branch), |
| 252 | '%s/config/%s_aosp.yaml' % (_DIR, branch)] |
Michael Bestas | 6c327e6 | 2015-05-02 01:58:01 +0300 | [diff] [blame] | 253 | for c in files: |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 254 | cmd = ['crowdin-cli', '--config=%s' % c, 'list', 'project', |
| 255 | '--branch=%s' % branch] |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 256 | comm, ret = run_subprocess(cmd) |
| 257 | if ret != 0: |
| 258 | sys.exit(ret) |
| 259 | for p in str(comm[0]).split("\n"): |
| 260 | paths.append(p.replace('/%s' % branch, '')) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 261 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 262 | print('\nUploading translations to Gerrit') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 263 | items = [x for sub in xml for x in sub.getElementsByTagName('project')] |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 264 | all_projects = [] |
| 265 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 266 | for path in paths: |
| 267 | path = path.strip() |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 268 | if not path: |
| 269 | continue |
| 270 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 271 | if "/res" not in path: |
| 272 | print('WARNING: Cannot determine project root dir of ' |
| 273 | '[%s], skipping.' % path) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 274 | continue |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 275 | result = path.split('/res')[0].strip('/') |
| 276 | if result == path.strip('/'): |
| 277 | print('WARNING: Cannot determine project root dir of ' |
| 278 | '[%s], skipping.' % path) |
| 279 | continue |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 280 | |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 281 | if result in all_projects: |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 282 | continue |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 283 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 284 | # When a project has multiple translatable files, Crowdin will |
| 285 | # give duplicates. |
| 286 | # We don't want that (useless empty commits), so we save each |
| 287 | # project in all_projects and check if it's already in there. |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 288 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 289 | |
Michael Bestas | 42e25e3 | 2016-03-12 20:18:39 +0200 | [diff] [blame] | 290 | # Search android/default.xml or config/%(branch)_extra_packages.xml |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 291 | # for the project's name |
| 292 | for project in items: |
| 293 | path = project.attributes['path'].value |
| 294 | if not (result + '/').startswith(path +'/'): |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 295 | continue |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 296 | if result != path: |
| 297 | if path in all_projects: |
| 298 | break |
| 299 | result = path |
| 300 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 301 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 302 | br = project.getAttribute('revision') or branch |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 303 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 304 | push_as_commit(base_path, result, |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame^] | 305 | project.getAttribute('name'), br, username) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 306 | break |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 307 | |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 308 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 309 | def main(): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 310 | args = parse_args() |
| 311 | default_branch = args.branch |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 312 | |
Michael Bestas | 602fab2 | 2018-11-14 23:14:33 +0200 | [diff] [blame] | 313 | base_path = os.getenv('CM_CROWDIN_BASE_PATH') |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 314 | if base_path is None: |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 315 | cwd = os.getcwd() |
Michael Bestas | 602fab2 | 2018-11-14 23:14:33 +0200 | [diff] [blame] | 316 | print('You have not set CM_CROWDIN_BASE_PATH. Defaulting to %s' % cwd) |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 317 | base_path = cwd |
| 318 | else: |
| 319 | base_path = os.path.join(os.path.realpath(base_path), default_branch) |
| 320 | if not os.path.isdir(base_path): |
Michael Bestas | 602fab2 | 2018-11-14 23:14:33 +0200 | [diff] [blame] | 321 | print('CM_CROWDIN_BASE_PATH + branch is not a real directory: %s' |
| 322 | % base_path) |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 323 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 324 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 325 | if not check_dependencies(): |
| 326 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 327 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 328 | xml_android = load_xml(x='%s/android/default.xml' % base_path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 329 | if xml_android is None: |
| 330 | sys.exit(1) |
| 331 | |
Michael Bestas | 42e25e3 | 2016-03-12 20:18:39 +0200 | [diff] [blame] | 332 | xml_extra = load_xml(x='%s/config/%s_extra_packages.xml' |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 333 | % (_DIR, default_branch)) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 334 | if xml_extra is None: |
| 335 | sys.exit(1) |
| 336 | |
Michael Bestas | 19dc335 | 2018-02-03 20:24:00 +0200 | [diff] [blame] | 337 | xml_snippet = load_xml(x='%s/android/snippets/lineage.xml' % base_path) |
| 338 | if xml_snippet is None: |
| 339 | xml_snippet = load_xml(x='%s/android/snippets/cm.xml' % base_path) |
| 340 | if xml_snippet is None: |
| 341 | xml_snippet = load_xml(x='%s/android/snippets/hal_cm_all.xml' % base_path) |
| 342 | if xml_snippet is not None: |
| 343 | xml_files = (xml_android, xml_snippet, xml_extra) |
Michael Bestas | 687679f | 2016-12-07 23:20:12 +0200 | [diff] [blame] | 344 | else: |
| 345 | xml_files = (xml_android, xml_extra) |
| 346 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 347 | if args.config: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 348 | files = ['%s/config/%s' % (_DIR, args.config)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 349 | else: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 350 | files = ['%s/config/%s.yaml' % (_DIR, default_branch), |
| 351 | '%s/config/%s_aosp.yaml' % (_DIR, default_branch)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 352 | if not check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 353 | sys.exit(1) |
| 354 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 355 | if args.download and args.username is None: |
| 356 | print('Argument -u/--username is required for translations download') |
| 357 | sys.exit(1) |
| 358 | |
| 359 | if args.upload_sources: |
| 360 | upload_sources_crowdin(default_branch, args.config) |
| 361 | if args.upload_translations: |
| 362 | upload_translations_crowdin(default_branch, args.config) |
| 363 | if args.download: |
Michael Bestas | 687679f | 2016-12-07 23:20:12 +0200 | [diff] [blame] | 364 | download_crowdin(base_path, default_branch, xml_files, |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame^] | 365 | args.username, args.config) |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 366 | |
| 367 | if _COMMITS_CREATED: |
| 368 | print('\nDone!') |
| 369 | sys.exit(0) |
Tom Powell | f42586f | 2016-07-11 11:02:54 -0700 | [diff] [blame] | 370 | else: |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 371 | print('\nNothing to commit') |
| 372 | sys.exit(-1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 373 | |
| 374 | if __name__ == '__main__': |
| 375 | main() |