|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import absolute_import, division, print_function, \ |
| 4 | + with_statement, unicode_literals |
| 5 | + |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import subprocess |
| 10 | + |
| 11 | + |
| 12 | +def warning(*objs): |
| 13 | + print("WARNING: ", *objs, file=sys.stderr) |
| 14 | + |
| 15 | + |
| 16 | +def fail(message): |
| 17 | + sys.exit("Error: {message}".format(message=message)) |
| 18 | + |
| 19 | + |
| 20 | +PY2 = sys.version_info[0] == 2 |
| 21 | +if PY2: |
| 22 | + from urllib import urlretrieve |
| 23 | +else: |
| 24 | + from urllib.request import urlretrieve |
| 25 | + |
| 26 | + |
| 27 | +def has_module(module_name): |
| 28 | + try: |
| 29 | + import imp |
| 30 | + imp.find_module(module_name) |
| 31 | + del imp |
| 32 | + return True |
| 33 | + except ImportError: |
| 34 | + return False |
| 35 | + |
| 36 | + |
| 37 | +def which(exe=None, throw=True): |
| 38 | + """Return path of bin. Python clone of /usr/bin/which. |
| 39 | +
|
| 40 | + from salt.util - https://www.github.com/saltstack/salt - license apache |
| 41 | +
|
| 42 | + :param exe: Application to search PATHs for. |
| 43 | + :type exe: string |
| 44 | + :param throw: Raise ``Exception`` if not found in paths |
| 45 | + :type throw: bool |
| 46 | + :rtype: string |
| 47 | +
|
| 48 | + """ |
| 49 | + if exe: |
| 50 | + if os.access(exe, os.X_OK): |
| 51 | + return exe |
| 52 | + |
| 53 | + # default path based on busybox's default |
| 54 | + default_path = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin' |
| 55 | + search_path = os.environ.get('PATH', default_path) |
| 56 | + |
| 57 | + for path in search_path.split(os.pathsep): |
| 58 | + full_path = os.path.join(path, exe) |
| 59 | + if os.access(full_path, os.X_OK): |
| 60 | + return full_path |
| 61 | + |
| 62 | + message = ( |
| 63 | + '{0!r} could not be found in the following search ' |
| 64 | + 'path: {1!r}'.format( |
| 65 | + exe, search_path |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + if throw: |
| 70 | + raise Exception(message) |
| 71 | + else: |
| 72 | + print(message) |
| 73 | + return None |
| 74 | + |
| 75 | + |
| 76 | +project_dir = os.path.dirname(os.path.realpath(__file__)) |
| 77 | +env_dir = os.path.join(project_dir, '.env') |
| 78 | +pip_bin = os.path.join(env_dir, 'bin', 'pip') |
| 79 | +python_bin = os.path.join(env_dir, 'bin', 'python') |
| 80 | +virtualenv_bin = which('virtualenv', throw=False) |
| 81 | +virtualenv_exists = os.path.exists(env_dir) and os.path.isfile(python_bin) |
| 82 | +sphinx_requirements_filepath = os.path.join(project_dir, 'doc', 'requirements.pip') |
| 83 | + |
| 84 | + |
| 85 | +try: |
| 86 | + import virtualenv |
| 87 | +except ImportError: |
| 88 | + message = ( |
| 89 | + 'Virtualenv is required for this bootstrap to run.\n' |
| 90 | + 'Install virtualenv via:\n' |
| 91 | + '\t$ [sudo] pip install virtualenv' |
| 92 | + ) |
| 93 | + fail(message) |
| 94 | + |
| 95 | + |
| 96 | +try: |
| 97 | + import pip |
| 98 | +except ImportError: |
| 99 | + message = ( |
| 100 | + 'pip is required for this bootstrap to run.\n' |
| 101 | + 'Find instructions on how to install at: %s' % |
| 102 | + 'http://pip.readthedocs.org/en/latest/installing.html' |
| 103 | + ) |
| 104 | + fail(message) |
| 105 | + |
| 106 | + |
| 107 | +def main(): |
| 108 | + if not virtualenv_exists: |
| 109 | + virtualenv_bin = which('virtualenv', throw=False) |
| 110 | + |
| 111 | + subprocess.check_call( |
| 112 | + [virtualenv_bin, env_dir] |
| 113 | + ) |
| 114 | + |
| 115 | + subprocess.check_call( |
| 116 | + [pip_bin, 'install', '-e', project_dir] |
| 117 | + ) |
| 118 | + |
| 119 | + if not os.path.isfile(os.path.join(env_dir, 'bin', 'watching_testrunner')): |
| 120 | + subprocess.check_call( |
| 121 | + [pip_bin, 'install', 'watching-testrunner'] |
| 122 | + ) |
| 123 | + |
| 124 | + if not os.path.isfile(os.path.join(env_dir, 'bin', 'sphinx-quickstart')): |
| 125 | + subprocess.check_call( |
| 126 | + [pip_bin, 'install', '-r', sphinx_requirements_filepath] |
| 127 | + ) |
| 128 | + |
| 129 | + if os.path.exists(os.path.join(env_dir, 'build')): |
| 130 | + os.removedirs(os.path.join(env_dir, 'build')) |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + main() |
0 commit comments