setup.py
changeset 11632 b05f361db666
child 11642 3defbb0f147a
equal deleted inserted replaced
-1:000000000000 11632:b05f361db666
       
     1 #!/usr/bin/env python
       
     2 # pylint: disable=W0142,W0403,W0404,W0613,W0622,W0622,W0704,R0904,C0103,E0611
       
     3 #
       
     4 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     5 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 #
       
     7 # This file is part of CubicWeb tag cube.
       
     8 #
       
     9 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
    10 # terms of the GNU Lesser General Public License as published by the Free
       
    11 # Software Foundation, either version 2.1 of the License, or (at your option)
       
    12 # any later version.
       
    13 #
       
    14 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    15 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    16 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    17 # details.
       
    18 #
       
    19 # You should have received a copy of the GNU Lesser General Public License along
       
    20 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    21 """Generic Setup script, takes package info from __pkginfo__.py file
       
    22 """
       
    23 __docformat__ = "restructuredtext en"
       
    24 
       
    25 import os
       
    26 import sys
       
    27 import shutil
       
    28 from os.path import isdir, exists, join, walk
       
    29 
       
    30 try:
       
    31     if os.environ.get('NO_SETUPTOOLS'):
       
    32         raise ImportError() # do as there is no setuptools
       
    33     from setuptools import setup
       
    34     from setuptools.command import install_lib
       
    35     USE_SETUPTOOLS = True
       
    36 except ImportError:
       
    37     from distutils.core import setup
       
    38     from distutils.command import install_lib
       
    39     USE_SETUPTOOLS = False
       
    40 from distutils.command import install_data
       
    41 
       
    42 # import required features
       
    43 from __pkginfo__ import modname, version, license, description, web, \
       
    44      author, author_email, classifiers
       
    45 
       
    46 if exists('README'):
       
    47     long_description = file('README').read()
       
    48 else:
       
    49     long_description = ''
       
    50 
       
    51 # import optional features
       
    52 import __pkginfo__
       
    53 if USE_SETUPTOOLS:
       
    54     requires = {}
       
    55     for entry in ("__depends__",): # "__recommends__"):
       
    56         requires.update(getattr(__pkginfo__, entry, {}))
       
    57     install_requires = [("%s %s" % (d, v and v or "")).strip()
       
    58                        for d, v in requires.iteritems()]
       
    59 else:
       
    60     install_requires = []
       
    61 
       
    62 distname = getattr(__pkginfo__, 'distname', modname)
       
    63 scripts = getattr(__pkginfo__, 'scripts', ())
       
    64 include_dirs = getattr(__pkginfo__, 'include_dirs', ())
       
    65 data_files = getattr(__pkginfo__, 'data_files', None)
       
    66 ext_modules = getattr(__pkginfo__, 'ext_modules', None)
       
    67 dependency_links = getattr(__pkginfo__, 'dependency_links', ())
       
    68 
       
    69 BASE_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build')
       
    70 IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~')
       
    71 
       
    72 
       
    73 def ensure_scripts(linux_scripts):
       
    74     """
       
    75     Creates the proper script names required for each platform
       
    76     (taken from 4Suite)
       
    77     """
       
    78     from distutils import util
       
    79     if util.get_platform()[:3] == 'win':
       
    80         scripts_ = [script + '.bat' for script in linux_scripts]
       
    81     else:
       
    82         scripts_ = linux_scripts
       
    83     return scripts_
       
    84 
       
    85 def export(from_dir, to_dir,
       
    86            blacklist=BASE_BLACKLIST,
       
    87            ignore_ext=IGNORED_EXTENSIONS,
       
    88            verbose=True):
       
    89     """make a mirror of from_dir in to_dir, omitting directories and files
       
    90     listed in the black list
       
    91     """
       
    92     def make_mirror(arg, directory, fnames):
       
    93         """walk handler"""
       
    94         for norecurs in blacklist:
       
    95             try:
       
    96                 fnames.remove(norecurs)
       
    97             except ValueError:
       
    98                 pass
       
    99         for filename in fnames:
       
   100             # don't include binary files
       
   101             if filename[-4:] in ignore_ext:
       
   102                 continue
       
   103             if filename[-1] == '~':
       
   104                 continue
       
   105             src = join(directory, filename)
       
   106             dest = to_dir + src[len(from_dir):]
       
   107             if verbose:
       
   108                 sys.stderr.write('%s -> %s\n' % (src, dest))
       
   109             if os.path.isdir(src):
       
   110                 if not exists(dest):
       
   111                     os.mkdir(dest)
       
   112             else:
       
   113                 if exists(dest):
       
   114                     os.remove(dest)
       
   115                 shutil.copy2(src, dest)
       
   116     try:
       
   117         os.mkdir(to_dir)
       
   118     except OSError as ex:
       
   119         # file exists ?
       
   120         import errno
       
   121         if ex.errno != errno.EEXIST:
       
   122             raise
       
   123     walk(from_dir, make_mirror, None)
       
   124 
       
   125 
       
   126 class MyInstallLib(install_lib.install_lib):
       
   127     """extend install_lib command to handle  package __init__.py and
       
   128     include_dirs variable if necessary
       
   129     """
       
   130     def run(self):
       
   131         """overridden from install_lib class"""
       
   132         install_lib.install_lib.run(self)
       
   133         # manually install included directories if any
       
   134         if include_dirs:
       
   135             base = modname
       
   136             for directory in include_dirs:
       
   137                 dest = join(self.install_dir, base, directory)
       
   138                 export(directory, dest, verbose=False)
       
   139 
       
   140 # re-enable copying data files in sys.prefix
       
   141 old_install_data = install_data.install_data
       
   142 if USE_SETUPTOOLS:
       
   143     # overwrite InstallData to use sys.prefix instead of the egg directory
       
   144     class MyInstallData(old_install_data):
       
   145         """A class that manages data files installation"""
       
   146         def run(self):
       
   147             _old_install_dir = self.install_dir
       
   148             if self.install_dir.endswith('egg'):
       
   149                 self.install_dir = sys.prefix
       
   150             old_install_data.run(self)
       
   151             self.install_dir = _old_install_dir
       
   152     try:
       
   153         import setuptools.command.easy_install # only if easy_install available
       
   154         # monkey patch: Crack SandboxViolation verification
       
   155         from setuptools.sandbox import DirectorySandbox as DS
       
   156         old_ok = DS._ok
       
   157         def _ok(self, path):
       
   158             """Return True if ``path`` can be written during installation."""
       
   159             out = old_ok(self, path) # here for side effect from setuptools
       
   160             realpath = os.path.normcase(os.path.realpath(path))
       
   161             allowed_path = os.path.normcase(sys.prefix)
       
   162             if realpath.startswith(allowed_path):
       
   163                 out = True
       
   164             return out
       
   165         DS._ok = _ok
       
   166     except ImportError:
       
   167         pass
       
   168 
       
   169 def install(**kwargs):
       
   170     """setup entry point"""
       
   171     if USE_SETUPTOOLS:
       
   172         if '--force-manifest' in sys.argv:
       
   173             sys.argv.remove('--force-manifest')
       
   174     # install-layout option was introduced in 2.5.3-1~exp1
       
   175     elif sys.version_info < (2, 5, 4) and '--install-layout=deb' in sys.argv:
       
   176         sys.argv.remove('--install-layout=deb')
       
   177     cmdclass = {'install_lib': MyInstallLib}
       
   178     if USE_SETUPTOOLS:
       
   179         kwargs['install_requires'] = install_requires
       
   180         kwargs['dependency_links'] = dependency_links
       
   181         kwargs['zip_safe'] = False
       
   182         cmdclass['install_data'] = MyInstallData
       
   183 
       
   184     return setup(name = distname,
       
   185                  version = version,
       
   186                  license = license,
       
   187                  description = description,
       
   188                  long_description = long_description,
       
   189                  author = author,
       
   190                  author_email = author_email,
       
   191                  url = web,
       
   192                  scripts = ensure_scripts(scripts),
       
   193                  data_files = data_files,
       
   194                  ext_modules = ext_modules,
       
   195                  cmdclass = cmdclass,
       
   196                  classifiers = classifiers,
       
   197                  **kwargs
       
   198                  )
       
   199 
       
   200 if __name__ == '__main__' :
       
   201     install()