Deepanjan Roy | f1ba1dc | 2018-06-28 16:51:20 +0100 | [diff] [blame] | 1 | # Copyright (C) 2018 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | import time |
Deepanjan Roy | f1ba1dc | 2018-06-28 16:51:20 +0100 | [diff] [blame] | 17 | import subprocess |
Hector Dearman | c56c242 | 2018-07-03 09:50:03 +0100 | [diff] [blame] | 18 | from os.path import relpath |
| 19 | |
Deepanjan Roy | f1ba1dc | 2018-06-28 16:51:20 +0100 | [diff] [blame] | 20 | |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 21 | def RunAndReportIfLong(func, *args, **kargs): |
| 22 | start = time.time() |
| 23 | results = func(*args, **kargs) |
| 24 | end = time.time() |
| 25 | limit = 0.5 # seconds |
| 26 | name = func.__name__ |
| 27 | runtime = end - start |
| 28 | if runtime > limit: |
| 29 | print("{} took >{:.2}s ({:.2}s)".format(name, limit, runtime)) |
| 30 | return results |
| 31 | |
| 32 | |
Deepanjan Roy | f1ba1dc | 2018-06-28 16:51:20 +0100 | [diff] [blame] | 33 | def CheckChange(input, output): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 34 | results = [] |
Hector Dearman | 2cc71f6 | 2021-07-14 10:28:58 +0100 | [diff] [blame] | 35 | results += RunAndReportIfLong(CheckTslint, input, output) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 36 | return results |
Deepanjan Roy | f1ba1dc | 2018-06-28 16:51:20 +0100 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def CheckChangeOnUpload(input_api, output_api): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 40 | return CheckChange(input_api, output_api) |
Deepanjan Roy | f1ba1dc | 2018-06-28 16:51:20 +0100 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def CheckChangeOnCommit(input_api, output_api): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 44 | return CheckChange(input_api, output_api) |
Deepanjan Roy | f1ba1dc | 2018-06-28 16:51:20 +0100 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def CheckTslint(input_api, output_api): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 48 | path = input_api.os_path |
| 49 | ui_path = input_api.PresubmitLocalPath() |
| 50 | node = path.join(ui_path, 'node') |
| 51 | tslint = path.join(ui_path, 'node_modules', '.bin', 'tslint') |
Deepanjan Roy | f1ba1dc | 2018-06-28 16:51:20 +0100 | [diff] [blame] | 52 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 53 | if not path.exists(tslint): |
| 54 | repo_root = input_api.change.RepositoryRoot() |
| 55 | install_path = path.join(repo_root, 'tools', 'install-build-deps') |
| 56 | return [ |
| 57 | output_api.PresubmitError("Tslint not found. Please first run\n" + |
| 58 | "$ {0} --ui".format(install_path)) |
| 59 | ] |
Deepanjan Roy | acf8c07 | 2018-07-13 11:37:04 -0400 | [diff] [blame] | 60 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 61 | # Some tslint rules require type information and thus need the whole |
| 62 | # project. We therefore call tslint on the whole project instead of only the |
| 63 | # changed files. It is possible to break tslint on files that was not |
| 64 | # changed by changing the type of an object. |
| 65 | if subprocess.call( |
| 66 | [node, tslint, '--project', ui_path, '--format', 'codeFrame']): |
| 67 | return [ |
| 68 | output_api.PresubmitError("""\ |
Hector Dearman | c56c242 | 2018-07-03 09:50:03 +0100 | [diff] [blame] | 69 | There were tslint errors. You may be able to fix some of them using |
Florian Mayer | 6e407f8 | 2019-10-10 16:23:20 +0100 | [diff] [blame] | 70 | $ {} {} --project {} --fix |
| 71 | |
| 72 | If this is unexpected: did you remember to do a UI build before running the |
| 73 | presubmit?""".format(relpath(node), relpath(tslint), ui_path)) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 74 | ] |
| 75 | return [] |