| 1 | #!/usr/bin/env python |
|---|
| 2 | # pylint: disable-msg=W0142,W0403,W0404,E0611,W0613,W0622,W0622,W0704,R0904 |
|---|
| 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 | from __future__ import nested_scopes |
|---|
| 22 | |
|---|
| 23 | __revision__ = '$Id: setup.py,v 1.3 2006-01-03 14:30:39 syt Exp $' |
|---|
| 24 | |
|---|
| 25 | import os |
|---|
| 26 | import sys |
|---|
| 27 | import shutil |
|---|
| 28 | from distutils.core import setup |
|---|
| 29 | from distutils.command import install_lib |
|---|
| 30 | from os.path import isdir, exists, join, walk |
|---|
| 31 | |
|---|
| 32 | # import required features |
|---|
| 33 | from __pkginfo__ import modname, version, license, short_desc, long_desc, \ |
|---|
| 34 | web, author, author_email |
|---|
| 35 | # import optional features |
|---|
| 36 | try: |
|---|
| 37 | from __pkginfo__ import distname |
|---|
| 38 | except ImportError: |
|---|
| 39 | distname = modname |
|---|
| 40 | try: |
|---|
| 41 | from __pkginfo__ import scripts |
|---|
| 42 | except ImportError: |
|---|
| 43 | scripts = [] |
|---|
| 44 | try: |
|---|
| 45 | from __pkginfo__ import data_files |
|---|
| 46 | except ImportError: |
|---|
| 47 | data_files = None |
|---|
| 48 | try: |
|---|
| 49 | from __pkginfo__ import subpackage_of |
|---|
| 50 | except ImportError: |
|---|
| 51 | subpackage_of = None |
|---|
| 52 | try: |
|---|
| 53 | from __pkginfo__ import include_dirs |
|---|
| 54 | except ImportError: |
|---|
| 55 | include_dirs = [] |
|---|
| 56 | try: |
|---|
| 57 | from __pkginfo__ import ext_modules |
|---|
| 58 | except ImportError: |
|---|
| 59 | ext_modules = None |
|---|
| 60 | |
|---|
| 61 | BASE_BLACKLIST = ('CVS', 'debian', 'dist', 'build', '__buildlog', '.svn') |
|---|
| 62 | IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc') |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | def ensure_scripts(linux_scripts): |
|---|
| 66 | """ |
|---|
| 67 | Creates the proper script names required for each platform |
|---|
| 68 | (taken from 4Suite) |
|---|
| 69 | """ |
|---|
| 70 | from distutils import util |
|---|
| 71 | if util.get_platform()[:3] == 'win': |
|---|
| 72 | scripts_ = [script + '.bat' for script in linux_scripts] |
|---|
| 73 | else: |
|---|
| 74 | scripts_ = linux_scripts |
|---|
| 75 | return scripts_ |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | def get_packages(directory, prefix): |
|---|
| 79 | """return a list of subpackages for the given directory |
|---|
| 80 | """ |
|---|
| 81 | result = [] |
|---|
| 82 | for package in os.listdir(directory): |
|---|
| 83 | absfile = join(directory, package) |
|---|
| 84 | if isdir(absfile): |
|---|
| 85 | if exists(join(absfile, '__init__.py')) or \ |
|---|
| 86 | package in ('test', 'tests'): |
|---|
| 87 | if prefix: |
|---|
| 88 | result.append('%s.%s' % (prefix, package)) |
|---|
| 89 | else: |
|---|
| 90 | result.append(package) |
|---|
| 91 | result += get_packages(absfile, result[-1]) |
|---|
| 92 | return result |
|---|
| 93 | |
|---|
| 94 | def export(from_dir, to_dir, |
|---|
| 95 | blacklist=BASE_BLACKLIST, |
|---|
| 96 | ignore_ext=IGNORED_EXTENSIONS): |
|---|
| 97 | """make a mirror of from_dir in to_dir, omitting directories and files |
|---|
| 98 | listed in the black list |
|---|
| 99 | """ |
|---|
| 100 | def make_mirror(arg, directory, fnames): |
|---|
| 101 | """walk handler""" |
|---|
| 102 | for norecurs in blacklist: |
|---|
| 103 | try: |
|---|
| 104 | fnames.remove(norecurs) |
|---|
| 105 | except ValueError: |
|---|
| 106 | pass |
|---|
| 107 | for filename in fnames: |
|---|
| 108 | # don't include binary files |
|---|
| 109 | if filename[-4:] in ignore_ext: |
|---|
| 110 | continue |
|---|
| 111 | if filename[-1] == '~': |
|---|
| 112 | continue |
|---|
| 113 | src = '%s/%s' % (directory, filename) |
|---|
| 114 | dest = to_dir + src[len(from_dir):] |
|---|
| 115 | print >> sys.stderr, src, '->', dest |
|---|
| 116 | if os.path.isdir(src): |
|---|
| 117 | if not exists(dest): |
|---|
| 118 | os.mkdir(dest) |
|---|
| 119 | else: |
|---|
| 120 | if exists(dest): |
|---|
| 121 | os.remove(dest) |
|---|
| 122 | shutil.copy2(src, dest) |
|---|
| 123 | try: |
|---|
| 124 | os.mkdir(to_dir) |
|---|
| 125 | except OSError, ex: |
|---|
| 126 | # file exists ? |
|---|
| 127 | import errno |
|---|
| 128 | if ex.errno != errno.EEXIST: |
|---|
| 129 | raise |
|---|
| 130 | walk(from_dir, make_mirror, None) |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | EMPTY_FILE = '"""generated file, don\'t modify or your data will be lost"""\n' |
|---|
| 134 | |
|---|
| 135 | class MyInstallLib(install_lib.install_lib): |
|---|
| 136 | """extend install_lib command to handle package __init__.py and |
|---|
| 137 | include_dirs variable if necessary |
|---|
| 138 | """ |
|---|
| 139 | def run(self): |
|---|
| 140 | """overridden from install_lib class""" |
|---|
| 141 | install_lib.install_lib.run(self) |
|---|
| 142 | # create Products.__init__.py if needed |
|---|
| 143 | if subpackage_of: |
|---|
| 144 | product_init = join(self.install_dir, subpackage_of, '__init__.py') |
|---|
| 145 | if not exists(product_init): |
|---|
| 146 | self.announce('creating %s' % product_init) |
|---|
| 147 | stream = open(product_init, 'w') |
|---|
| 148 | stream.write(EMPTY_FILE) |
|---|
| 149 | stream.close() |
|---|
| 150 | # manually install included directories if any |
|---|
| 151 | if include_dirs: |
|---|
| 152 | if subpackage_of: |
|---|
| 153 | base = join(subpackage_of, modname) |
|---|
| 154 | else: |
|---|
| 155 | base = modname |
|---|
| 156 | for directory in include_dirs: |
|---|
| 157 | dest = join(self.install_dir, base, directory) |
|---|
| 158 | export(directory, dest) |
|---|
| 159 | |
|---|
| 160 | def install(**kwargs): |
|---|
| 161 | """setup entry point""" |
|---|
| 162 | if subpackage_of: |
|---|
| 163 | package = subpackage_of + '.' + modname |
|---|
| 164 | kwargs['package_dir'] = {package : '.'} |
|---|
| 165 | packages = [package] + get_packages(os.getcwd(), package) |
|---|
| 166 | else: |
|---|
| 167 | kwargs['package_dir'] = {modname : '.'} |
|---|
| 168 | packages = [modname] + get_packages(os.getcwd(), modname) |
|---|
| 169 | kwargs['packages'] = packages |
|---|
| 170 | return setup(name = distname, |
|---|
| 171 | version = version, |
|---|
| 172 | license =license, |
|---|
| 173 | description = short_desc, |
|---|
| 174 | long_description = long_desc, |
|---|
| 175 | author = author, |
|---|
| 176 | author_email = author_email, |
|---|
| 177 | url = web, |
|---|
| 178 | scripts = ensure_scripts(scripts), |
|---|
| 179 | data_files=data_files, |
|---|
| 180 | ext_modules=ext_modules, |
|---|
| 181 | cmdclass={'install_lib': MyInstallLib}, |
|---|
| 182 | **kwargs |
|---|
| 183 | ) |
|---|
| 184 | |
|---|
| 185 | if __name__ == '__main__' : |
|---|
| 186 | install() |
|---|