forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
third_party_size_check.py
77 lines (66 loc) · 2.58 KB
/
third_party_size_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# coding: utf-8
#
# Copyright 2018 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script that runs a size check in third-party folder and errors if
size limit is exceeded. The aim of this is to prevent us accidentally
breaching the 10k file limit on App Engine.
"""
import os
import sys
THIRD_PARTY_PATH = os.path.join(os.getcwd(), 'third_party')
THIRD_PARTY_SIZE_LIMIT = 7000
def _check_size_in_dir(dir_path):
"""Recursive method that checks the number of files inside the given
directory.
Args:
dir_path: str. The directory which files will be counted.
Returns:
The number of files inside the given directory.
"""
number_of_files_in_dir = 0
for name in os.listdir(dir_path):
if os.path.isfile(os.path.join(dir_path, name)):
number_of_files_in_dir += 1
else:
if os.path.isdir(os.path.join(dir_path, name)):
number_of_files_in_dir += _check_size_in_dir(
os.path.join(dir_path, name))
return number_of_files_in_dir
def _check_third_party_size():
"""Checks if the third-party size limit has been exceeded."""
number_of_files_in_third_party = _check_size_in_dir(THIRD_PARTY_PATH)
print ''
print '------------------------------------------------------'
print ' Number of files in third-party folder: %d' % (
number_of_files_in_third_party)
print ''
if number_of_files_in_third_party > THIRD_PARTY_SIZE_LIMIT:
print(
' ERROR: The third-party folder size exceeded the %d files'
' limit.' % THIRD_PARTY_SIZE_LIMIT)
print '------------------------------------------------------'
print ''
sys.exit(1)
else:
print ' The size of third-party folder is within the limits.'
print '------------------------------------------------------'
print ''
print 'Done!'
print ''
if __name__ == '__main__':
print 'Running third-party size check'
_check_third_party_size()
print 'Third-party folder size check passed.'
print ''