aboutsummaryrefslogtreecommitdiff
path: root/checkcolor.py
blob: 9e62d3d78378b2b380ab32f6c1f99d5d34b1c444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python

#
# Copyright 2017, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Script used by developers to run hardcoded color check."""

import argparse
import errno
import os
import shutil
import subprocess
import sys

MAIN_DIRECTORY = os.path.normpath(os.path.dirname(__file__))
CHECKCOLOR_JAR = os.path.join(MAIN_DIRECTORY, 'checkcolor.jar')
LINT_BASELINE_FILENAME = 'color-check-baseline.xml'
COLOR_CHECK_README_PATH = 'prebuilts/checkcolor/README.md'
TMP_FOLDER = '/tmp/color'


def CopyProject(root):
  """Copy the project to tmp folder and remove the translation files.

  Args:
    root: Root folder of the project
  """
  cmd = 'cp -r {0}/* {1}/&& rm -r {1}/res/values-?? {1}/res/values-??-*'.format(root, TMP_FOLDER)
  os.system(cmd)

def Mkdir():
  """Create the tmp folder is necessary."""
  if os.path.exists(TMP_FOLDER):
    shutil.rmtree(TMP_FOLDER)
  os.makedirs(TMP_FOLDER)


def RunCheckOnProject(root):
  """Run color lint check on project.

  Args:
    root: Root folder of the project.
  Returns:
    exitcode and stdout
  """
  checkcolor_env = os.environ.copy()
  checkcolor_env['ANDROID_LINT_JARS'] = CHECKCOLOR_JAR

  Mkdir()
  CopyProject(root)

  try:
    baseline = os.path.join(root, LINT_BASELINE_FILENAME)
    check = subprocess.Popen(['lint', '--check', 'HardCodedColor', '--disable', 'LintError',
                              '--baseline', baseline, '--exitcode', TMP_FOLDER],
                             stdout=subprocess.PIPE, env=checkcolor_env)
    stdout, _ = check.communicate()
    exitcode = check.returncode
  except OSError as e:
    if e.errno == errno.ENOENT:
      print 'Error in color lint check'
      sys.exit(1)

  return (exitcode, stdout)


def main():
  """Run color lint check on all projects listed in parameter -p."""
  parser = argparse.ArgumentParser(description='Check hardcoded colors.')
  parser.add_argument('--project', '-p', nargs='+')
  args = parser.parse_args()
  exitcode = 0

  for rootdir in args.project:
    code, message = RunCheckOnProject(rootdir)
    if code != 0:
      exitcode = 1
      print message

  if exitcode == 1:
    print 'Please check link for more info:', COLOR_CHECK_README_PATH
  sys.exit(exitcode)


if __name__ == '__main__':
  main()