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 | |
| 7 | import macfs |
| 8 | import Res |
| 9 | import os |
Jack Jansen | a8a277c | 1995-10-09 23:27:06 +0000 | [diff] [blame] | 10 | from MACFS import * |
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: |
| 14 | openrf = MacOS.openrf |
| 15 | except AttributeError: |
| 16 | # Backward compatability |
| 17 | openrf = open |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 18 | |
| 19 | Error = 'macostools.Error' |
| 20 | |
| 21 | FSSpecType = type(macfs.FSSpec(':')) |
| 22 | |
Jack Jansen | 0603319 | 1996-03-12 13:33:34 +0000 | [diff] [blame] | 23 | BUFSIZ=0x80000 # Copy in 0.5Mb chunks |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 24 | |
| 25 | # |
| 26 | # Not guaranteed to be correct or stay correct (Apple doesn't tell you |
| 27 | # how to do this), but it seems to work. |
| 28 | # |
Jack Jansen | 48f662d | 1997-08-08 15:00:59 +0000 | [diff] [blame] | 29 | def mkalias(src, dst, relative=None): |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 30 | """Create a finder alias""" |
| 31 | srcfss = macfs.FSSpec(src) |
| 32 | dstfss = macfs.FSSpec(dst) |
Jack Jansen | b8fd1f1 | 1998-04-15 14:35:16 +0000 | [diff] [blame] | 33 | if relative: |
| 34 | relativefss = macfs.FSSpec(relative) |
| 35 | # ik mag er geen None in stoppen :-( |
| 36 | alias = srcfss.NewAlias(relativefss) |
| 37 | else: |
| 38 | alias = srcfss.NewAlias() |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 39 | srcfinfo = srcfss.GetFInfo() |
| 40 | |
| 41 | Res.FSpCreateResFile(dstfss, srcfinfo.Creator, srcfinfo.Type, -1) |
| 42 | h = Res.FSpOpenResFile(dstfss, 3) |
| 43 | resource = Res.Resource(alias.data) |
| 44 | resource.AddResource('alis', 0, '') |
| 45 | Res.CloseResFile(h) |
| 46 | |
| 47 | dstfinfo = dstfss.GetFInfo() |
| 48 | dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag |
| 49 | dstfss.SetFInfo(dstfinfo) |
| 50 | |
Jack Jansen | 423c798 | 1995-08-31 13:40:03 +0000 | [diff] [blame] | 51 | def mkdirs(dst): |
| 52 | """Make directories leading to 'dst' if they don't exist yet""" |
| 53 | if dst == '' or os.path.exists(dst): |
| 54 | return |
| 55 | head, tail = os.path.split(dst) |
Jack Jansen | 423c798 | 1995-08-31 13:40:03 +0000 | [diff] [blame] | 56 | if not ':' in head: |
| 57 | head = head + ':' |
| 58 | mkdirs(head) |
| 59 | os.mkdir(dst, 0777) |
| 60 | |
Jack Jansen | 57d53a9 | 1996-09-15 22:13:26 +0000 | [diff] [blame] | 61 | def touched(dst): |
| 62 | """Tell the finder a file has changed""" |
| 63 | file_fss = macfs.FSSpec(dst) |
| 64 | vRefNum, dirID, name = file_fss.as_tuple() |
| 65 | dir_fss = macfs.FSSpec((vRefNum, dirID, '')) |
| 66 | crdate, moddate, bkdate = dir_fss.GetDates() |
| 67 | now = time.time() |
| 68 | if now == moddate: |
| 69 | now = now + 1 |
| 70 | dir_fss.SetDates(crdate, now, bkdate) |
| 71 | |
| 72 | def copy(src, dst, createpath=0, copydates=1): |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 73 | """Copy a file, including finder info, resource fork, etc""" |
Jack Jansen | 423c798 | 1995-08-31 13:40:03 +0000 | [diff] [blame] | 74 | if createpath: |
| 75 | mkdirs(os.path.split(dst)[0]) |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 76 | srcfss = macfs.FSSpec(src) |
| 77 | dstfss = macfs.FSSpec(dst) |
| 78 | |
Jack Jansen | 423c798 | 1995-08-31 13:40:03 +0000 | [diff] [blame] | 79 | ifp = open(srcfss.as_pathname(), 'rb') |
| 80 | ofp = open(dstfss.as_pathname(), 'wb') |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 81 | d = ifp.read(BUFSIZ) |
| 82 | while d: |
| 83 | ofp.write(d) |
| 84 | d = ifp.read(BUFSIZ) |
| 85 | ifp.close() |
| 86 | ofp.close() |
| 87 | |
Jack Jansen | 0603319 | 1996-03-12 13:33:34 +0000 | [diff] [blame] | 88 | ifp = openrf(srcfss.as_pathname(), '*rb') |
| 89 | ofp = openrf(dstfss.as_pathname(), '*wb') |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 90 | d = ifp.read(BUFSIZ) |
| 91 | while d: |
| 92 | ofp.write(d) |
| 93 | d = ifp.read(BUFSIZ) |
| 94 | ifp.close() |
| 95 | ofp.close() |
| 96 | |
| 97 | sf = srcfss.GetFInfo() |
| 98 | df = dstfss.GetFInfo() |
Jack Jansen | a8a277c | 1995-10-09 23:27:06 +0000 | [diff] [blame] | 99 | df.Creator, df.Type = sf.Creator, sf.Type |
| 100 | df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias)) |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 101 | dstfss.SetFInfo(df) |
Jack Jansen | 57d53a9 | 1996-09-15 22:13:26 +0000 | [diff] [blame] | 102 | if copydates: |
| 103 | crdate, mddate, bkdate = srcfss.GetDates() |
| 104 | dstfss.SetDates(crdate, mddate, bkdate) |
| 105 | touched(dstfss) |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 106 | |
Jack Jansen | 57d53a9 | 1996-09-15 22:13:26 +0000 | [diff] [blame] | 107 | def copytree(src, dst, copydates=1): |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 108 | """Copy a complete file tree to a new destination""" |
| 109 | if os.path.isdir(src): |
Jack Jansen | 423c798 | 1995-08-31 13:40:03 +0000 | [diff] [blame] | 110 | mkdirs(dst) |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 111 | files = os.listdir(src) |
| 112 | for f in files: |
Jack Jansen | 57d53a9 | 1996-09-15 22:13:26 +0000 | [diff] [blame] | 113 | copytree(os.path.join(src, f), os.path.join(dst, f), copydates) |
Jack Jansen | 01c2309 | 1995-08-14 12:38:42 +0000 | [diff] [blame] | 114 | else: |
Jack Jansen | 57d53a9 | 1996-09-15 22:13:26 +0000 | [diff] [blame] | 115 | copy(src, dst, 1, copydates) |