cubicweb/skeleton/setup.py
changeset 11057 0b59724cb3f2
parent 10948 3ffacbdf7e9c
child 11071 fdadf59be612
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     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.
       
     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
       
    20 # along 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 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 = open('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.items()]
       
    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 
       
    86 def export(from_dir, to_dir,
       
    87            blacklist=BASE_BLACKLIST,
       
    88            ignore_ext=IGNORED_EXTENSIONS,
       
    89            verbose=True):
       
    90     """make a mirror of from_dir in to_dir, omitting directories and files
       
    91     listed in the black list
       
    92     """
       
    93     def make_mirror(arg, directory, fnames):
       
    94         """walk handler"""
       
    95         for norecurs in blacklist:
       
    96             try:
       
    97                 fnames.remove(norecurs)
       
    98             except ValueError:
       
    99                 pass
       
   100         for filename in fnames:
       
   101             # don't include binary files
       
   102             if filename[-4:] in ignore_ext:
       
   103                 continue
       
   104             if filename[-1] == '~':
       
   105                 continue
       
   106             src = join(directory, filename)
       
   107             dest = to_dir + src[len(from_dir):]
       
   108             if verbose:
       
   109                 sys.stderr.write('%s -> %s\n' % (src, dest))
       
   110             if os.path.isdir(src):
       
   111                 if not exists(dest):
       
   112                     os.mkdir(dest)
       
   113             else:
       
   114                 if exists(dest):
       
   115                     os.remove(dest)
       
   116                 shutil.copy2(src, dest)
       
   117     try:
       
   118         os.mkdir(to_dir)
       
   119     except OSError as ex:
       
   120         # file exists ?
       
   121         import errno
       
   122         if ex.errno != errno.EEXIST:
       
   123             raise
       
   124     walk(from_dir, make_mirror, None)
       
   125 
       
   126 
       
   127 class MyInstallLib(install_lib.install_lib):
       
   128     """extend install_lib command to handle  package __init__.py and
       
   129     include_dirs variable if necessary
       
   130     """
       
   131     def run(self):
       
   132         """overridden from install_lib class"""
       
   133         install_lib.install_lib.run(self)
       
   134         # manually install included directories if any
       
   135         if include_dirs:
       
   136             base = modname
       
   137             for directory in include_dirs:
       
   138                 dest = join(self.install_dir, base, directory)
       
   139                 export(directory, dest, verbose=False)
       
   140 
       
   141 # re-enable copying data files in sys.prefix
       
   142 old_install_data = install_data.install_data
       
   143 if USE_SETUPTOOLS:
       
   144     # overwrite InstallData to use sys.prefix instead of the egg directory
       
   145     class MyInstallData(old_install_data):
       
   146         """A class that manages data files installation"""
       
   147         def run(self):
       
   148             _old_install_dir = self.install_dir
       
   149             if self.install_dir.endswith('egg'):
       
   150                 self.install_dir = sys.prefix
       
   151             old_install_data.run(self)
       
   152             self.install_dir = _old_install_dir
       
   153     try:
       
   154         # only if easy_install available
       
   155         import setuptools.command.easy_install  # noqa
       
   156         # monkey patch: Crack SandboxViolation verification
       
   157         from setuptools.sandbox import DirectorySandbox as DS
       
   158         old_ok = DS._ok
       
   159 
       
   160         def _ok(self, path):
       
   161             """Return True if ``path`` can be written during installation."""
       
   162             out = old_ok(self, path)  # here for side effect from setuptools
       
   163             realpath = os.path.normcase(os.path.realpath(path))
       
   164             allowed_path = os.path.normcase(sys.prefix)
       
   165             if realpath.startswith(allowed_path):
       
   166                 out = True
       
   167             return out
       
   168         DS._ok = _ok
       
   169     except ImportError:
       
   170         pass
       
   171 
       
   172 
       
   173 def install(**kwargs):
       
   174     """setup entry point"""
       
   175     if USE_SETUPTOOLS:
       
   176         if '--force-manifest' in sys.argv:
       
   177             sys.argv.remove('--force-manifest')
       
   178     # install-layout option was introduced in 2.5.3-1~exp1
       
   179     elif sys.version_info < (2, 5, 4) and '--install-layout=deb' in sys.argv:
       
   180         sys.argv.remove('--install-layout=deb')
       
   181     cmdclass = {'install_lib': MyInstallLib}
       
   182     if USE_SETUPTOOLS:
       
   183         kwargs['install_requires'] = install_requires
       
   184         kwargs['dependency_links'] = dependency_links
       
   185         kwargs['zip_safe'] = False
       
   186         cmdclass['install_data'] = MyInstallData
       
   187 
       
   188     return setup(name=distname,
       
   189                  version=version,
       
   190                  license=license,
       
   191                  description=description,
       
   192                  long_description=long_description,
       
   193                  author=author,
       
   194                  author_email=author_email,
       
   195                  url=web,
       
   196                  scripts=ensure_scripts(scripts),
       
   197                  data_files=data_files,
       
   198                  ext_modules=ext_modules,
       
   199                  cmdclass=cmdclass,
       
   200                  classifiers=classifiers,
       
   201                  **kwargs
       
   202                  )
       
   203 
       
   204 
       
   205 if __name__ == '__main__':
       
   206     install()