blob: 8e25d62099ce4c62e37f4d76cc89ccbd16d4e075 [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
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for 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',
33 '.dia',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070034 '.gif',
Dirk Pranked4a1f152021-11-03 12:37:42 -070035 '.graffle',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070036 '.ico',
37 '.jpg',
38 '.jpeg',
39 '.mp4',
Dirk Pranked4a1f152021-11-03 12:37:42 -070040 '.msi',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070041 '.pdf',
Dirk Pranked4a1f152021-11-03 12:37:42 -070042 'pdf', # Some files are missing the '.' :(.
Dirk Pranke7bbb5472021-11-02 16:33:21 -070043 '.png',
Dirk Pranked4a1f152021-11-03 12:37:42 -070044 'png', # Some files are missing the '.' :(.
Dirk Pranke7bbb5472021-11-02 16:33:21 -070045 '.PNG',
46 '.swf',
Dirk Pranked4a1f152021-11-03 12:37:42 -070047 '.svg',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070048 '.tar.gz',
49 '.tiff',
Dirk Pranked4a1f152021-11-03 12:37:42 -070050 '_trace',
Dirk Pranke7bbb5472021-11-02 16:33:21 -070051 '.webp',
52 '.xcf',
53 '.xlsx',
54 '.zip'
55 ]
56
57
Dirk Pranke1316af52021-11-02 12:45:39 -070058def CheckPatchFormatted(input_api, output_api):
59 return input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
60
61
62def CheckChangeHasDescription(input_api, output_api):
63 return input_api.canned_checks.CheckChangeHasDescription(
64 input_api, output_api)
Dirk Pranke7bbb5472021-11-02 16:33:21 -070065
66
67def CheckForLobs(input_api, output_api):
68 output_status = []
69 for file in input_api.change.AffectedFiles():
70 # The tar.gz for example prevents using a hashmap to look up the extension
71 for ext in LOB_EXTENSIONS:
72 if str(file).endswith(ext) and file.Action() != 'D':
73 error_msg = ('The file \'{file_name}\' is a binary that has not been '
74 'uploaded to GCE. Please run:\n\tscripts/upload_lobs.py '
75 '"{file_name}"\nand commit {file_name}.sha1 instead\n'
76 'Run:\n\tgit rm --cached "{file_name}"\nto remove the lob from git'
77 .format(file_name = file.LocalPath()))
78
79 error = output_api.PresubmitError(error_msg)
80 output_status.append(error)
81 break
82
83 return output_status
84
85
86def CheckLobIgnores(input_api, output_api):
87 output_status = []
88 with open("site/.gitignore", 'r') as ignore_file:
89 ignored_lobs = list(line.rstrip() for line in ignore_file.readlines())
90 ignored_lobs = set(ignored_lobs[
91 ignored_lobs.index('#start_lob_ignore') + 1 :
92 ignored_lobs.index('#end_lob_ignore')])
93
94 for ignored_lob in ignored_lobs:
95 lob_sha_file = os.path.join('site', ignored_lob + '.sha1')
96 if not lob_sha_file.startswith('#') and not os.path.exists(lob_sha_file):
97 error_msg = ('The sha1 file \'{removed_file}\' no longer exists, '
98 'please remove "{ignored_file}" from site/.gitignore'
99 .format(removed_file = lob_sha_file, ignored_file = ignored_lob))
100
101 error = output_api.PresubmitError(error_msg)
102 output_status.append(error)
103 return output_status