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'
|
|
|
|
|
|
|
|
FSSpecType = type(macfs.FSSpec(':'))
|
|
|
|
|
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)
|
|
|
|
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()
|
1995-08-14 09:38:42 -03: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 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)
|
|
|
|
if not ':' in head:
|
|
|
|
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"""
|
1995-08-31 10:40:03 -03:00
|
|
|
if createpath:
|
|
|
|
mkdirs(os.path.split(dst)[0])
|
1995-08-14 09:38:42 -03:00
|
|
|
srcfss = macfs.FSSpec(src)
|
|
|
|
dstfss = macfs.FSSpec(dst)
|
|
|
|
|
1995-08-31 10:40:03 -03:00
|
|
|
ifp = open(srcfss.as_pathname(), 'rb')
|
|
|
|
ofp = open(dstfss.as_pathname(), '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()
|
|
|
|
|
1996-03-12 09:33:34 -04:00
|
|
|
ifp = openrf(srcfss.as_pathname(), '*rb')
|
|
|
|
ofp = openrf(dstfss.as_pathname(), '*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()
|
|
|
|
|
|
|
|
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)
|