skeleton/setup.py
changeset 5331 f7ee75da6102
parent 5184 955ee1b24756
child 5334 7da1a6ca8f65
equal deleted inserted replaced
5330:19bc44f5f9d1 5331:f7ee75da6102
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     2 """Generic Setup script, takes package info from __pkginfo__.py file
     2 # pylint: disable-msg=W0404,W0622,W0704,W0613,W0152
       
     3 """Generic Setup script, takes package info from __pkginfo__.py file.
     3 
     4 
     4 :organization: Logilab
     5 :copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
       
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     8 """
     8 """
     9 # pylint: disable-msg=W0142,W0403,W0404,W0613,W0622,W0622,W0704,R0904,C0103,E0611
     9 __docformat__ = "restructuredtext en"
    10 #
       
    11 # This program is free software; you can redistribute it and/or modify it under
       
    12 # the terms of the GNU General Public License as published by the Free Software
       
    13 # Foundation; either version 2 of the License, or (at your option) any later
       
    14 # version.
       
    15 #
       
    16 # This program is distributed in the hope that it will be useful, but WITHOUT
       
    17 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    18 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
       
    19 #
       
    20 # You should have received a copy of the GNU General Public License along with
       
    21 # this program; if not, write to the Free Software Foundation, Inc.,
       
    22 # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
       
    23 
    10 
    24 import os
    11 import os
    25 import sys
    12 import sys
    26 import shutil
    13 import shutil
    27 from os.path import isdir, exists, join, walk
    14 from os.path import isdir, exists, join, walk
    28 
    15 
    29 try:
    16 try:
    30    if os.environ.get('NO_SETUPTOOLS'):
    17     if os.environ.get('NO_SETUPTOOLS'):
    31       raise ImportError() # do as there is no setuptools
    18         raise ImportError()
    32    from setuptools import setup
    19     from setuptools import setup
    33    from setuptools.command import install_lib
    20     from setuptools.command import install_lib
    34    USE_SETUPTOOLS = True
    21     USE_SETUPTOOLS = 1
    35 except ImportError:
    22 except ImportError:
    36    from distutils.core import setup
    23     from distutils.core import setup
    37    from distutils.command import install_lib
    24     from distutils.command import install_lib
    38    USE_SETUPTOOLS = False
    25     USE_SETUPTOOLS = 0
    39 
    26 
       
    27 
       
    28 sys.modules.pop('__pkginfo__', None)
    40 # import required features
    29 # import required features
    41 from __pkginfo__ import modname, version, license, description, web, \
    30 from __pkginfo__ import modname, version, license, short_desc, \
    42      author, author_email
    31      web, author, author_email
       
    32 # import optional features
       
    33 import __pkginfo__
       
    34 distname = getattr(__pkginfo__, 'distname', modname)
       
    35 scripts = getattr(__pkginfo__, 'scripts', [])
       
    36 data_files = getattr(__pkginfo__, 'data_files', None)
       
    37 include_dirs = getattr(__pkginfo__, 'include_dirs', [])
       
    38 ext_modules = getattr(__pkginfo__, 'ext_modules', None)
       
    39 dependency_links = getattr(__pkginfo__, 'dependency_links', None)
       
    40 
       
    41 STD_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build')
       
    42 
       
    43 IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~')
    43 
    44 
    44 if exists('README'):
    45 if exists('README'):
    45    long_description = file('README').read()
    46     long_description = file('README').read()
    46 else:
    47 else:
    47    long_description = ''
    48     long_description = ''
    48 
       
    49 # import optional features
       
    50 import __pkginfo__
       
    51 if USE_SETUPTOOLS:
    49 if USE_SETUPTOOLS:
    52    requires = {}
    50    requires = {}
    53    for entry in ("__depends__", "__recommends__"):
    51    for entry in ("__depends__", "__recommends__"):
    54       requires.update(getattr(__pkginfo__, entry, {}))
    52       requires.update(getattr(__pkginfo__, entry, {}))
    55    install_requires = [("%s %s" % (d, v and v or "")).strip()
    53    install_requires = [("%s %s" % (d, v and v or "")).strip()
    56                        for d, v in requires.iteritems()]
    54                        for d, v in requires.iteritems()]
    57 else:
    55 else:
    58    install_requires = []
    56    install_requires = []
    59 
    57 
    60 distname = getattr(__pkginfo__, 'distname', modname)
       
    61 scripts = getattr(__pkginfo__, 'scripts', ())
       
    62 include_dirs = getattr(__pkginfo__, 'include_dirs', ())
       
    63 data_files = getattr(__pkginfo__, 'data_files', None)
       
    64 subpackage_of = getattr(__pkginfo__, 'subpackage_of', None)
       
    65 ext_modules = getattr(__pkginfo__, 'ext_modules', None)
       
    66 
       
    67 
       
    68 BASE_BLACKLIST = ('CVS', 'debian', 'dist', 'build', '__buildlog')
       
    69 IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc')
       
    70 
       
    71 
    58 
    72 def ensure_scripts(linux_scripts):
    59 def ensure_scripts(linux_scripts):
    73     """
    60     """Creates the proper script names required for each platform
    74     Creates the proper script names required for each platform
       
    75     (taken from 4Suite)
    61     (taken from 4Suite)
    76     """
    62     """
    77     from distutils import util
    63     from distutils import util
    78     if util.get_platform()[:3] == 'win':
    64     if util.get_platform()[:3] == 'win':
    79         scripts_ = [script + '.bat' for script in linux_scripts]
    65         scripts_ = [script + '.bat' for script in linux_scripts]
    80     else:
    66     else:
    81         scripts_ = linux_scripts
    67         scripts_ = linux_scripts
    82     return scripts_
    68     return scripts_
    83 
    69 
    84 
       
    85 def get_packages(directory, prefix):
    70 def get_packages(directory, prefix):
    86     """return a list of subpackages for the given directory
    71     """return a list of subpackages for the given directory"""
    87     """
       
    88     result = []
    72     result = []
    89     for package in os.listdir(directory):
    73     for package in os.listdir(directory):
    90         absfile = join(directory, package)
    74         absfile = join(directory, package)
    91         if isdir(absfile):
    75         if isdir(absfile):
    92             if exists(join(absfile, '__init__.py')) or \
    76             if exists(join(absfile, '__init__.py')) or \
    97                     result.append(package)
    81                     result.append(package)
    98                 result += get_packages(absfile, result[-1])
    82                 result += get_packages(absfile, result[-1])
    99     return result
    83     return result
   100 
    84 
   101 def export(from_dir, to_dir,
    85 def export(from_dir, to_dir,
   102            blacklist=BASE_BLACKLIST,
    86            blacklist=STD_BLACKLIST,
   103            ignore_ext=IGNORED_EXTENSIONS,
    87            ignore_ext=IGNORED_EXTENSIONS,
   104            verbose=True):
    88            verbose=True):
   105     """make a mirror of from_dir in to_dir, omitting directories and files
    89     """make a mirror of from_dir in to_dir, omitting directories and files
   106     listed in the black list
    90     listed in the black list
   107     """
    91     """
   116             # don't include binary files
   100             # don't include binary files
   117             if filename[-4:] in ignore_ext:
   101             if filename[-4:] in ignore_ext:
   118                 continue
   102                 continue
   119             if filename[-1] == '~':
   103             if filename[-1] == '~':
   120                 continue
   104                 continue
   121             src = '%s/%s' % (directory, filename)
   105             src = join(directory, filename)
   122             dest = to_dir + src[len(from_dir):]
   106             dest = to_dir + src[len(from_dir):]
   123             if verbose:
   107             if verbose:
   124                print >> sys.stderr, src, '->', dest
   108                 print >> sys.stderr, src, '->', dest
   125             if os.path.isdir(src):
   109             if os.path.isdir(src):
   126                 if not exists(dest):
   110                 if not exists(dest):
   127                     os.mkdir(dest)
   111                     os.mkdir(dest)
   128             else:
   112             else:
   129                 if exists(dest):
   113                 if exists(dest):
   137         if ex.errno != errno.EEXIST:
   121         if ex.errno != errno.EEXIST:
   138             raise
   122             raise
   139     walk(from_dir, make_mirror, None)
   123     walk(from_dir, make_mirror, None)
   140 
   124 
   141 
   125 
   142 EMPTY_FILE = '"""generated file, don\'t modify or your data will be lost"""\n'
       
   143 
       
   144 class MyInstallLib(install_lib.install_lib):
   126 class MyInstallLib(install_lib.install_lib):
   145     """extend install_lib command to handle  package __init__.py and
   127     """extend install_lib command to handle  package __init__.py and
   146     include_dirs variable if necessary
   128     include_dirs variable if necessary
   147     """
   129     """
   148     def run(self):
   130     def run(self):
   149         """overridden from install_lib class"""
   131         """overridden from install_lib class"""
   150         install_lib.install_lib.run(self)
   132         install_lib.install_lib.run(self)
   151         # create Products.__init__.py if needed
       
   152         if subpackage_of:
       
   153             product_init = join(self.install_dir, subpackage_of, '__init__.py')
       
   154             if not exists(product_init):
       
   155                 self.announce('creating %s' % product_init)
       
   156                 stream = open(product_init, 'w')
       
   157                 stream.write(EMPTY_FILE)
       
   158                 stream.close()
       
   159         # manually install included directories if any
   133         # manually install included directories if any
   160         if include_dirs:
   134         if include_dirs:
   161             if subpackage_of:
   135             base = modname
   162                 base = join(subpackage_of, modname)
       
   163             else:
       
   164                 base = modname
       
   165             for directory in include_dirs:
   136             for directory in include_dirs:
   166                 dest = join(self.install_dir, base, directory)
   137                 dest = join(self.install_dir, base, directory)
   167                 export(directory, dest, verbose=False)
   138                 export(directory, dest, verbose=False)
   168 
   139 
   169 def install(**kwargs):
   140 def install(**kwargs):
   170     """setup entry point"""
   141     """setup entry point"""
   171     if not USE_SETUPTOOLS and '--install-layout=deb' in sys.argv and \
   142     try:
   172            sys.versioninfo < (2, 5, 4):
       
   173        sys.argv.remove('--install-layout=deb')
       
   174        print "W: remove '--install-layout=deb' option"
       
   175     if subpackage_of:
       
   176         package = subpackage_of + '.' + modname
       
   177         kwargs['package_dir'] = {package : '.'}
       
   178         packages = [package] + get_packages(os.getcwd(), package)
       
   179         if USE_SETUPTOOLS:
   143         if USE_SETUPTOOLS:
   180             kwargs['namespace_packages'] = [subpackage_of]
   144             sys.argv.remove('--force-manifest')
   181     else:
   145     except:
   182         kwargs['package_dir'] = {modname : '.'}
   146         pass
   183         packages = [modname] + get_packages(os.getcwd(), modname)
   147     try:
       
   148         if not USE_SETUPTOOLS:
       
   149             # install-layout option was introduced in 2.5.3-1~exp1
       
   150             if sys.versioninfo < (2, 5, 4):
       
   151                 sys.argv.remove('--install-layout=deb')
       
   152                 print "W: remove '--install-layout=deb' option"
       
   153     except:
       
   154         pass
       
   155     kwargs['package_dir'] = {modname : '.'}
       
   156     packages = [modname] + get_packages(os.getcwd(), modname)
       
   157     if USE_SETUPTOOLS and install_requires:
       
   158         kwargs['install_requires'] = install_requires
       
   159         kwargs['dependency_links'] = dependency_links
   184     kwargs['packages'] = packages
   160     kwargs['packages'] = packages
   185     return setup(name=distname, version=version, license=license, url=web,
   161     return setup(name = distname,
   186                  description=description, long_description=long_description,
   162                  version = version,
   187                  author=author, author_email=author_email,
   163                  license = license,
   188                  scripts=ensure_scripts(scripts), data_files=data_files,
   164                  description = short_desc,
   189                  ext_modules=ext_modules,
   165                  long_description = long_description,
   190                  install_requires=install_requires,
   166                  author = author,
   191                  #dependency_links=["http://alain:alain@intranet.logilab.fr/~alain/"],
   167                  author_email = author_email,
   192                  cmdclass={'install_lib': MyInstallLib},
   168                  url = web,
       
   169                  scripts = ensure_scripts(scripts),
       
   170                  data_files = data_files,
       
   171                  ext_modules = ext_modules,
       
   172                  cmdclass = {'install_lib': MyInstallLib},
   193                  **kwargs
   173                  **kwargs
   194                  )
   174                  )
   195 
   175 
   196 if __name__ == '__main__' :
   176 if __name__ == '__main__' :
   197     install()
   177     install()