mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +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 @@ from Resources import *
|
||||||
import os
|
import os
|
||||||
import macfs
|
import macfs
|
||||||
import sys
|
import sys
|
||||||
|
import py_resource
|
||||||
READ = 1
|
|
||||||
WRITE = 2
|
|
||||||
smAllScripts = -3
|
|
||||||
|
|
||||||
error = 'mkpycresourcefile.error'
|
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):
|
def mkpycresourcefile(src, dst):
|
||||||
"""Copy pyc file/dir src to resource file dst."""
|
"""Copy pyc file/dir src to resource file dst."""
|
||||||
|
|
||||||
if not os.path.isdir(src) and src[-4:] <> '.pyc':
|
if not os.path.isdir(src) and src[-4:] <> '.pyc':
|
||||||
raise error, 'I can only handle .pyc files or directories'
|
raise error, 'I can only handle .pyc files or directories'
|
||||||
handle, oid = createoutput(dst)
|
fsid = py_resource.create(dst)
|
||||||
if os.path.isdir(src):
|
if os.path.isdir(src):
|
||||||
id = handlesubdir(handle, oid, src)
|
handlesubdir(src)
|
||||||
else:
|
else:
|
||||||
id = handleonepycfile(handle, oid, src)
|
id, name = py_resource.frompycfile(src)
|
||||||
print 'Wrote',id-oid,'PYC resources to', dst
|
print 'Wrote %d: %s %s'%(id, name, src)
|
||||||
CloseResFile(handle)
|
CloseResFile(fsid)
|
||||||
|
|
||||||
def handleonepycfile(handle, id, file):
|
def handlesubdir(srcdir):
|
||||||
"""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):
|
|
||||||
"""Recursively scan a directory for pyc files and copy to resources"""
|
"""Recursively scan a directory for pyc files and copy to resources"""
|
||||||
print 'Directory', srcdir
|
|
||||||
src = os.listdir(srcdir)
|
src = os.listdir(srcdir)
|
||||||
for file in src:
|
for file in src:
|
||||||
file = os.path.join(srcdir, file)
|
file = os.path.join(srcdir, file)
|
||||||
if os.path.isdir(file):
|
if os.path.isdir(file):
|
||||||
id = handlesubdir(handle, id, file)
|
handlesubdir(file)
|
||||||
elif file[-4:] == '.pyc':
|
elif file[-4:] == '.pyc':
|
||||||
id = handleonepycfile(handle, id, file)
|
id, name = py_resource.frompycfile(file)
|
||||||
return id
|
print 'Wrote %d: %s %s'%(id, name, file)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
args = sys.argv[1:]
|
args = sys.argv[1:]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue