blob: 72f2032adf98abd7a022d72580ed6c319da4b076 [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
7import macfs
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00008from Carbon import Res
Jack Jansen01c23091995-08-14 12:38:42 +00009import os
Jack Jansena8a277c1995-10-09 23:27:06 +000010from MACFS import *
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:
14 openrf = MacOS.openrf
15except AttributeError:
16 # Backward compatability
17 openrf = open
Jack Jansen01c23091995-08-14 12:38:42 +000018
19Error = 'macostools.Error'
20
Jack Jansen06033191996-03-12 13:33:34 +000021BUFSIZ=0x80000 # Copy in 0.5Mb chunks
Jack Jansen01c23091995-08-14 12:38:42 +000022
23#
24# Not guaranteed to be correct or stay correct (Apple doesn't tell you
25# how to do this), but it seems to work.
26#
Jack Jansen48f662d1997-08-08 15:00:59 +000027def mkalias(src, dst, relative=None):
Jack Jansen01c23091995-08-14 12:38:42 +000028 """Create a finder alias"""
29 srcfss = macfs.FSSpec(src)
30 dstfss = macfs.FSSpec(dst)
Jack Jansenb8fd1f11998-04-15 14:35:16 +000031 if relative:
32 relativefss = macfs.FSSpec(relative)
33 # ik mag er geen None in stoppen :-(
34 alias = srcfss.NewAlias(relativefss)
35 else:
36 alias = srcfss.NewAlias()
Jack Jansen01c23091995-08-14 12:38:42 +000037 srcfinfo = srcfss.GetFInfo()
38
39 Res.FSpCreateResFile(dstfss, srcfinfo.Creator, srcfinfo.Type, -1)
40 h = Res.FSpOpenResFile(dstfss, 3)
41 resource = Res.Resource(alias.data)
42 resource.AddResource('alis', 0, '')
43 Res.CloseResFile(h)
44
45 dstfinfo = dstfss.GetFInfo()
46 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
47 dstfss.SetFInfo(dstfinfo)
48
Jack Jansen423c7981995-08-31 13:40:03 +000049def mkdirs(dst):
50 """Make directories leading to 'dst' if they don't exist yet"""
51 if dst == '' or os.path.exists(dst):
52 return
53 head, tail = os.path.split(dst)
Jack Jansen423c7981995-08-31 13:40:03 +000054 if not ':' in head:
55 head = head + ':'
56 mkdirs(head)
57 os.mkdir(dst, 0777)
58
Jack Jansen3ff82a32001-02-14 17:06:32 +000059def touched(dst):
Jack Jansen57d53a91996-09-15 22:13:26 +000060 """Tell the finder a file has changed"""
61 file_fss = macfs.FSSpec(dst)
62 vRefNum, dirID, name = file_fss.as_tuple()
63 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
64 crdate, moddate, bkdate = dir_fss.GetDates()
65 now = time.time()
66 if now == moddate:
67 now = now + 1
68 dir_fss.SetDates(crdate, now, bkdate)
69
Jack Jansen3ff82a32001-02-14 17:06:32 +000070def touched_ae(dst):
Jack Jansen7e31f682001-02-09 15:58:34 +000071 """Tell the finder a file has changed"""
72 import Finder
73 f = Finder.Finder()
74 file_fss = macfs.FSSpec(dst)
75 vRefNum, dirID, name = file_fss.as_tuple()
76 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
77 f.update(dir_fss)
78
Jack Jansenc1463c982001-03-06 22:46:25 +000079def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen01c23091995-08-14 12:38:42 +000080 """Copy a file, including finder info, resource fork, etc"""
Jack Jansen423c7981995-08-31 13:40:03 +000081 if createpath:
82 mkdirs(os.path.split(dst)[0])
Jack Jansen01c23091995-08-14 12:38:42 +000083 srcfss = macfs.FSSpec(src)
84 dstfss = macfs.FSSpec(dst)
85
Jack Jansen423c7981995-08-31 13:40:03 +000086 ifp = open(srcfss.as_pathname(), 'rb')
87 ofp = open(dstfss.as_pathname(), 'wb')
Jack Jansen01c23091995-08-14 12:38:42 +000088 d = ifp.read(BUFSIZ)
89 while d:
90 ofp.write(d)
91 d = ifp.read(BUFSIZ)
92 ifp.close()
93 ofp.close()
94
Jack Jansen06033191996-03-12 13:33:34 +000095 ifp = openrf(srcfss.as_pathname(), '*rb')
96 ofp = openrf(dstfss.as_pathname(), '*wb')
Jack Jansen01c23091995-08-14 12:38:42 +000097 d = ifp.read(BUFSIZ)
98 while d:
99 ofp.write(d)
100 d = ifp.read(BUFSIZ)
101 ifp.close()
102 ofp.close()
103
104 sf = srcfss.GetFInfo()
105 df = dstfss.GetFInfo()
Jack Jansena8a277c1995-10-09 23:27:06 +0000106 df.Creator, df.Type = sf.Creator, sf.Type
Jack Jansenc1463c982001-03-06 22:46:25 +0000107 if forcetype != None:
108 df.Type = forcetype
Jack Jansena8a277c1995-10-09 23:27:06 +0000109 df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
Jack Jansen01c23091995-08-14 12:38:42 +0000110 dstfss.SetFInfo(df)
Jack Jansen57d53a91996-09-15 22:13:26 +0000111 if copydates:
112 crdate, mddate, bkdate = srcfss.GetDates()
113 dstfss.SetDates(crdate, mddate, bkdate)
114 touched(dstfss)
Jack Jansen01c23091995-08-14 12:38:42 +0000115
Jack Jansen57d53a91996-09-15 22:13:26 +0000116def copytree(src, dst, copydates=1):
Jack Jansen01c23091995-08-14 12:38:42 +0000117 """Copy a complete file tree to a new destination"""
118 if os.path.isdir(src):
Jack Jansen423c7981995-08-31 13:40:03 +0000119 mkdirs(dst)
Jack Jansen01c23091995-08-14 12:38:42 +0000120 files = os.listdir(src)
121 for f in files:
Jack Jansen57d53a91996-09-15 22:13:26 +0000122 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
Jack Jansen01c23091995-08-14 12:38:42 +0000123 else:
Jack Jansen57d53a91996-09-15 22:13:26 +0000124 copy(src, dst, 1, copydates)