|
1 """macostools - Various utility functions for MacOS. |
|
2 |
|
3 mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src' |
|
4 copy(src, dst) - Full copy of 'src' to 'dst' |
|
5 """ |
|
6 |
|
7 from warnings import warnpy3k |
|
8 warnpy3k("In 3.x, the macostools module is removed.", stacklevel=2) |
|
9 |
|
10 from Carbon import Res |
|
11 from Carbon import File, Files |
|
12 import os |
|
13 import MacOS |
|
14 try: |
|
15 openrf = MacOS.openrf |
|
16 except AttributeError: |
|
17 # Backward compatibility |
|
18 openrf = open |
|
19 |
|
20 Error = 'macostools.Error' |
|
21 |
|
22 BUFSIZ=0x80000 # Copy in 0.5Mb chunks |
|
23 |
|
24 COPY_FLAGS = (Files.kIsStationary|Files.kNameLocked|Files.kHasBundle| |
|
25 Files.kIsInvisible|Files.kIsAlias) |
|
26 |
|
27 # |
|
28 # Not guaranteed to be correct or stay correct (Apple doesn't tell you |
|
29 # how to do this), but it seems to work. |
|
30 # |
|
31 def mkalias(src, dst, relative=None): |
|
32 """Create a finder alias""" |
|
33 srcfsr = File.FSRef(src) |
|
34 # The next line will fail under unix-Python if the destination |
|
35 # doesn't exist yet. We should change this code to be fsref-based. |
|
36 dstdir, dstname = os.path.split(dst) |
|
37 if not dstdir: dstdir = os.curdir |
|
38 dstdirfsr = File.FSRef(dstdir) |
|
39 if relative: |
|
40 relativefsr = File.FSRef(relative) |
|
41 # ik mag er geen None in stoppen :-( |
|
42 alias = File.FSNewAlias(relativefsr, srcfsr) |
|
43 else: |
|
44 alias = srcfsr.FSNewAliasMinimal() |
|
45 |
|
46 dstfsr, dstfss = Res.FSCreateResourceFile(dstdirfsr, unicode(dstname), |
|
47 File.FSGetResourceForkName()) |
|
48 h = Res.FSOpenResourceFile(dstfsr, File.FSGetResourceForkName(), 3) |
|
49 resource = Res.Resource(alias.data) |
|
50 resource.AddResource('alis', 0, '') |
|
51 Res.CloseResFile(h) |
|
52 |
|
53 dstfinfo = dstfss.FSpGetFInfo() |
|
54 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag |
|
55 dstfss.FSpSetFInfo(dstfinfo) |
|
56 |
|
57 def mkdirs(dst): |
|
58 """Make directories leading to 'dst' if they don't exist yet""" |
|
59 if dst == '' or os.path.exists(dst): |
|
60 return |
|
61 head, tail = os.path.split(dst) |
|
62 if os.sep == ':' and not ':' in head: |
|
63 head = head + ':' |
|
64 mkdirs(head) |
|
65 os.mkdir(dst, 0777) |
|
66 |
|
67 def touched(dst): |
|
68 """Tell the finder a file has changed. No-op on MacOSX.""" |
|
69 import warnings |
|
70 warnings.warn("macostools.touched() has been deprecated", |
|
71 DeprecationWarning, 2) |
|
72 |
|
73 def touched_ae(dst): |
|
74 """Tell the finder a file has changed""" |
|
75 pardir = os.path.split(dst)[0] |
|
76 if not pardir: |
|
77 pardir = os.curdir |
|
78 import Finder |
|
79 f = Finder.Finder() |
|
80 f.update(File.FSRef(pardir)) |
|
81 |
|
82 def copy(src, dst, createpath=0, copydates=1, forcetype=None): |
|
83 """Copy a file, including finder info, resource fork, etc""" |
|
84 src = File.pathname(src) |
|
85 dst = File.pathname(dst) |
|
86 if createpath: |
|
87 mkdirs(os.path.split(dst)[0]) |
|
88 |
|
89 ifp = open(src, 'rb') |
|
90 ofp = open(dst, 'wb') |
|
91 d = ifp.read(BUFSIZ) |
|
92 while d: |
|
93 ofp.write(d) |
|
94 d = ifp.read(BUFSIZ) |
|
95 ifp.close() |
|
96 ofp.close() |
|
97 |
|
98 ifp = openrf(src, '*rb') |
|
99 ofp = openrf(dst, '*wb') |
|
100 d = ifp.read(BUFSIZ) |
|
101 while d: |
|
102 ofp.write(d) |
|
103 d = ifp.read(BUFSIZ) |
|
104 ifp.close() |
|
105 ofp.close() |
|
106 |
|
107 srcfss = File.FSSpec(src) |
|
108 dstfss = File.FSSpec(dst) |
|
109 sf = srcfss.FSpGetFInfo() |
|
110 df = dstfss.FSpGetFInfo() |
|
111 df.Creator, df.Type = sf.Creator, sf.Type |
|
112 if forcetype is not None: |
|
113 df.Type = forcetype |
|
114 df.Flags = (sf.Flags & COPY_FLAGS) |
|
115 dstfss.FSpSetFInfo(df) |
|
116 if copydates: |
|
117 srcfsr = File.FSRef(src) |
|
118 dstfsr = File.FSRef(dst) |
|
119 catinfo, _, _, _ = srcfsr.FSGetCatalogInfo(Files.kFSCatInfoAllDates) |
|
120 dstfsr.FSSetCatalogInfo(Files.kFSCatInfoAllDates, catinfo) |
|
121 |
|
122 def copytree(src, dst, copydates=1): |
|
123 """Copy a complete file tree to a new destination""" |
|
124 if os.path.isdir(src): |
|
125 mkdirs(dst) |
|
126 files = os.listdir(src) |
|
127 for f in files: |
|
128 copytree(os.path.join(src, f), os.path.join(dst, f), copydates) |
|
129 else: |
|
130 copy(src, dst, 1, copydates) |