Ember Rose | ee59816 | 2022-02-03 15:31:18 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
jackqdyulei | 11b0281 | 2017-01-12 15:14:41 -0800 | [diff] [blame] | 2 | |
| 3 | # |
| 4 | # Copyright 2017, The Android Open Source Project |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
| 19 | """Script used by developers to run hardcoded color check.""" |
| 20 | |
Luca Stefani | dc6654a | 2020-02-09 13:32:31 +0100 | [diff] [blame] | 21 | from __future__ import print_function |
| 22 | |
jackqdyulei | 11b0281 | 2017-01-12 15:14:41 -0800 | [diff] [blame] | 23 | import argparse |
| 24 | import errno |
| 25 | import os |
| 26 | import shutil |
| 27 | import subprocess |
| 28 | import sys |
| 29 | |
| 30 | MAIN_DIRECTORY = os.path.normpath(os.path.dirname(__file__)) |
| 31 | CHECKCOLOR_JAR = os.path.join(MAIN_DIRECTORY, 'checkcolor.jar') |
| 32 | LINT_BASELINE_FILENAME = 'color-check-baseline.xml' |
| 33 | COLOR_CHECK_README_PATH = 'prebuilts/checkcolor/README.md' |
| 34 | TMP_FOLDER = '/tmp/color' |
| 35 | |
| 36 | |
| 37 | def CopyProject(root): |
| 38 | """Copy the project to tmp folder and remove the translation files. |
| 39 | |
| 40 | Args: |
| 41 | root: Root folder of the project |
| 42 | """ |
| 43 | cmd = 'cp -r {0}/* {1}/&& rm -r {1}/res/values-?? {1}/res/values-??-*'.format(root, TMP_FOLDER) |
| 44 | os.system(cmd) |
| 45 | |
| 46 | def Mkdir(): |
| 47 | """Create the tmp folder is necessary.""" |
| 48 | if os.path.exists(TMP_FOLDER): |
| 49 | shutil.rmtree(TMP_FOLDER) |
| 50 | os.makedirs(TMP_FOLDER) |
| 51 | |
| 52 | |
| 53 | def RunCheckOnProject(root): |
| 54 | """Run color lint check on project. |
| 55 | |
| 56 | Args: |
| 57 | root: Root folder of the project. |
| 58 | Returns: |
| 59 | exitcode and stdout |
| 60 | """ |
| 61 | checkcolor_env = os.environ.copy() |
| 62 | checkcolor_env['ANDROID_LINT_JARS'] = CHECKCOLOR_JAR |
| 63 | |
| 64 | Mkdir() |
| 65 | CopyProject(root) |
| 66 | |
| 67 | try: |
| 68 | baseline = os.path.join(root, LINT_BASELINE_FILENAME) |
jackqdyulei | 7894d58 | 2017-09-15 10:23:47 -0700 | [diff] [blame] | 69 | check = subprocess.Popen(['lint', '--check', 'HardCodedColor', '--disable', 'LintError', |
| 70 | '--baseline', baseline, '--exitcode', TMP_FOLDER], |
jackqdyulei | 11b0281 | 2017-01-12 15:14:41 -0800 | [diff] [blame] | 71 | stdout=subprocess.PIPE, env=checkcolor_env) |
| 72 | stdout, _ = check.communicate() |
| 73 | exitcode = check.returncode |
| 74 | except OSError as e: |
| 75 | if e.errno == errno.ENOENT: |
Luca Stefani | dc6654a | 2020-02-09 13:32:31 +0100 | [diff] [blame] | 76 | print('Error in color lint check') |
jackqdyulei | ec3da48 | 2017-09-18 17:05:12 -0700 | [diff] [blame] | 77 | if not os.environ.get('ANDROID_BUILD_TOP'): |
Luca Stefani | dc6654a | 2020-02-09 13:32:31 +0100 | [diff] [blame] | 78 | print('Try setting up your environment first:') |
| 79 | print(' source build/envsetup.sh && lunch <target>') |
jackqdyulei | 11b0281 | 2017-01-12 15:14:41 -0800 | [diff] [blame] | 80 | sys.exit(1) |
| 81 | |
| 82 | return (exitcode, stdout) |
| 83 | |
| 84 | |
| 85 | def main(): |
| 86 | """Run color lint check on all projects listed in parameter -p.""" |
| 87 | parser = argparse.ArgumentParser(description='Check hardcoded colors.') |
| 88 | parser.add_argument('--project', '-p', nargs='+') |
| 89 | args = parser.parse_args() |
| 90 | exitcode = 0 |
| 91 | |
| 92 | for rootdir in args.project: |
| 93 | code, message = RunCheckOnProject(rootdir) |
| 94 | if code != 0: |
| 95 | exitcode = 1 |
Luca Stefani | dc6654a | 2020-02-09 13:32:31 +0100 | [diff] [blame] | 96 | print(message) |
jackqdyulei | 11b0281 | 2017-01-12 15:14:41 -0800 | [diff] [blame] | 97 | |
| 98 | if exitcode == 1: |
Luca Stefani | dc6654a | 2020-02-09 13:32:31 +0100 | [diff] [blame] | 99 | print('Please check link for more info:', COLOR_CHECK_README_PATH) |
jackqdyulei | 11b0281 | 2017-01-12 15:14:41 -0800 | [diff] [blame] | 100 | sys.exit(exitcode) |
| 101 | |
| 102 | |
| 103 | if __name__ == '__main__': |
| 104 | main() |