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