install_venv.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2010 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # All Rights Reserved.
  5. #
  6. # Copyright 2010 OpenStack, LLC
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. # not use this file except in compliance with the License. You may obtain
  10. # a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. # License for the specific language governing permissions and limitations
  18. # under the License.
  19. """
  20. Installation script for Nova's development virtualenv
  21. """
  22. import optparse
  23. import os
  24. import subprocess
  25. import sys
  26. import platform
  27. ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  28. VENV = os.path.join(ROOT, '.venv')
  29. PIP_REQUIRES = os.path.join(ROOT, 'tools', 'pip-requires')
  30. TEST_REQUIRES = os.path.join(ROOT, 'tools', 'test-requires')
  31. PY_VERSION = "python%s.%s" % (sys.version_info[0], sys.version_info[1])
  32. def die(message, *args):
  33. print(sys.stderr, message % args)
  34. sys.exit(1)
  35. def check_python_version():
  36. if sys.version_info < (2, 6):
  37. die("Need Python Version >= 2.6")
  38. def run_command_with_code(cmd, redirect_output=True, check_exit_code=True):
  39. """
  40. Runs a command in an out-of-process shell, returning the
  41. output of that command. Working directory is ROOT.
  42. """
  43. if redirect_output:
  44. stdout = subprocess.PIPE
  45. else:
  46. stdout = None
  47. proc = subprocess.Popen(cmd, cwd=ROOT, stdout=stdout)
  48. output = proc.communicate()[0]
  49. if check_exit_code and proc.returncode != 0:
  50. die('Command "%s" failed.\n%s', ' '.join(cmd), output)
  51. return (output, proc.returncode)
  52. def run_command(cmd, redirect_output=True, check_exit_code=True):
  53. return run_command_with_code(cmd, redirect_output, check_exit_code)[0]
  54. class Distro(object):
  55. def check_cmd(self, cmd):
  56. return bool(run_command(['which', cmd], check_exit_code=False).strip())
  57. def install_virtualenv(self):
  58. if self.check_cmd('virtualenv'):
  59. return
  60. if self.check_cmd('easy_install'):
  61. print('Installing virtualenv via easy_install...', )
  62. if run_command(['easy_install', 'virtualenv']):
  63. print('Succeeded')
  64. return
  65. else:
  66. print('Failed')
  67. die('ERROR: virtualenv not found.\n\nDevelopment'
  68. ' requires virtualenv, please install it using your'
  69. ' favorite package management tool')
  70. def post_process(self):
  71. """Any distribution-specific post-processing gets done here.
  72. In particular, this is useful for applying patches to code inside
  73. the venv."""
  74. pass
  75. class Debian(Distro):
  76. """This covers all Debian-based distributions."""
  77. def check_pkg(self, pkg):
  78. return run_command_with_code(['dpkg', '-l', pkg],
  79. check_exit_code=False)[1] == 0
  80. def apt_install(self, pkg, **kwargs):
  81. run_command(['sudo', 'apt-get', 'install', '-y', pkg], **kwargs)
  82. def apply_patch(self, originalfile, patchfile):
  83. run_command(['patch', originalfile, patchfile])
  84. def install_virtualenv(self):
  85. if self.check_cmd('virtualenv'):
  86. return
  87. if not self.check_pkg('python-virtualenv'):
  88. self.apt_install('python-virtualenv', check_exit_code=False)
  89. super(Debian, self).install_virtualenv()
  90. class Fedora(Distro):
  91. """This covers all Fedora-based distributions.
  92. Includes: Fedora, RHEL, CentOS, Scientific Linux"""
  93. def check_pkg(self, pkg):
  94. return run_command_with_code(['rpm', '-q', pkg],
  95. check_exit_code=False)[1] == 0
  96. def yum_install(self, pkg, **kwargs):
  97. run_command(['sudo', 'yum', 'install', '-y', pkg], **kwargs)
  98. def apply_patch(self, originalfile, patchfile):
  99. run_command(['patch', originalfile, patchfile])
  100. def install_virtualenv(self):
  101. if self.check_cmd('virtualenv'):
  102. return
  103. if not self.check_pkg('python-virtualenv'):
  104. self.yum_install('python-virtualenv', check_exit_code=False)
  105. super(Fedora, self).install_virtualenv()
  106. def get_distro():
  107. if os.path.exists('/etc/fedora-release') or \
  108. os.path.exists('/etc/redhat-release'):
  109. return Fedora()
  110. elif os.path.exists('/etc/debian_version'):
  111. return Debian()
  112. else:
  113. return Distro()
  114. def check_dependencies():
  115. get_distro().install_virtualenv()
  116. def create_virtualenv(venv=VENV, no_site_packages=True):
  117. """Creates the virtual environment and installs PIP only into the
  118. virtual environment
  119. """
  120. print('Creating venv...', )
  121. if no_site_packages:
  122. run_command(['virtualenv', '-q', '--no-site-packages', VENV])
  123. else:
  124. run_command(['virtualenv', '-q', '--system-site-packages', VENV])
  125. print('done.')
  126. print('Installing pip in virtualenv...', )
  127. if not run_command(['tools/with_venv.sh', 'easy_install',
  128. 'pip>1.0']).strip():
  129. die("Failed to install pip.")
  130. print('done.')
  131. def pip_install(*args):
  132. run_command(['tools/with_venv.sh',
  133. 'pip', 'install', '--upgrade'] + list(args),
  134. redirect_output=False)
  135. def install_dependencies(venv=VENV):
  136. print('Installing dependencies with pip (this can take a while)...')
  137. # First things first, make sure our venv has the latest pip and distribute.
  138. pip_install('pip')
  139. pip_install('distribute')
  140. pip_install('-r', PIP_REQUIRES)
  141. pip_install('-r', TEST_REQUIRES)
  142. # Tell the virtual env how to "import nova"
  143. pthfile = os.path.join(venv, "lib", PY_VERSION, "site-packages",
  144. "novaclient.pth")
  145. f = open(pthfile, 'w')
  146. f.write("%s\n" % ROOT)
  147. def post_process():
  148. get_distro().post_process()
  149. def print_help():
  150. help = """
  151. python-novaclient development environment setup is complete.
  152. python-novaclient development uses virtualenv to track and manage Python
  153. dependencies while in development and testing.
  154. To activate the python-novaclient virtualenv for the extent of your current
  155. shell session you can run:
  156. $ source .venv/bin/activate
  157. Or, if you prefer, you can run commands in the virtualenv on a case by case
  158. basis by running:
  159. $ tools/with_venv.sh <your command>
  160. Also, make test will automatically use the virtualenv.
  161. """
  162. print(help)
  163. def parse_args():
  164. """Parse command-line arguments"""
  165. parser = optparse.OptionParser()
  166. parser.add_option("-n", "--no-site-packages", dest="no_site_packages",
  167. default=False, action="store_true",
  168. help="Do not inherit packages from global Python install")
  169. return parser.parse_args()
  170. def main(argv):
  171. (options, args) = parse_args()
  172. check_python_version()
  173. check_dependencies()
  174. create_virtualenv(no_site_packages=options.no_site_packages)
  175. install_dependencies()
  176. post_process()
  177. print_help()
  178. if __name__ == '__main__':
  179. main(sys.argv)