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