1#!/usr/bin/env python3 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 21from __future__ import print_function 22 23import argparse 24import errno 25import os 26import shutil 27import subprocess 28import sys 29 30MAIN_DIRECTORY = os.path.normpath(os.path.dirname(__file__)) 31CHECKCOLOR_JAR = os.path.join(MAIN_DIRECTORY, 'checkcolor.jar') 32LINT_BASELINE_FILENAME = 'color-check-baseline.xml' 33COLOR_CHECK_README_PATH = 'prebuilts/checkcolor/README.md' 34TMP_FOLDER = '/tmp/color' 35 36 37def 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 46def 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 53def 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) 69 check = subprocess.Popen(['lint', '--check', 'HardCodedColor', '--disable', 'LintError', 70 '--baseline', baseline, '--exitcode', TMP_FOLDER], 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: 76 print('Error in color lint check') 77 if not os.environ.get('ANDROID_BUILD_TOP'): 78 print('Try setting up your environment first:') 79 print(' source build/envsetup.sh && lunch <target>') 80 sys.exit(1) 81 82 return (exitcode, stdout) 83 84 85def 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 96 print(message) 97 98 if exitcode == 1: 99 print('Please check link for more info:', COLOR_CHECK_README_PATH) 100 sys.exit(exitcode) 101 102 103if __name__ == '__main__': 104 main() 105