mirror of
https://github.com/python/cpython.git
synced 2025-11-11 14:44:57 +00:00
Converted to not use macfs whenever possible.
This commit is contained in:
parent
60ffc2bafc
commit
2b88dec606
2 changed files with 132 additions and 129 deletions
|
|
@ -20,7 +20,7 @@ from types import *
|
||||||
from Carbon import AE
|
from Carbon import AE
|
||||||
from Carbon.AppleEvents import *
|
from Carbon.AppleEvents import *
|
||||||
import MacOS
|
import MacOS
|
||||||
import macfs
|
import Carbon.File
|
||||||
import StringIO
|
import StringIO
|
||||||
import aetypes
|
import aetypes
|
||||||
from aetypes import mkenum, mktype
|
from aetypes import mkenum, mktype
|
||||||
|
|
@ -59,8 +59,9 @@ unpacker_coercions = {
|
||||||
# Some python types we need in the packer:
|
# Some python types we need in the packer:
|
||||||
#
|
#
|
||||||
AEDescType = AE.AEDescType
|
AEDescType = AE.AEDescType
|
||||||
FSSType = macfs.FSSpecType
|
FSSType = Carbon.File.FSSpecType
|
||||||
AliasType = macfs.AliasType
|
FSRefType = Carbon.File.FSRefType
|
||||||
|
AliasType = Carbon.File.AliasType
|
||||||
|
|
||||||
def packkey(ae, key, value):
|
def packkey(ae, key, value):
|
||||||
if hasattr(key, 'which'):
|
if hasattr(key, 'which'):
|
||||||
|
|
@ -83,36 +84,35 @@ def pack(x, forcetype = None):
|
||||||
if x == None:
|
if x == None:
|
||||||
return AE.AECreateDesc('null', '')
|
return AE.AECreateDesc('null', '')
|
||||||
|
|
||||||
t = type(x)
|
if isinstance(x, AEDescType):
|
||||||
if t == AEDescType:
|
|
||||||
return x
|
return x
|
||||||
if t == FSSType:
|
if isinstance(x, FSSType):
|
||||||
return AE.AECreateDesc('fss ', x.data)
|
return AE.AECreateDesc('fss ', x.data)
|
||||||
if t == AliasType:
|
if isinstance(x, AliasType):
|
||||||
return AE.AECreateDesc('alis', x.data)
|
return AE.AECreateDesc('alis', x.data)
|
||||||
if t == IntType:
|
if isinstance(x, IntType):
|
||||||
return AE.AECreateDesc('long', struct.pack('l', x))
|
return AE.AECreateDesc('long', struct.pack('l', x))
|
||||||
if t == FloatType:
|
if isinstance(x, FloatType):
|
||||||
return AE.AECreateDesc('doub', struct.pack('d', x))
|
return AE.AECreateDesc('doub', struct.pack('d', x))
|
||||||
if t == StringType:
|
if isinstance(x, StringType):
|
||||||
return AE.AECreateDesc('TEXT', x)
|
return AE.AECreateDesc('TEXT', x)
|
||||||
if t == UnicodeType:
|
if isinstance(x, UnicodeType):
|
||||||
data = t.encode('utf16')
|
data = t.encode('utf16')
|
||||||
if data[:2] == '\xfe\xff':
|
if data[:2] == '\xfe\xff':
|
||||||
data = data[2:]
|
data = data[2:]
|
||||||
return AE.AECreateDesc('utxt', data)
|
return AE.AECreateDesc('utxt', data)
|
||||||
if t == ListType:
|
if isinstance(x, ListType):
|
||||||
list = AE.AECreateList('', 0)
|
list = AE.AECreateList('', 0)
|
||||||
for item in x:
|
for item in x:
|
||||||
list.AEPutDesc(0, pack(item))
|
list.AEPutDesc(0, pack(item))
|
||||||
return list
|
return list
|
||||||
if t == DictionaryType:
|
if isinstance(x, DictionaryType):
|
||||||
record = AE.AECreateList('', 1)
|
record = AE.AECreateList('', 1)
|
||||||
for key, value in x.items():
|
for key, value in x.items():
|
||||||
packkey(record, key, value)
|
packkey(record, key, value)
|
||||||
#record.AEPutParamDesc(key, pack(value))
|
#record.AEPutParamDesc(key, pack(value))
|
||||||
return record
|
return record
|
||||||
if t == InstanceType and hasattr(x, '__aepack__'):
|
if hasattr(x, '__aepack__'):
|
||||||
return x.__aepack__()
|
return x.__aepack__()
|
||||||
if hasattr(x, 'which'):
|
if hasattr(x, 'which'):
|
||||||
return AE.AECreateDesc('TEXT', x.which)
|
return AE.AECreateDesc('TEXT', x.which)
|
||||||
|
|
@ -144,7 +144,7 @@ def unpack(desc, formodulename=""):
|
||||||
record = desc.AECoerceDesc('reco')
|
record = desc.AECoerceDesc('reco')
|
||||||
return mkaetext(unpack(record, formodulename))
|
return mkaetext(unpack(record, formodulename))
|
||||||
if t == typeAlias:
|
if t == typeAlias:
|
||||||
return macfs.RawAlias(desc.data)
|
return Carbon.File.Alias(rawdata=desc.data)
|
||||||
# typeAppleEvent returned as unknown
|
# typeAppleEvent returned as unknown
|
||||||
if t == typeBoolean:
|
if t == typeBoolean:
|
||||||
return struct.unpack('b', desc.data)[0]
|
return struct.unpack('b', desc.data)[0]
|
||||||
|
|
@ -165,7 +165,7 @@ def unpack(desc, formodulename=""):
|
||||||
data = desc.data
|
data = desc.data
|
||||||
return struct.unpack('d', data)[0]
|
return struct.unpack('d', data)[0]
|
||||||
if t == typeFSS:
|
if t == typeFSS:
|
||||||
return macfs.RawFSSpec(desc.data)
|
return Carbon.File.FSSpec(rawdata=desc.data)
|
||||||
if t == typeInsertionLoc:
|
if t == typeInsertionLoc:
|
||||||
record = desc.AECoerceDesc('reco')
|
record = desc.AECoerceDesc('reco')
|
||||||
return mkinsertionloc(unpack(record, formodulename))
|
return mkinsertionloc(unpack(record, formodulename))
|
||||||
|
|
@ -353,8 +353,8 @@ def _test():
|
||||||
None,
|
None,
|
||||||
['a', 'list', 'of', 'strings'],
|
['a', 'list', 'of', 'strings'],
|
||||||
{'key1': 'value1', 'key2':'value2'},
|
{'key1': 'value1', 'key2':'value2'},
|
||||||
macfs.FSSpec(':'),
|
Carbon.File.FSSpec(os.curdir),
|
||||||
macfs.FSSpec(':').NewAliasMinimal(),
|
Carbon.File.FSSpec(os.curdir).NewAliasMinimal(),
|
||||||
aetypes.Enum('enum'),
|
aetypes.Enum('enum'),
|
||||||
aetypes.Type('type'),
|
aetypes.Type('type'),
|
||||||
aetypes.Keyword('kwrd'),
|
aetypes.Keyword('kwrd'),
|
||||||
|
|
|
||||||
|
|
@ -4,134 +4,137 @@ mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src'
|
||||||
copy(src, dst) - Full copy of 'src' to 'dst'
|
copy(src, dst) - Full copy of 'src' to 'dst'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import macfs
|
|
||||||
from Carbon import Res
|
from Carbon import Res
|
||||||
|
from Carbon import File, Files
|
||||||
import os
|
import os
|
||||||
from MACFS import *
|
import sys
|
||||||
import MacOS
|
import MacOS
|
||||||
import time
|
import time
|
||||||
try:
|
try:
|
||||||
openrf = MacOS.openrf
|
openrf = MacOS.openrf
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# Backward compatability
|
# Backward compatability
|
||||||
openrf = open
|
openrf = open
|
||||||
|
|
||||||
Error = 'macostools.Error'
|
Error = 'macostools.Error'
|
||||||
|
|
||||||
BUFSIZ=0x80000 # Copy in 0.5Mb chunks
|
BUFSIZ=0x80000 # Copy in 0.5Mb chunks
|
||||||
|
|
||||||
|
COPY_FLAGS = (Files.kIsStationary|Files.kNameLocked|Files.kHasBundle|
|
||||||
|
Files.kIsInvisible|Files.kIsAlias)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Not guaranteed to be correct or stay correct (Apple doesn't tell you
|
# Not guaranteed to be correct or stay correct (Apple doesn't tell you
|
||||||
# how to do this), but it seems to work.
|
# how to do this), but it seems to work.
|
||||||
#
|
#
|
||||||
def mkalias(src, dst, relative=None):
|
def mkalias(src, dst, relative=None):
|
||||||
"""Create a finder alias"""
|
"""Create a finder alias"""
|
||||||
srcfss = macfs.FSSpec(src)
|
srcfsr = File.FSRef(src)
|
||||||
# The next line will fail under unix-Python if the destination
|
# The next line will fail under unix-Python if the destination
|
||||||
# doesn't exist yet. We should change this code to be fsref-based.
|
# doesn't exist yet. We should change this code to be fsref-based.
|
||||||
dstfss = macfs.FSSpec(dst)
|
dstdir, dstname = os.path.split(dst)
|
||||||
if relative:
|
if not dstdir: dstdir = os.curdir
|
||||||
relativefss = macfs.FSSpec(relative)
|
dstdirfsr = File.FSRef(dstdir)
|
||||||
# ik mag er geen None in stoppen :-(
|
if relative:
|
||||||
alias = srcfss.NewAlias(relativefss)
|
relativefsr = File.FSRef(relative)
|
||||||
else:
|
# ik mag er geen None in stoppen :-(
|
||||||
alias = srcfss.NewAlias()
|
alias = File.FSNewAlias(relativefsr, srcfsr)
|
||||||
|
else:
|
||||||
if os.path.isdir(src):
|
alias = srcfsr.FSNewAliasMinimal()
|
||||||
cr, tp = 'MACS', 'fdrp'
|
|
||||||
else:
|
dstfsr, dstfss = Res.FSCreateResourceFile(dstdirfsr, unicode(dstname),
|
||||||
cr, tp = srcfss.GetCreatorType()
|
File.FSGetResourceForkName())
|
||||||
|
h = Res.FSOpenResourceFile(dstfsr, File.FSGetResourceForkName(), 3)
|
||||||
Res.FSpCreateResFile(dstfss, cr, tp, -1)
|
resource = Res.Resource(alias.data)
|
||||||
h = Res.FSpOpenResFile(dstfss, 3)
|
resource.AddResource('alis', 0, '')
|
||||||
resource = Res.Resource(alias.data)
|
Res.CloseResFile(h)
|
||||||
resource.AddResource('alis', 0, '')
|
|
||||||
Res.CloseResFile(h)
|
dstfinfo = dstfss.FSpGetFInfo()
|
||||||
|
dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
|
||||||
dstfinfo = dstfss.GetFInfo()
|
dstfss.FSpSetFInfo(dstfinfo)
|
||||||
dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
|
|
||||||
dstfss.SetFInfo(dstfinfo)
|
|
||||||
|
|
||||||
def mkdirs(dst):
|
def mkdirs(dst):
|
||||||
"""Make directories leading to 'dst' if they don't exist yet"""
|
"""Make directories leading to 'dst' if they don't exist yet"""
|
||||||
if dst == '' or os.path.exists(dst):
|
if dst == '' or os.path.exists(dst):
|
||||||
return
|
return
|
||||||
head, tail = os.path.split(dst)
|
head, tail = os.path.split(dst)
|
||||||
if os.sep == ':' and not ':' in head:
|
if os.sep == ':' and not ':' in head:
|
||||||
head = head + ':'
|
head = head + ':'
|
||||||
mkdirs(head)
|
mkdirs(head)
|
||||||
os.mkdir(dst, 0777)
|
os.mkdir(dst, 0777)
|
||||||
|
|
||||||
def touched(dst):
|
def touched(dst):
|
||||||
"""Tell the finder a file has changed"""
|
"""Tell the finder a file has changed. No-op on MacOSX."""
|
||||||
file_fss = macfs.FSSpec(dst)
|
if sys.platform != 'mac': return
|
||||||
vRefNum, dirID, name = file_fss.as_tuple()
|
import macfs
|
||||||
dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
|
file_fss = macfs.FSSpec(dst)
|
||||||
crdate, moddate, bkdate = dir_fss.GetDates()
|
vRefNum, dirID, name = file_fss.as_tuple()
|
||||||
now = time.time()
|
dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
|
||||||
if now == moddate:
|
crdate, moddate, bkdate = dir_fss.GetDates()
|
||||||
now = now + 1
|
now = time.time()
|
||||||
try:
|
if now == moddate:
|
||||||
dir_fss.SetDates(crdate, now, bkdate)
|
now = now + 1
|
||||||
except macfs.error:
|
try:
|
||||||
pass
|
dir_fss.SetDates(crdate, now, bkdate)
|
||||||
|
except macfs.error:
|
||||||
|
pass
|
||||||
|
|
||||||
def touched_ae(dst):
|
def touched_ae(dst):
|
||||||
"""Tell the finder a file has changed"""
|
"""Tell the finder a file has changed"""
|
||||||
import Finder
|
pardir = os.path.split(dst)[0]
|
||||||
f = Finder.Finder()
|
if not pardir:
|
||||||
file_fss = macfs.FSSpec(dst)
|
pardir = os.curdir
|
||||||
vRefNum, dirID, name = file_fss.as_tuple()
|
import Finder
|
||||||
dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
|
f = Finder.Finder()
|
||||||
f.update(dir_fss)
|
f.update(File.FSRef(pardir))
|
||||||
|
|
||||||
def copy(src, dst, createpath=0, copydates=1, forcetype=None):
|
def copy(src, dst, createpath=0, copydates=1, forcetype=None):
|
||||||
"""Copy a file, including finder info, resource fork, etc"""
|
"""Copy a file, including finder info, resource fork, etc"""
|
||||||
if hasattr(src, 'as_pathname'):
|
src = File.pathname(src)
|
||||||
src = src.as_pathname()
|
dst = File.pathname(dst)
|
||||||
if hasattr(dst, 'as_pathname'):
|
if createpath:
|
||||||
dst = dst.as_pathname()
|
mkdirs(os.path.split(dst)[0])
|
||||||
if createpath:
|
|
||||||
mkdirs(os.path.split(dst)[0])
|
ifp = open(src, 'rb')
|
||||||
|
ofp = open(dst, 'wb')
|
||||||
ifp = open(src, 'rb')
|
d = ifp.read(BUFSIZ)
|
||||||
ofp = open(dst, 'wb')
|
while d:
|
||||||
d = ifp.read(BUFSIZ)
|
ofp.write(d)
|
||||||
while d:
|
d = ifp.read(BUFSIZ)
|
||||||
ofp.write(d)
|
ifp.close()
|
||||||
d = ifp.read(BUFSIZ)
|
ofp.close()
|
||||||
ifp.close()
|
|
||||||
ofp.close()
|
ifp = openrf(src, '*rb')
|
||||||
|
ofp = openrf(dst, '*wb')
|
||||||
ifp = openrf(src, '*rb')
|
d = ifp.read(BUFSIZ)
|
||||||
ofp = openrf(dst, '*wb')
|
while d:
|
||||||
d = ifp.read(BUFSIZ)
|
ofp.write(d)
|
||||||
while d:
|
d = ifp.read(BUFSIZ)
|
||||||
ofp.write(d)
|
ifp.close()
|
||||||
d = ifp.read(BUFSIZ)
|
ofp.close()
|
||||||
ifp.close()
|
|
||||||
ofp.close()
|
srcfss = File.FSSpec(src)
|
||||||
|
dstfss = File.FSSpec(dst)
|
||||||
srcfss = macfs.FSSpec(src)
|
sf = srcfss.FSpGetFInfo()
|
||||||
dstfss = macfs.FSSpec(dst)
|
df = dstfss.FSpGetFInfo()
|
||||||
sf = srcfss.GetFInfo()
|
df.Creator, df.Type = sf.Creator, sf.Type
|
||||||
df = dstfss.GetFInfo()
|
if forcetype != None:
|
||||||
df.Creator, df.Type = sf.Creator, sf.Type
|
df.Type = forcetype
|
||||||
if forcetype != None:
|
df.Flags = (sf.Flags & COPY_FLAGS)
|
||||||
df.Type = forcetype
|
dstfss.FSpSetFInfo(df)
|
||||||
df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
|
if copydates:
|
||||||
dstfss.SetFInfo(df)
|
srcfsr = File.FSRef(src)
|
||||||
if copydates:
|
dstfsr = File.FSRef(dst)
|
||||||
crdate, mddate, bkdate = srcfss.GetDates()
|
catinfo, _, _, _ = srcfsr.FSGetCatalogInfo(Files.kFSCatInfoAllDates)
|
||||||
dstfss.SetDates(crdate, mddate, bkdate)
|
dstfsr.FSSetCatalogInfo(Files.kFSCatInfoAllDates, catinfo)
|
||||||
touched(dstfss)
|
touched(dstfss)
|
||||||
|
|
||||||
def copytree(src, dst, copydates=1):
|
def copytree(src, dst, copydates=1):
|
||||||
"""Copy a complete file tree to a new destination"""
|
"""Copy a complete file tree to a new destination"""
|
||||||
if os.path.isdir(src):
|
if os.path.isdir(src):
|
||||||
mkdirs(dst)
|
mkdirs(dst)
|
||||||
files = os.listdir(src)
|
files = os.listdir(src)
|
||||||
for f in files:
|
for f in files:
|
||||||
copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
|
copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
|
||||||
else:
|
else:
|
||||||
copy(src, dst, 1, copydates)
|
copy(src, dst, 1, copydates)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue