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'
7 from warnings
import warnpy3k
8 warnpy3k("In 3.x, the macostools module is removed.", stacklevel
=2)
10 from Carbon
import Res
11 from Carbon
import File
, Files
16 except AttributeError:
17 # Backward compatibility
20 Error
= 'macostools.Error'
22 BUFSIZ
=0x80000 # Copy in 0.5Mb chunks
24 COPY_FLAGS
= (Files
.kIsStationary|Files
.kNameLocked|Files
.kHasBundle|
25 Files
.kIsInvisible|Files
.kIsAlias
)
28 # Not guaranteed to be correct or stay correct (Apple doesn't tell you
29 # how to do this), but it seems to work.
31 def mkalias(src
, dst
, relative
=None):
32 """Create a finder alias"""
33 srcfsr
= File
.FSRef(src
)
34 # The next line will fail under unix-Python if the destination
35 # doesn't exist yet. We should change this code to be fsref-based.
36 dstdir
, dstname
= os
.path
.split(dst
)
37 if not dstdir
: dstdir
= os
.curdir
38 dstdirfsr
= File
.FSRef(dstdir
)
40 relativefsr
= File
.FSRef(relative
)
41 # ik mag er geen None in stoppen :-(
42 alias
= File
.FSNewAlias(relativefsr
, srcfsr
)
44 alias
= srcfsr
.FSNewAliasMinimal()
46 dstfsr
, dstfss
= Res
.FSCreateResourceFile(dstdirfsr
, unicode(dstname
),
47 File
.FSGetResourceForkName())
48 h
= Res
.FSOpenResourceFile(dstfsr
, File
.FSGetResourceForkName(), 3)
49 resource
= Res
.Resource(alias
.data
)
50 resource
.AddResource('alis', 0, '')
53 dstfinfo
= dstfss
.FSpGetFInfo()
54 dstfinfo
.Flags
= dstfinfo
.Flags|
0x8000 # Alias flag
55 dstfss
.FSpSetFInfo(dstfinfo
)
58 """Make directories leading to 'dst' if they don't exist yet"""
59 if dst
== '' or os
.path
.exists(dst
):
61 head
, tail
= os
.path
.split(dst
)
62 if os
.sep
== ':' and not ':' in head
:
69 # be happy if someone already created the path
70 if e
.errno
!= errno
.EEXIST
:
75 """Tell the finder a file has changed. No-op on MacOSX."""
77 warnings
.warn("macostools.touched() has been deprecated",
78 DeprecationWarning, 2)
81 """Tell the finder a file has changed"""
82 pardir
= os
.path
.split(dst
)[0]
87 f
.update(File
.FSRef(pardir
))
89 def copy(src
, dst
, createpath
=0, copydates
=1, forcetype
=None):
90 """Copy a file, including finder info, resource fork, etc"""
91 src
= File
.pathname(src
)
92 dst
= File
.pathname(dst
)
94 mkdirs(os
.path
.split(dst
)[0])
105 ifp
= openrf(src
, '*rb')
106 ofp
= openrf(dst
, '*wb')
114 srcfss
= File
.FSSpec(src
)
115 dstfss
= File
.FSSpec(dst
)
116 sf
= srcfss
.FSpGetFInfo()
117 df
= dstfss
.FSpGetFInfo()
118 df
.Creator
, df
.Type
= sf
.Creator
, sf
.Type
119 if forcetype
is not None:
121 df
.Flags
= (sf
.Flags
& COPY_FLAGS
)
122 dstfss
.FSpSetFInfo(df
)
124 srcfsr
= File
.FSRef(src
)
125 dstfsr
= File
.FSRef(dst
)
126 catinfo
, _
, _
, _
= srcfsr
.FSGetCatalogInfo(Files
.kFSCatInfoAllDates
)
127 dstfsr
.FSSetCatalogInfo(Files
.kFSCatInfoAllDates
, catinfo
)
129 def copytree(src
, dst
, copydates
=1):
130 """Copy a complete file tree to a new destination"""
131 if os
.path
.isdir(src
):
133 files
= os
.listdir(src
)
135 copytree(os
.path
.join(src
, f
), os
.path
.join(dst
, f
), copydates
)
137 copy(src
, dst
, 1, copydates
)