Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 1 | #!/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. |
| 16 | import logging |
| 17 | import os |
| 18 | import sys |
| 19 | |
| 20 | from acts.libs.proc import job |
| 21 | |
| 22 | COMMIT_ID_ENV_KEY = 'PREUPLOAD_COMMIT' |
| 23 | REPO_PATH_KEY = 'REPO_PATH' |
| 24 | GIT_COMMAND = 'git diff-tree --no-commit-id --name-only -r %s' |
Benny Peake | 9e5c231 | 2017-01-10 12:00:24 -0800 | [diff] [blame] | 25 | YAPF_COMMAND = 'yapf -d -p %s' |
| 26 | YAPF_OLD_COMMAND = 'yapf -d %s' |
Jack He | 9406390 | 2017-01-24 22:23:54 -0800 | [diff] [blame^] | 27 | YAPF_INPLACE_FORMAT = 'yapf -p -i %s' |
| 28 | YAPF_INPLACE_FORMAT_OLD = 'yapf -i %s' |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def 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() |
tturney | 7ac082f | 2017-01-04 12:41:24 -0800 | [diff] [blame] | 44 | full_files = [os.path.abspath(f) for f in files if f.endswith('.py')] |
| 45 | if not full_files: |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 46 | return |
| 47 | |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 48 | files_param_string = ' '.join(full_files) |
| 49 | |
| 50 | result = job.run(YAPF_COMMAND % files_param_string, ignore_status=True) |
Jack He | 9406390 | 2017-01-24 22:23:54 -0800 | [diff] [blame^] | 51 | yapf_inplace_format = YAPF_INPLACE_FORMAT |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 52 | 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 He | 9406390 | 2017-01-24 22:23:54 -0800 | [diff] [blame^] | 55 | yapf_inplace_format = YAPF_INPLACE_FORMAT_OLD |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 56 | |
| 57 | if result.stdout: |
| 58 | logging.error(result.stdout) |
| 59 | logging.error('INVALID FORMATTING.') |
Jack He | 9406390 | 2017-01-24 22:23:54 -0800 | [diff] [blame^] | 60 | logging.error('Consider run:') |
| 61 | logging.error(yapf_inplace_format % files_param_string) |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 62 | exit(1) |
| 63 | |
| 64 | |
| 65 | if __name__ == '__main__': |
| 66 | main(sys.argv[1:]) |