-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_sanity_check
executable file
·53 lines (49 loc) · 2.28 KB
/
binary_sanity_check
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
#!/usr/bin/python -B
# -*- coding: utf-8 -*-
###############################################################################
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
###############################################################################
#
# slic wants to avoid checking binary files. For speed, it does this at first
# using a filename extension check, and assumes that all extensions for which
# we have a configured set of comment markers are text. However, this
# configured list may be wrong, or some file extensions may be 'dual use'
# (like e.g. doc, which can be a Word document, or just a text file).
#
# Therefore, this script iterates over a tree of files, finds all those which
# slic would assume are not binary, and checks them explicitly for binaryness
# using the check in utils.py (which basically looks for UTF8-invalid bytes).
# The output can be used to update the 'force_binary_check' list of file
# extensions, which are those for which a binary check should always be done.
#
# In other words, if you are using slic on a new tree of files, you should
# run this script over it. If you see files in the list this outputs which
# _are_ binary, you should consider adding their file extensions to
# force_binary_check in slic.ini.
import utils
import config
import os
import sys
path = sys.argv[1]
path = os.path.abspath(path)
# The base config file is slic.ini in the script's directory
scriptpath = os.path.realpath(sys.argv[0])
slicinipath = os.path.join(os.path.dirname(scriptpath), "slic.ini")
config.read([slicinipath])
for root, dirs, files in os.walk(path):
# Avoid modify-in-place leading to off-by-one errors
# Stack Overflow 1207406
for file in files:
path = os.path.normpath(os.path.join(root, file))
ext = os.path.splitext(path)[1]
if config.has_option("ext_to_comment", ext) \
and not config.has_option("force_binary_check", ext):
binary = utils.is_binary(path)
try:
if binary and os.stat(path).st_size > 0:
print "%s" % path
except OSError:
# Dead symlink or the like
pass