PageRenderTime 184ms CodeModel.GetById 81ms app.highlight 15ms RepoModel.GetById 85ms app.codeStats 0ms

/Lib/plat-mac/macostools.py

http://unladen-swallow.googlecode.com/
Python | 137 lines | 128 code | 1 blank | 8 comment | 3 complexity | e86ca529817f476dd387f5935c8b3ed3 MD5 | raw file
  1"""macostools - Various utility functions for MacOS.
  2
  3mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src'
  4copy(src, dst) - Full copy of 'src' to 'dst'
  5"""
  6
  7from warnings import warnpy3k
  8warnpy3k("In 3.x, the macostools module is removed.", stacklevel=2)
  9
 10from Carbon import Res
 11from Carbon import File, Files
 12import os
 13import MacOS
 14try:
 15    openrf = MacOS.openrf
 16except AttributeError:
 17    # Backward compatibility
 18    openrf = open
 19
 20Error = 'macostools.Error'
 21
 22BUFSIZ=0x80000      # Copy in 0.5Mb chunks
 23
 24COPY_FLAGS = (Files.kIsStationary|Files.kNameLocked|Files.kHasBundle|
 25              Files.kIsInvisible|Files.kIsAlias)
 26
 27#
 28# Not guaranteed to be correct or stay correct (Apple doesn't tell you
 29# how to do this), but it seems to work.
 30#
 31def 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)
 39    if relative:
 40        relativefsr = File.FSRef(relative)
 41        # ik mag er geen None in stoppen :-(
 42        alias = File.FSNewAlias(relativefsr, srcfsr)
 43    else:
 44        alias = srcfsr.FSNewAliasMinimal()
 45
 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, '')
 51    Res.CloseResFile(h)
 52
 53    dstfinfo = dstfss.FSpGetFInfo()
 54    dstfinfo.Flags = dstfinfo.Flags|0x8000    # Alias flag
 55    dstfss.FSpSetFInfo(dstfinfo)
 56
 57def mkdirs(dst):
 58    """Make directories leading to 'dst' if they don't exist yet"""
 59    if dst == '' or os.path.exists(dst):
 60        return
 61    head, tail = os.path.split(dst)
 62    if os.sep == ':' and not ':' in head:
 63        head = head + ':'
 64    mkdirs(head)
 65
 66    try:
 67        os.mkdir(dst, 0777)
 68    except OSError, e:
 69        # be happy if someone already created the path
 70        if e.errno != errno.EEXIST:
 71            raise
 72
 73
 74def touched(dst):
 75    """Tell the finder a file has changed. No-op on MacOSX."""
 76    import warnings
 77    warnings.warn("macostools.touched() has been deprecated",
 78                    DeprecationWarning, 2)
 79
 80def touched_ae(dst):
 81    """Tell the finder a file has changed"""
 82    pardir = os.path.split(dst)[0]
 83    if not pardir:
 84        pardir = os.curdir
 85    import Finder
 86    f = Finder.Finder()
 87    f.update(File.FSRef(pardir))
 88
 89def 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)
 93    if createpath:
 94        mkdirs(os.path.split(dst)[0])
 95
 96    ifp = open(src, 'rb')
 97    ofp = open(dst, 'wb')
 98    d = ifp.read(BUFSIZ)
 99    while d:
100        ofp.write(d)
101        d = ifp.read(BUFSIZ)
102    ifp.close()
103    ofp.close()
104
105    ifp = openrf(src, '*rb')
106    ofp = openrf(dst, '*wb')
107    d = ifp.read(BUFSIZ)
108    while d:
109        ofp.write(d)
110        d = ifp.read(BUFSIZ)
111    ifp.close()
112    ofp.close()
113
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:
120        df.Type = forcetype
121    df.Flags = (sf.Flags & COPY_FLAGS)
122    dstfss.FSpSetFInfo(df)
123    if copydates:
124        srcfsr = File.FSRef(src)
125        dstfsr = File.FSRef(dst)
126        catinfo, _, _, _ = srcfsr.FSGetCatalogInfo(Files.kFSCatInfoAllDates)
127        dstfsr.FSSetCatalogInfo(Files.kFSCatInfoAllDates, catinfo)
128
129def copytree(src, dst, copydates=1):
130    """Copy a complete file tree to a new destination"""
131    if os.path.isdir(src):
132        mkdirs(dst)
133        files = os.listdir(src)
134        for f in files:
135            copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
136    else:
137        copy(src, dst, 1, copydates)