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