blob: 643ba67c08758be87eab284d9703c7afd9438fa5 [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
8warnpy3k("In 3.x, the macostools module is removed.")
9
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)
65 os.mkdir(dst, 0777)
Tim Peters182b5ac2004-07-18 06:16:08 +000066
Jack Jansen3ff82a32001-02-14 17:06:32 +000067def touched(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000068 """Tell the finder a file has changed. No-op on MacOSX."""
Jack Jansen1c8501e2003-03-07 15:36:49 +000069 import warnings
Brett Cannon5e263512007-05-20 23:17:38 +000070 warnings.warn("macostools.touched() has been deprecated",
71 DeprecationWarning, 2)
Tim Peters182b5ac2004-07-18 06:16:08 +000072
Jack Jansen3ff82a32001-02-14 17:06:32 +000073def touched_ae(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000074 """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))
Tim Peters182b5ac2004-07-18 06:16:08 +000081
Jack Jansenc1463c982001-03-06 22:46:25 +000082def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000083 """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])
Tim Peters182b5ac2004-07-18 06:16:08 +000088
Jack Jansen2b88dec2003-01-28 23:53:40 +000089 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()
Tim Peters182b5ac2004-07-18 06:16:08 +000097
Jack Jansen2b88dec2003-01-28 23:53:40 +000098 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()
Tim Peters182b5ac2004-07-18 06:16:08 +0000106
Jack Jansen2b88dec2003-01-28 23:53:40 +0000107 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
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000112 if forcetype is not None:
Jack Jansen2b88dec2003-01-28 23:53:40 +0000113 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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000121
Jack Jansen57d53a91996-09-15 22:13:26 +0000122def copytree(src, dst, copydates=1):
Jack Jansen2b88dec2003-01-28 23:53:40 +0000123 """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)