1995-08-14 09:38:42 -03:00
|
|
|
"""macostools - Various utility functions for MacOS.
|
|
|
|
|
|
|
|
mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src'
|
|
|
|
copy(src, dst) - Full copy of 'src' to 'dst'
|
|
|
|
"""
|
|
|
|
|
|
|
|
import macfs
|
2001-08-25 09:15:04 -03:00
|
|
|
from Carbon import Res
|
1995-08-14 09:38:42 -03:00
|
|
|
import os
|
1995-10-09 20:27:06 -03:00
|
|
|
from MACFS import *
|
1996-03-12 09:33:34 -04:00
|
|
|
import MacOS
|
1996-09-15 19:13:26 -03:00
|
|
|
import time
|
1996-03-12 09:33:34 -04:00
|
|
|
try:
|
|
|
|
openrf = MacOS.openrf
|
|
|
|
except AttributeError:
|
|
|
|
# Backward compatability
|
|
|
|
openrf = open
|
1995-08-14 09:38:42 -03:00
|
|
|
|
|
|
|
Error = 'macostools.Error'
|
|
|
|
|
1996-03-12 09:33:34 -04:00
|
|
|
BUFSIZ=0x80000 # Copy in 0.5Mb chunks
|
1995-08-14 09:38:42 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Not guaranteed to be correct or stay correct (Apple doesn't tell you
|
|
|
|
# how to do this), but it seems to work.
|
|
|
|
#
|
1997-08-08 12:00:59 -03:00
|
|
|
def mkalias(src, dst, relative=None):
|
1995-08-14 09:38:42 -03:00
|
|
|
"""Create a finder alias"""
|
|
|
|
srcfss = macfs.FSSpec(src)
|
2002-08-05 18:53:57 -03:00
|
|
|
# The next line will fail under unix-Python if the destination
|
|
|
|
# doesn't exist yet. We should change this code to be fsref-based.
|
1995-08-14 09:38:42 -03:00
|
|
|
dstfss = macfs.FSSpec(dst)
|
1998-04-15 11:35:16 -03:00
|
|
|
if relative:
|
|
|
|
relativefss = macfs.FSSpec(relative)
|
|
|
|
# ik mag er geen None in stoppen :-(
|
|
|
|
alias = srcfss.NewAlias(relativefss)
|
|
|
|
else:
|
|
|
|
alias = srcfss.NewAlias()
|
2002-03-10 15:28:02 -04:00
|
|
|
|
|
|
|
if os.path.isdir(src):
|
|
|
|
cr, tp = 'MACS', 'fdrp'
|
|
|
|
else:
|
|
|
|
cr, tp = srcfss.GetCreatorType()
|
|
|
|
|
|
|
|
Res.FSpCreateResFile(dstfss, cr, tp, -1)
|
1995-08-14 09:38:42 -03:00
|
|
|
h = Res.FSpOpenResFile(dstfss, 3)
|
|
|
|
resource = Res.Resource(alias.data)
|
|
|
|
resource.AddResource('alis', 0, '')
|
|
|
|
Res.CloseResFile(h)
|
|
|
|
|
|
|
|
dstfinfo = dstfss.GetFInfo()
|
|
|
|
dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
|
|
|
|
dstfss.SetFInfo(dstfinfo)
|
|
|
|
|
1995-08-31 10:40:03 -03:00
|
|
|
def mkdirs(dst):
|
|
|
|
"""Make directories leading to 'dst' if they don't exist yet"""
|
|
|
|
if dst == '' or os.path.exists(dst):
|
|
|
|
return
|
|
|
|
head, tail = os.path.split(dst)
|
2002-08-03 17:49:10 -03:00
|
|
|
if os.sep == ':' and not ':' in head:
|
1995-08-31 10:40:03 -03:00
|
|
|
head = head + ':'
|
|
|
|
mkdirs(head)
|
|
|
|
os.mkdir(dst, 0777)
|
|
|
|
|
2001-02-14 13:06:32 -04:00
|
|
|
def touched(dst):
|
1996-09-15 19:13:26 -03:00
|
|
|
"""Tell the finder a file has changed"""
|
|
|
|
file_fss = macfs.FSSpec(dst)
|
|
|
|
vRefNum, dirID, name = file_fss.as_tuple()
|
|
|
|
dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
|
|
|
|
crdate, moddate, bkdate = dir_fss.GetDates()
|
|
|
|
now = time.time()
|
|
|
|
if now == moddate:
|
|
|
|
now = now + 1
|
|
|
|
dir_fss.SetDates(crdate, now, bkdate)
|
|
|
|
|
2001-02-14 13:06:32 -04:00
|
|
|
def touched_ae(dst):
|
2001-02-09 11:58:34 -04:00
|
|
|
"""Tell the finder a file has changed"""
|
|
|
|
import Finder
|
|
|
|
f = Finder.Finder()
|
|
|
|
file_fss = macfs.FSSpec(dst)
|
|
|
|
vRefNum, dirID, name = file_fss.as_tuple()
|
|
|
|
dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
|
|
|
|
f.update(dir_fss)
|
|
|
|
|
2001-03-06 18:46:25 -04:00
|
|
|
def copy(src, dst, createpath=0, copydates=1, forcetype=None):
|
1995-08-14 09:38:42 -03:00
|
|
|
"""Copy a file, including finder info, resource fork, etc"""
|
2002-08-05 18:53:57 -03:00
|
|
|
if hasattr(src, 'as_pathname'):
|
|
|
|
src = src.as_pathname()
|
|
|
|
if hasattr(dst, 'as_pathname'):
|
|
|
|
dst = dst.as_pathname()
|
1995-08-31 10:40:03 -03:00
|
|
|
if createpath:
|
|
|
|
mkdirs(os.path.split(dst)[0])
|
1995-08-14 09:38:42 -03:00
|
|
|
|
2002-08-05 18:53:57 -03:00
|
|
|
ifp = open(src, 'rb')
|
|
|
|
ofp = open(dst, 'wb')
|
1995-08-14 09:38:42 -03:00
|
|
|
d = ifp.read(BUFSIZ)
|
|
|
|
while d:
|
|
|
|
ofp.write(d)
|
|
|
|
d = ifp.read(BUFSIZ)
|
|
|
|
ifp.close()
|
|
|
|
ofp.close()
|
|
|
|
|
2002-08-05 18:53:57 -03:00
|
|
|
ifp = openrf(src, '*rb')
|
|
|
|
ofp = openrf(dst, '*wb')
|
1995-08-14 09:38:42 -03:00
|
|
|
d = ifp.read(BUFSIZ)
|
|
|
|
while d:
|
|
|
|
ofp.write(d)
|
|
|
|
d = ifp.read(BUFSIZ)
|
|
|
|
ifp.close()
|
|
|
|
ofp.close()
|
|
|
|
|
2002-08-05 18:53:57 -03:00
|
|
|
srcfss = macfs.FSSpec(src)
|
|
|
|
dstfss = macfs.FSSpec(dst)
|
1995-08-14 09:38:42 -03:00
|
|
|
sf = srcfss.GetFInfo()
|
|
|
|
df = dstfss.GetFInfo()
|
1995-10-09 20:27:06 -03:00
|
|
|
df.Creator, df.Type = sf.Creator, sf.Type
|
2001-03-06 18:46:25 -04:00
|
|
|
if forcetype != None:
|
|
|
|
df.Type = forcetype
|
1995-10-09 20:27:06 -03:00
|
|
|
df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
|
1995-08-14 09:38:42 -03:00
|
|
|
dstfss.SetFInfo(df)
|
1996-09-15 19:13:26 -03:00
|
|
|
if copydates:
|
|
|
|
crdate, mddate, bkdate = srcfss.GetDates()
|
|
|
|
dstfss.SetDates(crdate, mddate, bkdate)
|
|
|
|
touched(dstfss)
|
1995-08-14 09:38:42 -03:00
|
|
|
|
1996-09-15 19:13:26 -03:00
|
|
|
def copytree(src, dst, copydates=1):
|
1995-08-14 09:38:42 -03:00
|
|
|
"""Copy a complete file tree to a new destination"""
|
|
|
|
if os.path.isdir(src):
|
1995-08-31 10:40:03 -03:00
|
|
|
mkdirs(dst)
|
1995-08-14 09:38:42 -03:00
|
|
|
files = os.listdir(src)
|
|
|
|
for f in files:
|
1996-09-15 19:13:26 -03:00
|
|
|
copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
|
1995-08-14 09:38:42 -03:00
|
|
|
else:
|
1996-09-15 19:13:26 -03:00
|
|
|
copy(src, dst, 1, copydates)
|