def createNamespacePackage(package_name, module_relpath): parts = package_name.split('.') source_ref = SourceCodeReference.fromFilenameAndLine( filename = module_relpath, line = 1, future_spec = FutureSpec(), ) source_ref = source_ref.atInternal() package_package_name = '.'.join(parts[:-1]) or None package = CompiledPythonPackage( name = parts[-1], package_name = package_package_name, source_ref = source_ref, ) if python_version >= 300: statement = createPython3NamespacePath( package_name = package_name, module_relpath = module_relpath, source_ref = source_ref ) else: statement = createPathAssignment(source_ref) package.setBody( makeStatementsSequenceFromStatement( statement = statement ) ) completeVariableClosures(package) return source_ref, package
def createNamespacePackage(module_name, is_top, module_relpath): source_ref = SourceCodeReference.fromFilenameAndLine( filename=module_relpath, line=1) source_ref = source_ref.atInternal() package = CompiledPythonPackage( module_name=module_name, is_top=is_top, mode="compiled", future_spec=FutureSpec(), source_ref=source_ref, ) if python_version >= 300: statement = createPython3NamespacePath(package=package, module_relpath=module_relpath, source_ref=source_ref) else: statement = createPathAssignment(package, source_ref) package.setBody(makeStatementsSequenceFromStatement(statement=statement)) completeVariableClosures(package) return source_ref, package
def createNamespacePackage(package_name, module_relpath): parts = package_name.split('.') source_ref = SourceCodeReference.fromFilenameAndLine( filename=module_relpath, line=1, future_spec=FutureSpec(), ) source_ref = source_ref.atInternal() package_package_name = '.'.join(parts[:-1]) or None package = CompiledPythonPackage( name=parts[-1], mode="compiled", package_name=package_package_name, source_ref=source_ref, ) if python_version >= 300: statement = createPython3NamespacePath(package_name=package_name, module_relpath=module_relpath, source_ref=source_ref) else: statement = createPathAssignment(source_ref) package.setBody(makeStatementsSequenceFromStatement(statement=statement)) completeVariableClosures(package) return source_ref, package
def createNamespacePackage(module_name, package_name, is_top, module_relpath): source_ref = SourceCodeReference.fromFilenameAndLine( filename=module_relpath, line=1 ) source_ref = source_ref.atInternal() package_name = package_name or None package = CompiledPythonPackage( name=module_name, package_name=package_name, is_top=is_top, mode="compiled", future_spec=FutureSpec(), source_ref=source_ref, ) if python_version >= 300: statement = createPython3NamespacePath( package=package, module_relpath=module_relpath, source_ref=source_ref ) else: statement = createPathAssignment(package, source_ref) package.setBody(makeStatementsSequenceFromStatement(statement=statement)) completeVariableClosures(package) return source_ref, package
def createNamespacePackage(module_name, is_top, source_ref): package = CompiledPythonPackage( module_name=module_name, is_top=is_top, mode="compiled", future_spec=FutureSpec(), source_ref=source_ref, ) if python_version >= 0x300: statement = createPython3NamespacePath(package=package, source_ref=source_ref) else: statement = createPathAssignment(package, source_ref) package.setChild("body", makeStatementsSequenceFromStatement(statement=statement)) completeVariableClosures(package) return package
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 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 = CompiledPythonModule(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 = CompiledPythonPackage(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 _createModule( module_name, source_code, source_ref, is_extension, is_namespace, is_package, is_top, is_main, main_added, ): if is_extension: result = PythonExtensionModule(module_name=module_name, technical=False, source_ref=source_ref) elif is_main: result = PythonMainModule( main_added=main_added, module_name=module_name, mode=decideCompilationMode(is_top=is_top, module_name=module_name, for_pgo=False), future_spec=None, source_ref=source_ref, ) checkPythonVersionFromCode(source_code) elif is_namespace: result = createNamespacePackage(module_name, is_top, source_ref) else: mode = decideCompilationMode(is_top=is_top, module_name=module_name, for_pgo=False) if (mode == "bytecode" and not is_top and not Options.shallDisableBytecodeCacheUsage() and hasCachedImportedModulesNames(module_name, source_code)): result = _loadUncompiledModuleFromCache( module_name=module_name, is_package=is_package, source_code=source_code, source_ref=source_ref, ) # Not used anymore source_code = None else: if is_package: result = CompiledPythonPackage( module_name=module_name, is_top=is_top, mode=mode, future_spec=None, source_ref=source_ref, ) else: result = CompiledPythonModule( module_name=module_name, is_top=is_top, mode=mode, future_spec=None, source_ref=source_ref, ) return result
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