blob: 67d32e74ce2ca60b3c376984c600dff926cdc366 [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
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00007from Carbon import Res
Jack Jansen2b88dec2003-01-28 23:53:40 +00008from Carbon import File, Files
Jack Jansen01c23091995-08-14 12:38:42 +00009import os
Jack Jansen2b88dec2003-01-28 23:53:40 +000010import sys
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:
Jack Jansen2b88dec2003-01-28 23:53:40 +000014 openrf = MacOS.openrf
Jack Jansen06033191996-03-12 13:33:34 +000015except AttributeError:
Georg Brandl08c02db2005-07-22 18:39:19 +000016 # Backward compatibility
Jack Jansen2b88dec2003-01-28 23:53:40 +000017 openrf = open
Jack Jansen01c23091995-08-14 12:38:42 +000018
19Error = 'macostools.Error'
20
Jack Jansen2b88dec2003-01-28 23:53:40 +000021BUFSIZ=0x80000 # Copy in 0.5Mb chunks
22
23COPY_FLAGS = (Files.kIsStationary|Files.kNameLocked|Files.kHasBundle|
24 Files.kIsInvisible|Files.kIsAlias)
Jack Jansen01c23091995-08-14 12:38:42 +000025
26#
27# Not guaranteed to be correct or stay correct (Apple doesn't tell you
28# how to do this), but it seems to work.
29#
Jack Jansen48f662d1997-08-08 15:00:59 +000030def mkalias(src, dst, relative=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000031 """Create a finder alias"""
32 srcfsr = File.FSRef(src)
33 # The next line will fail under unix-Python if the destination
34 # doesn't exist yet. We should change this code to be fsref-based.
35 dstdir, dstname = os.path.split(dst)
36 if not dstdir: dstdir = os.curdir
37 dstdirfsr = File.FSRef(dstdir)
38 if relative:
39 relativefsr = File.FSRef(relative)
40 # ik mag er geen None in stoppen :-(
41 alias = File.FSNewAlias(relativefsr, srcfsr)
42 else:
43 alias = srcfsr.FSNewAliasMinimal()
Tim Peters182b5ac2004-07-18 06:16:08 +000044
Guido van Rossumef87d6e2007-05-02 19:09:54 +000045 dstfsr, dstfss = Res.FSCreateResourceFile(dstdirfsr, str(dstname),
Jack Jansen2b88dec2003-01-28 23:53:40 +000046 File.FSGetResourceForkName())
47 h = Res.FSOpenResourceFile(dstfsr, File.FSGetResourceForkName(), 3)
48 resource = Res.Resource(alias.data)
49 resource.AddResource('alis', 0, '')
50 Res.CloseResFile(h)
Tim Peters182b5ac2004-07-18 06:16:08 +000051
Jack Jansen2b88dec2003-01-28 23:53:40 +000052 dstfinfo = dstfss.FSpGetFInfo()
53 dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
54 dstfss.FSpSetFInfo(dstfinfo)
Tim Peters182b5ac2004-07-18 06:16:08 +000055
Jack Jansen423c7981995-08-31 13:40:03 +000056def mkdirs(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000057 """Make directories leading to 'dst' if they don't exist yet"""
58 if dst == '' or os.path.exists(dst):
59 return
60 head, tail = os.path.split(dst)
61 if os.sep == ':' and not ':' in head:
62 head = head + ':'
63 mkdirs(head)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000064 os.mkdir(dst, 0o777)
Tim Peters182b5ac2004-07-18 06:16:08 +000065
Jack Jansen3ff82a32001-02-14 17:06:32 +000066def touched(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000067 """Tell the finder a file has changed. No-op on MacOSX."""
Jack Jansen1c8501e2003-03-07 15:36:49 +000068 import warnings
Guido van Rossumd59da4b2007-05-22 18:11:13 +000069 warnings.warn("macostools.touched() has been deprecated",
70 DeprecationWarning, 2)
Tim Peters182b5ac2004-07-18 06:16:08 +000071
Jack Jansen3ff82a32001-02-14 17:06:32 +000072def touched_ae(dst):
Jack Jansen2b88dec2003-01-28 23:53:40 +000073 """Tell the finder a file has changed"""
74 pardir = os.path.split(dst)[0]
75 if not pardir:
76 pardir = os.curdir
77 import Finder
78 f = Finder.Finder()
79 f.update(File.FSRef(pardir))
Tim Peters182b5ac2004-07-18 06:16:08 +000080
Jack Jansenc1463c982001-03-06 22:46:25 +000081def copy(src, dst, createpath=0, copydates=1, forcetype=None):
Jack Jansen2b88dec2003-01-28 23:53:40 +000082 """Copy a file, including finder info, resource fork, etc"""
83 src = File.pathname(src)
84 dst = File.pathname(dst)
85 if createpath:
86 mkdirs(os.path.split(dst)[0])
Tim Peters182b5ac2004-07-18 06:16:08 +000087
Jack Jansen2b88dec2003-01-28 23:53:40 +000088 ifp = open(src, 'rb')
89 ofp = open(dst, 'wb')
90 d = ifp.read(BUFSIZ)
91 while d:
92 ofp.write(d)
93 d = ifp.read(BUFSIZ)
94 ifp.close()
95 ofp.close()
Tim Peters182b5ac2004-07-18 06:16:08 +000096
Jack Jansen2b88dec2003-01-28 23:53:40 +000097 ifp = openrf(src, '*rb')
98 ofp = openrf(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()
Tim Peters182b5ac2004-07-18 06:16:08 +0000105
Jack Jansen2b88dec2003-01-28 23:53:40 +0000106 srcfss = File.FSSpec(src)
107 dstfss = File.FSSpec(dst)
108 sf = srcfss.FSpGetFInfo()
109 df = dstfss.FSpGetFInfo()
110 df.Creator, df.Type = sf.Creator, sf.Type
111 if forcetype != None:
112 df.Type = forcetype
113 df.Flags = (sf.Flags & COPY_FLAGS)
114 dstfss.FSpSetFInfo(df)
115 if copydates:
116 srcfsr = File.FSRef(src)
117 dstfsr = File.FSRef(dst)
118 catinfo, _, _, _ = srcfsr.FSGetCatalogInfo(Files.kFSCatInfoAllDates)
119 dstfsr.FSSetCatalogInfo(Files.kFSCatInfoAllDates, catinfo)
Tim Peters182b5ac2004-07-18 06:16:08 +0000120
Jack Jansen57d53a91996-09-15 22:13:26 +0000121def copytree(src, dst, copydates=1):
Jack Jansen2b88dec2003-01-28 23:53:40 +0000122 """Copy a complete file tree to a new destination"""
123 if os.path.isdir(src):
124 mkdirs(dst)
125 files = os.listdir(src)
126 for f in files:
127 copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
128 else:
129 copy(src, dst, 1, copydates)