blob: 3272cfc1be91df3170566caf46504597bff960f9 [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',
30 '.bz2',
31 '.crx',
32 '.dia',
33 '.msi',
34 '.gif',
35 '.ico',
36 '.jpg',
37 '.jpeg',
38 '.mp4',
39 '.pdf',
40 '.png',
41 '.PNG',
42 '.swf',
43 '.tar.gz',
44 '.tiff',
45 '.webp',
46 '.xcf',
47 '.xlsx',
48 '.zip'
49 ]
50
51
Dirk Pranke1316af52021-11-02 12:45:39 -070052def CheckPatchFormatted(input_api, output_api):
53 return input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
54
55
56def CheckChangeHasDescription(input_api, output_api):
57 return input_api.canned_checks.CheckChangeHasDescription(
58 input_api, output_api)
Dirk Pranke7bbb5472021-11-02 16:33:21 -070059
60
61def CheckForLobs(input_api, output_api):
62 output_status = []
63 for file in input_api.change.AffectedFiles():
64 # The tar.gz for example prevents using a hashmap to look up the extension
65 for ext in LOB_EXTENSIONS:
66 if str(file).endswith(ext) and file.Action() != 'D':
67 error_msg = ('The file \'{file_name}\' is a binary that has not been '
68 'uploaded to GCE. Please run:\n\tscripts/upload_lobs.py '
69 '"{file_name}"\nand commit {file_name}.sha1 instead\n'
70 'Run:\n\tgit rm --cached "{file_name}"\nto remove the lob from git'
71 .format(file_name = file.LocalPath()))
72
73 error = output_api.PresubmitError(error_msg)
74 output_status.append(error)
75 break
76
77 return output_status
78
79
80def CheckLobIgnores(input_api, output_api):
81 output_status = []
82 with open("site/.gitignore", 'r') as ignore_file:
83 ignored_lobs = list(line.rstrip() for line in ignore_file.readlines())
84 ignored_lobs = set(ignored_lobs[
85 ignored_lobs.index('#start_lob_ignore') + 1 :
86 ignored_lobs.index('#end_lob_ignore')])
87
88 for ignored_lob in ignored_lobs:
89 lob_sha_file = os.path.join('site', ignored_lob + '.sha1')
90 if not lob_sha_file.startswith('#') and not os.path.exists(lob_sha_file):
91 error_msg = ('The sha1 file \'{removed_file}\' no longer exists, '
92 'please remove "{ignored_file}" from site/.gitignore'
93 .format(removed_file = lob_sha_file, ignored_file = ignored_lob))
94
95 error = output_api.PresubmitError(error_msg)
96 output_status.append(error)
97 return output_status