blob: 5b94ee20dd16ed8268a6e4109db4d7ca4a1fe649 [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)
Jack Jansen0a9d7552002-08-05 21:53:57 +000030 # The next line will fail under unix-Python if the destination
31 # doesn't exist yet. We should change this code to be fsref-based.
Jack Jansen01c23091995-08-14 12:38:42 +000032 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()
Just van Rossuma2168ec2002-03-10 19:28:02 +000039
40 if os.path.isdir(src):
41 cr, tp = 'MACS', 'fdrp'
42 else:
43 cr, tp = srcfss.GetCreatorType()
44
45 Res.FSpCreateResFile(dstfss, cr, tp, -1)
Jack Jansen01c23091995-08-14 12:38:42 +000046 h = Res.FSpOpenResFile(dstfss, 3)
47 resource = Res.Resource(alias.data)
48 resource.AddResource('alis', 0, '')
49 Res.CloseResFile(h)
50
51 dstfinfo = dstfss.GetFInfo()
52 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
53 dstfss.SetFInfo(dstfinfo)
54
Jack Jansen423c7981995-08-31 13:40:03 +000055def mkdirs(dst):
56 """Make directories leading to 'dst' if they don't exist yet"""
57 if dst == '' or os.path.exists(dst):
58 return
59 head, tail = os.path.split(dst)
Jack Jansend64845d2002-08-03 20:49:10 +000060 if os.sep == ':' and not ':' in head:
Jack Jansen423c7981995-08-31 13:40:03 +000061 head = head + ':'
62 mkdirs(head)
63 os.mkdir(dst, 0777)
64
Jack Jansen3ff82a32001-02-14 17:06:32 +000065def touched(dst):
Jack Jansen57d53a91996-09-15 22:13:26 +000066 """Tell the finder a file has changed"""
67 file_fss = macfs.FSSpec(dst)
68 vRefNum, dirID, name = file_fss.as_tuple()
69 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
70 crdate, moddate, bkdate = dir_fss.GetDates()
71 now = time.time()
72 if now == moddate:
73 now = now + 1
74 dir_fss.SetDates(crdate, now, bkdate)
75
Jack Jansen3ff82a32001-02-14 17:06:32 +000076def touched_ae(dst):
Jack Jansen7e31f682001-02-09 15:58:34 +000077 """Tell the finder a file has changed"""
78 import Finder
79 f = Finder.Finder()
80 file_fss = macfs.FSSpec(dst)
81 vRefNum, dirID, name = file_fss.as_tuple()
82 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
83 f.update(dir_fss)
84
Jack Jansenc1463c982001-03-06 22:46:25 +000085def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen01c23091995-08-14 12:38:42 +000086 """Copy a file, including finder info, resource fork, etc"""
Jack Jansen0a9d7552002-08-05 21:53:57 +000087 if hasattr(src, 'as_pathname'):
88 src = src.as_pathname()
89 if hasattr(dst, 'as_pathname'):
90 dst = dst.as_pathname()
Jack Jansen423c7981995-08-31 13:40:03 +000091 if createpath:
92 mkdirs(os.path.split(dst)[0])
Jack Jansen0a9d7552002-08-05 21:53:57 +000093
94 ifp = open(src, 'rb')
95 ofp = open(dst, 'wb')
96 d = ifp.read(BUFSIZ)
97 while d:
98 ofp.write(d)
99 d = ifp.read(BUFSIZ)
100 ifp.close()
101 ofp.close()
102
103 ifp = openrf(src, '*rb')
104 ofp = openrf(dst, '*wb')
105 d = ifp.read(BUFSIZ)
106 while d:
107 ofp.write(d)
108 d = ifp.read(BUFSIZ)
109 ifp.close()
110 ofp.close()
111
Jack Jansen01c23091995-08-14 12:38:42 +0000112 srcfss = macfs.FSSpec(src)
113 dstfss = macfs.FSSpec(dst)
Jack Jansen01c23091995-08-14 12:38:42 +0000114 sf = srcfss.GetFInfo()
115 df = dstfss.GetFInfo()
Jack Jansena8a277c1995-10-09 23:27:06 +0000116 df.Creator, df.Type = sf.Creator, sf.Type
Jack Jansenc1463c982001-03-06 22:46:25 +0000117 if forcetype != None:
118 df.Type = forcetype
Jack Jansena8a277c1995-10-09 23:27:06 +0000119 df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
Jack Jansen01c23091995-08-14 12:38:42 +0000120 dstfss.SetFInfo(df)
Jack Jansen57d53a91996-09-15 22:13:26 +0000121 if copydates:
122 crdate, mddate, bkdate = srcfss.GetDates()
123 dstfss.SetDates(crdate, mddate, bkdate)
124 touched(dstfss)
Jack Jansen01c23091995-08-14 12:38:42 +0000125
Jack Jansen57d53a91996-09-15 22:13:26 +0000126def copytree(src, dst, copydates=1):
Jack Jansen01c23091995-08-14 12:38:42 +0000127 """Copy a complete file tree to a new destination"""
128 if os.path.isdir(src):
Jack Jansen423c7981995-08-31 13:40:03 +0000129 mkdirs(dst)
Jack Jansen01c23091995-08-14 12:38:42 +0000130 files = os.listdir(src)
131 for f in files:
Jack Jansen57d53a91996-09-15 22:13:26 +0000132 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
Jack Jansen01c23091995-08-14 12:38:42 +0000133 else:
Jack Jansen57d53a91996-09-15 22:13:26 +0000134 copy(src, dst, 1, copydates)