Issue #6644: Fix compile error on AIX.
[python.git] / Lib / plat-mac / macostools.py
blob337cc7f1f1d46c5d5693d160de2d5b358e7a8085
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 warnings import warnpy3k
8 warnpy3k("In 3.x, the macostools module is removed.", stacklevel=2)
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
20 Error = 'macostools.Error'
22 BUFSIZ=0x80000 # Copy in 0.5Mb chunks
24 COPY_FLAGS = (Files.kIsStationary|Files.kNameLocked|Files.kHasBundle|
25 Files.kIsInvisible|Files.kIsAlias)
28 # Not guaranteed to be correct or stay correct (Apple doesn't tell you
29 # how to do this), but it seems to work.
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()
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)
53 dstfinfo = dstfss.FSpGetFInfo()
54 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
55 dstfss.FSpSetFInfo(dstfinfo)
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)
66 try:
67 os.mkdir(dst, 0777)
68 except OSError, e:
69 # be happy if someone already created the path
70 if e.errno != errno.EEXIST:
71 raise
74 def touched(dst):
75 """Tell the finder a file has changed. No-op on MacOSX."""
76 import warnings
77 warnings.warn("macostools.touched() has been deprecated",
78 DeprecationWarning, 2)
80 def touched_ae(dst):
81 """Tell the finder a file has changed"""
82 pardir = os.path.split(dst)[0]
83 if not pardir:
84 pardir = os.curdir
85 import Finder
86 f = Finder.Finder()
87 f.update(File.FSRef(pardir))
89 def copy(src, dst, createpath=0, copydates=1, forcetype=None):
90 """Copy a file, including finder info, resource fork, etc"""
91 src = File.pathname(src)
92 dst = File.pathname(dst)
93 if createpath:
94 mkdirs(os.path.split(dst)[0])
96 ifp = open(src, 'rb')
97 ofp = open(dst, 'wb')
98 d = ifp.read(BUFSIZ)
99 while d:
100 ofp.write(d)
101 d = ifp.read(BUFSIZ)
102 ifp.close()
103 ofp.close()
105 ifp = openrf(src, '*rb')
106 ofp = openrf(dst, '*wb')
107 d = ifp.read(BUFSIZ)
108 while d:
109 ofp.write(d)
110 d = ifp.read(BUFSIZ)
111 ifp.close()
112 ofp.close()
114 srcfss = File.FSSpec(src)
115 dstfss = File.FSSpec(dst)
116 sf = srcfss.FSpGetFInfo()
117 df = dstfss.FSpGetFInfo()
118 df.Creator, df.Type = sf.Creator, sf.Type
119 if forcetype is not None:
120 df.Type = forcetype
121 df.Flags = (sf.Flags & COPY_FLAGS)
122 dstfss.FSpSetFInfo(df)
123 if copydates:
124 srcfsr = File.FSRef(src)
125 dstfsr = File.FSRef(dst)
126 catinfo, _, _, _ = srcfsr.FSGetCatalogInfo(Files.kFSCatInfoAllDates)
127 dstfsr.FSSetCatalogInfo(Files.kFSCatInfoAllDates, catinfo)
129 def copytree(src, dst, copydates=1):
130 """Copy a complete file tree to a new destination"""
131 if os.path.isdir(src):
132 mkdirs(dst)
133 files = os.listdir(src)
134 for f in files:
135 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
136 else:
137 copy(src, dst, 1, copydates)