~andrewsomething/ubuntu-dev-tools/665202

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python

# Copyright (c) 2010, Benjamin Drung <bdrung@ubuntu.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import magic
import optparse
import os
import sys

default_whitelisted_mimetypes = [
	"application/vnd.font-fontforge-sfd", # font source: fontforge
	"application/x-elc",
	"application/x-empty",
	"application/x-font-otf",             # font object and source
	"application/x-font-ttf",             # font object and source
	"application/x-font-woff",            # font object and source
	"application/x-symlink",
	"application/xml",
	"audio/x-wav",
	"font/otf",                           # font object and source
	"font/ttf",                           # font object and source
	"image/gif",
	"image/jpeg",
	"image/png",
	"image/svg+xml",
	"image/x-icns",
	"image/x-ico",
	"image/x-ms-bmp",
	"image/x-portable-pixmap",
	"message/rfc822",
	"text/html",
	"text/plain",
	"text/rtf",
	"text/troff",
	"text/x-asm",
	"text/x-c",
	"text/x-c++",
	"text/x-diff",
	"text/x-fortran",
	"text/x-java",
	"text/x-lisp",
	"text/x-m4",
	"text/x-makefile",
	"text/x-msdos-batch",
	"text/x-pascal",
	"text/x-perl",
	"text/x-php",
	"text/x-po",
	"text/x-shellscript",
	"text/x-tex",
	"text/x-texinfo",
]

default_whitelisted_extensions = [
	".fea",   # font source format: afdko (Adobe font development kit for OpenType)
	".fog",   # font source format: Fontographer
	".g2n",   # font source format: fontforge
	".gdh",   # font source format: Graphite (headers)
	".gdl",   # font source format: Graphite
	".glyph", # font source format: cross-toolkit UFO
	".plate", # font source format: Spiro
	".rsa",
	".sfd",   # font source format: fontforge
	".sfdir", # font source format: fontforge
	".ttx",   # font source format: fonttools
	".ufo",   # font source format: cross-toolkit UFO
	".vfb"    # font source format: FontLab
	".vtp",   # font source format: OpenType (VOLT)
	".xgf",   # font source format: Xgridfit
]

def main(whitelisted_mimetypes, whitelisted_extensions, directory, verbose=False):
	ms = magic.open(magic.MAGIC_MIME_TYPE)
	ms.load()

	for root, dirs, files in os.walk(directory):
		for f in files:
			mimetype = ms.file(os.path.join(root, f))
			if mimetype not in whitelisted_mimetypes:
				if not filter(lambda x: f.lower().endswith(x), whitelisted_extensions):
					if verbose:
						print "%s (%s)" % (os.path.join(root, f), mimetype)
					else:
						print os.path.join(root, f)
		for d in (".bzr", "CVS", ".git", ".svn"):
			if d in dirs:
				dirs.remove(d)

if __name__ == "__main__":
	script_name = os.path.basename(sys.argv[0])
	usage = "%s [options]" % (script_name)
	epilog = "See %s(1) for more info." % (script_name)
	parser = optparse.OptionParser(usage=usage, epilog=epilog)

	parser.add_option("-v", "--verbose", help="print more information",
			dest="verbose", action="store_true", default=False)
	parser.add_option("-d", "--directory",
			help="check the files in the specified directory",
			dest="directory", default=".")
	parser.add_option("-m", "--mimetype", metavar="MIMETYPE",
			help="Add MIMETYPE to list of whitelisted mimetypes.",
			dest="whitelisted_mimetypes", action="append",
			default=default_whitelisted_mimetypes)
	parser.add_option("-e", "--extension", metavar="EXTENSION",
			help="Add EXTENSION to list of whitelisted extensions.",
			dest="whitelisted_extensions", action="append",
			default=default_whitelisted_extensions)

	(options, args) = parser.parse_args()

	if len(args) != 0:
		print >> sys.stderr, "%s: This script does not take any additional parameters." % \
				(script_name)
		sys.exit(1)

	whitelisted_extensions = map(lambda x: x.lower(), options.whitelisted_extensions)
	main(options.whitelisted_mimetypes, whitelisted_extensions,
			options.directory, options.verbose)