annotate Mac/Lib/macostools.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 3f6d261654ca
children 43054021bb57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1 """macostools - Various utility functions for MacOS.
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
2
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
3 mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src'
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
4 copy(src, dst) - Full copy of 'src' to 'dst'
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
5 """
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
6
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
7 import macfs
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
8 import Res
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
9 import os
3191
f9fd5122680f Fixed positioning of icon on copy()
Jack Jansen <jack.jansen@cwi.nl>
parents: 3039
diff changeset
10 from MACFS import *
3450
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
11 import MacOS
4310
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
12 import time
3450
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
13 try:
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
14 openrf = MacOS.openrf
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
15 except AttributeError:
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
16 # Backward compatability
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
17 openrf = open
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
18
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
19 Error = 'macostools.Error'
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
20
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
21 FSSpecType = type(macfs.FSSpec(':'))
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
22
3450
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
23 BUFSIZ=0x80000 # Copy in 0.5Mb chunks
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
24
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
25 #
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
26 # Not guaranteed to be correct or stay correct (Apple doesn't tell you
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27 # how to do this), but it seems to work.
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
28 #
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
29 def mkalias(src, dst):
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
30 """Create a finder alias"""
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
31 srcfss = macfs.FSSpec(src)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
32 dstfss = macfs.FSSpec(dst)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
33 alias = srcfss.NewAlias()
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
34 srcfinfo = srcfss.GetFInfo()
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
35
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36 Res.FSpCreateResFile(dstfss, srcfinfo.Creator, srcfinfo.Type, -1)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37 h = Res.FSpOpenResFile(dstfss, 3)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
38 resource = Res.Resource(alias.data)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
39 resource.AddResource('alis', 0, '')
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
40 Res.CloseResFile(h)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
41
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
42 dstfinfo = dstfss.GetFInfo()
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
43 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
44 dstfss.SetFInfo(dstfinfo)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
45
3039
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
46 def mkdirs(dst):
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
47 """Make directories leading to 'dst' if they don't exist yet"""
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
48 if dst == '' or os.path.exists(dst):
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
49 return
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
50 head, tail = os.path.split(dst)
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
51 if not ':' in head:
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
52 head = head + ':'
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
53 mkdirs(head)
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
54 os.mkdir(dst, 0777)
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
55
4310
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
56 def touched(dst):
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
57 """Tell the finder a file has changed"""
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
58 file_fss = macfs.FSSpec(dst)
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
59 vRefNum, dirID, name = file_fss.as_tuple()
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
60 dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
61 crdate, moddate, bkdate = dir_fss.GetDates()
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
62 now = time.time()
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
63 if now == moddate:
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
64 now = now + 1
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
65 dir_fss.SetDates(crdate, now, bkdate)
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
66
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
67 def copy(src, dst, createpath=0, copydates=1):
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
68 """Copy a file, including finder info, resource fork, etc"""
3039
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
69 if createpath:
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
70 mkdirs(os.path.split(dst)[0])
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
71 srcfss = macfs.FSSpec(src)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
72 dstfss = macfs.FSSpec(dst)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
73
3039
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
74 ifp = open(srcfss.as_pathname(), 'rb')
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
75 ofp = open(dstfss.as_pathname(), 'wb')
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
76 d = ifp.read(BUFSIZ)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
77 while d:
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
78 ofp.write(d)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
79 d = ifp.read(BUFSIZ)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
80 ifp.close()
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
81 ofp.close()
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
82
3450
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
83 ifp = openrf(srcfss.as_pathname(), '*rb')
713bd8377382 Use MacOS.openrf (if it is available) for opening resource forks
Jack Jansen <jack.jansen@cwi.nl>
parents: 3191
diff changeset
84 ofp = openrf(dstfss.as_pathname(), '*wb')
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
85 d = ifp.read(BUFSIZ)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
86 while d:
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
87 ofp.write(d)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
88 d = ifp.read(BUFSIZ)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
89 ifp.close()
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
90 ofp.close()
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
91
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
92 sf = srcfss.GetFInfo()
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
93 df = dstfss.GetFInfo()
3191
f9fd5122680f Fixed positioning of icon on copy()
Jack Jansen <jack.jansen@cwi.nl>
parents: 3039
diff changeset
94 df.Creator, df.Type = sf.Creator, sf.Type
f9fd5122680f Fixed positioning of icon on copy()
Jack Jansen <jack.jansen@cwi.nl>
parents: 3039
diff changeset
95 df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
96 dstfss.SetFInfo(df)
4310
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
97 if copydates:
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
98 crdate, mddate, bkdate = srcfss.GetDates()
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
99 dstfss.SetDates(crdate, mddate, bkdate)
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
100 touched(dstfss)
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
101
4310
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
102 def copytree(src, dst, copydates=1):
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
103 """Copy a complete file tree to a new destination"""
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
104 if os.path.isdir(src):
3039
016f3662ef98 copy() can now create destination path
Jack Jansen <jack.jansen@cwi.nl>
parents: 2990
diff changeset
105 mkdirs(dst)
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
106 files = os.listdir(src)
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
107 for f in files:
4310
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
108 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
2990
9477a4457e56 Useful routines on a mac:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
109 else:
4310
3f6d261654ca - Optionally copy file times for copy/copytree (default on)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3917
diff changeset
110 copy(src, dst, 1, copydates)