crowdin_sync: Switch to new java crowdin cli tool

Change-Id: Id2efe3c387d8a1cc88e12211066d92ab535f22a7
diff --git a/README.mkdn b/README.mkdn
index 36ce518..44a1f4a 100644
--- a/README.mkdn
+++ b/README.mkdn
@@ -8,11 +8,11 @@
 
 Prerequisites
 -------------
-The Ruby variant of crowdin-cli >= 0.5.2 is required for this to work.
+The Java variant of crowdin is required for this to work.
 
-    \curl -sSL https://get.rvm.io | bash -s stable --ruby
-    source /home/your_username/.rvm/scripts/rvm # Add this to your .bashrc file!
-    rvm all do gem install crowdin-cli
+    wget -qO - https://artifacts.crowdin.com/repo/GPG-KEY-crowdin | sudo apt-key add -
+    echo 'deb https://artifacts.crowdin.com/repo/deb/ /' | sudo tee /etc/apt/sources.list.d/crowdin.list > /dev/null
+    sudo apt-get update && sudo apt-get install crowdin
 
 python-git is used for Git integration.
 
@@ -24,17 +24,21 @@
 Executing
 ---------
 Export the following environment variables to set the API keys and the base path.
-Base path should contain all LineageOS trees in subfolders, named after LineageOS branches:
+Each LineageOS version requires a different environment variable set for the base path that depends on the branch name:
 
-    export CM_CROWDIN_API_KEY=your_api_key
-    export CM_AOSP_CROWDIN_API_KEY=your_aosp_api_key
-    export CM_CROWDIN_BASE_PATH=your_base_path
+    export LINEAGE_CROWDIN_API_KEY=your_api_key
+    export LINEAGE_AOSP_CROWDIN_API_KEY=your_aosp_api_key
+    export LINEAGE_CROWDIN_BASE_PATH_CM_13_0=your_13.0_base_path
+    export LINEAGE_CROWDIN_BASE_PATH_CM_14_1=your_14.1_base_path
+    export LINEAGE_CROWDIN_BASE_PATH_LINEAGE_15_1=your_15.1_base_path
 
 Example:
 
-    export CM_CROWDIN_API_KEY=1234567890
-    export CM_AOSP_CROWDIN_API_KEY=0987654321
-    export CM_CROWDIN_BASE_PATH=/mnt/android/lineageos
+    export LINEAGE_CROWDIN_API_KEY=1234567890
+    export LINEAGE_AOSP_CROWDIN_API_KEY=0987654321
+    export LINEAGE_CROWDIN_BASE_PATH_CM_13_0=/mnt/android/lineageos/cm-13.0
+    export LINEAGE_CROWDIN_BASE_PATH_CM_14_1=/mnt/android/lineageos/cm-14.1
+    export LINEAGE_CROWDIN_BASE_PATH_LINEAGE_15_1=/mnt/android/lineageos/cm-15.1
 
 Execute:
 
diff --git a/config/cm-13.0.yaml b/config/cm-13.0.yaml
index 719fe4a..b29d403 100644
--- a/config/cm-13.0.yaml
+++ b/config/cm-13.0.yaml
@@ -2,8 +2,8 @@
 #
 # Crowdin configuration file for LineageOS
 #
-# Copyright (C) 2014-2015 The CyanogenMod Project
-# Copyright (C) 2017 The LineageOS Project
+# Copyright (C) 2014-2016 The CyanogenMod Project
+# Copyright (C) 2017-2018 The LineageOS Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -17,9 +17,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-api_key_env: CM_CROWDIN_API_KEY
-base_path_env: CM_CROWDIN_BASE_PATH
-base_path_contains_branch_subfolders: true
+api_key_env: LINEAGE_CROWDIN_API_KEY
+base_path_env: LINEAGE_CROWDIN_BASE_PATH_CM_13_0
 project_identifier: lineageos
 preserve_hierarchy: true
 
diff --git a/config/cm-13.0_aosp.yaml b/config/cm-13.0_aosp.yaml
index 9d421e1..afd8fc4 100644
--- a/config/cm-13.0_aosp.yaml
+++ b/config/cm-13.0_aosp.yaml
@@ -3,8 +3,8 @@
 # Crowdin configuration file for LineageOS'
 # additional languages not supported by AOSP
 #
-# Copyright (C) 2014-2015 The CyanogenMod Project
-# Copyright (C) 2017 The LineageOS Project
+# Copyright (C) 2014-2016 The CyanogenMod Project
+# Copyright (C) 2017-2018 The LineageOS Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -18,9 +18,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-api_key_env: CM_AOSP_CROWDIN_API_KEY
-base_path_env: CM_CROWDIN_BASE_PATH
-base_path_contains_branch_subfolders: true
+api_key_env: LINEAGE_AOSP_CROWDIN_API_KEY
+base_path_env: LINEAGE_CROWDIN_BASE_PATH_CM_13_0
 project_identifier: lineageos-aosp
 preserve_hierarchy: true
 
diff --git a/config/cm-14.1.yaml b/config/cm-14.1.yaml
index c58e105..aa95d3d 100644
--- a/config/cm-14.1.yaml
+++ b/config/cm-14.1.yaml
@@ -3,7 +3,7 @@
 # Crowdin configuration file for LineageOS
 #
 # Copyright (C) 2014-2016 The CyanogenMod Project
-# Copyright (C) 2017 The LineageOS Project
+# Copyright (C) 2017-2018 The LineageOS Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -17,9 +17,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-api_key_env: CM_CROWDIN_API_KEY
-base_path_env: CM_CROWDIN_BASE_PATH
-base_path_contains_branch_subfolders: true
+api_key_env: LINEAGE_CROWDIN_API_KEY
+base_path_env: LINEAGE_CROWDIN_BASE_PATH_CM_14_1
 project_identifier: lineageos
 preserve_hierarchy: true
 
diff --git a/config/cm-14.1_aosp.yaml b/config/cm-14.1_aosp.yaml
index 11b9b87..71558e6 100644
--- a/config/cm-14.1_aosp.yaml
+++ b/config/cm-14.1_aosp.yaml
@@ -4,7 +4,7 @@
 # additional languages not supported by AOSP
 #
 # Copyright (C) 2014-2016 The CyanogenMod Project
-# Copyright (C) 2017 The LineageOS Project
+# Copyright (C) 2017-2018 The LineageOS Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -18,9 +18,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-api_key_env: CM_AOSP_CROWDIN_API_KEY
-base_path_env: CM_CROWDIN_BASE_PATH
-base_path_contains_branch_subfolders: true
+api_key_env: LINEAGE_AOSP_CROWDIN_API_KEY
+base_path_env: LINEAGE_CROWDIN_BASE_PATH_CM_14_1
 project_identifier: lineageos-aosp
 preserve_hierarchy: true
 
diff --git a/config/lineage-15.1.yaml b/config/lineage-15.1.yaml
index 386a25b..0e90192 100644
--- a/config/lineage-15.1.yaml
+++ b/config/lineage-15.1.yaml
@@ -17,9 +17,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-api_key_env: CM_CROWDIN_API_KEY
-base_path_env: CM_CROWDIN_BASE_PATH
-base_path_contains_branch_subfolders: true
+api_key_env: LINEAGE_CROWDIN_API_KEY
+base_path_env: LINEAGE_CROWDIN_BASE_PATH_LINEAGE_15_1
 project_identifier: lineageos
 preserve_hierarchy: true
 
diff --git a/config/lineage-15.1_aosp.yaml b/config/lineage-15.1_aosp.yaml
index 7a56b6f..63e18a6 100644
--- a/config/lineage-15.1_aosp.yaml
+++ b/config/lineage-15.1_aosp.yaml
@@ -18,9 +18,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-api_key_env: CM_AOSP_CROWDIN_API_KEY
-base_path_env: CM_CROWDIN_BASE_PATH
-base_path_contains_branch_subfolders: true
+api_key_env: LINEAGE_AOSP_CROWDIN_API_KEY
+base_path_env: LINEAGE_CROWDIN_BASE_PATH_LINEAGE_15_1
 project_identifier: lineageos-aosp
 preserve_hierarchy: true
 
diff --git a/crowdin_sync.py b/crowdin_sync.py
index 99d2325..165596b 100755
--- a/crowdin_sync.py
+++ b/crowdin_sync.py
@@ -5,7 +5,8 @@
 # Updates Crowdin source translations and pushes translations
 # directly to LineageOS' Gerrit.
 #
-# Copyright (C) 2014-2015 The CyanogenMod Project
+# Copyright (C) 2014-2016 The CyanogenMod Project
+# Copyright (C) 2017-2018 The LineageOS Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -128,10 +129,10 @@
 
 
 def check_dependencies():
-    # Check for Ruby version of crowdin-cli
-    cmd = ['gem', 'list', 'crowdin-cli', '-i']
+    # Check for Java version of crowdin
+    cmd = ['dpkg-query', '-W', 'crowdin']
     if run_subprocess(cmd, silent=True)[1] != 0:
-        print('You have not installed crowdin-cli.', file=sys.stderr)
+        print('You have not installed crowdin.', file=sys.stderr)
         return False
     return True
 
@@ -161,17 +162,17 @@
 def upload_sources_crowdin(branch, config):
     if config:
         print('\nUploading sources to Crowdin (custom config)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s' % (_DIR, config),
                    'upload', 'sources', '--branch=%s' % branch])
     else:
         print('\nUploading sources to Crowdin (AOSP supported languages)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s.yaml' % (_DIR, branch),
                    'upload', 'sources', '--branch=%s' % branch])
 
         print('\nUploading sources to Crowdin (non-AOSP supported languages)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
                    'upload', 'sources', '--branch=%s' % branch])
 
@@ -179,7 +180,7 @@
 def upload_translations_crowdin(branch, config):
     if config:
         print('\nUploading translations to Crowdin (custom config)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s' % (_DIR, config),
                    'upload', 'translations', '--branch=%s' % branch,
                    '--no-import-duplicates', '--import-eq-suggestions',
@@ -187,7 +188,7 @@
     else:
         print('\nUploading translations to Crowdin '
               '(AOSP supported languages)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s.yaml' % (_DIR, branch),
                    'upload', 'translations', '--branch=%s' % branch,
                    '--no-import-duplicates', '--import-eq-suggestions',
@@ -195,7 +196,7 @@
 
         print('\nUploading translations to Crowdin '
               '(non-AOSP supported languages)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
                    'upload', 'translations', '--branch=%s' % branch,
                    '--no-import-duplicates', '--import-eq-suggestions',
@@ -205,19 +206,19 @@
 def download_crowdin(base_path, branch, xml, username, config):
     if config:
         print('\nDownloading translations from Crowdin (custom config)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s' % (_DIR, config),
                    'download', '--branch=%s' % branch])
     else:
         print('\nDownloading translations from Crowdin '
               '(AOSP supported languages)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s.yaml' % (_DIR, branch),
                    'download', '--branch=%s' % branch])
 
         print('\nDownloading translations from Crowdin '
               '(non-AOSP supported languages)')
-        check_run(['crowdin-cli',
+        check_run(['crowdin',
                    '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
                    'download', '--branch=%s' % branch])
 
@@ -230,7 +231,7 @@
         files = ['%s/config/%s.yaml' % (_DIR, branch),
                  '%s/config/%s_aosp.yaml' % (_DIR, branch)]
     for c in files:
-        cmd = ['crowdin-cli', '--config=%s' % c, 'list', 'project',
+        cmd = ['crowdin', '--config=%s' % c, 'list', 'project',
                '--branch=%s' % branch]
         comm, ret = run_subprocess(cmd)
         if ret != 0:
@@ -289,16 +290,15 @@
     args = parse_args()
     default_branch = args.branch
 
-    base_path = os.getenv('CM_CROWDIN_BASE_PATH')
+    base_path_branch_suffix = default_branch.replace('-', '_').replace('.', '_').upper()
+    base_path_env = 'LINEAGE_CROWDIN_BASE_PATH_%s' % base_path_branch_suffix
+    base_path = os.getenv(base_path_env)
     if base_path is None:
         cwd = os.getcwd()
-        print('You have not set CM_CROWDIN_BASE_PATH. Defaulting to %s' % cwd)
+        print('You have not set %s. Defaulting to %s' % (base_path_env, cwd))
         base_path = cwd
-    else:
-        base_path = os.path.join(os.path.realpath(base_path), default_branch)
     if not os.path.isdir(base_path):
-        print('CM_CROWDIN_BASE_PATH + branch is not a real directory: %s'
-              % base_path)
+        print('%s is not a real directory: %s' % (base_path_env, base_path))
         sys.exit(1)
 
     if not check_dependencies():