def find_needed_modules(mf=None, scripts=(), includes=(), packages=(), warn=warnings.warn): if mf is None: mf = modulegraph.ModuleGraph() # feed Modulefinder with everything, and return it. for path in scripts: mf.run_script(path) for mod in includes: try: if mod[-2:] == '.*': mf.import_hook(mod[:-2], None, ['*']) else: mf.import_hook(mod) except ImportError: warn("No module named %s" % (mod, )) for f in packages: # If modulegraph has seen a reference to the package, then # we prefer to believe that (imp_find_module doesn't seem to locate # sub-packages) m = mf.findNode(f) if m is not None: path = m.packagepath[0] else: # Find path of package # TODO: use imp_find_module_or_importer try: path = imp_find_module(f, mf.path)[1] except ImportError: warn("No package named %s" % f) continue # walk the path to find subdirs containing __init__.py files # scan the results (directory of __init__.py files) # first trim the path (of the head package), # then convert directory name in package name, # finally push into modulegraph. # FIXME: # 1) Needs to be adjusted for namespace packages in python 3.3 # 2) Code is fairly dodgy and needs better tests for (dirpath, dirnames, filenames) in os.walk(path): if '__init__.py' in filenames and dirpath.startswith(path): package = f + '.' + dirpath[len(path) + 1:].replace( os.sep, '.') if package.endswith('.'): package = package[:-1] m = mf.import_hook(package, None, ["*"]) else: # Exclude subtrees that aren't packages dirnames[:] = [] return mf
def find_needed_modules(mf=None, scripts=(), includes=(), packages=(), warn=warnings.warn): if mf is None: mf = modulegraph.ModuleGraph() # feed Modulefinder with everything, and return it. for path in scripts: mf.run_script(path) for mod in includes: try: if mod[-2:] == '.*': mf.import_hook(mod[:-2], None, ['*']) else: mf.import_hook(mod) except ImportError: warn("No module named %s"%(mod,)) for f in packages: # If modulegraph has seen a reference to the package, then # we prefer to believe that (imp_find_module doesn't seem to locate # sub-packages) m = mf.findNode(f) if m is not None: path = m.packagepath[0] else: # Find path of package # TODO: use imp_find_module_or_importer try: path = imp_find_module(f, mf.path)[1] except ImportError: warn("No package named %s" % f) continue # walk the path to find subdirs containing __init__.py files # scan the results (directory of __init__.py files) # first trim the path (of the head package), # then convert directory name in package name, # finally push into modulegraph. # FIXME: # 1) Needs to be adjusted for namespace packages in python 3.3 # 2) Code is fairly dodgy and needs better tests for (dirpath, dirnames, filenames) in os.walk(path): if '__init__.py' in filenames and dirpath.startswith(path): package = f + '.' + dirpath[len(path)+1:].replace(os.sep, '.') if package.endswith('.'): package = package[:-1] m = mf.import_hook(package, None, ["*"]) else: # Exclude subtrees that aren't packages dirnames[:] = [] return mf
def check(mf): m = mf.findNode('Image') or mf.findNode('PIL.Image') if m is None or m.filename is None: return None plugins = set() visited = set() for folder in sys.path: if not isinstance(folder, str): continue folder = os.path.realpath(folder) if (not os.path.isdir(folder)) or (folder in visited): continue for fn in os.listdir(folder): if not fn.endswith('ImagePlugin.py'): continue mod, ext = os.path.splitext(fn) try: imp_find_module(mod) except ImportError: pass else: plugins.add(mod) visited.add(folder) s = StringIO('_recipes_pil_prescript(%r)\n' % list(plugins)) for plugin in plugins: mf.implyNodeReference(m, plugin) mf.removeReference(m, 'FixTk') # Since Imaging-1.1.5, SpiderImagePlugin imports ImageTk conditionally. # This is not ever used unless the user is explicitly using Tk elsewhere. sip = mf.findNode('SpiderImagePlugin') if sip is not None: mf.removeReference(sip, 'ImageTk') return dict( prescripts=['pluginbuilder.recipes.PIL.prescript', s], flatpackages=[os.path.dirname(m.filename)], )
def check(mf): m = mf.findNode('Image') or mf.findNode('PIL.Image') if m is None or m.filename is None: return None plugins = set() visited = set() for folder in sys.path: if not isinstance(folder, str): continue folder = os.path.realpath(folder) if (not os.path.isdir(folder)) or (folder in visited): continue for fn in os.listdir(folder): if not fn.endswith('ImagePlugin.py'): continue mod, ext = os.path.splitext(fn) try: imp_find_module(mod) except ImportError: pass else: plugins.add(mod) visited.add(folder) s = StringIO('_recipes_pil_prescript(%r)\n' % list(plugins)) for plugin in plugins: mf.implyNodeReference(m, plugin) mf.removeReference(m, 'FixTk') # Since Imaging-1.1.5, SpiderImagePlugin imports ImageTk conditionally. # This is not ever used unless the user is explicitly using Tk elsewhere. sip = mf.findNode('SpiderImagePlugin') if sip is not None: mf.removeReference(sip, 'ImageTk') return dict( prescripts = ['pluginbuilder.recipes.PIL.prescript', s], flatpackages = [os.path.dirname(m.filename)], )
def plat_prepare(includes, packages, excludes): # used by Python itself includes.update(["warnings", "unicodedata", "weakref"]) #if os.uname()[0] != 'java': # Jython specific imports in the stdlib: #excludes.update([ # 'java.lang', # 'org.python.core', #]) if not sys.platform.startswith('irix'): excludes.update([ 'AL', 'sgi', 'vms_lib', ]) if not sys.platform in ('mac', 'darwin'): # XXX - this doesn't look nearly complete excludes.update([ 'Audio_mac', 'Carbon.File', 'Carbon.Folder', 'Carbon.Folders', 'EasyDialogs', 'MacOS', 'macfs', 'macostools', #'macpath', '_scproxy', ]) if not sys.platform == 'win32': # only win32 excludes.update([ #'ntpath', 'nturl2path', 'win32api', 'win32con', 'win32event', 'win32evtlogutil', 'win32evtlog', 'win32file', 'win32gui', 'win32pipe', 'win32process', 'win32security', 'pywintypes', 'winsound', 'win32', '_winreg', '_winapi', 'msvcrt', 'winreg', '_subprocess', ]) if not sys.platform == 'riscos': excludes.update([ 'riscosenviron', #'riscospath', 'rourl2path', ]) if not sys.platform == 'dos' or sys.platform.startswith('ms-dos'): excludes.update([ 'dos', ]) if not sys.platform == 'os2emx': excludes.update([ #'os2emxpath', '_emx_link', ]) excludes.update({'posix', 'nt', 'os2', 'mac', 'ce', 'riscos'} - set(sys.builtin_module_names)) # Carbon.Res depends on this, but the module hasn't been present # for a while... excludes.add('OverrideFrom23') excludes.add('OverrideFrom23._Res') # import trickery in the dummy_threading module (stdlib) excludes.add('_dummy_threading') try: imp_find_module('poll') except ImportError: excludes.update([ 'poll', ])
def test_imp_find_module(self): fn = util.imp_find_module('encodings.aliases')[1] self.assertSamePath(encodings.aliases.__file__, fn)
def check(cmd, mf): m = mf.findNode("Image") or mf.findNode("PIL.Image") if m is None or m.filename is None: return None have_PIL = bool(mf.findNode("PIL.Image")) plugins = set() visited = set() # XXX: Most users should now use Pillow, which always uses # "PIL.Image", which can simply the code below. for folder in sys.path: if not isinstance(folder, str): continue for extra in ("", "PIL"): folder = os.path.realpath(os.path.join(folder, extra)) if (not os.path.isdir(folder)) or (folder in visited): continue for fn in os.listdir(folder): if not fn.endswith("ImagePlugin.py"): continue mod, ext = os.path.splitext(fn) try: sys.path.insert(0, folder) imp_find_module(mod) del sys.path[0] except ImportError: pass else: plugins.add(mod) visited.add(folder) s = StringIO("_recipes_pil_prescript(%r)\n" % list(plugins)) print(plugins) plugins = set() # sys.exit(1) for plugin in plugins: if have_PIL: mf.implyNodeReference(m, "PIL." + plugin) else: mf.implyNodeReference(m, plugin) mf.removeReference(m, "FixTk") # Since Imaging-1.1.5, SpiderImagePlugin imports ImageTk conditionally. # This is not ever used unless the user is explicitly using Tk elsewhere. sip = mf.findNode("SpiderImagePlugin") if sip is not None: mf.removeReference(sip, "ImageTk") # The ImageQt plugin should only be useful when using PyQt, which # would then be explicitly imported. # Note: this code doesn't have the right side-effect at the moment # due to the way the PyQt5 recipe is structured. sip = mf.findNode("PIL.ImageQt") if sip is not None: mf.removeReference(sip, "PyQt5") mf.removeReference(sip, "PyQt5.QtGui") mf.removeReference(sip, "PyQt5.QtCore") mf.removeReference(sip, "PyQt4") mf.removeReference(sip, "PyQt4.QtGui") mf.removeReference(sip, "PyQt4.QtCore") pass imagefilter = mf.findNode("PIL.ImageFilter") if imagefilter is not None: # Optional dependency on numpy to process # numpy data passed into the filter. Remove # this reference to ensure numpy is only copied # when it is actually used in the application. mf.removeReference(imagefilter, "numpy") image = mf.findNode("PIL.Image") if image is not None: # Optional dependency on numpy to convert # to a numpy array. mf.removeReference(image, "numpy") return { "prescripts": ["py2app.recipes.PIL.prescript", s], "include": "PIL.JpegPresets", # import from PIL.JpegPlugin in Pillow 2.0 "flatpackages": [os.path.dirname(m.filename)], }
def check(cmd, mf): m = mf.findNode('Image') or mf.findNode('PIL.Image') if m is None or m.filename is None: return None if mf.findNode('PIL.Image'): have_PIL = True else: have_PIL = False plugins = set() visited = set() for folder in sys.path: if not isinstance(folder, basestring): continue for extra in ('', 'PIL'): folder = os.path.realpath(os.path.join(folder, extra)) if (not os.path.isdir(folder)) or (folder in visited): continue for fn in os.listdir(folder): if not fn.endswith('ImagePlugin.py'): continue mod, ext = os.path.splitext(fn) try: sys.path.insert(0, folder) imp_find_module(mod) del sys.path[0] except ImportError: pass else: plugins.add(mod) visited.add(folder) s = StringIO('_recipes_pil_prescript(%r)\n' % list(plugins)) for plugin in plugins: if have_PIL: mf.implyNodeReference(m, 'PIL.' + plugin) else: mf.implyNodeReference(m, plugin) mf.removeReference(m, 'FixTk') # Since Imaging-1.1.5, SpiderImagePlugin imports ImageTk conditionally. # This is not ever used unless the user is explicitly using Tk elsewhere. sip = mf.findNode('SpiderImagePlugin') if sip is not None: mf.removeReference(sip, 'ImageTk') # The ImageQt plugin should only be usefull when using PyQt, which # would then be explicitly imported. # Note: this code doesn't have the right side-effect at the moment # due to the way the PyQt5 recipe is structured. sip = mf.findNode('PIL.ImageQt') if sip is not None: mf.removeReference(sip, 'PyQt5') mf.removeReference(sip, 'PyQt5.QtGui') mf.removeReference(sip, 'PyQt5.QtCore') mf.removeReference(sip, 'PyQt4') mf.removeReference(sip, 'PyQt4.QtGui') mf.removeReference(sip, 'PyQt4.QtCore') pass return dict( prescripts=['py2app.recipes.PIL.prescript', s], include="PIL.JpegPresets", # import from PIL.JpegPlugin in Pillow 2.0 flatpackages=[os.path.dirname(m.filename)], )
def test_imp_find_module(self): fn = util.imp_find_module('encodings.aliases')[1] self.assertTrue(encodings.aliases.__file__.startswith(fn))
def plat_prepare(includes, packages, excludes): # used by Python itself includes.update(["warnings", "unicodedata", "weakref"]) if not sys.platform.startswith('irix'): excludes.update([ 'AL', 'sgi', ]) if not sys.platform in ('mac', 'darwin'): # XXX - this doesn't look nearly complete excludes.update([ 'Audio_mac', 'Carbon.File', 'Carbon.Folder', 'Carbon.Folders', 'EasyDialogs', 'MacOS', 'macfs', 'macostools', 'macpath', ]) if not sys.platform == 'win32': # only win32 excludes.update([ 'ntpath', 'nturl2path', 'win32api', 'win32con', 'win32event', 'win32evtlogutil', 'win32evtlog', 'win32file', 'win32gui', 'win32pipe', 'win32process', 'win32security', 'pywintypes', 'winsound', 'win32', '_winreg', ]) if not sys.platform == 'riscos': excludes.update([ 'riscosenviron', 'riscospath', 'rourl2path', ]) if not sys.platform == 'dos' or sys.platform.startswith('ms-dos'): excludes.update([ 'dos', ]) if not sys.platform == 'os2emx': excludes.update([ 'os2emxpath' ]) excludes.update(set(['posix', 'nt', 'os2', 'mac', 'ce', 'riscos']) - set(sys.builtin_module_names)) try: imp_find_module('poll') except ImportError: excludes.update([ 'poll', ])
def plat_prepare(includes, packages, excludes): # used by Python itself includes.update(["warnings", "unicodedata", "weakref"]) if not sys.platform.startswith('irix'): excludes.update([ 'AL', 'sgi', ]) if not sys.platform in ('mac', 'darwin'): # XXX - this doesn't look nearly complete excludes.update([ 'Audio_mac', 'Carbon.File', 'Carbon.Folder', 'Carbon.Folders', 'EasyDialogs', 'MacOS', 'macfs', 'macostools', 'macpath', ]) if not sys.platform == 'win32': # only win32 excludes.update([ 'ntpath', 'nturl2path', 'win32api', 'win32con', 'win32event', 'win32evtlogutil', 'win32evtlog', 'win32file', 'win32gui', 'win32pipe', 'win32process', 'win32security', 'pywintypes', 'winsound', 'win32', '_winreg', ]) if not sys.platform == 'riscos': excludes.update([ 'riscosenviron', 'riscospath', 'rourl2path', ]) if not sys.platform == 'dos' or sys.platform.startswith('ms-dos'): excludes.update([ 'dos', ]) if not sys.platform == 'os2emx': excludes.update(['os2emxpath']) excludes.update( set(['posix', 'nt', 'os2', 'mac', 'ce', 'riscos']) - set(sys.builtin_module_names)) try: imp_find_module('poll') except ImportError: excludes.update([ 'poll', ])
def plat_prepare(includes, packages, excludes): # used by Python itself includes.update(["warnings", "unicodedata", "weakref"]) #if os.uname()[0] != 'java': # Jython specific imports in the stdlib: #excludes.update([ # 'java.lang', # 'org.python.core', #]) if not sys.platform.startswith('irix'): excludes.update([ 'AL', 'sgi', 'vms_lib', ]) if not sys.platform in ('mac', 'darwin'): # XXX - this doesn't look nearly complete excludes.update([ 'Audio_mac', 'Carbon.File', 'Carbon.Folder', 'Carbon.Folders', 'EasyDialogs', 'MacOS', 'macfs', 'macostools', #'macpath', '_scproxy', ]) if not sys.platform == 'win32': # only win32 excludes.update([ #'ntpath', 'nturl2path', 'win32api', 'win32con', 'win32event', 'win32evtlogutil', 'win32evtlog', 'win32file', 'win32gui', 'win32pipe', 'win32process', 'win32security', 'pywintypes', 'winsound', 'win32', '_winreg', '_winapi', 'msvcrt', 'winreg', '_subprocess', ]) if not sys.platform == 'riscos': excludes.update([ 'riscosenviron', #'riscospath', 'rourl2path', ]) if not sys.platform == 'dos' or sys.platform.startswith('ms-dos'): excludes.update([ 'dos', ]) if not sys.platform == 'os2emx': excludes.update([ #'os2emxpath', '_emx_link', ]) excludes.update(set(['posix', 'nt', 'os2', 'mac', 'ce', 'riscos']) - set(sys.builtin_module_names)) # Carbon.Res depends on this, but the module hasn't been present # for a while... excludes.add('OverrideFrom23') excludes.add('OverrideFrom23._Res') # import trickery in the dummy_threading module (stdlib) excludes.add('_dummy_threading') try: imp_find_module('poll') except ImportError: excludes.update([ 'poll', ])
def get_bootstrap(bootstrap): if isinstance(bootstrap, str): if not os.path.exists(bootstrap): bootstrap = imp_find_module(bootstrap)[1] return bootstrap