forked from djblets/djblets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·381 lines (295 loc) · 12.1 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
import json
import os
import subprocess
import sys
from distutils.core import Command
from setuptools import find_packages, setup
from setuptools.command.develop import develop
from setuptools.command.egg_info import egg_info
from setuptools.command.test import test
from djblets import get_package_version, VERSION
from djblets.dependencies import (build_dependency_list, npm_dependencies,
package_dependencies)
# Make sure this is a version of Python we are compatible with. This should
# prevent people on older versions from unintentionally trying to install
# the source tarball, and failing.
if sys.hexversion < 0x02060000:
sys.stderr.write('This version of Djblets is incompatible with your '
'version of Python.\n')
sys.exit(1)
class BuildEggInfoCommand(egg_info):
"""Build the egg information for the package.
If this is called when building a distribution (source, egg, or wheel),
or when installing the package from source, this will kick off tasks for
building static media and string localization files.
"""
def run(self):
"""Build the egg information."""
if ('sdist' in sys.argv or
'bdist_egg' in sys.argv or
'bdist_wheel' in sys.argv or
'install' in sys.argv):
self.run_command('build_media')
self.run_command('build_i18n')
egg_info.run(self)
class DevelopCommand(develop):
"""Installs Djblets in developer mode.
This will install all standard and development dependencies (using Python
wheels and node.js packages from npm) and add the source tree to the
Python module search path. That includes updating the versions of pip
and setuptools on the system.
To speed up subsequent runs, callers can pass ``--no-npm`` to prevent
installing node.js packages.
"""
user_options = develop.user_options + [
('no-npm', None, "Don't install packages from npm"),
('use-npm-cache', None, 'Use npm-cache to install packages'),
]
boolean_options = develop.boolean_options + ['no-npm', 'use-npm-cache']
def initialize_options(self):
"""Initialize options for the command."""
develop.initialize_options(self)
self.no_npm = None
self.use_npm_cache = None
def install_for_development(self):
"""Install the package for development.
This takes care of the work of installing all dependencies.
"""
if self.no_deps:
# In this case, we don't want to install any of the dependencies
# below. However, it's really unlikely that a user is going to
# want to pass --no-deps.
#
# Instead, what this really does is give us a way to know we've
# been called by `pip install -e .`. That will call us with
# --no-deps, as it's going to actually handle all dependency
# installation, rather than having easy_install do it.
develop.install_for_development(self)
return
try:
subprocess.check_output(['node', '--version'])
except (subprocess.CalledProcessError, OSError):
try:
subprocess.check_output(['nodejs', '--version'])
raise RuntimeError(
'Unable to find "node" in the path, but "nodejs" was '
'found. You will need to ensure "nodejs" can be run '
'by typing "node". You can do this by typing `ln -s '
'nodejs node` in the directory containing "nodejs".')
except:
# nodejs wasn't found, which is fine. We want to ignore this.
pass
raise RuntimeError(
'Unable to find "node" in the path. You will need to '
'install a modern version of NodeJS and ensure you can '
'run it by typing "node" on the command line.')
# Install the latest pip and setuptools. Note that the order here
# matters, as otherwise a stale setuptools can be left behind,
# causing installation errors.
self._run_pip(['install', '-U', 'setuptools'])
self._run_pip(['install', '-U', 'pip'])
# Install the dependencies using pip instead of easy_install. This
# will use wheels instead of eggs, which are ideal for our users.
self._run_pip(['install', '-e', '.'])
self._run_pip(['install', '-r', 'dev-requirements.txt'])
if not self.no_npm:
if self.use_npm_cache:
self.distribution.command_options['install_node_deps'] = {
'use_npm_cache': ('install_node_deps', 1),
}
self.run_command('install_node_deps')
def _run_pip(self, args):
"""Run pip.
Args:
args (list):
Arguments to pass to :command:`pip`.
Raises:
RuntimeError:
The :command:`pip` command returned a non-zero exit code.
"""
cmd = subprocess.list2cmdline([sys.executable, '-m', 'pip'] + args)
ret = os.system(cmd)
if ret != 0:
raise RuntimeError('Failed to run `%s`' % cmd)
class BuildMediaCommand(Command):
"""Builds static media files for the package.
This requires first having the node.js dependencies installed.
"""
user_options = []
def initialize_options(self):
"""Initialize options for the command.
This is required, but does not actually do anything.
"""
pass
def finalize_options(self):
"""Finalize options for the command.
This is required, but does not actually do anything.
"""
pass
def run(self):
"""Runs the commands to build the static media files.
Raises:
RuntimeError:
Static media failed to build.
"""
retcode = subprocess.call([
sys.executable, 'contrib/internal/build-media.py'])
if retcode != 0:
raise RuntimeError('Failed to build media files')
class BuildI18nCommand(Command):
"""Builds string localization files."""
description = 'Compile message catalogs to .mo'
user_options = []
def initialize_options(self):
"""Initialize options for the command.
This is required, but does not actually do anything.
"""
pass
def finalize_options(self):
"""Finalize options for the command.
This is required, but does not actually do anything.
"""
pass
def run(self):
"""Runs the commands to build the string localization files.
Raises:
RuntimeError:
Localization files failed to build.
"""
# If we are attempting to build on a system without an
# existing copy of Djblets installed in a reachable
# location (such as distribution packaging), we need to
# ensure that the source directory is in the PYTHONPATH
# or the import of djblets.util.filesystem will fail.
current_path = os.getenv('PYTHONPATH')
if current_path:
os.putenv('PYTHONPATH', '%s:%s' % (current_path, os.getcwd()))
else:
os.putenv('PYTHONPATH', os.getcwd())
retcode = subprocess.call([
sys.executable, 'contrib/internal/build-i18n.py'])
if retcode != 0:
raise RuntimeError('Failed to build i18n files')
class FetchPublicSuffixListCommand(Command):
"""Fetches the DNS public suffix list for use in DMARC lookups."""
description = 'Fetch the DNS public suffix list from publicsuffix.org.'
user_options = []
def initialize_options(self):
"""Initialize options for the command.
This is required, but does not actually do anything.
"""
pass
def finalize_options(self):
"""Finalize options for the command.
This is required, but does not actually do anything.
"""
pass
def run(self):
"""Run the commands to fetch the DNS public suffix list."""
from publicsuffix import fetch as fetch_public_suffix
print 'Fetching DNS public suffix list...'
filename = os.path.join('djblets', 'mail', 'public_suffix_list.dat')
with open(filename, 'w') as fp:
fp.write(fetch_public_suffix().read().encode('utf-8'))
print 'Public suffix list stored at %s' % filename
class InstallNodeDependenciesCommand(Command):
"""Installs all node.js dependencies from npm.
If ``--use-npm-cache`` is passed, this will use :command:`npm-cache`
to install the packages, which is best for Continuous Integration setups.
Otherwise, :command:`npm` is used.
"""
description = \
'Install the node packages required for building static media.'
user_options = [
('use-npm-cache', None, 'Use npm-cache to install packages'),
]
boolean_options = ['use-npm-cache']
def initialize_options(self):
"""Initialize options for the command."""
self.use_npm_cache = None
def finalize_options(self):
"""Finalize options for the command.
This is required, but does not actually do anything.
"""
pass
def run(self):
"""Run the commands to install packages from npm.
Raises:
RuntimeError:
There was an error finding or invoking the package manager.
"""
if self.use_npm_cache:
npm_command = 'npm-cache'
else:
npm_command = 'npm'
try:
subprocess.check_output([npm_command, '--version'])
except subprocess.CalledProcessError:
raise RuntimeError(
'Unable to locate %s in the path, which is needed to '
'install dependencies required to build this package.'
% npm_command)
with open('package.json', 'w') as fp:
fp.write(json.dumps(
{
'name': 'djblets',
'private': 'true',
'devDependencies': {},
'dependencies': npm_dependencies,
},
indent=2))
print 'Installing node.js modules...'
result = os.system('%s install' % npm_command)
os.unlink('package.json')
if result != 0:
raise RuntimeError(
'One or more node.js modules could not be installed.')
# Tell `setup.py tests` how to invoke our test suite.
test.run_tests = lambda *args, **kwargs: os.system('tests/runtests.py')
PACKAGE_NAME = 'Djblets'
setup(
name=PACKAGE_NAME,
version=get_package_version(),
license='MIT',
description=(
'A collection of useful classes and functions for developing '
'large-scale Django-based web applications.'
),
author='Beanbag, Inc.',
author_email='reviewboard@googlegroups.com',
url='https://www.reviewboard.org/downloads/djblets/',
download_url=('https://downloads.reviewboard.org/releases/%s/%s.%s/'
% (PACKAGE_NAME, VERSION[0], VERSION[1])),
packages=find_packages(exclude=['tests']),
install_requires=build_dependency_list(package_dependencies),
include_package_data=True,
zip_safe=False,
test_suite='dummy',
data_files=[
('contrib/internal', [
'contrib/internal/less-imports.js',
]),
],
cmdclass={
'develop': DevelopCommand,
'egg_info': BuildEggInfoCommand,
'build_media': BuildMediaCommand,
'build_i18n': BuildI18nCommand,
'fetch_public_suffix_list': FetchPublicSuffixListCommand,
'install_node_deps': InstallNodeDependenciesCommand,
},
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Topic :: Software Development',
'Topic :: Software Development :: Libraries :: Python Modules',
],
)