web2py.gluon.import_all

Package web2py :: Package gluon :: Module import_all |
[hide private]
[frames] | no frames]

# Source Code for Module web2py.gluon.import_all

      1  #!/usr/bin/env python
      2
      3  """
      4  This file is part of the web2py Web Framework
      5  Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
      6  License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
      7
      8  This file is not strictly required by web2py. It is used for three purposes:
      9
     10  1) check that all required modules are installed properly
     11  2) provide py2exe and py2app a list of modules to be packaged in the binary
     12  3) (optional) preload modules in memory to speed up http responses
     13
     14  """
     15
     16  import os
     17  import sys
     18
     19  base_modules = ['aifc', 'anydbm', 'array', 'asynchat', 'asyncore', 'atexit',
     20                  'audioop', 'base64', 'BaseHTTPServer', 'Bastion', 'binascii',
     21                  'binhex', 'bisect', 'bz2', 'calendar', 'cgi', 'CGIHTTPServer',
     22                  'cgitb', 'chunk', 'cmath', 'cmd', 'code', 'codecs', 'codeop',
     23                  'collections', 'colorsys', 'compileall', 'compiler',
     24                  'compiler.ast', 'compiler.visitor', 'ConfigParser',
     25                  'contextlib', 'Cookie', 'cookielib', 'copy', 'copy_reg',
     26                  'cPickle', 'cProfile', 'cStringIO', 'csv', 'ctypes',
     27                  'datetime', 'decimal', 'difflib', 'dircache', 'dis',
     28                  'doctest', 'DocXMLRPCServer', 'dumbdbm', 'dummy_thread',
     29                  'dummy_threading', 'email', 'email.charset', 'email.encoders',
     30                  'email.errors', 'email.generator', 'email.header',
     31                  'email.iterators', 'email.message', 'email.mime',
     32                  'email.mime.audio', 'email.mime.base', 'email.mime.image',
     33                  'email.mime.message', 'email.mime.multipart',
     34                  'email.mime.nonmultipart', 'email.mime.text', 'email.parser',
     35                  'email.utils', 'encodings.idna', 'errno', 'exceptions',
     36                  'filecmp', 'fileinput', 'fnmatch', 'formatter', 'fpformat',
     37                  'ftplib', 'functools', 'gc', 'getopt', 'getpass', 'gettext',
     38                  'glob', 'gzip', 'hashlib', 'heapq', 'hmac', 'hotshot',
     39                  'hotshot.stats', 'htmlentitydefs', 'htmllib', 'HTMLParser',
     40                  'httplib', 'imaplib', 'imghdr', 'imp', 'inspect',
     41                  'itertools', 'keyword', 'linecache', 'locale', 'logging',
     42                  'macpath', 'mailbox', 'mailcap', 'marshal', 'math',
     43                  'mimetools', 'mimetypes', 'mmap', 'modulefinder', 'mutex',
     44                  'netrc', 'new', 'nntplib', 'operator', 'optparse', 'os',
     45                  'parser', 'pdb', 'pickle', 'pickletools', 'pkgutil',
     46                  'platform', 'poplib', 'pprint', 'py_compile', 'pyclbr',
     47                  'pydoc', 'Queue', 'quopri', 'random', 're', 'repr',
     48                  'rexec', 'rfc822', 'rlcompleter', 'robotparser', 'runpy',
     49                  'sched', 'select', 'sgmllib', 'shelve',
     50                  'shlex', 'shutil', 'signal', 'SimpleHTTPServer',
     51                  'SimpleXMLRPCServer', 'site', 'smtpd', 'smtplib',
     52                  'sndhdr', 'socket', 'SocketServer', 'sqlite3',
     53                  'stat', 'statvfs', 'string', 'StringIO',
     54                  'stringprep', 'struct', 'subprocess', 'sunau', 'symbol',
     55                  'tabnanny', 'tarfile', 'telnetlib', 'tempfile', 'textwrap', 'thread', 'threading',
     56                  'time', 'timeit', 'Tix', 'Tkinter', 'token',
     57                  'tokenize', 'trace', 'traceback', 'types',
     58                  'unicodedata', 'unittest', 'urllib', 'urllib2',
     59                  'urlparse', 'user', 'UserDict', 'UserList', 'UserString',
     60                  'uu', 'uuid', 'warnings', 'wave', 'weakref', 'webbrowser',
     61                  'whichdb', 'wsgiref', 'wsgiref.handlers', 'wsgiref.headers',
     62                  'wsgiref.simple_server', 'wsgiref.util', 'wsgiref.validate',
     63                  'xdrlib', 'xml.dom', 'xml.dom.minidom', 'xml.dom.pulldom',
     64                  'xml.etree.ElementTree', 'xml.parsers.expat', 'xml.sax',
     65                  'xml.sax.handler', 'xml.sax.saxutils', 'xml.sax.xmlreader',
     66                  'xmlrpclib', 'zipfile', 'zipimport', 'zlib', 'mhlib',
     67                  'MimeWriter', 'mimify', 'multifile', 'sets']
     68
     69  contributed_modules = []
     70  for root, dirs, files in os.walk('gluon'):
     71      for candidate in ['.'.join(
     72        os.path.join(root, os.path.splitext(name)[0]).split(os.sep))
     73        for name in files if name.endswith('.py')
     74          and root.split(os.sep) != ['gluon', 'tests']
     75        ]:
     76          contributed_modules.append(candidate)
     77
     78  # Python base version
     79  python_version = sys.version[:3]
     80
     81  # Modules which we want to raise an Exception if they are missing
     82  alert_dependency = ['hashlib', 'uuid']
     83
     84  # Now we remove the blacklisted modules if we are using the stated
     85  # python version.
     86  #
     87  # List of modules deprecated in Python 2.6 or 2.7 that are in the above set
     88  py26_deprecated = ['mhlib', 'multifile', 'mimify', 'sets', 'MimeWriter']
     89  py27_deprecated = [] # ['optparse'] but we need it for now
     90
     91  if python_version >= '2.6':
     92      base_modules += ['json', 'multiprocessing']
     93      base_modules = list(set(base_modules).difference(set(py26_deprecated)))
     94
     95  if python_version >= '2.7':
     96      base_modules += ['argparse']
     97      base_modules = list(set(base_modules).difference(set(py27_deprecated)))
     98
     99  # Now iterate in the base_modules, trying to do the import
    100  for module in base_modules + contributed_modules:
    101      try:
    102           __import__(module, globals(), locals(), [])
    103      except:
    104          # Raise an exception if the current module is a dependency
    105          if module in alert_dependency:
    106              msg = "Missing dependency: %(module)s\n" % locals()
    107              msg += "Try the following command: "
    108              msg += "easy_install-%(python_version)s -U %(module)s" % locals()
    109              raise ImportError, msg
    110