def __get_code(self, modname, filename): """ Get the code-object for a module. This is a extra-simple version for compiling a module. It's not worth spending more effort here, as it is only used in the rare case if outXX-Analysis.toc exists, but outXX-PYZ.toc does not. """ def load_code(modname, filename): path_item = os.path.dirname(filename) if os.path.basename(filename).startswith('__init__.py'): # this is a package path_item = os.path.dirname(path_item) if os.path.basename(path_item) == '__pycache__': path_item = os.path.dirname(path_item) importer = pkgutil.get_importer(path_item) package, _, modname = modname.rpartition('.') if sys.version_info >= (3,3) and hasattr(importer, 'find_loader'): loader, portions = importer.find_loader(modname) else: loader = importer.find_module(modname) portions = [] assert loader and hasattr(loader, 'get_code') logger.debug('Compiling %s', filename) return loader.get_code(modname) try: if filename in ('-', None): # This is a NamespacePackage, modulegraph marks them # by using the filename '-'. (But wants to use None, # so check for None, too, to be forward-compatible.) logger.debug('Compiling namespace package %s', modname) txt = '#\n' return compile(txt, filename, 'exec') else: logger.debug('Compiling %s', filename) co = load_code(modname, filename) if not co: raise ValueError("Module file %s is missing" % filename) return co except SyntaxError as e: print("Syntax error in ", filename) print(e.args) raise
def _set_dependencies(self, analysis, path): """ Synchronize the Analysis result with the needed dependencies. """ for toc in (analysis.binaries, analysis.datas): for i, tpl in enumerate(toc): if not tpl[1] in self._dependencies: logger.debug("Adding dependency %s located in %s" % (tpl[1], path)) self._dependencies[tpl[1]] = path else: dep_path = self._get_relative_path(path, self._dependencies[tpl[1]]) logger.debug("Referencing %s to be a dependecy for %s, located in %s" % (tpl[1], path, dep_path)) analysis.dependencies.append((":".join((dep_path, tpl[0])), tpl[1], "DEPENDENCY")) toc[i] = (None, None, None) # Clean the list toc[:] = [tpl for tpl in toc if tpl != (None, None, None)]
def load_code(modname, filename): path_item = os.path.dirname(filename) if os.path.basename(filename).startswith('__init__.py'): # this is a package path_item = os.path.dirname(path_item) if os.path.basename(path_item) == '__pycache__': path_item = os.path.dirname(path_item) importer = pkgutil.get_importer(path_item) package, _, modname = modname.rpartition('.') if sys.version_info >= (3,3) and hasattr(importer, 'find_loader'): loader, portions = importer.find_loader(modname) else: loader = importer.find_module(modname) portions = [] assert loader and hasattr(loader, 'get_code') logger.debug('Compiling %s', filename) return loader.get_code(modname)
def assemble(self): logger.info("Building EXE from %s", self.tocbasename) trash = [] if not os.path.exists(os.path.dirname(self.name)): os.makedirs(os.path.dirname(self.name)) exe = self.exefiles[0][1] # pathname of bootloader if not os.path.exists(exe): raise SystemExit(_MISSING_BOOTLOADER_ERRORMSG) if is_win and (self.icon or self.versrsrc or self.resources): tmpnm = tempfile.mktemp() self._copyfile(exe, tmpnm) os.chmod(tmpnm, 0o755) if self.icon: icon.CopyIcons(tmpnm, self.icon) if self.versrsrc: versioninfo.SetVersion(tmpnm, self.versrsrc) for res in self.resources: res = res.split(",") for i in range(1, len(res)): try: res[i] = int(res[i]) except ValueError: pass resfile = res[0] restype = resname = reslang = None if len(res) > 1: restype = res[1] if len(res) > 2: resname = res[2] if len(res) > 3: reslang = res[3] try: winresource.UpdateResourcesFromResFile(tmpnm, resfile, [restype or "*"], [resname or "*"], [reslang or "*"]) except winresource.pywintypes.error as exc: if exc.args[0] != winresource.ERROR_BAD_EXE_FORMAT: logger.error("Error while updating resources in %s" " from resource file %s", tmpnm, resfile, exc_info=1) continue # Handle the case where the file contains no resources, and is # intended as a single resource to be added to the exe. if not restype or not resname: logger.error("resource type and/or name not specified") continue if "*" in (restype, resname): logger.error("no wildcards allowed for resource type " "and name when source file does not " "contain resources") continue try: winresource.UpdateResourcesFromDataFile(tmpnm, resfile, restype, [resname], [reslang or 0]) except winresource.pywintypes.error: logger.error("Error while updating resource %s %s in %s" " from data file %s", restype, resname, tmpnm, resfile, exc_info=1) trash.append(tmpnm) exe = tmpnm # NOTE: Do not look up for bootloader file in the cache because it might # get corrupted by UPX when UPX is available. See #1863 for details. if not self.append_pkg: logger.info("Copying bootloader exe to %s", self.name) self._copyfile(exe, self.name) logger.info("Copying archive to %s", self.pkgname) self._copyfile(self.pkg.name, self.pkgname) elif is_linux: self._copyfile(exe, self.name) logger.info("Appending archive to ELF section in EXE %s", self.name) retcode, stdout, stderr = exec_command_all( 'objcopy', '--add-section', 'pydata=%s' % self.pkg.name, self.name) logger.debug("objcopy returned %i", retcode) if stdout: logger.debug(stdout) if stderr: logger.debug(stderr) if retcode != 0: raise SystemError("objcopy Failure: %s" % stderr) else: # Fall back to just append on end of file logger.info("Appending archive to EXE %s", self.name) with open(self.name, 'wb') as outf: # write the bootloader data with open(exe, 'rb') as infh: shutil.copyfileobj(infh, outf, length=64*1024) # write the archive data with open(self.pkg.name, 'rb') as infh: shutil.copyfileobj(infh, outf, length=64*1024) if is_darwin: # Fix Mach-O header for codesigning on OS X. logger.info("Fixing EXE for code signing %s", self.name) import PyInstaller.utils.osx as osxutils osxutils.fix_exe_for_code_signing(self.name) os.chmod(self.name, 0o755) # get mtime for storing into the guts self.mtm = misc.mtime(self.name) for item in trash: os.remove(item) logger.info("Building EXE from %s completed successfully.", self.tocbasename)
def assemble(self): logger.info("Building EXE from %s", self.tocbasename) trash = [] if os.path.exists(self.name): os.remove(self.name) if not os.path.exists(os.path.dirname(self.name)): os.makedirs(os.path.dirname(self.name)) exe = self.exefiles[0][1] # pathname of bootloader if not os.path.exists(exe): raise SystemExit(_MISSING_BOOTLOADER_ERRORMSG) if is_win and (self.icon or self.versrsrc or self.resources): tmpnm = tempfile.mktemp() self._copyfile(exe, tmpnm) os.chmod(tmpnm, 0o755) if self.icon: icon.CopyIcons(tmpnm, self.icon) if self.versrsrc: versioninfo.SetVersion(tmpnm, self.versrsrc) for res in self.resources: res = res.split(",") for i in range(1, len(res)): try: res[i] = int(res[i]) except ValueError: pass resfile = res[0] restype = resname = reslang = None if len(res) > 1: restype = res[1] if len(res) > 2: resname = res[2] if len(res) > 3: reslang = res[3] try: winresource.UpdateResourcesFromResFile(tmpnm, resfile, [restype or "*"], [resname or "*"], [reslang or "*"]) except winresource.pywintypes.error as exc: if exc.args[0] != winresource.ERROR_BAD_EXE_FORMAT: logger.error("Error while updating resources in %s" " from resource file %s", tmpnm, resfile, exc_info=1) continue # Handle the case where the file contains no resources, and is # intended as a single resource to be added to the exe. if not restype or not resname: logger.error("resource type and/or name not specified") continue if "*" in (restype, resname): logger.error("no wildcards allowed for resource type " "and name when source file does not " "contain resources") continue try: winresource.UpdateResourcesFromDataFile(tmpnm, resfile, restype, [resname], [reslang or 0]) except winresource.pywintypes.error: logger.error("Error while updating resource %s %s in %s" " from data file %s", restype, resname, tmpnm, resfile, exc_info=1) trash.append(tmpnm) exe = tmpnm # NOTE: Do not look up for bootloader file in the cache because it might # get corrupted by UPX when UPX is available. See #1863 for details. if not self.append_pkg: logger.info("Copying bootloader exe to %s", self.name) self._copyfile(exe, self.name) logger.info("Copying archive to %s", self.pkgname) self._copyfile(self.pkg.name, self.pkgname) elif is_linux: self._copyfile(exe, self.name) logger.info("Appending archive to ELF section in EXE %s", self.name) retcode, stdout, stderr = exec_command_all( 'objcopy', '--add-section', 'pydata=%s' % self.pkg.name, self.name) logger.debug("objcopy returned %i", retcode) if stdout: logger.debug(stdout) if stderr: logger.debug(stderr) if retcode != 0: raise SystemError("objcopy Failure: %s" % stderr) else: # Fall back to just append on end of file logger.info("Appending archive to EXE %s", self.name) with open(self.name, 'wb') as outf: # write the bootloader data with open(exe, 'rb') as infh: shutil.copyfileobj(infh, outf, length=64*1024) # write the archive data with open(self.pkg.name, 'rb') as infh: shutil.copyfileobj(infh, outf, length=64*1024) if is_darwin: # Fix Mach-O header for codesigning on OS X. logger.info("Fixing EXE for code signing %s", self.name) import PyInstaller.utils.osx as osxutils osxutils.fix_exe_for_code_signing(self.name) os.chmod(self.name, 0o755) # get mtime for storing into the guts self.mtm = misc.mtime(self.name) for item in trash: os.remove(item) logger.info("Building EXE from %s completed successfully.", self.tocbasename)