author | David Douard <david.douard@logilab.fr> |
Mon, 22 Feb 2016 15:57:10 +0100 | |
changeset 11673 | c3a583e38360 |
parent 11653 | 18939907a115 |
permissions | -rw-r--r-- |
11632 | 1 |
#!/usr/bin/env python |
2 |
# pylint: disable=W0142,W0403,W0404,W0613,W0622,W0622,W0704,R0904,C0103,E0611 |
|
3 |
# |
|
11653
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
4 |
# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
5 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
6 |
# |
11642
3defbb0f147a
Prepare release
Christophe de Vienne <christophe@unlish.com>
parents:
11632
diff
changeset
|
7 |
# This file is part of CubicWeb pyramid cube. |
11632 | 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 |
|
11653
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
28 |
from os.path import isdir, exists, join, walk, dirname |
11632 | 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 |
||
11653
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
42 |
# load metadata from the __pkginfo__.py file so there is no risk of conflict |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
43 |
# see https://packaging.python.org/en/latest/single_source_version.html |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
44 |
base_dir = dirname(__file__) |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
45 |
|
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
46 |
pkginfo = {} |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
47 |
with open(join(base_dir, "__pkginfo__.py")) as f: |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
48 |
exec(f.read(), pkginfo) |
11632 | 49 |
|
11653
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
50 |
|
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
51 |
# get required metadatas |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
52 |
modname = pkginfo['modname'] |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
53 |
version = pkginfo['version'] |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
54 |
license = pkginfo['license'] |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
55 |
description = pkginfo['description'] |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
56 |
web = pkginfo['web'] |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
57 |
author = pkginfo['author'] |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
58 |
author_email = pkginfo['author_email'] |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
59 |
classifiers = pkginfo['classifiers'] |
11632 | 60 |
|
11653
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
61 |
with open(join(base_dir, 'README')) as f: |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
62 |
long_description = f.read() |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
63 |
|
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
64 |
# get optional metadatas |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
65 |
distname = pkginfo.get('distname', modname) |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
66 |
scripts = pkginfo.get('scripts', ()) |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
67 |
include_dirs = pkginfo.get('include_dirs', ()) |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
68 |
data_files = pkginfo.get('data_files', None) |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
69 |
ext_modules = pkginfo.get('ext_modules', None) |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
70 |
dependency_links = pkginfo.get('dependency_links', ()) |
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
71 |
|
11632 | 72 |
if USE_SETUPTOOLS: |
73 |
requires = {} |
|
74 |
for entry in ("__depends__",): # "__recommends__"): |
|
11653
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
75 |
requires.update(pkginfo.get(entry, {})) |
11632 | 76 |
install_requires = [("%s %s" % (d, v and v or "")).strip() |
77 |
for d, v in requires.iteritems()] |
|
78 |
else: |
|
79 |
install_requires = [] |
|
80 |
||
11653
18939907a115
[pkg] update to dh9 and dh_python2
David Douard <david.douard@logilab.fr>
parents:
11642
diff
changeset
|
81 |
BASE_BLACKLIST = ('CVS', '.svn', '.hg', '.git', 'debian', 'dist', 'build') |
11632 | 82 |
IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~') |
83 |
||
84 |
def ensure_scripts(linux_scripts): |
|
85 |
""" |
|
86 |
Creates the proper script names required for each platform |
|
87 |
(taken from 4Suite) |
|
88 |
""" |
|
89 |
from distutils import util |
|
90 |
if util.get_platform()[:3] == 'win': |
|
91 |
scripts_ = [script + '.bat' for script in linux_scripts] |
|
92 |
else: |
|
93 |
scripts_ = linux_scripts |
|
94 |
return scripts_ |
|
95 |
||
96 |
def export(from_dir, to_dir, |
|
97 |
blacklist=BASE_BLACKLIST, |
|
98 |
ignore_ext=IGNORED_EXTENSIONS, |
|
99 |
verbose=True): |
|
100 |
"""make a mirror of from_dir in to_dir, omitting directories and files |
|
101 |
listed in the black list |
|
102 |
""" |
|
103 |
def make_mirror(arg, directory, fnames): |
|
104 |
"""walk handler""" |
|
105 |
for norecurs in blacklist: |
|
106 |
try: |
|
107 |
fnames.remove(norecurs) |
|
108 |
except ValueError: |
|
109 |
pass |
|
110 |
for filename in fnames: |
|
111 |
# don't include binary files |
|
112 |
if filename[-4:] in ignore_ext: |
|
113 |
continue |
|
114 |
if filename[-1] == '~': |
|
115 |
continue |
|
116 |
src = join(directory, filename) |
|
117 |
dest = to_dir + src[len(from_dir):] |
|
118 |
if verbose: |
|
119 |
sys.stderr.write('%s -> %s\n' % (src, dest)) |
|
120 |
if os.path.isdir(src): |
|
121 |
if not exists(dest): |
|
122 |
os.mkdir(dest) |
|
123 |
else: |
|
124 |
if exists(dest): |
|
125 |
os.remove(dest) |
|
126 |
shutil.copy2(src, dest) |
|
127 |
try: |
|
128 |
os.mkdir(to_dir) |
|
129 |
except OSError as ex: |
|
130 |
# file exists ? |
|
131 |
import errno |
|
132 |
if ex.errno != errno.EEXIST: |
|
133 |
raise |
|
134 |
walk(from_dir, make_mirror, None) |
|
135 |
||
136 |
||
137 |
class MyInstallLib(install_lib.install_lib): |
|
138 |
"""extend install_lib command to handle package __init__.py and |
|
139 |
include_dirs variable if necessary |
|
140 |
""" |
|
141 |
def run(self): |
|
142 |
"""overridden from install_lib class""" |
|
143 |
install_lib.install_lib.run(self) |
|
144 |
# manually install included directories if any |
|
145 |
if include_dirs: |
|
146 |
base = modname |
|
147 |
for directory in include_dirs: |
|
148 |
dest = join(self.install_dir, base, directory) |
|
149 |
export(directory, dest, verbose=False) |
|
150 |
||
151 |
# re-enable copying data files in sys.prefix |
|
152 |
old_install_data = install_data.install_data |
|
153 |
if USE_SETUPTOOLS: |
|
154 |
# overwrite InstallData to use sys.prefix instead of the egg directory |
|
155 |
class MyInstallData(old_install_data): |
|
156 |
"""A class that manages data files installation""" |
|
157 |
def run(self): |
|
158 |
_old_install_dir = self.install_dir |
|
159 |
if self.install_dir.endswith('egg'): |
|
160 |
self.install_dir = sys.prefix |
|
161 |
old_install_data.run(self) |
|
162 |
self.install_dir = _old_install_dir |
|
163 |
try: |
|
164 |
import setuptools.command.easy_install # only if easy_install available |
|
165 |
# monkey patch: Crack SandboxViolation verification |
|
166 |
from setuptools.sandbox import DirectorySandbox as DS |
|
167 |
old_ok = DS._ok |
|
168 |
def _ok(self, path): |
|
169 |
"""Return True if ``path`` can be written during installation.""" |
|
170 |
out = old_ok(self, path) # here for side effect from setuptools |
|
171 |
realpath = os.path.normcase(os.path.realpath(path)) |
|
172 |
allowed_path = os.path.normcase(sys.prefix) |
|
173 |
if realpath.startswith(allowed_path): |
|
174 |
out = True |
|
175 |
return out |
|
176 |
DS._ok = _ok |
|
177 |
except ImportError: |
|
178 |
pass |
|
179 |
||
180 |
def install(**kwargs): |
|
181 |
"""setup entry point""" |
|
182 |
if USE_SETUPTOOLS: |
|
183 |
if '--force-manifest' in sys.argv: |
|
184 |
sys.argv.remove('--force-manifest') |
|
185 |
# install-layout option was introduced in 2.5.3-1~exp1 |
|
186 |
elif sys.version_info < (2, 5, 4) and '--install-layout=deb' in sys.argv: |
|
187 |
sys.argv.remove('--install-layout=deb') |
|
188 |
cmdclass = {'install_lib': MyInstallLib} |
|
189 |
if USE_SETUPTOOLS: |
|
190 |
kwargs['install_requires'] = install_requires |
|
191 |
kwargs['dependency_links'] = dependency_links |
|
192 |
kwargs['zip_safe'] = False |
|
193 |
cmdclass['install_data'] = MyInstallData |
|
194 |
||
195 |
return setup(name = distname, |
|
196 |
version = version, |
|
197 |
license = license, |
|
198 |
description = description, |
|
199 |
long_description = long_description, |
|
200 |
author = author, |
|
201 |
author_email = author_email, |
|
202 |
url = web, |
|
203 |
scripts = ensure_scripts(scripts), |
|
204 |
data_files = data_files, |
|
205 |
ext_modules = ext_modules, |
|
206 |
cmdclass = cmdclass, |
|
207 |
classifiers = classifiers, |
|
208 |
**kwargs |
|
209 |
) |
|
210 |
||
211 |
if __name__ == '__main__' : |
|
212 |
install() |