blob: 3644e7ec0766423a672ee097d2d31773a2fdf880 [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
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00007from Carbon import Res
Jack Jansen2b88dec2003-01-28 23:53:40 +00008from Carbon import File, Files
Jack Jansen01c23091995-08-14 12:38:42 +00009import os
Jack Jansen2b88dec2003-01-28 23:53:40 +000010import sys
Jack Jansen06033191996-03-12 13:33:34 +000011import MacOS
Jack Jansen57d53a91996-09-15 22:13:26 +000012import time
Jack Jansen06033191996-03-12 13:33:34 +000013try:
Jack Jansen2b88dec2003-01-28 23:53:40 +000014 openrf = MacOS.openrf
Jack Jansen06033191996-03-12 13:33:34 +000015except AttributeError:
Jack Jansen2b88dec2003-01-28 23:53:40 +000016 # Backward compatability
17 openrf = open
Jack Jansen01c23091995-08-14 12:38:42 +000018
19Error = 'macostools.Error'
20
Jack Jansen2b88dec2003-01-28 23:53:40 +000021BUFSIZ=0x80000 # Copy in 0.5Mb chunks
22
23COPY_FLAGS = (Files.kIsStationary|Files.kNameLocked|Files.kHasBundle|
24 Files.kIsInvisible|Files.kIsAlias)
Jack Jansen01c23091995-08-14 12:38:42 +000025
26#
27# Not guaranteed to be correct or stay correct (Apple doesn't tell you
28# how to do this), but it seems to work.
29#
Jack Jansen48f662d1997-08-08 15:00:59 +000030def mkalias(src, dst, relative=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000031 """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()
44
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)
51
52 dstfinfo = dstfss.FSpGetFInfo()
53 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
54 dstfss.FSpSetFInfo(dstfinfo)
55
Jack Jansen423c7981995-08-31 13:40:03 +000056def mkdirs(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000057 """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)
65
Jack Jansen3ff82a32001-02-14 17:06:32 +000066def touched(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000067 """Tell the finder a file has changed. No-op on MacOSX."""
68 if sys.platform != 'mac': return
Jack Jansen1c8501e2003-03-07 15:36:49 +000069 import warnings
70 warnings.filterwarnings("ignore", "macfs.*", DeprecationWarning, __name__)
Jack Jansen2b88dec2003-01-28 23:53:40 +000071 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
83
Jack Jansen3ff82a32001-02-14 17:06:32 +000084def touched_ae(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000085 """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))
92
Jack Jansenc1463c982001-03-06 22:46:25 +000093def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000094 """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])
99
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()
108
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()
117
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)
133
Jack Jansen57d53a91996-09-15 22:13:26 +0000134def copytree(src, dst, copydates=1):
Jack Jansen2b88dec2003-01-28 23:53:40 +0000135 """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)