blob: c6035b4052aa6bd6b04a4ab0f8cd53ffbd8465d0 [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
8import Res
9import 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
21FSSpecType = type(macfs.FSSpec(':'))
22
Jack Jansen06033191996-03-12 13:33:34 +000023BUFSIZ=0x80000 # Copy in 0.5Mb chunks
Jack Jansen01c23091995-08-14 12:38:42 +000024
25#
26# Not guaranteed to be correct or stay correct (Apple doesn't tell you
27# how to do this), but it seems to work.
28#
Jack Jansen48f662d1997-08-08 15:00:59 +000029def mkalias(src, dst, relative=None):
Jack Jansen01c23091995-08-14 12:38:42 +000030 """Create a finder alias"""
31 srcfss = macfs.FSSpec(src)
32 dstfss = macfs.FSSpec(dst)
Jack Jansenb8fd1f11998-04-15 14:35:16 +000033 if relative:
34 relativefss = macfs.FSSpec(relative)
35 # ik mag er geen None in stoppen :-(
36 alias = srcfss.NewAlias(relativefss)
37 else:
38 alias = srcfss.NewAlias()
Jack Jansen01c23091995-08-14 12:38:42 +000039 srcfinfo = srcfss.GetFInfo()
40
41 Res.FSpCreateResFile(dstfss, srcfinfo.Creator, srcfinfo.Type, -1)
42 h = Res.FSpOpenResFile(dstfss, 3)
43 resource = Res.Resource(alias.data)
44 resource.AddResource('alis', 0, '')
45 Res.CloseResFile(h)
46
47 dstfinfo = dstfss.GetFInfo()
48 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
49 dstfss.SetFInfo(dstfinfo)
50
Jack Jansen423c7981995-08-31 13:40:03 +000051def mkdirs(dst):
52 """Make directories leading to 'dst' if they don't exist yet"""
53 if dst == '' or os.path.exists(dst):
54 return
55 head, tail = os.path.split(dst)
Jack Jansen423c7981995-08-31 13:40:03 +000056 if not ':' in head:
57 head = head + ':'
58 mkdirs(head)
59 os.mkdir(dst, 0777)
60
Jack Jansen57d53a91996-09-15 22:13:26 +000061def touched(dst):
62 """Tell the finder a file has changed"""
63 file_fss = macfs.FSSpec(dst)
64 vRefNum, dirID, name = file_fss.as_tuple()
65 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
66 crdate, moddate, bkdate = dir_fss.GetDates()
67 now = time.time()
68 if now == moddate:
69 now = now + 1
70 dir_fss.SetDates(crdate, now, bkdate)
71
72def copy(src, dst, createpath=0, copydates=1):
Jack Jansen01c23091995-08-14 12:38:42 +000073 """Copy a file, including finder info, resource fork, etc"""
Jack Jansen423c7981995-08-31 13:40:03 +000074 if createpath:
75 mkdirs(os.path.split(dst)[0])
Jack Jansen01c23091995-08-14 12:38:42 +000076 srcfss = macfs.FSSpec(src)
77 dstfss = macfs.FSSpec(dst)
78
Jack Jansen423c7981995-08-31 13:40:03 +000079 ifp = open(srcfss.as_pathname(), 'rb')
80 ofp = open(dstfss.as_pathname(), 'wb')
Jack Jansen01c23091995-08-14 12:38:42 +000081 d = ifp.read(BUFSIZ)
82 while d:
83 ofp.write(d)
84 d = ifp.read(BUFSIZ)
85 ifp.close()
86 ofp.close()
87
Jack Jansen06033191996-03-12 13:33:34 +000088 ifp = openrf(srcfss.as_pathname(), '*rb')
89 ofp = openrf(dstfss.as_pathname(), '*wb')
Jack Jansen01c23091995-08-14 12:38:42 +000090 d = ifp.read(BUFSIZ)
91 while d:
92 ofp.write(d)
93 d = ifp.read(BUFSIZ)
94 ifp.close()
95 ofp.close()
96
97 sf = srcfss.GetFInfo()
98 df = dstfss.GetFInfo()
Jack Jansena8a277c1995-10-09 23:27:06 +000099 df.Creator, df.Type = sf.Creator, sf.Type
100 df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
Jack Jansen01c23091995-08-14 12:38:42 +0000101 dstfss.SetFInfo(df)
Jack Jansen57d53a91996-09-15 22:13:26 +0000102 if copydates:
103 crdate, mddate, bkdate = srcfss.GetDates()
104 dstfss.SetDates(crdate, mddate, bkdate)
105 touched(dstfss)
Jack Jansen01c23091995-08-14 12:38:42 +0000106
Jack Jansen57d53a91996-09-15 22:13:26 +0000107def copytree(src, dst, copydates=1):
Jack Jansen01c23091995-08-14 12:38:42 +0000108 """Copy a complete file tree to a new destination"""
109 if os.path.isdir(src):
Jack Jansen423c7981995-08-31 13:40:03 +0000110 mkdirs(dst)
Jack Jansen01c23091995-08-14 12:38:42 +0000111 files = os.listdir(src)
112 for f in files:
Jack Jansen57d53a91996-09-15 22:13:26 +0000113 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
Jack Jansen01c23091995-08-14 12:38:42 +0000114 else:
Jack Jansen57d53a91996-09-15 22:13:26 +0000115 copy(src, dst, 1, copydates)