blob: 337cc7f1f1d46c5d5693d160de2d5b358e7a8085 [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
Jack Jansen06033191996-03-12 13:33:34 +000013import MacOS
14try:
Jack Jansen2b88dec2003-01-28 23:53:40 +000015 openrf = MacOS.openrf
Jack Jansen06033191996-03-12 13:33:34 +000016except AttributeError:
Georg Brandl08c02db2005-07-22 18:39:19 +000017 # Backward compatibility
Jack Jansen2b88dec2003-01-28 23:53:40 +000018 openrf = open
Jack Jansen01c23091995-08-14 12:38:42 +000019
20Error = 'macostools.Error'
21
Jack Jansen2b88dec2003-01-28 23:53:40 +000022BUFSIZ=0x80000 # Copy in 0.5Mb chunks
23
24COPY_FLAGS = (Files.kIsStationary|Files.kNameLocked|Files.kHasBundle|
25 Files.kIsInvisible|Files.kIsAlias)
Jack Jansen01c23091995-08-14 12:38:42 +000026
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#
Jack Jansen48f662d1997-08-08 15:00:59 +000031def mkalias(src, dst, relative=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000032 """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()
Tim Peters182b5ac2004-07-18 06:16:08 +000045
46 dstfsr, dstfss = Res.FSCreateResourceFile(dstdirfsr, unicode(dstname),
Jack Jansen2b88dec2003-01-28 23:53:40 +000047 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)
Tim Peters182b5ac2004-07-18 06:16:08 +000052
Jack Jansen2b88dec2003-01-28 23:53:40 +000053 dstfinfo = dstfss.FSpGetFInfo()
54 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
55 dstfss.FSpSetFInfo(dstfinfo)
Tim Peters182b5ac2004-07-18 06:16:08 +000056
Jack Jansen423c7981995-08-31 13:40:03 +000057def mkdirs(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000058 """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)
Ronald Oussorenabcc1682009-01-02 15:00:05 +000065
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
72
Tim Peters182b5ac2004-07-18 06:16:08 +000073
Jack Jansen3ff82a32001-02-14 17:06:32 +000074def touched(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000075 """Tell the finder a file has changed. No-op on MacOSX."""
Jack Jansen1c8501e2003-03-07 15:36:49 +000076 import warnings
Brett Cannon5e263512007-05-20 23:17:38 +000077 warnings.warn("macostools.touched() has been deprecated",
78 DeprecationWarning, 2)
Tim Peters182b5ac2004-07-18 06:16:08 +000079
Jack Jansen3ff82a32001-02-14 17:06:32 +000080def touched_ae(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000081 """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))
Tim Peters182b5ac2004-07-18 06:16:08 +000088
Jack Jansenc1463c982001-03-06 22:46:25 +000089def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000090 """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])
Tim Peters182b5ac2004-07-18 06:16:08 +000095
Jack Jansen2b88dec2003-01-28 23:53:40 +000096 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()
Tim Peters182b5ac2004-07-18 06:16:08 +0000104
Jack Jansen2b88dec2003-01-28 23:53:40 +0000105 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()
Tim Peters182b5ac2004-07-18 06:16:08 +0000113
Jack Jansen2b88dec2003-01-28 23:53:40 +0000114 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
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000119 if forcetype is not None:
Jack Jansen2b88dec2003-01-28 23:53:40 +0000120 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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000128
Jack Jansen57d53a91996-09-15 22:13:26 +0000129def copytree(src, dst, copydates=1):
Jack Jansen2b88dec2003-01-28 23:53:40 +0000130 """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)