mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Use py_resource module
This commit is contained in:
		
							parent
							
								
									0acb7f7a57
								
							
						
					
					
						commit
						eaa1959c07
					
				
					 1 changed files with 10 additions and 46 deletions
				
			
		|  | @ -6,69 +6,33 @@ | |||
| import os | ||||
| import macfs | ||||
| import sys | ||||
| 
 | ||||
| READ = 1 | ||||
| WRITE = 2 | ||||
| smAllScripts = -3 | ||||
| import py_resource | ||||
| 
 | ||||
| error = 'mkpycresourcefile.error' | ||||
| 
 | ||||
| def Pstring(str): | ||||
| 	if len(str) > 255: | ||||
| 		raise ValueError, 'String too large' | ||||
| 	return chr(len(str))+str | ||||
| 	 | ||||
| def createoutput(dst): | ||||
| 	"""Create output file. Return handle and first id to use.""" | ||||
| 	 | ||||
| 
 | ||||
| 	FSpCreateResFile(dst, 'Pyth', 'rsrc', smAllScripts) | ||||
| 	output = FSpOpenResFile(dst, WRITE) | ||||
| 	UseResFile(output) | ||||
| 	num = 128 | ||||
| 	return output, num | ||||
| 	 | ||||
| def writemodule(name, id, data): | ||||
| 	"""Write pyc code to a PYC resource with given name and id.""" | ||||
| 	# XXXX Check that it doesn't exist | ||||
| 	res = Resource(data) | ||||
| 	res.AddResource('PYC ', id, name) | ||||
| 	res.WriteResource() | ||||
| 	res.ReleaseResource() | ||||
| 		 | ||||
| def mkpycresourcefile(src, dst): | ||||
| 	"""Copy pyc file/dir src to resource file dst.""" | ||||
| 	 | ||||
| 	if not os.path.isdir(src) and src[-4:] <> '.pyc': | ||||
| 			raise error, 'I can only handle .pyc files or directories' | ||||
| 	handle, oid = createoutput(dst) | ||||
| 	fsid = py_resource.create(dst) | ||||
| 	if os.path.isdir(src): | ||||
| 		id = handlesubdir(handle, oid, src) | ||||
| 		handlesubdir(src) | ||||
| 	else: | ||||
| 		id = handleonepycfile(handle, oid, src) | ||||
| 	print 'Wrote',id-oid,'PYC resources to', dst | ||||
| 	CloseResFile(handle) | ||||
| 		id, name = py_resource.frompycfile(src) | ||||
| 		print 'Wrote %d: %s %s'%(id, name, src) | ||||
| 	CloseResFile(fsid) | ||||
| 			 | ||||
| def handleonepycfile(handle, id, file): | ||||
| 	"""Copy one pyc file to the open resource file""" | ||||
| 	d, name = os.path.split(file) | ||||
| 	name = name[:-4] | ||||
| 	print '  module', name | ||||
| 	writemodule(name, id, open(file, 'rb').read()) | ||||
| 	return id+1 | ||||
| 	 | ||||
| def handlesubdir(handle, id, srcdir): | ||||
| def handlesubdir(srcdir): | ||||
| 	"""Recursively scan a directory for pyc files and copy to resources""" | ||||
| 	print 'Directory', srcdir | ||||
| 	src = os.listdir(srcdir) | ||||
| 	for file in src: | ||||
| 		file = os.path.join(srcdir, file) | ||||
| 		if os.path.isdir(file): | ||||
| 			id = handlesubdir(handle, id, file) | ||||
| 			handlesubdir(file) | ||||
| 		elif file[-4:] == '.pyc': | ||||
| 			id = handleonepycfile(handle, id, file) | ||||
| 	return id | ||||
| 				 | ||||
| 			id, name = py_resource.frompycfile(file) | ||||
| 			print 'Wrote %d: %s %s'%(id, name, file) | ||||
| 	 | ||||
| if __name__ == '__main__': | ||||
| 	args = sys.argv[1:] | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Jack Jansen
						Jack Jansen