def checkPluginPath(plugin_filename, module_package): debug( "Checking top level plug-in path %s %s", plugin_filename, module_package ) plugin_info = considerFilename( module_package = module_package, module_filename = plugin_filename ) if plugin_info is not None: # File or package makes a difference, handle that if Utils.isFile(plugin_info[0]) or \ Importing.isPackageDir(plugin_info[0]): _checkPluginPath(plugin_filename, module_package) elif Utils.isDir(plugin_info[0]): for sub_path, sub_filename in Utils.listDir(plugin_info[0]): assert sub_filename != "__init__.py" if Importing.isPackageDir(sub_path) or \ sub_path.endswith(".py"): _checkPluginPath(sub_path, None) else: warning("Failed to include module from '%s'.", plugin_info[0]) else: warning("Failed to recurse to directory '%s'.", plugin_filename)
def checkPluginPath(plugin_filename, module_package): debug( "Checking top level plug-in path %s %s", plugin_filename, module_package ) plugin_info = considerFilename( module_filename = plugin_filename ) if plugin_info is not None: # File or package makes a difference, handle that if Utils.isFile(plugin_info[0]) or \ Importing.isPackageDir(plugin_info[0]): _checkPluginPath(plugin_filename, module_package) elif Utils.isDir(plugin_info[0]): for sub_path, sub_filename in Utils.listDir(plugin_info[0]): assert sub_filename != "__init__.py" if Importing.isPackageDir(sub_path) or \ sub_path.endswith(".py"): _checkPluginPath(sub_path, None) else: warning("Failed to include module from '%s'.", plugin_info[0]) else: warning("Failed to recurse to directory '%s'.", plugin_filename)
def checkPluginPath(plugin_filename, module_package): plugin_filename = os.path.normpath(plugin_filename) if Options.isShowInclusion(): recursion_logger.info("Checking top level plug-in path %s %s" % (plugin_filename, module_package)) plugin_info = considerFilename(module_filename=plugin_filename) if plugin_info is not None: # File or package makes a difference, handle that if os.path.isfile(plugin_info[0]) or Importing.isPackageDir( plugin_info[0]): checkPluginSinglePath(plugin_filename, module_package=module_package) elif os.path.isdir(plugin_info[0]): for sub_path, sub_filename in listDir(plugin_info[0]): assert sub_filename != "__init__.py" if Importing.isPackageDir(sub_path) or sub_path.endswith( ".py"): checkPluginSinglePath(sub_path, module_package=None) else: recursion_logger.warning("Failed to include module from %r." % plugin_info[0]) else: recursion_logger.warning("Failed to recurse to directory %r." % plugin_filename)
def _checkPluginPath(plugin_filename, module_package): # Many branches, for the decision is very complex, pylint: disable=too-many-branches debug("Checking detail plug-in path '%s' '%s':", plugin_filename, module_package) module_name, module_kind = Importing.getModuleNameAndKindFromFilename( plugin_filename) if module_kind is not None: decision, reason = decideRecursion(module_filename=plugin_filename, module_name=module_name, module_package=module_package, module_kind=module_kind, extra_recursion=True) if decision: module_relpath = relpath(plugin_filename) module, is_added = recurseTo(module_filename=plugin_filename, module_relpath=module_relpath, module_package=module_package, module_kind="py", reason=reason) if module: if not is_added: warning( "Recursed to %s '%s' at '%s' twice.", "package" if module.isCompiledPythonPackage() else "module", module.getName(), plugin_filename) if not isSameModulePath(module.getFilename(), plugin_filename): warning("Duplicate '%s' of '%s' ignored .", plugin_filename, module.getFilename()) return debug("Recursed to %s %s %s", module.getName(), module.getPackage(), module) ImportCache.addImportedModule(module) if module.isCompiledPythonPackage(): package_filename = module.getFilename() if os.path.isdir(package_filename): # Must be a namespace package. assert python_version >= 330 package_dir = package_filename # Only include it, if it contains actual modules, which will # recurse to this one and find it again. else: package_dir = os.path.dirname(package_filename) # Real packages will always be included. ModuleRegistry.addRootModule(module) debug("Package directory %s", package_dir) for sub_path, sub_filename in listDir(package_dir): if sub_filename in ("__init__.py", "__pycache__"): continue assert sub_path != plugin_filename if Importing.isPackageDir(sub_path) or \ sub_path.endswith(".py"): _checkPluginPath(sub_path, module.getFullName()) elif module.isCompiledPythonModule(): ModuleRegistry.addRootModule(module) else: warning("Failed to include module from '%s'.", plugin_filename)
def _checkPluginPath(plugin_filename, module_package): # Many branches, for the decision is very complex, pylint: disable=R0912 debug( "Checking detail plug-in path '%s' '%s':", plugin_filename, module_package ) plugin_info = considerFilename( module_package = module_package, module_filename = plugin_filename ) if plugin_info is not None: module, is_added = recurseTo( module_filename = plugin_info[0], module_relpath = plugin_info[1], module_package = module_package, module_kind = "py", reason = "Lives in plug-in directory." ) if module: if not is_added: warning( "Recursed to %s '%s' at '%s' twice.", "package" if module.isPythonPackage() else "module", module.getName(), plugin_info[0] ) if not isSameModulePath(module.getFilename(), plugin_info[0]): warning( "Duplicate ignored '%s'.", plugin_info[1] ) return debug( "Recursed to %s %s %s", module.getName(), module.getPackage(), module ) if module.isPythonPackage(): package_filename = module.getFilename() if Utils.isDir(package_filename): # Must be a namespace package. assert Utils.python_version >= 330 package_dir = package_filename # Only include it, if it contains actual modules, which will # recurse to this one and find it again. useful = False else: package_dir = Utils.dirname(package_filename) # Real packages will always be included. useful = True debug( "Package directory %s", package_dir ) for sub_path, sub_filename in Utils.listDir(package_dir): if sub_filename in ("__init__.py", "__pycache__"): continue assert sub_path != plugin_filename if Importing.isPackageDir(sub_path) or \ sub_path.endswith(".py"): _checkPluginPath(sub_path, module.getFullName()) else: # Modules should always be included. useful = True if useful: ModuleRegistry.addRootModule(module) else: warning("Failed to include module from '%s'.", plugin_info[0])
def decideModuleTree(filename, package, is_shlib, is_top, is_main): # Many variables, branches, due to the many cases, pylint: disable=too-many-branches,too-many-statements assert package is None or type(package) is str assert filename is not None if is_main and os.path.isdir(filename): source_filename = os.path.join(filename, "__main__.py") if not os.path.isfile(source_filename): sys.stderr.write("%s: can't find '__main__' module in '%s'\n" % (os.path.basename(sys.argv[0]), filename)) sys.exit(2) filename = source_filename main_added = True else: main_added = False if os.path.isfile(filename): source_filename = filename source_ref = SourceCodeReferences.fromFilename(filename=filename, ) if is_main: module_name = "__main__" else: module_name = os.path.basename(filename) if module_name.endswith(".py"): module_name = module_name[:-3] if is_shlib: module_name = module_name.split('.')[0] if '.' in module_name: sys.stderr.write( "Error, '%s' is not a proper python module name.\n" % (module_name)) sys.exit(2) if is_shlib: result = PythonShlibModule(name=module_name, package_name=package, source_ref=source_ref) elif is_main: result = PythonMainModule(main_added=main_added, mode=Plugins.decideCompilation( "__main__", source_ref), future_spec=None, source_ref=source_ref) else: if package is not None: full_name = package + '.' + module_name else: full_name = module_name result = CompiledPythonModule(name=module_name, package_name=package, mode=Plugins.decideCompilation( full_name, source_ref), future_spec=None, source_ref=source_ref) elif Importing.isPackageDir(filename): if is_top: module_name = splitPath(filename)[-1] else: module_name = os.path.basename(filename) source_filename = os.path.join(filename, "__init__.py") if not os.path.isfile(source_filename): source_ref, result = createNamespacePackage( module_name=module_name, package_name=package, module_relpath=filename) source_filename = None else: source_ref = SourceCodeReferences.fromFilename( filename=os.path.abspath(source_filename), ) if package is not None: full_name = package + '.' + module_name else: full_name = module_name result = CompiledPythonPackage(name=module_name, package_name=package, mode=Plugins.decideCompilation( full_name, source_ref), future_spec=None, source_ref=source_ref) assert result.getFullName() == full_name, result else: sys.stderr.write("%s: can't open file '%s'.\n" % (os.path.basename(sys.argv[0]), filename)) sys.exit(2) if not Options.shallHaveStatementLines(): source_ref = source_ref.atInternal() return result, source_ref, source_filename
def decideModuleTree(filename, package, is_shlib, is_top, is_main): # Many variables, branches, due to the many cases, pylint: disable=too-many-branches assert package is None or type(package) is ModuleName assert filename is not None if is_main and os.path.isdir(filename): source_filename = os.path.join(filename, "__main__.py") if not os.path.isfile(source_filename): sys.stderr.write( "%s: can't find '__main__' module in '%s'\n" % (os.path.basename(sys.argv[0]), filename) ) sys.exit(2) filename = source_filename main_added = True else: main_added = False if os.path.isfile(filename): source_filename = filename source_ref = SourceCodeReferences.fromFilename(filename=filename) if is_main: module_name = ModuleName("__main__") else: # Derive module name from filename. module_name = os.path.basename(filename) if is_shlib: module_name = module_name.split(".")[0] elif module_name.endswith(".py"): module_name = module_name[:-3] if "." in module_name: sys.stderr.write( "Error, '%s' is not a proper python module name.\n" % (module_name) ) sys.exit(2) module_name = ModuleName.makeModuleNameInPackage(module_name, package) if is_shlib: result = PythonShlibModule(module_name=module_name, source_ref=source_ref) elif is_main: result = PythonMainModule( main_added=main_added, mode=decideCompilationMode(False, module_name, source_ref), future_spec=None, source_ref=source_ref, ) else: result = CompiledPythonModule( module_name=module_name, is_top=is_top, mode=decideCompilationMode(is_top, module_name, source_ref), future_spec=None, source_ref=source_ref, ) elif Importing.isPackageDir(filename): if is_top: module_name = splitPath(filename)[-1] else: module_name = os.path.basename(filename) module_name = ModuleName.makeModuleNameInPackage(module_name, package) source_filename = os.path.join(filename, "__init__.py") if not os.path.isfile(source_filename): source_ref, result = createNamespacePackage( module_name=module_name, is_top=is_top, module_relpath=filename ) source_filename = None else: source_ref = SourceCodeReferences.fromFilename( filename=os.path.abspath(source_filename) ) result = CompiledPythonPackage( module_name=module_name, is_top=is_top, mode=decideCompilationMode(is_top, module_name, source_ref), future_spec=None, source_ref=source_ref, ) else: sys.stderr.write( "%s: can't open file '%s'.\n" % (os.path.basename(sys.argv[0]), filename) ) sys.exit(2) return result, source_ref, source_filename
def _checkPluginPath(plugin_filename, module_package): # Many branches, for the decision is very complex debug( "Checking detail plug-in path '%s' '%s':", plugin_filename, module_package ) plugin_info = considerFilename( module_filename = plugin_filename ) if plugin_info is not None: module, is_added = recurseTo( module_filename = plugin_info[0], module_relpath = plugin_info[1], module_package = module_package, module_kind = "py", reason = "Lives in plug-in directory." ) if module: if not is_added: warning( "Recursed to %s '%s' at '%s' twice.", "package" if module.isCompiledPythonPackage() else "module", module.getName(), plugin_info[0] ) if not isSameModulePath(module.getFilename(), plugin_info[0]): warning( "Duplicate ignored '%s'.", plugin_info[1] ) return debug( "Recursed to %s %s %s", module.getName(), module.getPackage(), module ) ImportCache.addImportedModule(module) if module.isCompiledPythonPackage(): package_filename = module.getFilename() if Utils.isDir(package_filename): # Must be a namespace package. assert python_version >= 330 package_dir = package_filename # Only include it, if it contains actual modules, which will # recurse to this one and find it again. else: package_dir = Utils.dirname(package_filename) # Real packages will always be included. ModuleRegistry.addRootModule(module) debug( "Package directory %s", package_dir ) for sub_path, sub_filename in Utils.listDir(package_dir): if sub_filename in ("__init__.py", "__pycache__"): continue assert sub_path != plugin_filename if Importing.isPackageDir(sub_path) or \ sub_path.endswith(".py"): _checkPluginPath(sub_path, module.getFullName()) elif module.isCompiledPythonModule(): ModuleRegistry.addRootModule(module) else: warning("Failed to include module from '%s'.", plugin_info[0])
def decideModuleTree(filename, package, is_shlib, is_top, is_main): # Many variables, branches, due to the many cases, pylint: disable=R0912 assert package is None or type(package) is str assert filename is not None if is_main and Utils.isDir(filename): source_filename = Utils.joinpath(filename, "__main__.py") if not Utils.isFile(source_filename): sys.stderr.write( "%s: can't find '__main__' module in '%s'\n" % ( Utils.basename(sys.argv[0]), filename ) ) sys.exit(2) filename = source_filename main_added = True else: main_added = False if Utils.isFile(filename): source_filename = filename source_ref = SourceCodeReferences.fromFilename( filename = filename, ) if is_main: module_name = "__main__" else: module_name = Utils.basename(filename) if module_name.endswith(".py"): module_name = module_name[:-3] if is_shlib: module_name = module_name.split('.')[0] if '.' in module_name: sys.stderr.write( "Error, '%s' is not a proper python module name.\n" % ( module_name ) ) sys.exit(2) if is_shlib: result = PythonShlibModule( name = module_name, package_name = package, source_ref = source_ref ) elif is_main: result = PythonMainModule( main_added = main_added, source_ref = source_ref ) else: result = PythonModule( name = module_name, package_name = package, source_ref = source_ref ) elif Importing.isPackageDir(filename): if is_top: package_name = Utils.splitpath(filename)[-1] else: package_name = Utils.basename(filename) source_filename = Utils.joinpath(filename, "__init__.py") if not Utils.isFile(source_filename): source_ref, result = createNamespacePackage( package_name = package_name, module_relpath = filename ) source_filename = None else: source_ref = SourceCodeReferences.fromFilename( filename = Utils.abspath(source_filename), ) result = PythonPackage( name = package_name, package_name = package, source_ref = source_ref ) else: sys.stderr.write( "%s: can't open file '%s'.\n" % ( Utils.basename(sys.argv[0]), filename ) ) sys.exit(2) if not Options.shallHaveStatementLines(): source_ref = source_ref.atInternal() return result, source_ref, source_filename
def checkPluginSinglePath(plugin_filename, module_package): # Many branches, for the decision is very complex, pylint: disable=too-many-branches if Options.isShowInclusion(): recursion_logger.info("Checking detail plug-in path '%s' '%s':" % (plugin_filename, module_package)) module_name, module_kind = Importing.getModuleNameAndKindFromFilename( plugin_filename) module_name = ModuleName.makeModuleNameInPackage(module_name, module_package) if module_kind is not None: decision, reason = decideRecursion( module_filename=plugin_filename, module_name=module_name, module_kind=module_kind, extra_recursion=True, ) if decision: module_relpath = relpath(plugin_filename) module, is_added = recurseTo( module_filename=plugin_filename, module_relpath=module_relpath, module_package=module_package, module_kind=module_kind, reason=reason, ) if module: if not is_added: recursion_logger.warning( "Recursed to %s '%s' at '%s' twice." % ( "package" if module.isCompiledPythonPackage() else "module", module.getName(), plugin_filename, )) if not isSameModulePath(module.getFilename(), plugin_filename): recursion_logger.warning( "Duplicate '%s' of '%s' ignored ." % ( plugin_filename, module.getFilename(), )) return if Options.isShowInclusion(): recursion_logger.info("Recursed to '%s' %s" % ( module.getFullName(), module, )) ImportCache.addImportedModule(module) if module.isCompiledPythonPackage(): package_filename = module.getFilename() if os.path.isdir(package_filename): # Must be a namespace package. assert python_version >= 0x300 package_dir = package_filename # Only include it, if it contains actual modules, which will # recurse to this one and find it again. else: package_dir = os.path.dirname(package_filename) # Real packages will always be included. ModuleRegistry.addRootModule(module) if Options.isShowInclusion(): recursion_logger.info("Package directory '%s'." % package_dir) for sub_path, sub_filename in listDir(package_dir): if sub_filename in ("__init__.py", "__pycache__"): continue assert sub_path != plugin_filename if Importing.isPackageDir( sub_path) and not os.path.exists(sub_path + ".py"): checkPluginSinglePath( sub_path, module_package=module.getFullName()) elif sub_path.endswith(".py"): checkPluginSinglePath( sub_path, module_package=module.getFullName()) elif module.isCompiledPythonModule(): ModuleRegistry.addRootModule(module) elif module.isPythonShlibModule(): if Options.isStandaloneMode(): ModuleRegistry.addRootModule(module) else: recursion_logger.warning( "Failed to include module from '%s'." % plugin_filename)
def checkPluginSinglePath(plugin_filename, module_package): # Many branches, for the decision is very complex, pylint: disable=too-many-branches debug("Checking detail plug-in path '%s' '%s':", plugin_filename, module_package) module_name, module_kind = Importing.getModuleNameAndKindFromFilename( plugin_filename ) if module_kind is not None: decision, reason = decideRecursion( module_filename=plugin_filename, module_name=module_name, module_package=module_package, module_kind=module_kind, extra_recursion=True, ) if decision: module_relpath = relpath(plugin_filename) module, is_added = recurseTo( module_filename=plugin_filename, module_relpath=module_relpath, module_package=module_package, module_kind=module_kind, reason=reason, ) if module: if not is_added: warning( "Recursed to %s '%s' at '%s' twice.", "package" if module.isCompiledPythonPackage() else "module", module.getName(), plugin_filename, ) if not isSameModulePath(module.getFilename(), plugin_filename): warning( "Duplicate '%s' of '%s' ignored .", plugin_filename, module.getFilename(), ) return debug( "Recursed to %s %s %s", module.getName(), module.getPackage(), module, ) ImportCache.addImportedModule(module) if module.isCompiledPythonPackage(): package_filename = module.getFilename() if os.path.isdir(package_filename): # Must be a namespace package. assert python_version >= 300 package_dir = package_filename # Only include it, if it contains actual modules, which will # recurse to this one and find it again. else: package_dir = os.path.dirname(package_filename) # Real packages will always be included. ModuleRegistry.addRootModule(module) debug("Package directory %s", package_dir) for sub_path, sub_filename in listDir(package_dir): if sub_filename in ("__init__.py", "__pycache__"): continue assert sub_path != plugin_filename if Importing.isPackageDir(sub_path) or sub_path.endswith(".py"): checkPluginSinglePath(sub_path, module.getFullName()) elif module.isCompiledPythonModule(): ModuleRegistry.addRootModule(module) else: warning("Failed to include module from '%s'.", plugin_filename)
def decideModuleTree(filename, package, is_shlib, is_top, is_main): # Many variables, branches, due to the many cases, pylint: disable=R0912,R0915 assert package is None or type(package) is str assert filename is not None if is_main and Utils.isDir(filename): source_filename = Utils.joinpath(filename, "__main__.py") if not Utils.isFile(source_filename): sys.stderr.write( "%s: can't find '__main__' module in '%s'\n" % ( Utils.basename(sys.argv[0]), filename ) ) sys.exit(2) filename = source_filename main_added = True else: main_added = False if Utils.isFile(filename): source_filename = filename source_ref = SourceCodeReferences.fromFilename( filename = filename, ) if is_main: module_name = "__main__" else: module_name = Utils.basename(filename) if module_name.endswith(".py"): module_name = module_name[:-3] if is_shlib: module_name = module_name.split('.')[0] if '.' in module_name: sys.stderr.write( "Error, '%s' is not a proper python module name.\n" % ( module_name ) ) sys.exit(2) if is_shlib: result = PythonShlibModule( name = module_name, package_name = package, source_ref = source_ref ) elif is_main: result = PythonMainModule( main_added = main_added, source_ref = source_ref ) else: if package is not None: full_name = package + "." + module_name else: full_name = module_name decision = Plugins.decideCompilation(full_name, source_ref) if decision == "compiled": result = CompiledPythonModule( name = module_name, package_name = package, source_ref = source_ref ) else: source_code = readSourceCodeFromFilename(module_name, filename) source_code = Plugins.onFrozenModuleSourceCode( module_name = full_name, is_package = False, source_code = source_code ) bytecode = compile(source_code, filename, "exec") bytecode = Plugins.onFrozenModuleBytecode( module_name = module_name, is_package = False, bytecode = bytecode ) result = UncompiledPythonModule( name = module_name, package_name = package, bytecode = bytecode, filename = filename, user_provided = True, technical = False, source_ref = source_ref ) # Don't read it anymore. source_filename = None elif Importing.isPackageDir(filename): if is_top: package_name = Utils.splitpath(filename)[-1] else: package_name = Utils.basename(filename) source_filename = Utils.joinpath(filename, "__init__.py") if not Utils.isFile(source_filename): source_ref, result = createNamespacePackage( package_name = package_name, module_relpath = filename ) source_filename = None else: source_ref = SourceCodeReferences.fromFilename( filename = Utils.abspath(source_filename), ) if package is not None: full_name = package + "." + package_name else: full_name = package_name decision = Plugins.decideCompilation(full_name, source_ref) if decision == "compiled": result = CompiledPythonPackage( name = package_name, package_name = package, source_ref = source_ref ) else: bytecode = compile(source_code, filename, "exec") bytecode = Plugins.onFrozenModuleBytecode( module_name = module_name, is_package = False, bytecode = bytecode ) result = UncompiledPythonPackage( name = module_name, package_name = package, bytecode = bytecode, filename = filename, user_provided = True, technical = False, source_ref = source_ref ) # Don't read it anymore. source_filename = None else: sys.stderr.write( "%s: can't open file '%s'.\n" % ( Utils.basename(sys.argv[0]), filename ) ) sys.exit(2) if not Options.shallHaveStatementLines(): source_ref = source_ref.atInternal() return result, source_ref, source_filename
def checkPluginSinglePath(plugin_filename, module_package): # Many branches, for the decision is very complex, pylint: disable=too-many-branches # The importing wants these to be unique. plugin_filename = os.path.abspath(plugin_filename) if Options.isShowInclusion(): recursion_logger.info("Checking detail plug-in path '%s' '%s':" % (plugin_filename, module_package)) module_name, module_kind = Importing.getModuleNameAndKindFromFilename( plugin_filename) module_name = ModuleName.makeModuleNameInPackage(module_name, module_package) if module_kind == "extension" and not Options.isStandaloneMode(): recursion_logger.warning( "Cannot include '%s' unless using at least standalone mode." % module_name.asString()) if module_kind is not None: decision, reason = decideRecursion( module_filename=plugin_filename, module_name=module_name, module_kind=module_kind, extra_recursion=True, ) if decision: module = recurseTo( signal_change=None, module_filename=plugin_filename, module_name=module_name, module_kind=module_kind, reason=reason, ) if module: if Options.isShowInclusion(): recursion_logger.info("Included '%s' as '%s'." % ( module.getFullName(), module, )) ImportCache.addImportedModule(module) if module.isCompiledPythonPackage(): package_filename = module.getFilename() if os.path.isdir(package_filename): # Must be a namespace package. assert python_version >= 0x300 package_dir = package_filename # Only include it, if it contains actual modules, which will # recurse to this one and find it again. else: package_dir = os.path.dirname(package_filename) # Real packages will always be included. ModuleRegistry.addRootModule(module) if Options.isShowInclusion(): recursion_logger.info("Package directory '%s'." % package_dir) for sub_path, sub_filename in listDir(package_dir): if sub_filename in ("__init__.py", "__pycache__"): continue assert sub_path != plugin_filename if Importing.isPackageDir( sub_path) and not os.path.exists(sub_path + ".py"): checkPluginSinglePath( sub_path, module_package=module.getFullName()) elif sub_path.endswith(".py"): checkPluginSinglePath( sub_path, module_package=module.getFullName()) elif module.isCompiledPythonModule(): ModuleRegistry.addRootModule(module) elif module.isPythonExtensionModule(): if Options.isStandaloneMode(): ModuleRegistry.addRootModule(module) else: recursion_logger.warning( "Failed to include module from '%s'." % plugin_filename)
def decideModuleTree(filename, package, is_shlib, is_top, is_main): # Many variables, branches, due to the many cases # pylint: disable=too-many-branches,too-many-locals,too-many-statements assert package is None or type(package) is ModuleName assert filename is not None if is_main and os.path.isdir(filename): source_filename = os.path.join(filename, "__main__.py") if not os.path.isfile(source_filename): sys.stderr.write("%s: can't find '__main__' module in '%s'\n" % (os.path.basename(sys.argv[0]), filename)) sys.exit(2) filename = source_filename main_added = True else: main_added = False if os.path.isfile(filename): source_filename = filename source_ref = SourceCodeReferences.fromFilename(filename=filename) if is_main: module_name = ModuleName("__main__") else: # Derive module name from filename. module_name = os.path.basename(filename) if is_shlib: module_name = module_name.split(".")[0] elif module_name.endswith(".py"): module_name = module_name[:-3] if "." in module_name: sys.stderr.write( "Error, '%s' is not a proper python module name.\n" % (module_name)) sys.exit(2) module_name = ModuleName.makeModuleNameInPackage( module_name, package) if is_shlib: result = PythonShlibModule(module_name=module_name, source_ref=source_ref) source_code = None else: source_code = readSourceCodeFromFilename( module_name=module_name, source_filename=source_filename) if is_main: result = PythonMainModule( main_added=main_added, mode=decideCompilationMode(False, module_name, source_ref), future_spec=None, source_ref=source_ref, ) checkPythonVersionFromCode(source_code) else: mode = decideCompilationMode(is_top, module_name, source_ref) if (mode == "bytecode" and not is_top and hasCachedImportedModulesNames( module_name, source_code)): optimization_logger.info("%r is included as bytecode." % (module_name.asString())) result = UncompiledPythonModule( module_name=module_name, filename=filename, bytecode=demoteSourceCodeToBytecode( module_name=module_name, source_code=source_code, filename=filename, ), source_ref=source_ref, user_provided=False, technical=False, ) used_modules = OrderedSet() for used_module_name in getCachedImportedModulesNames( module_name=module_name, source_code=source_code): ( _module_package, module_filename, _finding, ) = Importing.findModule( importing=result, module_name=used_module_name, parent_package=None, level=-1, warn=False, ) used_modules.add((used_module_name, os.path.relpath(module_filename))) result.setUsedModules(used_modules) # Not used anymore source_code = None else: result = CompiledPythonModule( module_name=module_name, is_top=is_top, mode=mode, future_spec=None, source_ref=source_ref, ) elif Importing.isPackageDir(filename): if is_top: module_name = splitPath(filename)[-1] else: module_name = os.path.basename(filename) module_name = ModuleName.makeModuleNameInPackage(module_name, package) source_filename = os.path.join(filename, "__init__.py") if not os.path.isfile(source_filename): source_ref, result = createNamespacePackage( module_name=module_name, is_top=is_top, module_relpath=filename) source_filename = None source_code = None else: source_ref = SourceCodeReferences.fromFilename( filename=os.path.abspath(source_filename)) result = CompiledPythonPackage( module_name=module_name, is_top=is_top, mode=decideCompilationMode(is_top, module_name, source_ref), future_spec=None, source_ref=source_ref, ) source_code = readSourceCodeFromFilename( module_name=module_name, source_filename=source_filename) else: sys.stderr.write("%s: can't open file '%s'.\n" % (os.path.basename(sys.argv[0]), filename)) sys.exit(2) return result, source_ref, source_code
def decideModuleTree(filename, package, is_shlib, is_top, is_main): # Many variables, branches, due to the many cases, pylint: disable=R0912,R0915 assert package is None or type(package) is str assert filename is not None if is_main and Utils.isDir(filename): source_filename = Utils.joinpath(filename, "__main__.py") if not Utils.isFile(source_filename): sys.stderr.write("%s: can't find '__main__' module in '%s'\n" % (Utils.basename(sys.argv[0]), filename)) sys.exit(2) filename = source_filename main_added = True else: main_added = False if Utils.isFile(filename): source_filename = filename source_ref = SourceCodeReferences.fromFilename(filename=filename, ) if is_main: module_name = "__main__" else: module_name = Utils.basename(filename) if module_name.endswith(".py"): module_name = module_name[:-3] if is_shlib: module_name = module_name.split('.')[0] if '.' in module_name: sys.stderr.write( "Error, '%s' is not a proper python module name.\n" % (module_name)) sys.exit(2) if is_shlib: result = PythonShlibModule(name=module_name, package_name=package, source_ref=source_ref) elif is_main: result = PythonMainModule(main_added=main_added, source_ref=source_ref) else: if package is not None: full_name = package + "." + module_name else: full_name = module_name decision = Plugins.decideCompilation(full_name, source_ref) if decision == "compiled": result = CompiledPythonModule(name=module_name, package_name=package, source_ref=source_ref) else: source_code = readSourceCodeFromFilename(module_name, filename) source_code = Plugins.onFrozenModuleSourceCode( module_name=full_name, is_package=False, source_code=source_code) bytecode = compile(source_code, filename, "exec") bytecode = Plugins.onFrozenModuleBytecode( module_name=module_name, is_package=False, bytecode=bytecode) result = UncompiledPythonModule(name=module_name, package_name=package, bytecode=bytecode, filename=filename, user_provided=True, technical=False, source_ref=source_ref) # Don't read it anymore. source_filename = None elif Importing.isPackageDir(filename): if is_top: package_name = Utils.splitpath(filename)[-1] else: package_name = Utils.basename(filename) source_filename = Utils.joinpath(filename, "__init__.py") if not Utils.isFile(source_filename): source_ref, result = createNamespacePackage( package_name=package_name, module_relpath=filename) source_filename = None else: source_ref = SourceCodeReferences.fromFilename( filename=Utils.abspath(source_filename), ) if package is not None: full_name = package + "." + package_name else: full_name = package_name decision = Plugins.decideCompilation(full_name, source_ref) if decision == "compiled": result = CompiledPythonPackage(name=package_name, package_name=package, source_ref=source_ref) else: bytecode = compile(source_code, filename, "exec") bytecode = Plugins.onFrozenModuleBytecode( module_name=module_name, is_package=False, bytecode=bytecode) result = UncompiledPythonPackage(name=module_name, package_name=package, bytecode=bytecode, filename=filename, user_provided=True, technical=False, source_ref=source_ref) # Don't read it anymore. source_filename = None else: sys.stderr.write("%s: can't open file '%s'.\n" % (Utils.basename(sys.argv[0]), filename)) sys.exit(2) if not Options.shallHaveStatementLines(): source_ref = source_ref.atInternal() return result, source_ref, source_filename