Dirk Pranke | 1316af5 | 2021-11-02 12:45:39 -0700 | [diff] [blame] | 1 | # Copyright (c) 2021 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Top-level presubmit script for the Git repo backing chromium.org. |
| 6 | |
Dirk Pranke | d3d094a | 2022-02-01 15:16:55 -0800 | [diff] [blame] | 7 | See http://www.chromium.org/developers/how-tos/depottools/presubmit-scripts |
Dirk Pranke | 1316af5 | 2021-11-02 12:45:39 -0700 | [diff] [blame] | 8 | for more details about the presubmit API built into depot_tools. |
| 9 | """ |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 10 | |
| 11 | import os |
| 12 | |
| 13 | |
Dirk Pranke | 1316af5 | 2021-11-02 12:45:39 -0700 | [diff] [blame] | 14 | PRESUBMIT_VERSION = '2.0.0' |
| 15 | |
| 16 | # This line is 'magic' in that git-cl looks for it to decide whether to |
| 17 | # use Python3 instead of Python2 when running the code in this file. |
| 18 | USE_PYTHON3 = True |
| 19 | |
| 20 | |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 21 | # This list must be kept in sync with the lists in //.eleventy.js and |
| 22 | # //scripts/upload_lobs.py. |
| 23 | # TODO(dpranke): Figure out how to share these lists to eliminate the |
| 24 | # duplication and need to keep them in sync. |
| 25 | |
| 26 | LOB_EXTENSIONS = [ |
| 27 | '.ai', |
| 28 | '.bin', |
| 29 | '.bmp', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 30 | '.brd', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 31 | '.bz2', |
| 32 | '.crx', |
Dirk Pranke | 4a3489a | 2021-11-03 12:45:42 -0700 | [diff] [blame] | 33 | '.config', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 34 | '.dia', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 35 | '.gif', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 36 | '.graffle', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 37 | '.ico', |
| 38 | '.jpg', |
Dirk Pranke | 4a3489a | 2021-11-03 12:45:42 -0700 | [diff] [blame] | 39 | 'jpg', # Some files are missing the '.' :(. |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 40 | '.jpeg', |
| 41 | '.mp4', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 42 | '.msi', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 43 | '.pdf', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 44 | 'pdf', # Some files are missing the '.' :(. |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 45 | '.png', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 46 | 'png', # Some files are missing the '.' :(. |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 47 | '.PNG', |
| 48 | '.swf', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 49 | '.svg', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 50 | '.tar.gz', |
| 51 | '.tiff', |
Dirk Pranke | d4a1f15 | 2021-11-03 12:37:42 -0700 | [diff] [blame] | 52 | '_trace', |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 53 | '.webp', |
| 54 | '.xcf', |
| 55 | '.xlsx', |
| 56 | '.zip' |
| 57 | ] |
| 58 | |
| 59 | |
Dirk Pranke | 1316af5 | 2021-11-02 12:45:39 -0700 | [diff] [blame] | 60 | def CheckPatchFormatted(input_api, output_api): |
| 61 | return input_api.canned_checks.CheckPatchFormatted(input_api, output_api) |
| 62 | |
| 63 | |
| 64 | def CheckChangeHasDescription(input_api, output_api): |
| 65 | return input_api.canned_checks.CheckChangeHasDescription( |
| 66 | input_api, output_api) |
Dirk Pranke | 7bbb547 | 2021-11-02 16:33:21 -0700 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def CheckForLobs(input_api, output_api): |
| 70 | output_status = [] |
| 71 | for file in input_api.change.AffectedFiles(): |
| 72 | # The tar.gz for example prevents using a hashmap to look up the extension |
| 73 | for ext in LOB_EXTENSIONS: |
| 74 | if str(file).endswith(ext) and file.Action() != 'D': |
| 75 | error_msg = ('The file \'{file_name}\' is a binary that has not been ' |
| 76 | 'uploaded to GCE. Please run:\n\tscripts/upload_lobs.py ' |
| 77 | '"{file_name}"\nand commit {file_name}.sha1 instead\n' |
| 78 | 'Run:\n\tgit rm --cached "{file_name}"\nto remove the lob from git' |
| 79 | .format(file_name = file.LocalPath())) |
| 80 | |
| 81 | error = output_api.PresubmitError(error_msg) |
| 82 | output_status.append(error) |
| 83 | break |
| 84 | |
| 85 | return output_status |
| 86 | |
| 87 | |
| 88 | def CheckLobIgnores(input_api, output_api): |
| 89 | output_status = [] |
| 90 | with open("site/.gitignore", 'r') as ignore_file: |
| 91 | ignored_lobs = list(line.rstrip() for line in ignore_file.readlines()) |
| 92 | ignored_lobs = set(ignored_lobs[ |
| 93 | ignored_lobs.index('#start_lob_ignore') + 1 : |
| 94 | ignored_lobs.index('#end_lob_ignore')]) |
| 95 | |
| 96 | for ignored_lob in ignored_lobs: |
| 97 | lob_sha_file = os.path.join('site', ignored_lob + '.sha1') |
| 98 | if not lob_sha_file.startswith('#') and not os.path.exists(lob_sha_file): |
| 99 | error_msg = ('The sha1 file \'{removed_file}\' no longer exists, ' |
| 100 | 'please remove "{ignored_file}" from site/.gitignore' |
| 101 | .format(removed_file = lob_sha_file, ignored_file = ignored_lob)) |
| 102 | |
| 103 | error = output_api.PresubmitError(error_msg) |
| 104 | output_status.append(error) |
| 105 | return output_status |