blob: c190886d16c0ba31a8443034a834e27a06edb054 [file] [log] [blame]
Justin Shepherd0d9bbd12011-08-11 12:57:44 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3"""
4Installation script for Kong's testing virtualenv
5"""
6
7import os
8import stat
9import string
10import subprocess
11import sys
12
13ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
14VENV = os.path.join(ROOT, '.kong-venv')
15PIP_REQUIRES = os.path.join(ROOT, 'tools', 'pip-requires')
16
17
18def die(message, *args):
19 print >> sys.stderr, message % args
20 sys.exit(1)
21
22
23def whereis(executable):
24 """
25 Detect whereis a binary and make sure it's executable we can execute.
26 """
27 for d in string.split(os.environ['PATH'], \
28 os.pathsep):
29 f = os.path.join(d, executable)
30 if os.path.isfile(f):
31 try:
32 st = os.stat(f)
33 except OSError:
34 continue
35 if stat.S_IMODE(st[stat.ST_MODE]) & 0111:
36 return True
37 return False
38
39
40def 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
57HAS_EASY_INSTALL = bool(whereis("easy_install"))
58HAS_VIRTUALENV = bool(whereis("virtualenv"))
59
60
61def check_dependencies():
62 """Make sure virtualenv is in the path."""
63
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(['easy_install', 'virtualenv']):
70 die('ERROR: virtualenv not found.\n\n'
71 'Glance development requires virtualenv, please install'
72 ' it using your favorite package management tool')
73 print 'done.'
74 print 'done.'
75
76
77def create_virtualenv(venv=VENV):
78 """Creates the virtual environment and installs PIP only into the
79 virtual environment
80 """
81 print 'Creating venv...',
82 run_command(['virtualenv', '-q', '--no-site-packages', VENV])
83 print 'done.'
84 print 'Installing pip in virtualenv...',
85 if not run_command(['tools/with_venv.sh', 'easy_install', 'pip']).strip():
86 die("Failed to install pip.")
87 print 'done.'
88
89
90def install_dependencies(venv=VENV):
91 print 'Installing dependencies with pip (this can take a while)...'
92
93 # Install greenlet by hand - just listing it in the requires file does not
94 # get it in stalled in the right order
95 venv_tool = 'tools/with_venv.sh'
96 run_command([venv_tool, 'pip', 'install', '-E', venv, '-r', PIP_REQUIRES],
97 redirect_output=False)
98
Aaron Lee78dbb642011-10-18 10:27:02 -050099 python_version = "python%s.%s" % (sys.version_info[0], sys.version_info[1])
100
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500101 # Tell the virtual env how to "import glance"
Aaron Lee78dbb642011-10-18 10:27:02 -0500102 pthfile = os.path.join(venv, "lib", python_version, "site-packages",
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500103 "glance.pth")
104 f = open(pthfile, 'w')
105 f.write("%s\n" % ROOT)
106
107
108def print_help():
109 help = """
110 Kong testing environment setup is complete.
111
112 Kong testing uses virtualenv to track and manage Python dependencies
113 while in development and testing.
114
115 To activate the Kong virtualenv for the extent of your current shell
116 session you can run:
117
118 $ source .kong-venv/bin/activate
119
120 Or, if you prefer, you can run commands in the virtualenv on a case by case
121 basis by running:
122
123 $ tools/with_venv.sh <your command>
124
125 Also, make test will automatically use the virtualenv.
126 """
127 print help
128
129
130def main(argv):
131 check_dependencies()
132 create_virtualenv()
133 install_dependencies()
134 print_help()
135
136if __name__ == '__main__':
137 main(sys.argv)