~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to tools/install_venv.py

  • Committer: Brian Waldon
  • Date: 2011-07-29 16:54:55 UTC
  • mto: This revision was merged to the branch mainline in revision 1364.
  • Revision ID: brian.waldon@rackspace.com-20110729165455-4ebqwv8s5pkscmmg
one last change

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
1
2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3
 
3
4
# Copyright 2010 United States Government as represented by the
30
31
ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
31
32
VENV = os.path.join(ROOT, '.nova-venv')
32
33
PIP_REQUIRES = os.path.join(ROOT, 'tools', 'pip-requires')
33
 
TWISTED_NOVA='http://nova.openstack.org/Twisted-10.0.0Nova.tar.gz'
 
34
TWISTED_NOVA = 'http://nova.openstack.org/Twisted-10.0.0Nova.tar.gz'
 
35
PY_VERSION = "python%s.%s" % (sys.version_info[0], sys.version_info[1])
 
36
 
34
37
 
35
38
def die(message, *args):
36
 
  print >>sys.stderr, message % args
37
 
  sys.exit(1)
 
39
    print >> sys.stderr, message % args
 
40
    sys.exit(1)
 
41
 
 
42
 
 
43
def check_python_version():
 
44
    if sys.version_info < (2, 6):
 
45
        die("Need Python Version >= 2.6")
38
46
 
39
47
 
40
48
def run_command(cmd, redirect_output=True, check_exit_code=True):
41
 
  """
42
 
  Runs a command in an out-of-process shell, returning the
43
 
  output of that command.  Working directory is ROOT.
44
 
  """
45
 
  if redirect_output:
46
 
    stdout = subprocess.PIPE
47
 
  else:
48
 
    stdout = None
49
 
 
50
 
  proc = subprocess.Popen(cmd, cwd=ROOT, stdout=stdout)
51
 
  output = proc.communicate()[0]
52
 
  if check_exit_code and proc.returncode != 0:
53
 
    die('Command "%s" failed.\n%s', ' '.join(cmd), output)
54
 
  return output
55
 
 
56
 
 
57
 
HAS_EASY_INSTALL = bool(run_command(['which', 'easy_install'], check_exit_code=False).strip())
58
 
HAS_VIRTUALENV = bool(run_command(['which', 'virtualenv'], check_exit_code=False).strip())
 
49
    """
 
50
    Runs a command in an out-of-process shell, returning the
 
51
    output of that command.  Working directory is ROOT.
 
52
    """
 
53
    if redirect_output:
 
54
        stdout = subprocess.PIPE
 
55
    else:
 
56
        stdout = None
 
57
 
 
58
    proc = subprocess.Popen(cmd, cwd=ROOT, stdout=stdout)
 
59
    output = proc.communicate()[0]
 
60
    if check_exit_code and proc.returncode != 0:
 
61
        die('Command "%s" failed.\n%s', ' '.join(cmd), output)
 
62
    return output
 
63
 
 
64
 
 
65
HAS_EASY_INSTALL = bool(run_command(['which', 'easy_install'],
 
66
                    check_exit_code=False).strip())
 
67
HAS_VIRTUALENV = bool(run_command(['which', 'virtualenv'],
 
68
                    check_exit_code=False).strip())
59
69
 
60
70
 
61
71
def check_dependencies():
62
 
  """Make sure virtualenv is in the path."""
 
72
    """Make sure virtualenv is in the path."""
63
73
 
64
 
  if not HAS_VIRTUALENV:
65
 
    print 'not found.'
66
 
    # Try installing it via easy_install...
67
 
    if HAS_EASY_INSTALL:
68
 
      print 'Installing virtualenv via easy_install...',
69
 
      if not run_command(['which', 'easy_install']):
70
 
        die('ERROR: virtualenv not found.\n\nNova development requires virtualenv,'
71
 
            ' please install it using your favorite package management tool')
72
 
      print 'done.'
73
 
  print 'done.'
 
74
    if not HAS_VIRTUALENV:
 
75
        print 'not found.'
 
76
        # Try installing it via easy_install...
 
77
        if HAS_EASY_INSTALL:
 
78
            print 'Installing virtualenv via easy_install...',
 
79
            if not (run_command(['which', 'easy_install']) and
 
80
                    run_command(['easy_install', 'virtualenv'])):
 
81
                die('ERROR: virtualenv not found.\n\nNova development'
 
82
                    ' requires virtualenv, please install it using your'
 
83
                    ' favorite package management tool')
 
84
            print 'done.'
 
85
    print 'done.'
74
86
 
75
87
 
76
88
def create_virtualenv(venv=VENV):
77
 
  """Creates the virtual environment and installs PIP only into the
78
 
  virtual environment
79
 
  """
80
 
  print 'Creating venv...',
81
 
  run_command(['virtualenv', '-q', '--no-site-packages', VENV])
82
 
  print 'done.'
83
 
  print 'Installing pip in virtualenv...',
84
 
  if not run_command(['tools/with_venv.sh', 'easy_install', 'pip']).strip():
85
 
    die("Failed to install pip.")
86
 
  print 'done.'
 
89
    """Creates the virtual environment and installs PIP only into the
 
90
    virtual environment
 
91
    """
 
92
    print 'Creating venv...',
 
93
    run_command(['virtualenv', '-q', '--no-site-packages', VENV])
 
94
    print 'done.'
 
95
    print 'Installing pip in virtualenv...',
 
96
    if not run_command(['tools/with_venv.sh', 'easy_install', 'pip']).strip():
 
97
        die("Failed to install pip.")
 
98
    print 'done.'
87
99
 
88
100
 
89
101
def install_dependencies(venv=VENV):
90
 
  print 'Installing dependencies with pip (this can take a while)...'
91
 
  run_command(['tools/with_venv.sh', 'pip', 'install', '-E', venv, '-r', PIP_REQUIRES],
92
 
              redirect_output=False)
93
 
  run_command(['tools/with_venv.sh', 'pip', 'install', '-E', venv, TWISTED_NOVA],
94
 
              redirect_output=False)
95
 
 
96
 
 
97
 
  # Tell the virtual env how to "import nova"
98
 
  pthfile = os.path.join(venv, "lib", "python2.6", "site-packages", "nova.pth")
99
 
  f = open(pthfile, 'w')
100
 
  f.write("%s\n" % ROOT)
 
102
    print 'Installing dependencies with pip (this can take a while)...'
 
103
    # Install greenlet by hand - just listing it in the requires file does not
 
104
    # get it in stalled in the right order
 
105
    run_command(['tools/with_venv.sh', 'pip', 'install', '-E', venv,
 
106
              'greenlet'], redirect_output=False)
 
107
    run_command(['tools/with_venv.sh', 'pip', 'install', '-E', venv, '-r',
 
108
              PIP_REQUIRES], redirect_output=False)
 
109
    run_command(['tools/with_venv.sh', 'pip', 'install', '-E', venv,
 
110
              TWISTED_NOVA], redirect_output=False)
 
111
 
 
112
    # Tell the virtual env how to "import nova"
 
113
    pthfile = os.path.join(venv, "lib", PY_VERSION, "site-packages",
 
114
                        "nova.pth")
 
115
    f = open(pthfile, 'w')
 
116
    f.write("%s\n" % ROOT)
 
117
    # Patch eventlet (see FAQ # 1485)
 
118
    patchsrc = os.path.join(ROOT, 'tools', 'eventlet-patch')
 
119
    patchfile = os.path.join(venv, "lib", PY_VERSION, "site-packages",
 
120
                          "eventlet", "green", "subprocess.py")
 
121
    patch_cmd = "patch %s %s" % (patchfile, patchsrc)
 
122
    os.system(patch_cmd)
101
123
 
102
124
 
103
125
def print_help():
104
 
  help = """
105
 
 Nova development environment setup is complete.
106
 
 
107
 
 Nova development uses virtualenv to track and manage Python dependencies
108
 
 while in development and testing.
109
 
 
110
 
 To activate the Nova virtualenv for the extent of your current shell session
111
 
 you can run:
112
 
 
113
 
 $ source .nova-venv/bin/activate 
114
 
 
115
 
 Or, if you prefer, you can run commands in the virtualenv on a case by case
116
 
 basis by running:
117
 
 
118
 
 $ tools/with_venv.sh <your command>
119
 
 
120
 
 Also, make test will automatically use the virtualenv.
121
 
  """
122
 
  print help
 
126
    help = """
 
127
    Nova development environment setup is complete.
 
128
 
 
129
    Nova development uses virtualenv to track and manage Python dependencies
 
130
    while in development and testing.
 
131
 
 
132
    To activate the Nova virtualenv for the extent of your current shell
 
133
    session you can run:
 
134
 
 
135
    $ source .nova-venv/bin/activate
 
136
 
 
137
    Or, if you prefer, you can run commands in the virtualenv on a case by case
 
138
    basis by running:
 
139
 
 
140
    $ tools/with_venv.sh <your command>
 
141
 
 
142
    Also, make test will automatically use the virtualenv.
 
143
    """
 
144
    print help
123
145
 
124
146
 
125
147
def main(argv):
126
 
  check_dependencies()
127
 
  create_virtualenv()
128
 
  install_dependencies()
129
 
  print_help()
 
148
    check_python_version()
 
149
    check_dependencies()
 
150
    create_virtualenv()
 
151
    install_dependencies()
 
152
    print_help()
130
153
 
131
154
if __name__ == '__main__':
132
 
  main(sys.argv)
 
155
    main(sys.argv)