blob: 46eef98a96c55bf204f892e058a1327152f5efed [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#
29def mkalias(src, dst):
30 """Create a finder alias"""
31 srcfss = macfs.FSSpec(src)
32 dstfss = macfs.FSSpec(dst)
33 alias = srcfss.NewAlias()
34 srcfinfo = srcfss.GetFInfo()
35
36 Res.FSpCreateResFile(dstfss, srcfinfo.Creator, srcfinfo.Type, -1)
37 h = Res.FSpOpenResFile(dstfss, 3)
38 resource = Res.Resource(alias.data)
39 resource.AddResource('alis', 0, '')
40 Res.CloseResFile(h)
41
42 dstfinfo = dstfss.GetFInfo()
43 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
44 dstfss.SetFInfo(dstfinfo)
45
Jack Jansen423c7981995-08-31 13:40:03 +000046def mkdirs(dst):
47 """Make directories leading to 'dst' if they don't exist yet"""
48 if dst == '' or os.path.exists(dst):
49 return
50 head, tail = os.path.split(dst)
Jack Jansen423c7981995-08-31 13:40:03 +000051 if not ':' in head:
52 head = head + ':'
53 mkdirs(head)
54 os.mkdir(dst, 0777)
55
Jack Jansen57d53a91996-09-15 22:13:26 +000056def touched(dst):
57 """Tell the finder a file has changed"""
58 file_fss = macfs.FSSpec(dst)
59 vRefNum, dirID, name = file_fss.as_tuple()
60 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
61 crdate, moddate, bkdate = dir_fss.GetDates()
62 now = time.time()
63 if now == moddate:
64 now = now + 1
65 dir_fss.SetDates(crdate, now, bkdate)
66
67def copy(src, dst, createpath=0, copydates=1):
Jack Jansen01c23091995-08-14 12:38:42 +000068 """Copy a file, including finder info, resource fork, etc"""
Jack Jansen423c7981995-08-31 13:40:03 +000069 if createpath:
70 mkdirs(os.path.split(dst)[0])
Jack Jansen01c23091995-08-14 12:38:42 +000071 srcfss = macfs.FSSpec(src)
72 dstfss = macfs.FSSpec(dst)
73
Jack Jansen423c7981995-08-31 13:40:03 +000074 ifp = open(srcfss.as_pathname(), 'rb')
75 ofp = open(dstfss.as_pathname(), 'wb')
Jack Jansen01c23091995-08-14 12:38:42 +000076 d = ifp.read(BUFSIZ)
77 while d:
78 ofp.write(d)
79 d = ifp.read(BUFSIZ)
80 ifp.close()
81 ofp.close()
82
Jack Jansen06033191996-03-12 13:33:34 +000083 ifp = openrf(srcfss.as_pathname(), '*rb')
84 ofp = openrf(dstfss.as_pathname(), '*wb')
Jack Jansen01c23091995-08-14 12:38:42 +000085 d = ifp.read(BUFSIZ)
86 while d:
87 ofp.write(d)
88 d = ifp.read(BUFSIZ)
89 ifp.close()
90 ofp.close()
91
92 sf = srcfss.GetFInfo()
93 df = dstfss.GetFInfo()
Jack Jansena8a277c1995-10-09 23:27:06 +000094 df.Creator, df.Type = sf.Creator, sf.Type
95 df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
Jack Jansen01c23091995-08-14 12:38:42 +000096 dstfss.SetFInfo(df)
Jack Jansen57d53a91996-09-15 22:13:26 +000097 if copydates:
98 crdate, mddate, bkdate = srcfss.GetDates()
99 dstfss.SetDates(crdate, mddate, bkdate)
100 touched(dstfss)
Jack Jansen01c23091995-08-14 12:38:42 +0000101
Jack Jansen57d53a91996-09-15 22:13:26 +0000102def copytree(src, dst, copydates=1):
Jack Jansen01c23091995-08-14 12:38:42 +0000103 """Copy a complete file tree to a new destination"""
104 if os.path.isdir(src):
Jack Jansen423c7981995-08-31 13:40:03 +0000105 mkdirs(dst)
Jack Jansen01c23091995-08-14 12:38:42 +0000106 files = os.listdir(src)
107 for f in files:
Jack Jansen57d53a91996-09-15 22:13:26 +0000108 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
Jack Jansen01c23091995-08-14 12:38:42 +0000109 else:
Jack Jansen57d53a91996-09-15 22:13:26 +0000110 copy(src, dst, 1, copydates)