File: freeze.py

package info (click to toggle)
jython 2.5.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 41,624 kB
  • ctags: 101,579
  • sloc: python: 351,444; java: 204,338; xml: 1,316; sh: 330; ansic: 126; perl: 114; makefile: 94
file content (114 lines) | stat: -rw-r--r-- 2,537 bytes parent folder | download | duplicates (2)
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
import Output, sys, os, string
from Freezer import Freezer

class YesNo: pass
class Required: pass

class Options:
	def __init__(self, opts, args=None):
		if args == None:
			args = sys.argv[1:]
		ret = {}
			
		index = 0
		while index < len(args):
			arg = args[index]
			if arg[0] == '-':
				name = arg[1:]
				value = opts[name]
				if value is YesNo:
					ret[name] = 1
					index = index+1
				else:
					ret[name] = args[index+1]
					index = index+2
				continue
			break
		
		for key, value in opts.items():
			if ret.has_key(key): continue
			elif value is YesNo:
				ret[key] = 0
			elif value is Required:
				raise ValueError, "argument %s is required" % key
			else:
				ret[key] = value
			
		for key, value in ret.items():
			setattr(self, key, value)
				
		self.args = args[index:]

opts = Options({'jar':None, 'cab':None, 'dir':None, 'core':YesNo, 'main':YesNo, 'deep':YesNo, 
			'lib':"", 'useproxy':YesNo})

outs = []
if opts.jar is not None:
	outs.append(Output.ZipOutput(opts.jar))

if opts.cab is not None:
	outs.append(Output.CabOutput(opts.cab))

if opts.dir is not None:
	outs.append(Output.DirectoryOutput(opts.dir))

if len(outs) > 1:
	raise ValueError, 'must specify only one of -jar, -cab, -dir'
	

directory = '.'
if len(opts.args) > 0:
	file = opts.args[0]
	path, name = os.path.split(file)
	if path != '':
		directory = path
		
if len(outs) == 0:
	outs.append(Output.DirectoryOutput(directory))


from java.util.zip import ZipFile

libs = string.split(opts.lib, os.pathsep)
classNames = {}
for lib in libs:
	if lib == '': continue
	#print 'lib: ', lib
	zf = ZipFile(lib)
	entries = zf.entries()
	while entries.hasMoreElements():
		entry = entries.nextElement()
		if entry.directory: continue
		name = string.join(string.split(entry.name, '/'), '.')[:-6]
		#print name
		classNames[name] = 1
		
def myFilter(name):
	if name[:5] == 'java.': return 0
	if name[:16] == 'org.python.core.': return 0
	if classNames.has_key(name): return 0
	return 1


#Prepend the current directory to the path
sys.path[0:0] = [directory]

#print opts.args
#sys.exit()
names = opts.args

f = Freezer(outs[0], not opts.deep, opts.useproxy, myFilter)
for name in names:
	name = os.path.split(name)[1]
	n, ext = os.path.splitext(name)
	if ext == '.py':
		name = n
	#print name
	f.freeze(name)
f.finish(opts.main)

if opts.core:
	skiplist = ['org.python.core.parser', 'org.python.core.BytecodeLoader', 'org.python.core.jython']
	f.addPackage(os.path.join(sys.prefix, 'JavaCode', 'org', 'python', 'core'), skiplist)

f.out.close()