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