home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / system / python / Lib / plat-mac / macostools.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-07-20  |  4.9 KB  |  150 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. """macostools - Various utility functions for MacOS.
  5.  
  6. mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src'
  7. copy(src, dst) - Full copy of 'src' to 'dst'
  8. """
  9. from Carbon import Res
  10. from Carbon import File, Files
  11. import os
  12. import sys
  13. import MacOS
  14. import time
  15.  
  16. try:
  17.     openrf = MacOS.openrf
  18. except AttributeError:
  19.     openrf = open
  20.  
  21. Error = 'macostools.Error'
  22. BUFSIZ = 524288
  23. COPY_FLAGS = Files.kIsStationary | Files.kNameLocked | Files.kHasBundle | Files.kIsInvisible | Files.kIsAlias
  24.  
  25. def mkalias(src, dst, relative = None):
  26.     '''Create a finder alias'''
  27.     srcfsr = File.FSRef(src)
  28.     (dstdir, dstname) = os.path.split(dst)
  29.     if not dstdir:
  30.         dstdir = os.curdir
  31.     
  32.     dstdirfsr = File.FSRef(dstdir)
  33.     if relative:
  34.         relativefsr = File.FSRef(relative)
  35.         alias = File.FSNewAlias(relativefsr, srcfsr)
  36.     else:
  37.         alias = srcfsr.FSNewAliasMinimal()
  38.     (dstfsr, dstfss) = Res.FSCreateResourceFile(dstdirfsr, unicode(dstname), File.FSGetResourceForkName())
  39.     h = Res.FSOpenResourceFile(dstfsr, File.FSGetResourceForkName(), 3)
  40.     resource = Res.Resource(alias.data)
  41.     resource.AddResource('alis', 0, '')
  42.     Res.CloseResFile(h)
  43.     dstfinfo = dstfss.FSpGetFInfo()
  44.     dstfinfo.Flags = dstfinfo.Flags | 32768
  45.     dstfss.FSpSetFInfo(dstfinfo)
  46.  
  47.  
  48. def mkdirs(dst):
  49.     """Make directories leading to 'dst' if they don't exist yet"""
  50.     if dst == '' or os.path.exists(dst):
  51.         return None
  52.     
  53.     (head, tail) = os.path.split(dst)
  54.     if os.sep == ':' and ':' not in head:
  55.         head = head + ':'
  56.     
  57.     mkdirs(head)
  58.     os.mkdir(dst, 511)
  59.  
  60.  
  61. def touched(dst):
  62.     '''Tell the finder a file has changed. No-op on MacOSX.'''
  63.     if sys.platform != 'mac':
  64.         return None
  65.     
  66.     import warnings
  67.     warnings.filterwarnings('ignore', 'macfs.*', DeprecationWarning, __name__)
  68.     import macfs
  69.     file_fss = macfs.FSSpec(dst)
  70.     (vRefNum, dirID, name) = file_fss.as_tuple()
  71.     dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
  72.     (crdate, moddate, bkdate) = dir_fss.GetDates()
  73.     now = time.time()
  74.     if now == moddate:
  75.         now = now + 1
  76.     
  77.     
  78.     try:
  79.         dir_fss.SetDates(crdate, now, bkdate)
  80.     except macfs.error:
  81.         pass
  82.  
  83.  
  84.  
  85. def touched_ae(dst):
  86.     '''Tell the finder a file has changed'''
  87.     pardir = os.path.split(dst)[0]
  88.     if not pardir:
  89.         pardir = os.curdir
  90.     
  91.     import Finder
  92.     f = Finder.Finder()
  93.     f.update(File.FSRef(pardir))
  94.  
  95.  
  96. def copy(src, dst, createpath = 0, copydates = 1, forcetype = None):
  97.     '''Copy a file, including finder info, resource fork, etc'''
  98.     src = File.pathname(src)
  99.     dst = File.pathname(dst)
  100.     if createpath:
  101.         mkdirs(os.path.split(dst)[0])
  102.     
  103.     ifp = open(src, 'rb')
  104.     ofp = open(dst, 'wb')
  105.     d = ifp.read(BUFSIZ)
  106.     while d:
  107.         ofp.write(d)
  108.         d = ifp.read(BUFSIZ)
  109.     ifp.close()
  110.     ofp.close()
  111.     ifp = openrf(src, '*rb')
  112.     ofp = openrf(dst, '*wb')
  113.     d = ifp.read(BUFSIZ)
  114.     while d:
  115.         ofp.write(d)
  116.         d = ifp.read(BUFSIZ)
  117.     ifp.close()
  118.     ofp.close()
  119.     srcfss = File.FSSpec(src)
  120.     dstfss = File.FSSpec(dst)
  121.     sf = srcfss.FSpGetFInfo()
  122.     df = dstfss.FSpGetFInfo()
  123.     df.Creator = sf.Creator
  124.     df.Type = sf.Type
  125.     if forcetype != None:
  126.         df.Type = forcetype
  127.     
  128.     df.Flags = sf.Flags & COPY_FLAGS
  129.     dstfss.FSpSetFInfo(df)
  130.     if copydates:
  131.         srcfsr = File.FSRef(src)
  132.         dstfsr = File.FSRef(dst)
  133.         (catinfo, _, _, _) = srcfsr.FSGetCatalogInfo(Files.kFSCatInfoAllDates)
  134.         dstfsr.FSSetCatalogInfo(Files.kFSCatInfoAllDates, catinfo)
  135.     
  136.     touched(dstfss)
  137.  
  138.  
  139. def copytree(src, dst, copydates = 1):
  140.     '''Copy a complete file tree to a new destination'''
  141.     if os.path.isdir(src):
  142.         mkdirs(dst)
  143.         files = os.listdir(src)
  144.         for f in files:
  145.             copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
  146.         
  147.     else:
  148.         copy(src, dst, 1, copydates)
  149.  
  150.