Mercurial > cpython-lru_cache-weakref
annotate Mac/Lib/py_resource.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | a94b0a122c98 |
| children | ff1a8a38f8c1 |
| rev | line source |
|---|---|
|
3656
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
1 """Creation of PYC resources""" |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
2 import os |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
3 import Res |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
4 import __builtin__ |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
5 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
6 READ = 1 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
7 WRITE = 2 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
8 smAllScripts = -3 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
9 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
10 def Pstring(str): |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
11 """Return a pascal-style string from a python-style string""" |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
12 if len(str) > 255: |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
13 raise ValueError, 'String too large' |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
14 return chr(len(str))+str |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
15 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
16 def create(dst, creator='Pyth'): |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
17 """Create output file. Return handle and first id to use.""" |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
18 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
19 try: |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
20 os.unlink(dst) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
21 except os.error: |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
22 pass |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
23 Res.FSpCreateResFile(dst, creator, 'rsrc', smAllScripts) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
24 return open(dst) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
25 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
26 def open(dst): |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
27 output = Res.FSpOpenResFile(dst, WRITE) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
28 Res.UseResFile(output) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
29 return output |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
30 |
|
5734
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
31 def writemodule(name, id, data, type='PYC ', preload=0): |
|
3656
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
32 """Write pyc code to a PYC resource with given name and id.""" |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
33 # XXXX Check that it doesn't exist |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
34 res = Res.Resource(data) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
35 res.AddResource(type, id, name) |
|
5734
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
36 if preload: |
|
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
37 attrs = res.GetResAttrs() |
|
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
38 attrs = attrs | 0x04 |
|
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
39 res.SetResAttrs(attrs) |
|
3656
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
40 res.WriteResource() |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
41 res.ReleaseResource() |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
42 |
|
5734
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
43 def frompycfile(file, name=None, preload=0): |
|
3656
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
44 """Copy one pyc file to the open resource file""" |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
45 if name == None: |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
46 d, name = os.path.split(file) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
47 name = name[:-4] |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
48 id = findfreeid() |
|
5734
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
49 writemodule(name, id, __builtin__.open(file, 'rb').read(), preload=preload) |
|
3656
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
50 return id, name |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
51 |
|
5734
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
52 def frompyfile(file, name=None, preload=0): |
|
3656
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
53 """Compile python source file to pyc file and add to resource file""" |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
54 import py_compile |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
55 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
56 py_compile.compile(file) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
57 file = file +'c' |
|
5734
a94b0a122c98
Added optional preload arg to some routines (which creates reloaded
Jack Jansen <jack.jansen@cwi.nl>
parents:
3656
diff
changeset
|
58 return frompycfile(file, name, preload=preload) |
|
3656
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
59 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
60 # XXXX Note this is incorrect, it only handles one type and one file.... |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
61 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
62 _firstfreeid = None |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
63 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
64 def findfreeid(type='PYC '): |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
65 """Find next free id-number for given resource type""" |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
66 global _firstfreeid |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
67 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
68 if _firstfreeid == None: |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
69 Res.SetResLoad(0) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
70 highest = 511 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
71 num = Res.Count1Resources(type) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
72 for i in range(1, num+1): |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
73 r = Res.Get1IndResource(type, i) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
74 id, d1, d2 = r.GetResInfo() |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
75 highest = max(highest, id) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
76 Res.SetResLoad(1) |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
77 _firstfreeid = highest+1 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
78 id = _firstfreeid |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
79 _firstfreeid = _firstfreeid+1 |
|
ade9f996590a
Module to handle PYC resources
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
80 return id |
