This commit was manufactured by cvs2svn to create tag 'r221c2'.
[python/dscho.git] / Mac / Lib / macostools.py
blob43ab74fb7760600745f9cb3e0144689b321d67b3
1 """macostools - Various utility functions for MacOS.
3 mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src'
4 copy(src, dst) - Full copy of 'src' to 'dst'
5 """
7 import macfs
8 from Carbon import Res
9 import os
10 from MACFS import *
11 import MacOS
12 import time
13 try:
14 openrf = MacOS.openrf
15 except AttributeError:
16 # Backward compatability
17 openrf = open
19 Error = 'macostools.Error'
21 BUFSIZ=0x80000 # Copy in 0.5Mb chunks
24 # Not guaranteed to be correct or stay correct (Apple doesn't tell you
25 # how to do this), but it seems to work.
27 def mkalias(src, dst, relative=None):
28 """Create a finder alias"""
29 srcfss = macfs.FSSpec(src)
30 dstfss = macfs.FSSpec(dst)
31 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()
38 if os.path.isdir(src):
39 cr, tp = 'MACS', 'fdrp'
40 else:
41 cr, tp = srcfss.GetCreatorType()
43 Res.FSpCreateResFile(dstfss, cr, tp, -1)
44 h = Res.FSpOpenResFile(dstfss, 3)
45 resource = Res.Resource(alias.data)
46 resource.AddResource('alis', 0, '')
47 Res.CloseResFile(h)
49 dstfinfo = dstfss.GetFInfo()
50 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
51 dstfss.SetFInfo(dstfinfo)
53 def 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)
58 if not ':' in head:
59 head = head + ':'
60 mkdirs(head)
61 os.mkdir(dst, 0777)
63 def touched(dst):
64 """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)
74 def touched_ae(dst):
75 """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)
83 def copy(src, dst, createpath=0, copydates=1, forcetype=None):
84 """Copy a file, including finder info, resource fork, etc"""
85 if createpath:
86 mkdirs(os.path.split(dst)[0])
87 srcfss = macfs.FSSpec(src)
88 dstfss = macfs.FSSpec(dst)
90 ifp = open(srcfss.as_pathname(), 'rb')
91 ofp = open(dstfss.as_pathname(), 'wb')
92 d = ifp.read(BUFSIZ)
93 while d:
94 ofp.write(d)
95 d = ifp.read(BUFSIZ)
96 ifp.close()
97 ofp.close()
99 ifp = openrf(srcfss.as_pathname(), '*rb')
100 ofp = openrf(dstfss.as_pathname(), '*wb')
101 d = ifp.read(BUFSIZ)
102 while d:
103 ofp.write(d)
104 d = ifp.read(BUFSIZ)
105 ifp.close()
106 ofp.close()
108 sf = srcfss.GetFInfo()
109 df = dstfss.GetFInfo()
110 df.Creator, df.Type = sf.Creator, sf.Type
111 if forcetype != None:
112 df.Type = forcetype
113 df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
114 dstfss.SetFInfo(df)
115 if copydates:
116 crdate, mddate, bkdate = srcfss.GetDates()
117 dstfss.SetDates(crdate, mddate, bkdate)
118 touched(dstfss)
120 def copytree(src, dst, copydates=1):
121 """Copy a complete file tree to a new destination"""
122 if os.path.isdir(src):
123 mkdirs(dst)
124 files = os.listdir(src)
125 for f in files:
126 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
127 else:
128 copy(src, dst, 1, copydates)