1995-08-14 12:38:42 +00: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 12:15:04 +00:00
|
|
|
from Carbon import Res
|
1995-08-14 12:38:42 +00:00
|
|
|
import os
|
1995-10-09 23:27:06 +00:00
|
|
|
from MACFS import *
|
1996-03-12 13:33:34 +00:00
|
|
|
import MacOS
|
1996-09-15 22:13:26 +00:00
|
|
|
import time
|
1996-03-12 13:33:34 +00:00
|
|
|
try:
|
|
|
|
openrf = MacOS.openrf
|
|
|
|
except AttributeError:
|
|
|
|
# Backward compatability
|
|
|
|
openrf = open
|
1995-08-14 12:38:42 +00:00
|
|
|
|
|
|
|
Error = 'macostools.Error'
|
|
|
|
|
|
|
|
FSSpecType = type(macfs.FSSpec(':'))
|
|
|
|
|
1996-03-12 13:33:34 +00:00
|
|
|
BUFSIZ=0x80000 # Copy in 0.5Mb chunks
|
1995-08-14 12:38:42 +00: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 15:00:59 +00:00
|
|
|
def mkalias(src, dst, relative=None):
|
1995-08-14 12:38:42 +00:00
|
|
|
"""Create a finder alias"""
|
|
|
|
srcfss = macfs.FSSpec(src)
|
|
|
|
dstfss = macfs.FSSpec(dst)
|
1998-04-15 14:35:16 +00:00
|
|
|
if relative:
|
|
|
|
relativefss = macfs.FSSpec(relative)
|
|
|
|
# ik mag er geen None in stoppen :-(
|
|
|
|
alias = srcfss.NewAlias(relativefss)
|
|
|
|
else:
|
|
|
|
alias = srcfss.NewAlias()
|
1995-08-14 12:38:42 +00:00
|
|
|
srcfinfo = srcfss.GetFInfo()
|
|
|
|
|
|
|
|
Res.FSpCreateResFile(dstfss, srcfinfo.Creator, srcfinfo.Type, -1)
|
|
|
|
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 13:40:03 +00: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)
|
|
|
|
if not ':' in head:
|
|
|
|
head = head + ':'
|
|
|
|
mkdirs(head)
|
|
|
|
os.mkdir(dst, 0777)
|
|
|
|
|
2001-02-14 17:06:32 +00:00
|
|
|
def touched(dst):
|
1996-09-15 22:13:26 +00: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 17:06:32 +00:00
|
|
|
def touched_ae(dst):
|
2001-02-09 15:58:34 +00: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 22:46:25 +00:00
|
|
|
def copy(src, dst, createpath=0, copydates=1, forcetype=None):
|
1995-08-14 12:38:42 +00:00
|
|
|
"""Copy a file, including finder info, resource fork, etc"""
|
1995-08-31 13:40:03 +00:00
|
|
|
if createpath:
|
|
|
|
mkdirs(os.path.split(dst)[0])
|
1995-08-14 12:38:42 +00:00
|
|
|
srcfss = macfs.FSSpec(src)
|
|
|
|
dstfss = macfs.FSSpec(dst)
|
|
|
|
|
1995-08-31 13:40:03 +00:00
|
|
|
ifp = open(srcfss.as_pathname(), 'rb')
|
|
|
|
ofp = open(dstfss.as_pathname(), 'wb')
|
1995-08-14 12:38:42 +00:00
|
|
|
d = ifp.read(BUFSIZ)
|
|
|
|
while d:
|
|
|
|
ofp.write(d)
|
|
|
|
d = ifp.read(BUFSIZ)
|
|
|
|
ifp.close()
|
|
|
|
ofp.close()
|
|
|
|
|
1996-03-12 13:33:34 +00:00
|
|
|
ifp = openrf(srcfss.as_pathname(), '*rb')
|
|
|
|
ofp = openrf(dstfss.as_pathname(), '*wb')
|
1995-08-14 12:38:42 +00:00
|
|
|
d = ifp.read(BUFSIZ)
|
|
|
|
while d:
|
|
|
|
ofp.write(d)
|
|
|
|
d = ifp.read(BUFSIZ)
|
|
|
|
ifp.close()
|
|
|
|
ofp.close()
|
|
|
|
|
|
|
|
sf = srcfss.GetFInfo()
|
|
|
|
df = dstfss.GetFInfo()
|
1995-10-09 23:27:06 +00:00
|
|
|
df.Creator, df.Type = sf.Creator, sf.Type
|
2001-03-06 22:46:25 +00:00
|
|
|
if forcetype != None:
|
|
|
|
df.Type = forcetype
|
1995-10-09 23:27:06 +00:00
|
|
|
df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
|
1995-08-14 12:38:42 +00:00
|
|
|
dstfss.SetFInfo(df)
|
1996-09-15 22:13:26 +00:00
|
|
|
if copydates:
|
|
|
|
crdate, mddate, bkdate = srcfss.GetDates()
|
|
|
|
dstfss.SetDates(crdate, mddate, bkdate)
|
|
|
|
touched(dstfss)
|
1995-08-14 12:38:42 +00:00
|
|
|
|
1996-09-15 22:13:26 +00:00
|
|
|
def copytree(src, dst, copydates=1):
|
1995-08-14 12:38:42 +00:00
|
|
|
"""Copy a complete file tree to a new destination"""
|
|
|
|
if os.path.isdir(src):
|
1995-08-31 13:40:03 +00:00
|
|
|
mkdirs(dst)
|
1995-08-14 12:38:42 +00:00
|
|
|
files = os.listdir(src)
|
|
|
|
for f in files:
|
1996-09-15 22:13:26 +00:00
|
|
|
copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
|
1995-08-14 12:38:42 +00:00
|
|
|
else:
|
1996-09-15 22:13:26 +00:00
|
|
|
copy(src, dst, 1, copydates)
|