blob: 43ab74fb7760600745f9cb3e0144689b321d67b3 [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()
Just van Rossuma2168ec2002-03-10 19:28:02 +000037
38 if os.path.isdir(src):
39 cr, tp = 'MACS', 'fdrp'
40 else:
41 cr, tp = srcfss.GetCreatorType()
42
43 Res.FSpCreateResFile(dstfss, cr, tp, -1)
Jack Jansen01c23091995-08-14 12:38:42 +000044 h = Res.FSpOpenResFile(dstfss, 3)
45 resource = Res.Resource(alias.data)
46 resource.AddResource('alis', 0, '')
47 Res.CloseResFile(h)
48
49 dstfinfo = dstfss.GetFInfo()
50 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
51 dstfss.SetFInfo(dstfinfo)
52
Jack Jansen423c7981995-08-31 13:40:03 +000053def mkdirs(dst):
54 """Make directories leading to 'dst' if they don't exist yet"""
55 if dst == '' or os.path.exists(dst):
56 return
57 head, tail = os.path.split(dst)
Jack Jansen423c7981995-08-31 13:40:03 +000058 if not ':' in head:
59 head = head + ':'
60 mkdirs(head)
61 os.mkdir(dst, 0777)
62
Jack Jansen3ff82a32001-02-14 17:06:32 +000063def touched(dst):
Jack Jansen57d53a91996-09-15 22:13:26 +000064 """Tell the finder a file has changed"""
65 file_fss = macfs.FSSpec(dst)
66 vRefNum, dirID, name = file_fss.as_tuple()
67 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
68 crdate, moddate, bkdate = dir_fss.GetDates()
69 now = time.time()
70 if now == moddate:
71 now = now + 1
72 dir_fss.SetDates(crdate, now, bkdate)
73
Jack Jansen3ff82a32001-02-14 17:06:32 +000074def touched_ae(dst):
Jack Jansen7e31f682001-02-09 15:58:34 +000075 """Tell the finder a file has changed"""
76 import Finder
77 f = Finder.Finder()
78 file_fss = macfs.FSSpec(dst)
79 vRefNum, dirID, name = file_fss.as_tuple()
80 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
81 f.update(dir_fss)
82
Jack Jansenc1463c982001-03-06 22:46:25 +000083def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen01c23091995-08-14 12:38:42 +000084 """Copy a file, including finder info, resource fork, etc"""
Jack Jansen423c7981995-08-31 13:40:03 +000085 if createpath:
86 mkdirs(os.path.split(dst)[0])
Jack Jansen01c23091995-08-14 12:38:42 +000087 srcfss = macfs.FSSpec(src)
88 dstfss = macfs.FSSpec(dst)
89
Jack Jansen423c7981995-08-31 13:40:03 +000090 ifp = open(srcfss.as_pathname(), 'rb')
91 ofp = open(dstfss.as_pathname(), 'wb')
Jack Jansen01c23091995-08-14 12:38:42 +000092 d = ifp.read(BUFSIZ)
93 while d:
94 ofp.write(d)
95 d = ifp.read(BUFSIZ)
96 ifp.close()
97 ofp.close()
98
Jack Jansen06033191996-03-12 13:33:34 +000099 ifp = openrf(srcfss.as_pathname(), '*rb')
100 ofp = openrf(dstfss.as_pathname(), '*wb')
Jack Jansen01c23091995-08-14 12:38:42 +0000101 d = ifp.read(BUFSIZ)
102 while d:
103 ofp.write(d)
104 d = ifp.read(BUFSIZ)
105 ifp.close()
106 ofp.close()
107
108 sf = srcfss.GetFInfo()
109 df = dstfss.GetFInfo()
Jack Jansena8a277c1995-10-09 23:27:06 +0000110 df.Creator, df.Type = sf.Creator, sf.Type
Jack Jansenc1463c982001-03-06 22:46:25 +0000111 if forcetype != None:
112 df.Type = forcetype
Jack Jansena8a277c1995-10-09 23:27:06 +0000113 df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
Jack Jansen01c23091995-08-14 12:38:42 +0000114 dstfss.SetFInfo(df)
Jack Jansen57d53a91996-09-15 22:13:26 +0000115 if copydates:
116 crdate, mddate, bkdate = srcfss.GetDates()
117 dstfss.SetDates(crdate, mddate, bkdate)
118 touched(dstfss)
Jack Jansen01c23091995-08-14 12:38:42 +0000119
Jack Jansen57d53a91996-09-15 22:13:26 +0000120def copytree(src, dst, copydates=1):
Jack Jansen01c23091995-08-14 12:38:42 +0000121 """Copy a complete file tree to a new destination"""
122 if os.path.isdir(src):
Jack Jansen423c7981995-08-31 13:40:03 +0000123 mkdirs(dst)
Jack Jansen01c23091995-08-14 12:38:42 +0000124 files = os.listdir(src)
125 for f in files:
Jack Jansen57d53a91996-09-15 22:13:26 +0000126 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
Jack Jansen01c23091995-08-14 12:38:42 +0000127 else:
Jack Jansen57d53a91996-09-15 22:13:26 +0000128 copy(src, dst, 1, copydates)