Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 1 | """tools for BuildApplet and BuildApplication""" |
| 2 | |
| 3 | import sys |
| 4 | import os |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 5 | import imp |
| 6 | import marshal |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame] | 7 | from Carbon import Res |
Jack Jansen | cc94764 | 2003-02-02 23:03:50 +0000 | [diff] [blame] | 8 | import Carbon.Files |
| 9 | import Carbon.File |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 10 | import MacOS |
| 11 | import macostools |
Jack Jansen | b2e33fe | 2002-03-29 21:21:28 +0000 | [diff] [blame] | 12 | import macresource |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 13 | import EasyDialogs |
Jack Jansen | b2e33fe | 2002-03-29 21:21:28 +0000 | [diff] [blame] | 14 | import shutil |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | BuildError = "BuildError" |
| 18 | |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 19 | # .pyc file (and 'PYC ' resource magic number) |
| 20 | MAGIC = imp.get_magic() |
| 21 | |
| 22 | # Template file (searched on sys.path) |
Jack Jansen | 3b80526 | 1999-02-14 23:12:06 +0000 | [diff] [blame] | 23 | TEMPLATE = "PythonInterpreter" |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 24 | |
| 25 | # Specification of our resource |
| 26 | RESTYPE = 'PYC ' |
| 27 | RESNAME = '__main__' |
| 28 | |
| 29 | # A resource with this name sets the "owner" (creator) of the destination |
Jack Jansen | 81da9f1 | 1999-03-17 22:57:55 +0000 | [diff] [blame] | 30 | # It should also have ID=0. Either of these alone is not enough. |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 31 | OWNERNAME = "owner resource" |
| 32 | |
Jack Jansen | 81da9f1 | 1999-03-17 22:57:55 +0000 | [diff] [blame] | 33 | # Default applet creator code |
| 34 | DEFAULT_APPLET_CREATOR="Pyta" |
| 35 | |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 36 | # OpenResFile mode parameters |
| 37 | READ = 1 |
| 38 | WRITE = 2 |
| 39 | |
Jack Jansen | cc94764 | 2003-02-02 23:03:50 +0000 | [diff] [blame] | 40 | # Parameter for FSOpenResourceFile |
| 41 | RESOURCE_FORK_NAME=Carbon.File.FSGetResourceForkName() |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 42 | |
Jack Jansen | a4f8e58 | 2001-02-17 23:30:19 +0000 | [diff] [blame] | 43 | def findtemplate(template=None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 44 | """Locate the applet template along sys.path""" |
| 45 | if MacOS.runtimemodel == 'macho': |
| 46 | return None |
| 47 | if not template: |
| 48 | template=TEMPLATE |
| 49 | for p in sys.path: |
| 50 | file = os.path.join(p, template) |
| 51 | try: |
| 52 | file, d1, d2 = Carbon.File.FSResolveAliasFile(file, 1) |
| 53 | break |
| 54 | except (Carbon.File.Error, ValueError): |
| 55 | continue |
| 56 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 57 | raise BuildError, "Template %r not found on sys.path" % (template,) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 58 | file = file.as_pathname() |
| 59 | return file |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 60 | |
| 61 | def process(template, filename, destname, copy_codefragment=0, |
Jack Jansen | c77f6df | 2004-12-27 15:51:03 +0000 | [diff] [blame] | 62 | rsrcname=None, others=[], raw=0, progress="default", destroot=""): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 63 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 64 | if progress == "default": |
| 65 | progress = EasyDialogs.ProgressBar("Processing %s..."%os.path.split(filename)[1], 120) |
| 66 | progress.label("Compiling...") |
| 67 | progress.inc(0) |
| 68 | # check for the script name being longer than 32 chars. This may trigger a bug |
| 69 | # on OSX that can destroy your sourcefile. |
| 70 | if '#' in os.path.split(filename)[1]: |
| 71 | raise BuildError, "BuildApplet could destroy your sourcefile on OSX, please rename: %s" % filename |
| 72 | # Read the source and compile it |
| 73 | # (there's no point overwriting the destination if it has a syntax error) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 74 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 75 | fp = open(filename, 'rU') |
| 76 | text = fp.read() |
| 77 | fp.close() |
| 78 | try: |
| 79 | code = compile(text + '\n', filename, "exec") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 80 | except SyntaxError as arg: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 81 | raise BuildError, "Syntax error in script %s: %s" % (filename, arg) |
| 82 | except EOFError: |
| 83 | raise BuildError, "End-of-file in script %s" % (filename,) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 84 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 85 | # Set the destination file name. Note that basename |
| 86 | # does contain the whole filepath, only a .py is stripped. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 87 | |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 88 | if filename[-3:].lower() == ".py": |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 89 | basename = filename[:-3] |
| 90 | if MacOS.runtimemodel != 'macho' and not destname: |
| 91 | destname = basename |
| 92 | else: |
| 93 | basename = filename |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 94 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 95 | if not destname: |
| 96 | if MacOS.runtimemodel == 'macho': |
| 97 | destname = basename + '.app' |
| 98 | else: |
| 99 | destname = basename + '.applet' |
| 100 | if not rsrcname: |
| 101 | rsrcname = basename + '.rsrc' |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 102 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 103 | # Try removing the output file. This fails in MachO, but it should |
| 104 | # do any harm. |
| 105 | try: |
| 106 | os.remove(destname) |
| 107 | except os.error: |
| 108 | pass |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 109 | process_common(template, progress, code, rsrcname, destname, 0, |
Jack Jansen | c77f6df | 2004-12-27 15:51:03 +0000 | [diff] [blame] | 110 | copy_codefragment, raw, others, filename, destroot) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 111 | |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 112 | |
| 113 | def update(template, filename, output): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 114 | if MacOS.runtimemodel == 'macho': |
| 115 | raise BuildError, "No updating yet for MachO applets" |
| 116 | if progress: |
| 117 | progress = EasyDialogs.ProgressBar("Updating %s..."%os.path.split(filename)[1], 120) |
| 118 | else: |
| 119 | progress = None |
| 120 | if not output: |
| 121 | output = filename + ' (updated)' |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 122 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 123 | # Try removing the output file |
| 124 | try: |
| 125 | os.remove(output) |
| 126 | except os.error: |
| 127 | pass |
| 128 | process_common(template, progress, None, filename, output, 1, 1) |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 129 | |
| 130 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 131 | def process_common(template, progress, code, rsrcname, destname, is_update, |
Jack Jansen | c77f6df | 2004-12-27 15:51:03 +0000 | [diff] [blame] | 132 | copy_codefragment, raw=0, others=[], filename=None, destroot=""): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 133 | if MacOS.runtimemodel == 'macho': |
| 134 | return process_common_macho(template, progress, code, rsrcname, destname, |
Jack Jansen | c77f6df | 2004-12-27 15:51:03 +0000 | [diff] [blame] | 135 | is_update, raw, others, filename, destroot) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 136 | if others: |
| 137 | raise BuildError, "Extra files only allowed for MachoPython applets" |
| 138 | # Create FSSpecs for the various files |
| 139 | template_fsr, d1, d2 = Carbon.File.FSResolveAliasFile(template, 1) |
| 140 | template = template_fsr.as_pathname() |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 141 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 142 | # Copy data (not resources, yet) from the template |
| 143 | if progress: |
| 144 | progress.label("Copy data fork...") |
| 145 | progress.set(10) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 146 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 147 | if copy_codefragment: |
| 148 | tmpl = open(template, "rb") |
| 149 | dest = open(destname, "wb") |
| 150 | data = tmpl.read() |
| 151 | if data: |
| 152 | dest.write(data) |
| 153 | dest.close() |
| 154 | tmpl.close() |
| 155 | del dest |
| 156 | del tmpl |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 157 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 158 | # Open the output resource fork |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 159 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 160 | if progress: |
| 161 | progress.label("Copy resources...") |
| 162 | progress.set(20) |
| 163 | try: |
| 164 | output = Res.FSOpenResourceFile(destname, RESOURCE_FORK_NAME, WRITE) |
| 165 | except MacOS.Error: |
| 166 | destdir, destfile = os.path.split(destname) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame^] | 167 | Res.FSCreateResourceFile(destdir, str(destfile), RESOURCE_FORK_NAME) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 168 | output = Res.FSOpenResourceFile(destname, RESOURCE_FORK_NAME, WRITE) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 169 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 170 | # Copy the resources from the target specific resource template, if any |
| 171 | typesfound, ownertype = [], None |
| 172 | try: |
| 173 | input = Res.FSOpenResourceFile(rsrcname, RESOURCE_FORK_NAME, READ) |
| 174 | except (MacOS.Error, ValueError): |
| 175 | pass |
| 176 | if progress: |
| 177 | progress.inc(50) |
| 178 | else: |
| 179 | if is_update: |
| 180 | skip_oldfile = ['cfrg'] |
| 181 | else: |
| 182 | skip_oldfile = [] |
| 183 | typesfound, ownertype = copyres(input, output, skip_oldfile, 0, progress) |
| 184 | Res.CloseResFile(input) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 185 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 186 | # Check which resource-types we should not copy from the template |
| 187 | skiptypes = [] |
| 188 | if 'vers' in typesfound: skiptypes.append('vers') |
| 189 | if 'SIZE' in typesfound: skiptypes.append('SIZE') |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 190 | if 'BNDL' in typesfound: skiptypes = skiptypes + ['BNDL', 'FREF', 'icl4', |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 191 | 'icl8', 'ics4', 'ics8', 'ICN#', 'ics#'] |
| 192 | if not copy_codefragment: |
| 193 | skiptypes.append('cfrg') |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 194 | ## skipowner = (ownertype != None) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 195 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 196 | # Copy the resources from the template |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 197 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 198 | input = Res.FSOpenResourceFile(template, RESOURCE_FORK_NAME, READ) |
| 199 | dummy, tmplowner = copyres(input, output, skiptypes, 1, progress) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 200 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 201 | Res.CloseResFile(input) |
| 202 | ## if ownertype == None: |
| 203 | ## raise BuildError, "No owner resource found in either resource file or template" |
| 204 | # Make sure we're manipulating the output resource file now |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 205 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 206 | Res.UseResFile(output) |
Jack Jansen | 81da9f1 | 1999-03-17 22:57:55 +0000 | [diff] [blame] | 207 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 208 | if ownertype == None: |
| 209 | # No owner resource in the template. We have skipped the |
| 210 | # Python owner resource, so we have to add our own. The relevant |
| 211 | # bundle stuff is already included in the interpret/applet template. |
| 212 | newres = Res.Resource('\0') |
| 213 | newres.AddResource(DEFAULT_APPLET_CREATOR, 0, "Owner resource") |
| 214 | ownertype = DEFAULT_APPLET_CREATOR |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 215 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 216 | if code: |
| 217 | # Delete any existing 'PYC ' resource named __main__ |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 218 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 219 | try: |
| 220 | res = Res.Get1NamedResource(RESTYPE, RESNAME) |
| 221 | res.RemoveResource() |
| 222 | except Res.Error: |
| 223 | pass |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 224 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 225 | # Create the raw data for the resource from the code object |
| 226 | if progress: |
| 227 | progress.label("Write PYC resource...") |
| 228 | progress.set(120) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 229 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 230 | data = marshal.dumps(code) |
| 231 | del code |
| 232 | data = (MAGIC + '\0\0\0\0') + data |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 233 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 234 | # Create the resource and write it |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 235 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 236 | id = 0 |
| 237 | while id < 128: |
| 238 | id = Res.Unique1ID(RESTYPE) |
| 239 | res = Res.Resource(data) |
| 240 | res.AddResource(RESTYPE, id, RESNAME) |
| 241 | attrs = res.GetResAttrs() |
| 242 | attrs = attrs | 0x04 # set preload |
| 243 | res.SetResAttrs(attrs) |
| 244 | res.WriteResource() |
| 245 | res.ReleaseResource() |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 246 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 247 | # Close the output file |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 248 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 249 | Res.CloseResFile(output) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 250 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 251 | # Now set the creator, type and bundle bit of the destination. |
| 252 | # Done with FSSpec's, FSRef FInfo isn't good enough yet (2.3a1+) |
| 253 | dest_fss = Carbon.File.FSSpec(destname) |
| 254 | dest_finfo = dest_fss.FSpGetFInfo() |
| 255 | dest_finfo.Creator = ownertype |
| 256 | dest_finfo.Type = 'APPL' |
| 257 | dest_finfo.Flags = dest_finfo.Flags | Carbon.Files.kHasBundle | Carbon.Files.kIsShared |
| 258 | dest_finfo.Flags = dest_finfo.Flags & ~Carbon.Files.kHasBeenInited |
| 259 | dest_fss.FSpSetFInfo(dest_finfo) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 260 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 261 | macostools.touched(destname) |
| 262 | if progress: |
| 263 | progress.label("Done.") |
| 264 | progress.inc(0) |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 265 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 266 | def process_common_macho(template, progress, code, rsrcname, destname, is_update, |
Jack Jansen | c77f6df | 2004-12-27 15:51:03 +0000 | [diff] [blame] | 267 | raw=0, others=[], filename=None, destroot=""): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 268 | # Check that we have a filename |
| 269 | if filename is None: |
| 270 | raise BuildError, "Need source filename on MacOSX" |
| 271 | # First make sure the name ends in ".app" |
| 272 | if destname[-4:] != '.app': |
| 273 | destname = destname + '.app' |
| 274 | # Now deduce the short name |
| 275 | destdir, shortname = os.path.split(destname) |
| 276 | if shortname[-4:] == '.app': |
| 277 | # Strip the .app suffix |
| 278 | shortname = shortname[:-4] |
| 279 | # And deduce the .plist and .icns names |
| 280 | plistname = None |
| 281 | icnsname = None |
| 282 | if rsrcname and rsrcname[-5:] == '.rsrc': |
| 283 | tmp = rsrcname[:-5] |
| 284 | plistname = tmp + '.plist' |
| 285 | if os.path.exists(plistname): |
| 286 | icnsname = tmp + '.icns' |
| 287 | if not os.path.exists(icnsname): |
| 288 | icnsname = None |
| 289 | else: |
| 290 | plistname = None |
Jack Jansen | d69b744 | 2003-04-22 14:33:48 +0000 | [diff] [blame] | 291 | if not icnsname: |
| 292 | dft_icnsname = os.path.join(sys.prefix, 'Resources/Python.app/Contents/Resources/PythonApplet.icns') |
| 293 | if os.path.exists(dft_icnsname): |
| 294 | icnsname = dft_icnsname |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 295 | if not os.path.exists(rsrcname): |
| 296 | rsrcname = None |
| 297 | if progress: |
| 298 | progress.label('Creating bundle...') |
| 299 | import bundlebuilder |
| 300 | builder = bundlebuilder.AppBuilder(verbosity=0) |
| 301 | builder.mainprogram = filename |
| 302 | builder.builddir = destdir |
| 303 | builder.name = shortname |
Jack Jansen | c77f6df | 2004-12-27 15:51:03 +0000 | [diff] [blame] | 304 | builder.destroot = destroot |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 305 | if rsrcname: |
| 306 | realrsrcname = macresource.resource_pathname(rsrcname) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 307 | builder.files.append((realrsrcname, |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 308 | os.path.join('Contents/Resources', os.path.basename(rsrcname)))) |
| 309 | for o in others: |
| 310 | if type(o) == str: |
| 311 | builder.resources.append(o) |
| 312 | else: |
| 313 | builder.files.append(o) |
| 314 | if plistname: |
| 315 | import plistlib |
| 316 | builder.plist = plistlib.Plist.fromFile(plistname) |
| 317 | if icnsname: |
| 318 | builder.iconfile = icnsname |
| 319 | if not raw: |
| 320 | builder.argv_emulation = 1 |
| 321 | builder.setup() |
| 322 | builder.build() |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 323 | if progress: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 324 | progress.label('Done.') |
| 325 | progress.inc(0) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 326 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 327 | ## macostools.touched(dest_fss) |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 328 | |
| 329 | # Copy resources between two resource file descriptors. |
Jack Jansen | 81da9f1 | 1999-03-17 22:57:55 +0000 | [diff] [blame] | 330 | # skip a resource named '__main__' or (if skipowner is set) with ID zero. |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 331 | # Also skip resources with a type listed in skiptypes. |
| 332 | # |
| 333 | def copyres(input, output, skiptypes, skipowner, progress=None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 334 | ctor = None |
| 335 | alltypes = [] |
| 336 | Res.UseResFile(input) |
| 337 | ntypes = Res.Count1Types() |
| 338 | progress_type_inc = 50/ntypes |
| 339 | for itype in range(1, 1+ntypes): |
| 340 | type = Res.Get1IndType(itype) |
| 341 | if type in skiptypes: |
| 342 | continue |
| 343 | alltypes.append(type) |
| 344 | nresources = Res.Count1Resources(type) |
| 345 | progress_cur_inc = progress_type_inc/nresources |
| 346 | for ires in range(1, 1+nresources): |
| 347 | res = Res.Get1IndResource(type, ires) |
| 348 | id, type, name = res.GetResInfo() |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 349 | lcname = name.lower() |
Jack Jansen | 81da9f1 | 1999-03-17 22:57:55 +0000 | [diff] [blame] | 350 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 351 | if lcname == OWNERNAME and id == 0: |
| 352 | if skipowner: |
| 353 | continue # Skip this one |
| 354 | else: |
| 355 | ctor = type |
| 356 | size = res.size |
| 357 | attrs = res.GetResAttrs() |
| 358 | if progress: |
| 359 | progress.label("Copy %s %d %s"%(type, id, name)) |
| 360 | progress.inc(progress_cur_inc) |
| 361 | res.LoadResource() |
| 362 | res.DetachResource() |
| 363 | Res.UseResFile(output) |
| 364 | try: |
| 365 | res2 = Res.Get1Resource(type, id) |
| 366 | except MacOS.Error: |
| 367 | res2 = None |
| 368 | if res2: |
| 369 | if progress: |
| 370 | progress.label("Overwrite %s %d %s"%(type, id, name)) |
| 371 | progress.inc(0) |
| 372 | res2.RemoveResource() |
| 373 | res.AddResource(type, id, name) |
| 374 | res.WriteResource() |
| 375 | attrs = attrs | res.GetResAttrs() |
| 376 | res.SetResAttrs(attrs) |
| 377 | Res.UseResFile(input) |
| 378 | return alltypes, ctor |
Jack Jansen | 813c997 | 1998-07-31 09:42:35 +0000 | [diff] [blame] | 379 | |
Jack Jansen | 388fbf3 | 2002-06-09 22:08:52 +0000 | [diff] [blame] | 380 | def copyapptree(srctree, dsttree, exceptlist=[], progress=None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 381 | names = [] |
| 382 | if os.path.exists(dsttree): |
| 383 | shutil.rmtree(dsttree) |
| 384 | os.mkdir(dsttree) |
| 385 | todo = os.listdir(srctree) |
| 386 | while todo: |
| 387 | this, todo = todo[0], todo[1:] |
| 388 | if this in exceptlist: |
| 389 | continue |
| 390 | thispath = os.path.join(srctree, this) |
| 391 | if os.path.isdir(thispath): |
| 392 | thiscontent = os.listdir(thispath) |
| 393 | for t in thiscontent: |
| 394 | todo.append(os.path.join(this, t)) |
| 395 | names.append(this) |
| 396 | for this in names: |
| 397 | srcpath = os.path.join(srctree, this) |
| 398 | dstpath = os.path.join(dsttree, this) |
| 399 | if os.path.isdir(srcpath): |
| 400 | os.mkdir(dstpath) |
| 401 | elif os.path.islink(srcpath): |
| 402 | endpoint = os.readlink(srcpath) |
| 403 | os.symlink(endpoint, dstpath) |
| 404 | else: |
| 405 | if progress: |
| 406 | progress.label('Copy '+this) |
| 407 | progress.inc(0) |
| 408 | shutil.copy2(srcpath, dstpath) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 409 | |
Jack Jansen | b2e33fe | 2002-03-29 21:21:28 +0000 | [diff] [blame] | 410 | def writepycfile(codeobject, cfile): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 411 | import marshal |
| 412 | fc = open(cfile, 'wb') |
| 413 | fc.write('\0\0\0\0') # MAGIC placeholder, written later |
| 414 | fc.write('\0\0\0\0') # Timestap placeholder, not needed |
| 415 | marshal.dump(codeobject, fc) |
| 416 | fc.flush() |
| 417 | fc.seek(0, 0) |
| 418 | fc.write(MAGIC) |
| 419 | fc.close() |