blob: 8822ab8f9787ce61ed8377c0719893123bb57bce [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
Jack Jansen37e47722002-10-29 22:48:43 +000074 try:
75 dir_fss.SetDates(crdate, now, bkdate)
76 except macfs.error:
77 pass
Jack Jansen57d53a91996-09-15 22:13:26 +000078
Jack Jansen3ff82a32001-02-14 17:06:32 +000079def touched_ae(dst):
Jack Jansen7e31f682001-02-09 15:58:34 +000080 """Tell the finder a file has changed"""
81 import Finder
82 f = Finder.Finder()
83 file_fss = macfs.FSSpec(dst)
84 vRefNum, dirID, name = file_fss.as_tuple()
85 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
86 f.update(dir_fss)
87
Jack Jansenc1463c982001-03-06 22:46:25 +000088def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen01c23091995-08-14 12:38:42 +000089 """Copy a file, including finder info, resource fork, etc"""
Jack Jansen0a9d7552002-08-05 21:53:57 +000090 if hasattr(src, 'as_pathname'):
91 src = src.as_pathname()
92 if hasattr(dst, 'as_pathname'):
93 dst = dst.as_pathname()
Jack Jansen423c7981995-08-31 13:40:03 +000094 if createpath:
95 mkdirs(os.path.split(dst)[0])
Jack Jansen0a9d7552002-08-05 21:53:57 +000096
97 ifp = open(src, 'rb')
98 ofp = open(dst, 'wb')
99 d = ifp.read(BUFSIZ)
100 while d:
101 ofp.write(d)
102 d = ifp.read(BUFSIZ)
103 ifp.close()
104 ofp.close()
105
106 ifp = openrf(src, '*rb')
107 ofp = openrf(dst, '*wb')
108 d = ifp.read(BUFSIZ)
109 while d:
110 ofp.write(d)
111 d = ifp.read(BUFSIZ)
112 ifp.close()
113 ofp.close()
114
Jack Jansen01c23091995-08-14 12:38:42 +0000115 srcfss = macfs.FSSpec(src)
116 dstfss = macfs.FSSpec(dst)
Jack Jansen01c23091995-08-14 12:38:42 +0000117 sf = srcfss.GetFInfo()
118 df = dstfss.GetFInfo()
Jack Jansena8a277c1995-10-09 23:27:06 +0000119 df.Creator, df.Type = sf.Creator, sf.Type
Jack Jansenc1463c982001-03-06 22:46:25 +0000120 if forcetype != None:
121 df.Type = forcetype
Jack Jansena8a277c1995-10-09 23:27:06 +0000122 df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
Jack Jansen01c23091995-08-14 12:38:42 +0000123 dstfss.SetFInfo(df)
Jack Jansen57d53a91996-09-15 22:13:26 +0000124 if copydates:
125 crdate, mddate, bkdate = srcfss.GetDates()
126 dstfss.SetDates(crdate, mddate, bkdate)
127 touched(dstfss)
Jack Jansen01c23091995-08-14 12:38:42 +0000128
Jack Jansen57d53a91996-09-15 22:13:26 +0000129def copytree(src, dst, copydates=1):
Jack Jansen01c23091995-08-14 12:38:42 +0000130 """Copy a complete file tree to a new destination"""
131 if os.path.isdir(src):
Jack Jansen423c7981995-08-31 13:40:03 +0000132 mkdirs(dst)
Jack Jansen01c23091995-08-14 12:38:42 +0000133 files = os.listdir(src)
134 for f in files:
Jack Jansen57d53a91996-09-15 22:13:26 +0000135 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
Jack Jansen01c23091995-08-14 12:38:42 +0000136 else:
Jack Jansen57d53a91996-09-15 22:13:26 +0000137 copy(src, dst, 1, copydates)