blob: 395001ddc128ea83644ef548e331341c62699ef8 [file] [log] [blame]
Jack Jansen01c23091995-08-14 12:38:42 +00001"""macostools - Various utility functions for MacOS.
2
3mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src'
4copy(src, dst) - Full copy of 'src' to 'dst'
5"""
6
Benjamin Peterson23681932008-05-12 21:42:13 +00007from warnings import warnpy3k
Benjamin Petersona6864e02008-07-14 17:42:17 +00008warnpy3k("In 3.x, the macostools module is removed.", stacklevel=2)
Benjamin Peterson23681932008-05-12 21:42:13 +00009
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000010from Carbon import Res
Jack Jansen2b88dec2003-01-28 23:53:40 +000011from Carbon import File, Files
Jack Jansen01c23091995-08-14 12:38:42 +000012import os
Georg Brandl1f6176e2010-02-06 23:54:43 +000013import errno
Jack Jansen06033191996-03-12 13:33:34 +000014import MacOS
15try:
Jack Jansen2b88dec2003-01-28 23:53:40 +000016 openrf = MacOS.openrf
Jack Jansen06033191996-03-12 13:33:34 +000017except AttributeError:
Georg Brandl08c02db2005-07-22 18:39:19 +000018 # Backward compatibility
Jack Jansen2b88dec2003-01-28 23:53:40 +000019 openrf = open
Jack Jansen01c23091995-08-14 12:38:42 +000020
21Error = 'macostools.Error'
22
Jack Jansen2b88dec2003-01-28 23:53:40 +000023BUFSIZ=0x80000 # Copy in 0.5Mb chunks
24
25COPY_FLAGS = (Files.kIsStationary|Files.kNameLocked|Files.kHasBundle|
26 Files.kIsInvisible|Files.kIsAlias)
Jack Jansen01c23091995-08-14 12:38:42 +000027
28#
29# Not guaranteed to be correct or stay correct (Apple doesn't tell you
30# how to do this), but it seems to work.
31#
Jack Jansen48f662d1997-08-08 15:00:59 +000032def mkalias(src, dst, relative=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000033 """Create a finder alias"""
34 srcfsr = File.FSRef(src)
35 # The next line will fail under unix-Python if the destination
36 # doesn't exist yet. We should change this code to be fsref-based.
37 dstdir, dstname = os.path.split(dst)
38 if not dstdir: dstdir = os.curdir
39 dstdirfsr = File.FSRef(dstdir)
40 if relative:
41 relativefsr = File.FSRef(relative)
42 # ik mag er geen None in stoppen :-(
43 alias = File.FSNewAlias(relativefsr, srcfsr)
44 else:
45 alias = srcfsr.FSNewAliasMinimal()
Tim Peters182b5ac2004-07-18 06:16:08 +000046
47 dstfsr, dstfss = Res.FSCreateResourceFile(dstdirfsr, unicode(dstname),
Jack Jansen2b88dec2003-01-28 23:53:40 +000048 File.FSGetResourceForkName())
49 h = Res.FSOpenResourceFile(dstfsr, File.FSGetResourceForkName(), 3)
50 resource = Res.Resource(alias.data)
51 resource.AddResource('alis', 0, '')
52 Res.CloseResFile(h)
Tim Peters182b5ac2004-07-18 06:16:08 +000053
Jack Jansen2b88dec2003-01-28 23:53:40 +000054 dstfinfo = dstfss.FSpGetFInfo()
55 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
56 dstfss.FSpSetFInfo(dstfinfo)
Tim Peters182b5ac2004-07-18 06:16:08 +000057
Jack Jansen423c7981995-08-31 13:40:03 +000058def mkdirs(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000059 """Make directories leading to 'dst' if they don't exist yet"""
60 if dst == '' or os.path.exists(dst):
61 return
62 head, tail = os.path.split(dst)
63 if os.sep == ':' and not ':' in head:
64 head = head + ':'
65 mkdirs(head)
Ronald Oussorenabcc1682009-01-02 15:00:05 +000066
67 try:
68 os.mkdir(dst, 0777)
69 except OSError, e:
70 # be happy if someone already created the path
71 if e.errno != errno.EEXIST:
72 raise
73
Tim Peters182b5ac2004-07-18 06:16:08 +000074
Jack Jansen3ff82a32001-02-14 17:06:32 +000075def touched(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000076 """Tell the finder a file has changed. No-op on MacOSX."""
Jack Jansen1c8501e2003-03-07 15:36:49 +000077 import warnings
Brett Cannon5e263512007-05-20 23:17:38 +000078 warnings.warn("macostools.touched() has been deprecated",
79 DeprecationWarning, 2)
Tim Peters182b5ac2004-07-18 06:16:08 +000080
Jack Jansen3ff82a32001-02-14 17:06:32 +000081def touched_ae(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000082 """Tell the finder a file has changed"""
83 pardir = os.path.split(dst)[0]
84 if not pardir:
85 pardir = os.curdir
86 import Finder
87 f = Finder.Finder()
88 f.update(File.FSRef(pardir))
Tim Peters182b5ac2004-07-18 06:16:08 +000089
Jack Jansenc1463c982001-03-06 22:46:25 +000090def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000091 """Copy a file, including finder info, resource fork, etc"""
92 src = File.pathname(src)
93 dst = File.pathname(dst)
94 if createpath:
95 mkdirs(os.path.split(dst)[0])
Tim Peters182b5ac2004-07-18 06:16:08 +000096
Jack Jansen2b88dec2003-01-28 23:53:40 +000097 ifp = open(src, 'rb')
98 ofp = open(dst, 'wb')
99 d = ifp.read(BUFSIZ)
100 while d:
101 ofp.write(d)
102 d = ifp.read(BUFSIZ)
103 ifp.close()
104 ofp.close()
Tim Peters182b5ac2004-07-18 06:16:08 +0000105
Jack Jansen2b88dec2003-01-28 23:53:40 +0000106 ifp = openrf(src, '*rb')
107 ofp = openrf(dst, '*wb')
108 d = ifp.read(BUFSIZ)
109 while d:
110 ofp.write(d)
111 d = ifp.read(BUFSIZ)
112 ifp.close()
113 ofp.close()
Tim Peters182b5ac2004-07-18 06:16:08 +0000114
Jack Jansen2b88dec2003-01-28 23:53:40 +0000115 srcfss = File.FSSpec(src)
116 dstfss = File.FSSpec(dst)
117 sf = srcfss.FSpGetFInfo()
118 df = dstfss.FSpGetFInfo()
119 df.Creator, df.Type = sf.Creator, sf.Type
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000120 if forcetype is not None:
Jack Jansen2b88dec2003-01-28 23:53:40 +0000121 df.Type = forcetype
122 df.Flags = (sf.Flags & COPY_FLAGS)
123 dstfss.FSpSetFInfo(df)
124 if copydates:
125 srcfsr = File.FSRef(src)
126 dstfsr = File.FSRef(dst)
127 catinfo, _, _, _ = srcfsr.FSGetCatalogInfo(Files.kFSCatInfoAllDates)
128 dstfsr.FSSetCatalogInfo(Files.kFSCatInfoAllDates, catinfo)
Tim Peters182b5ac2004-07-18 06:16:08 +0000129
Jack Jansen57d53a91996-09-15 22:13:26 +0000130def copytree(src, dst, copydates=1):
Jack Jansen2b88dec2003-01-28 23:53:40 +0000131 """Copy a complete file tree to a new destination"""
132 if os.path.isdir(src):
133 mkdirs(dst)
134 files = os.listdir(src)
135 for f in files:
136 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
137 else:
138 copy(src, dst, 1, copydates)