• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from Carbon.Res import *
2from Carbon.Resources import *
3import MacOS
4
5READ = 1
6WRITE = 2
7smAllScripts = -3
8
9def copyres(src, dst):
10    """Copy resource from src file to dst file."""
11
12    cur = CurResFile()
13    ctor, type = MacOS.GetCreatorAndType(src)
14    input = FSpOpenResFile(src, READ)
15    try:
16        FSpCreateResFile(dst, ctor, type, smAllScripts)
17    except:
18        raw_input("%s already exists...  CR to write anyway! " % dst)
19    output = FSpOpenResFile(dst, WRITE)
20    UseResFile(input)
21    ntypes = Count1Types()
22    for itype in range(1, 1+ntypes):
23        type = Get1IndType(itype)
24        nresources = Count1Resources(type)
25        for ires in range(1, 1+nresources):
26            res = Get1IndResource(type, ires)
27            res.LoadResource()
28            id, type, name = res.GetResInfo()
29            size = res.SizeResource()
30            attrs = res.GetResAttrs()
31            print id, type, name, size, hex(attrs)
32            res.DetachResource()
33            UseResFile(output)
34            try:
35                res2 = Get1Resource(type, id)
36            except (RuntimeError, Res.Error), msg:
37                res2 = None
38            if res2:
39                print "Duplicate type+id, not copied"
40                print (res2.size, res2.data)
41                print res2.GetResInfo()
42                if res2.HomeResFile() == output:
43                    'OK'
44                elif res2.HomeResFile() == input:
45                    'BAD!'
46                else:
47                    print 'Home:', res2.HomeResFile()
48            else:
49                res.AddResource(type, id, name)
50                #res.SetResAttrs(attrs)
51                res.WriteResource()
52            UseResFile(input)
53    UseResFile(cur)
54    CloseResFile(output)
55    CloseResFile(input)
56
57copyres('::python.�.rsrc', '::foo.rsrc')
58