File: old_compileall.py

package info (click to toggle)
python2.2 2.2.3dfsg-2sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,920 kB
  • ctags: 69,127
  • sloc: ansic: 219,839; python: 203,969; sh: 9,690; makefile: 3,468; perl: 3,454; lisp: 3,248; xml: 2,262; cpp: 106; sed: 2
file content (120 lines) | stat: -rw-r--r-- 3,880 bytes parent folder | download | duplicates (6)
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
# Routines to force "compilation" of all .py files in a directory
# tree or on sys.path.  By default recursion is pruned at a depth of
# 10 and the current directory, if it occurs in sys.path, is skipped.
# When called as a script, compiles argument directories, or sys.path
# if no arguments.
# After a similar module by Sjoerd Mullender.

# Addition by Gregor Hoffleit <flight@debian.org>:
# This version of compileall.py has been modified for Debian GNU/Linux
# to support quiet operation and removal of compiled .pyc and .pyo
# files (see the -q and -d options).
# There's a bug in the modified version that won't compile optimized
# (.pyo) files if there are .pyc versions. Therefore, if you want
# .pyo files, you have to run "python -O compileall.py" *before*
# "python compileall.py".

import os
import sys
import py_compile

def compile_dir(dir, maxlevels = 10, quiet = 0, delete = 0, destdir = None):
	if (not delete):
		if (not destdir):
			print 'Byte-compiling files in', dir, '...'
		else:
			print 'Byte-compiling files in', dir, ' for installation in', destdir, '...'
	else:
		print 'Removing unused byte-compiled files in', dir, '...'
	try:
		names = os.listdir(dir)
	except os.error:
		print "Can't list", dir
		names = []
	names.sort()
	for name in names:
		fullname = os.path.join(dir, name)
		if os.path.isfile(fullname):
			if (name[-4:] == '.pyc') or (name[-4:] == '.pyo'):
				head, tail = name[:-4], name[-4:]
				try:
					os.stat(os.path.join (dir, head + '.py'))
				except:
					if (not quiet): print fullname, 'removed, deleting %s' % tail
					if (delete):
						os.unlink (os.path.join (dir, head + tail))
			if (name[-3:] == '.py'):
				head, tail = name[:-3], name[-3:]
				spy = os.stat(os.path.join (dir, head + '.py'))[8]
				spyc = -1;
				try:
					spyc = os.stat(os.path.join (dir, head + '.pyc'))[8]
				except:
					pass
				if (spy < spyc):
					if (not quiet): print fullname, 'is current'
				else:
					if (not quiet): print 'Compiling', fullname, '...'
					try:
						if destdir == None:
							py_compile.compile(fullname)
						else:
							py_compile.compile(fullname, dest = os.path.join(destdir, name))
					except KeyboardInterrupt:
						del names[:]
						print '\n[interrupt]'
						break
					except:
						if type(sys.exc_type) == type(''):
							exc_type_name = sys.exc_type
						else: exc_type_name = sys.exc_type.__name__
						print 'Sorry:', exc_type_name + ':',
						print sys.exc_value
		elif maxlevels > 0 and \
			name != os.curdir and name != os.pardir and \
			os.path.isdir(fullname) and \
			not os.path.islink(fullname):
			if destdir == None:
				compile_dir(fullname, maxlevels - 1, quiet, delete)
			else:
				compile_dir(fullname, maxlevels - 1, quiet, delete, destdir = os.path.join(destdir, name))

def compile_path(skip_curdir = 1, quiet = 0, delete = 0):
	for dir in sys.path:
		if (not dir or dir == os.curdir) and skip_curdir:
			print 'Skipping current directory'
		else:
			compile_dir(dir, 0, quiet, delete)

def main():
	import getopt
	try:
		opts, args = getopt.getopt(sys.argv[1:], 'lqdi:')
	except getopt.error, msg:
		print msg
		print "usage: compileall [-l] [-q] [-d] [-i destdir] [directory ...]"
		print "-l: don't recurse down"
		print "-q: don't list files while compiling"
		print "-d: delete .pyc and .pyo files with no .py files"
		print "-i destdir: compile files for later installation in destdir"
		print "if no arguments, -l sys.path is assumed"
	maxlevels = 10
	quiet = 0
	delete = 0
	destdir = None
	for o, a in opts:
		if o == '-l': maxlevels = 0
		if o == '-q': quiet = 1
		if o == '-d': delete = 1
		if o == '-i': destdir = a
	if args:
		for dir in args:
			if destdir == None:
				compile_dir(dir, maxlevels, quiet, delete)
			else:
				compile_dir(dir, maxlevels, quiet, delete, destdir = destdir)
	else:
		compile_path(1, quiet)

if __name__ == '__main__':
	main()