blob: 4026723f59466e4a6f25053f0a9ed92f0c325cec [file] [log] [blame]
Dirk Pranke1316af52021-11-02 12:45:39 -07001# 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 Pranked3d094a2022-02-01 15:16:55 -08007See http://www.chromium.org/developers/how-tos/depottools/presubmit-scripts
Dirk Pranke1316af52021-11-02 12:45:39 -07008for more details about the presubmit API built into depot_tools.
9"""
Dirk Pranke7bbb5472021-11-02 16:33:21 -070010
11import os
12
13
Dirk Pranke1316af52021-11-02 12:45:39 -070014PRESUBMIT_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.
18USE_PYTHON3 = True
19
20
Dirk Pranke7bbb5472021-11-02 16:33:21 -070021# 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
26LOB_EXTENSIONS = [
27 '.ai',
28 '.bin',
29 '.bmp',
Dirk Pranked4a1f152021-11-03 12:37:42 -070030 '.brd',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070031 '.bz2',
32 '.crx',
Dirk Pranke4a3489a2021-11-03 12:45:42 -070033 '.config',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070034 '.dia',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070035 '.gif',
Dirk Pranked4a1f152021-11-03 12:37:42 -070036 '.graffle',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070037 '.ico',
38 '.jpg',
Dirk Pranke4a3489a2021-11-03 12:45:42 -070039 'jpg', # Some files are missing the '.' :(.
Dirk Pranke7bbb5472021-11-02 16:33:21 -070040 '.jpeg',
41 '.mp4',
Dirk Pranked4a1f152021-11-03 12:37:42 -070042 '.msi',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070043 '.pdf',
Dirk Pranked4a1f152021-11-03 12:37:42 -070044 'pdf', # Some files are missing the '.' :(.
Dirk Pranke7bbb5472021-11-02 16:33:21 -070045 '.png',
Dirk Pranked4a1f152021-11-03 12:37:42 -070046 'png', # Some files are missing the '.' :(.
Dirk Pranke7bbb5472021-11-02 16:33:21 -070047 '.PNG',
48 '.swf',
Dirk Pranked4a1f152021-11-03 12:37:42 -070049 '.svg',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070050 '.tar.gz',
51 '.tiff',
Dirk Pranked4a1f152021-11-03 12:37:42 -070052 '_trace',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070053 '.webp',
54 '.xcf',
55 '.xlsx',
56 '.zip'
57 ]
58
59
Dirk Pranke1316af52021-11-02 12:45:39 -070060def CheckPatchFormatted(input_api, output_api):
61 return input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
62
63
64def CheckChangeHasDescription(input_api, output_api):
65 return input_api.canned_checks.CheckChangeHasDescription(
66 input_api, output_api)
Dirk Pranke7bbb5472021-11-02 16:33:21 -070067
68
69def 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
88def 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