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