blob: c9bf2c891981eaf97c5fa2c3cf5bd2f8574fd91b [file] [log] [blame]
Benny Peake049ba032016-12-20 18:15:52 -08001#!/usr/bin/env python3.4
2#
3# Copyright 2016 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16import logging
17import os
18import sys
19
20from acts.libs.proc import job
21
22COMMIT_ID_ENV_KEY = 'PREUPLOAD_COMMIT'
23REPO_PATH_KEY = 'REPO_PATH'
24GIT_COMMAND = 'git diff-tree --no-commit-id --name-only -r %s'
Benny Peake9e5c2312017-01-10 12:00:24 -080025YAPF_COMMAND = 'yapf -d -p %s'
26YAPF_OLD_COMMAND = 'yapf -d %s'
Jack He94063902017-01-24 22:23:54 -080027YAPF_INPLACE_FORMAT = 'yapf -p -i %s'
28YAPF_INPLACE_FORMAT_OLD = 'yapf -i %s'
Benny Peake049ba032016-12-20 18:15:52 -080029
30
31def main(argv):
32 if COMMIT_ID_ENV_KEY not in os.environ:
33 logging.error('Missing commit id in environment.')
34 exit(1)
35
36 if REPO_PATH_KEY not in os.environ:
37 logging.error('Missing repo path in environment.')
38 exit(1)
39
40 commit_id = os.environ[COMMIT_ID_ENV_KEY]
41 full_git_command = GIT_COMMAND % commit_id
42
43 files = job.run(full_git_command).stdout.splitlines()
tturney7ac082f2017-01-04 12:41:24 -080044 full_files = [os.path.abspath(f) for f in files if f.endswith('.py')]
45 if not full_files:
Benny Peake049ba032016-12-20 18:15:52 -080046 return
47
Benny Peake049ba032016-12-20 18:15:52 -080048 files_param_string = ' '.join(full_files)
49
50 result = job.run(YAPF_COMMAND % files_param_string, ignore_status=True)
Jack He94063902017-01-24 22:23:54 -080051 yapf_inplace_format = YAPF_INPLACE_FORMAT
Benny Peake049ba032016-12-20 18:15:52 -080052 if result.exit_status:
53 logging.warning('Using an old version of yapf. Please update soon.')
54 result = job.run(YAPF_OLD_COMMAND % files_param_string)
Jack He94063902017-01-24 22:23:54 -080055 yapf_inplace_format = YAPF_INPLACE_FORMAT_OLD
Benny Peake049ba032016-12-20 18:15:52 -080056
57 if result.stdout:
58 logging.error(result.stdout)
59 logging.error('INVALID FORMATTING.')
Jack He94063902017-01-24 22:23:54 -080060 logging.error('Consider run:')
61 logging.error(yapf_inplace_format % files_param_string)
Benny Peake049ba032016-12-20 18:15:52 -080062 exit(1)
63
64
65if __name__ == '__main__':
66 main(sys.argv[1:])