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' |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 28 | |
| 29 | |
markdr | 6b82e9d | 2017-12-18 17:34:11 -0800 | [diff] [blame] | 30 | def main(): |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 31 | if COMMIT_ID_ENV_KEY not in os.environ: |
| 32 | logging.error('Missing commit id in environment.') |
| 33 | exit(1) |
| 34 | |
| 35 | if REPO_PATH_KEY not in os.environ: |
| 36 | logging.error('Missing repo path in environment.') |
| 37 | exit(1) |
| 38 | |
| 39 | commit_id = os.environ[COMMIT_ID_ENV_KEY] |
| 40 | full_git_command = GIT_COMMAND % commit_id |
| 41 | |
| 42 | files = job.run(full_git_command).stdout.splitlines() |
tturney | 7ac082f | 2017-01-04 12:41:24 -0800 | [diff] [blame] | 43 | full_files = [os.path.abspath(f) for f in files if f.endswith('.py')] |
| 44 | if not full_files: |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 45 | return |
| 46 | |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 47 | files_param_string = ' '.join(full_files) |
| 48 | |
| 49 | result = job.run(YAPF_COMMAND % files_param_string, ignore_status=True) |
Jack He | 9406390 | 2017-01-24 22:23:54 -0800 | [diff] [blame] | 50 | yapf_inplace_format = YAPF_INPLACE_FORMAT |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 51 | |
| 52 | if result.stdout: |
| 53 | logging.error(result.stdout) |
| 54 | logging.error('INVALID FORMATTING.') |
markdr | 6b82e9d | 2017-12-18 17:34:11 -0800 | [diff] [blame] | 55 | logging.error('Please run:\n' |
| 56 | '%s' % yapf_inplace_format % files_param_string) |
Benny Peake | 049ba03 | 2016-12-20 18:15:52 -0800 | [diff] [blame] | 57 | exit(1) |
| 58 | |
| 59 | |
| 60 | if __name__ == '__main__': |
markdr | 6b82e9d | 2017-12-18 17:34:11 -0800 | [diff] [blame] | 61 | main() |