fw4spl
filesize.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 
4 """
5 hooks to prevent adding too big binary file
6 
7 .gitconfig configuration :
8 
9 [fw4spl-hooks]
10  hooks = crlf tab digraphs doxygen
11 
12 example for 10MB limit :
13 [filesize-hook]
14  max-size = 10485760
15 
16 All files are checked by default. To check only binary files, use the type option :
17 [filesize-hook]
18  type = binary
19 
20 """
21 
22 import common
23 
24 WARNING = ('Attempt to commit or push too big file(s). '
25  'Limit is: %s bytes')
26 FILEWARN = (' - %s, %s > %s')
27 
28 
29 def filesize(files):
30  abort = False
31  limit = int(common.get_option('filesize-hook.max-size', default=1024 ** 2))
32  check_all_files = common.get_option('filesize-hook.type', "all").strip().lower() != "binary"
33  too_big_files = []
34 
35  common.note('Checking files size...')
36 
37  count = 0
38  for f in files:
39  check_file = check_all_files or common.binary(f.contents)
40 
41  if check_file:
42  common.trace('Checking ' + str(f.path) + ' size...')
43 
44  count += 1
45  if f.size > limit:
46  too_big_files.append(f)
47 
48  common.note('%d file(s) checked.' % count)
49 
50  if too_big_files:
51  common.error(WARNING % limit)
52  for f in too_big_files:
53  common.error(FILEWARN % (
54  f.path,
55  f.size,
56  limit
57  ))
58  abort = True
59  return abort
60 
61 
62 hooks = {
63  'filesize': filesize,
64 }
def error(msg)
Definition: common.py:52
def binary(s)
Definition: common.py:60
def trace(msg)
Definition: common.py:47
def note(msg)
Definition: common.py:43
def get_option(option, default, type="")
Definition: common.py:150