def readPlistFromResource(path, restype='plst', resid=0): """Read plst resource from the resource fork of path. """ warnings.warnpy3k("In 3.x, readPlistFromResource is removed.", stacklevel=2) from Carbon.File import FSRef, FSGetResourceForkName from Carbon.Files import fsRdPerm from Carbon import Res fsRef = FSRef(path) resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdPerm) Res.UseResFile(resNum) plistData = Res.Get1Resource(restype, resid).data Res.CloseResFile(resNum) return readPlistFromString(plistData)
def writePlistToResource(rootObject, path, restype='plst', resid=0): """Write 'rootObject' as a plst resource to the resource fork of path. """ from Carbon.File import FSRef, FSGetResourceForkName from Carbon.Files import fsRdWrPerm from Carbon import Res plistData = writePlistToString(rootObject) fsRef = FSRef(path) resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdWrPerm) Res.UseResFile(resNum) try: Res.Get1Resource(restype, resid).RemoveResource() except Res.Error: pass res = Res.Resource(plistData) res.AddResource(restype, resid, '') res.WriteResource() Res.CloseResFile(resNum)
def writePlistToResource(rootObject, path, restype='plst', resid=0): warnings.warnpy3k('In 3.x, writePlistToResource is removed.', stacklevel=2) from Carbon.File import FSRef, FSGetResourceForkName from Carbon.Files import fsRdWrPerm from Carbon import Res plistData = writePlistToString(rootObject) fsRef = FSRef(path) resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdWrPerm) Res.UseResFile(resNum) try: Res.Get1Resource(restype, resid).RemoveResource() except Res.Error: pass res = Res.Resource(plistData) res.AddResource(restype, resid, '') res.WriteResource() Res.CloseResFile(resNum)
def mkalias(src, dst, relative=None): srcfsr = File.FSRef(src) dstdir, dstname = os.path.split(dst) if not dstdir: dstdir = os.curdir dstdirfsr = File.FSRef(dstdir) if relative: relativefsr = File.FSRef(relative) alias = File.FSNewAlias(relativefsr, srcfsr) else: alias = srcfsr.FSNewAliasMinimal() dstfsr, dstfss = Res.FSCreateResourceFile(dstdirfsr, unicode(dstname), File.FSGetResourceForkName()) h = Res.FSOpenResourceFile(dstfsr, File.FSGetResourceForkName(), 3) resource = Res.Resource(alias.data) resource.AddResource('alis', 0, '') Res.CloseResFile(h) dstfinfo = dstfss.FSpGetFInfo() dstfinfo.Flags = dstfinfo.Flags | 32768 dstfss.FSpSetFInfo(dstfinfo)
def resource_pathname(pathname, verbose=0): """Return the pathname for a resource file (either DF or RF based). If the pathname given already refers to such a file simply return it, otherwise first decode it.""" try: refno = Res.FSpOpenResFile(pathname, 1) Res.CloseResFile(refno) except Res.Error, arg: if arg[0] in (-37, -39): # No resource fork. We may be on OSX, and this may be either # a data-fork based resource file or a AppleSingle file # from the CVS repository. try: refno = Res.FSOpenResourceFile(pathname, u'', 1) except Res.Error, arg: if arg[0] != -199: # -199 is "bad resource map" raise else: return refno # Finally try decoding an AppleSingle file pathname = _decode(pathname, verbose=verbose)
def dataFromFile(pathOrFSSpec, nameOrID="", resType='NFNT'): from Carbon import Res import macfs if type(pathOrFSSpec) == types.StringType: fss = macfs.FSSpec(pathOrFSSpec) else: fss = pathOrFSSpec resref = Res.FSOpenResourceFile(fss, u'rsrc', 1) # readonly try: Res.UseResFile(resref) if not nameOrID: # just take the first in the file res = Res.Get1IndResource(resType, 1) elif type(nameOrID) == types.IntType: res = Res.Get1Resource(resType, nameOrID) else: res = Res.Get1NamedResource(resType, nameOrID) theID, theType, name = res.GetResInfo() data = res.data finally: Res.CloseResFile(resref) return data
def writeLWFN(path, data): named, namef = os.path.split(self.name) Res.FSCreateResourceFile(named, namef, u'') MacOS.SetCreatorAndType(path, "just", "LWFN") resRef = Res.FSOpenResourceFile(path, u'rsrc', 2) # write-only try: Res.UseResFile(resRef) resID = 501 chunks = findEncryptedChunks(data) for isEncrypted, chunk in chunks: if isEncrypted: code = 2 else: code = 1 while chunk: res = Res.Resource( chr(code) + '\0' + chunk[:LWFNCHUNKSIZE - 2]) res.AddResource('POST', resID, '') chunk = chunk[LWFNCHUNKSIZE - 2:] resID = resID + 1 res = Res.Resource(chr(5) + '\0') res.AddResource('POST', resID, '') finally: Res.CloseResFile(resRef)
def process_common(template, progress, code, rsrcname, destname, is_update, copy_codefragment, raw=0, others=[], filename=None, destroot=""): if MacOS.runtimemodel == 'macho': return process_common_macho(template, progress, code, rsrcname, destname, is_update, raw, others, filename, destroot) if others: raise BuildError, "Extra files only allowed for MachoPython applets" # Create FSSpecs for the various files template_fsr, d1, d2 = Carbon.File.FSResolveAliasFile(template, 1) template = template_fsr.as_pathname() # Copy data (not resources, yet) from the template if progress: progress.label("Copy data fork...") progress.set(10) if copy_codefragment: tmpl = open(template, "rb") dest = open(destname, "wb") data = tmpl.read() if data: dest.write(data) dest.close() tmpl.close() del dest del tmpl # Open the output resource fork if progress: progress.label("Copy resources...") progress.set(20) try: output = Res.FSOpenResourceFile(destname, RESOURCE_FORK_NAME, WRITE) except MacOS.Error: destdir, destfile = os.path.split(destname) Res.FSCreateResourceFile(destdir, unicode(destfile), RESOURCE_FORK_NAME) output = Res.FSOpenResourceFile(destname, RESOURCE_FORK_NAME, WRITE) # Copy the resources from the target specific resource template, if any typesfound, ownertype = [], None try: input = Res.FSOpenResourceFile(rsrcname, RESOURCE_FORK_NAME, READ) except (MacOS.Error, ValueError): pass if progress: progress.inc(50) else: if is_update: skip_oldfile = ['cfrg'] else: skip_oldfile = [] typesfound, ownertype = copyres(input, output, skip_oldfile, 0, progress) Res.CloseResFile(input) # Check which resource-types we should not copy from the template skiptypes = [] if 'vers' in typesfound: skiptypes.append('vers') if 'SIZE' in typesfound: skiptypes.append('SIZE') if 'BNDL' in typesfound: skiptypes = skiptypes + [ 'BNDL', 'FREF', 'icl4', 'icl8', 'ics4', 'ics8', 'ICN#', 'ics#' ] if not copy_codefragment: skiptypes.append('cfrg') ## skipowner = (ownertype <> None) # Copy the resources from the template input = Res.FSOpenResourceFile(template, RESOURCE_FORK_NAME, READ) dummy, tmplowner = copyres(input, output, skiptypes, 1, progress) Res.CloseResFile(input) ## if ownertype == None: ## raise BuildError, "No owner resource found in either resource file or template" # Make sure we're manipulating the output resource file now Res.UseResFile(output) if ownertype == None: # No owner resource in the template. We have skipped the # Python owner resource, so we have to add our own. The relevant # bundle stuff is already included in the interpret/applet template. newres = Res.Resource('\0') newres.AddResource(DEFAULT_APPLET_CREATOR, 0, "Owner resource") ownertype = DEFAULT_APPLET_CREATOR if code: # Delete any existing 'PYC ' resource named __main__ try: res = Res.Get1NamedResource(RESTYPE, RESNAME) res.RemoveResource() except Res.Error: pass # Create the raw data for the resource from the code object if progress: progress.label("Write PYC resource...") progress.set(120) data = marshal.dumps(code) del code data = (MAGIC + '\0\0\0\0') + data # Create the resource and write it id = 0 while id < 128: id = Res.Unique1ID(RESTYPE) res = Res.Resource(data) res.AddResource(RESTYPE, id, RESNAME) attrs = res.GetResAttrs() attrs = attrs | 0x04 # set preload res.SetResAttrs(attrs) res.WriteResource() res.ReleaseResource() # Close the output file Res.CloseResFile(output) # Now set the creator, type and bundle bit of the destination. # Done with FSSpec's, FSRef FInfo isn't good enough yet (2.3a1+) dest_fss = Carbon.File.FSSpec(destname) dest_finfo = dest_fss.FSpGetFInfo() dest_finfo.Creator = ownertype dest_finfo.Type = 'APPL' dest_finfo.Flags = dest_finfo.Flags | Carbon.Files.kHasBundle | Carbon.Files.kIsShared dest_finfo.Flags = dest_finfo.Flags & ~Carbon.Files.kHasBeenInited dest_fss.FSpSetFInfo(dest_finfo) macostools.touched(destname) if progress: progress.label("Done.") progress.inc(0)
"""Open a resource file given by pathname, possibly decoding an AppleSingle file""" # No resource fork. We may be on OSX, and this may be either # a data-fork based resource file or an AppleSingle file # from the CVS repository. try: refno = Res.FSOpenResourceFile(pathname, u'', 1) except Res.Error, arg: if arg[0] != -199: # -199 is "bad resource map" raise else: return refno # Finally try decoding an AppleSingle file pathname = _decode(pathname, verbose=verbose) refno = Res.FSOpenResourceFile(pathname, u'', 1) def resource_pathname(pathname, verbose=0): """Return the pathname for a resource file (either DF or RF based). If the pathname given already refers to such a file simply return it, otherwise first decode it.""" # No resource fork. We may be on OSX, and this may be either # a data-fork based resource file or an AppleSingle file # from the CVS repository. try: refno = Res.FSOpenResourceFile(pathname, u'', 1) except Res.Error, arg: if arg[0] != -199: # -199 is "bad resource map" raise
def process_common(template, progress, code, rsrcname, destname, is_update, copy_codefragment, raw=0, others=[], filename=None, destroot=''): if MacOS.runtimemodel == 'macho': return process_common_macho(template, progress, code, rsrcname, destname, is_update, raw, others, filename, destroot) else: if others: raise BuildError, 'Extra files only allowed for MachoPython applets' template_fsr, d1, d2 = Carbon.File.FSResolveAliasFile(template, 1) template = template_fsr.as_pathname() if progress: progress.label('Copy data fork...') progress.set(10) if copy_codefragment: tmpl = open(template, 'rb') dest = open(destname, 'wb') data = tmpl.read() if data: dest.write(data) dest.close() tmpl.close() del dest del tmpl if progress: progress.label('Copy resources...') progress.set(20) try: output = Res.FSOpenResourceFile(destname, RESOURCE_FORK_NAME, WRITE) except MacOS.Error: destdir, destfile = os.path.split(destname) Res.FSCreateResourceFile(destdir, unicode(destfile), RESOURCE_FORK_NAME) output = Res.FSOpenResourceFile(destname, RESOURCE_FORK_NAME, WRITE) typesfound, ownertype = [], None try: input = Res.FSOpenResourceFile(rsrcname, RESOURCE_FORK_NAME, READ) except (MacOS.Error, ValueError): if progress: progress.inc(50) else: if is_update: skip_oldfile = ['cfrg'] else: skip_oldfile = [] typesfound, ownertype = copyres(input, output, skip_oldfile, 0, progress) Res.CloseResFile(input) skiptypes = [] if 'vers' in typesfound: skiptypes.append('vers') if 'SIZE' in typesfound: skiptypes.append('SIZE') if 'BNDL' in typesfound: skiptypes = skiptypes + [ 'BNDL', 'FREF', 'icl4', 'icl8', 'ics4', 'ics8', 'ICN#', 'ics#' ] if not copy_codefragment: skiptypes.append('cfrg') input = Res.FSOpenResourceFile(template, RESOURCE_FORK_NAME, READ) dummy, tmplowner = copyres(input, output, skiptypes, 1, progress) Res.CloseResFile(input) Res.UseResFile(output) if ownertype is None: newres = Res.Resource('\x00') newres.AddResource(DEFAULT_APPLET_CREATOR, 0, 'Owner resource') ownertype = DEFAULT_APPLET_CREATOR if code: try: res = Res.Get1NamedResource(RESTYPE, RESNAME) res.RemoveResource() except Res.Error: pass if progress: progress.label('Write PYC resource...') progress.set(120) data = marshal.dumps(code) del code data = MAGIC + '\x00\x00\x00\x00' + data id = 0 while id < 128: id = Res.Unique1ID(RESTYPE) res = Res.Resource(data) res.AddResource(RESTYPE, id, RESNAME) attrs = res.GetResAttrs() attrs = attrs | 4 res.SetResAttrs(attrs) res.WriteResource() res.ReleaseResource() Res.CloseResFile(output) dest_fss = Carbon.File.FSSpec(destname) dest_finfo = dest_fss.FSpGetFInfo() dest_finfo.Creator = ownertype dest_finfo.Type = 'APPL' dest_finfo.Flags = dest_finfo.Flags | Carbon.Files.kHasBundle | Carbon.Files.kIsShared dest_finfo.Flags = dest_finfo.Flags & ~Carbon.Files.kHasBeenInited dest_fss.FSpSetFInfo(dest_finfo) macostools.touched(destname) if progress: progress.label('Done.') progress.inc(0) return