-
Notifications
You must be signed in to change notification settings - Fork 462
/
setup_openstates_ubuntu.py
152 lines (110 loc) · 3.62 KB
/
setup_openstates_ubuntu.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
'''
This script sets up a virtualenv with openstates on ubunt.
usage: python setup_openstates_ubuntu.py myvirtualenv [whenIputmycode]
If you don't specify a second argument, the code goes in the virtualenv.
'''
import sys
import os
from os import chdir as cd
from os.path import join, abspath
import subprocess
import logging
# Logging config
logger = logging.getLogger('[openstates-installer]')
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
formatter = logging.Formatter('%(name)s %(asctime)s - %(message)s',
datefmt='%H:%M:%S')
ch.setFormatter(formatter)
logger.addHandler(ch)
packages = {
# The packages are required for use of lxml and git.
'core': '''
libxml2-dev
python-dev
libxslt1-dev
git'''.split(),
}
# ---------------------------------------------------------------------------
# Utility functions
def run(command, check=False):
logger.info('running "%s"' % command)
if check:
return subprocess.check_output(command, shell=True)
else:
subprocess.call(command, shell=True)
def run_each(*commands):
for c in commands:
run(c)
def package_install(package, update=False):
"""Installs the given package/list of package, optionnaly updating
the package database."""
if update:
run("sudo apt-get --yes update")
if type(package) in (list, tuple):
package = " ".join(package)
run("sudo apt-get --yes install %s" % (package))
def package_ensure(package):
"""Tests if the given package is installed, and installes it in
case it's not already there. Loosely stolen from cuisine."""
cmd = "dpkg-query -W -f='${Status}' %s ; true"
status = run(cmd % package, check=True)
if status.find("not-installed") != -1 or status.find("installed") == -1:
package_install(package)
return False
else:
return True
def create_virtualenv(ENV):
'Create the virtualenv.'
run_each(
('wget -nc http://pypi.python.org/packages/source/v/virtualenv'
'/virtualenv-1.7.tar.gz#md5=dcc105e5a3907a9dcaa978f813a4f526'),
'tar -zxvf virtualenv-1.7.tar.gz ',
'python virtualenv-1.7/virtualenv.py %s' % ENV,
)
def gitclone(repo, setup_arg='install'):
cd(CODE)
# Clone the code.
run('git clone %s' % repo)
# Install requirements.
_, folder = os.path.split(repo)
folder, _ = os.path.splitext(folder)
requirements = join(CODE, folder, 'requirements.txt')
try:
with open(requirements):
pass
except IOError:
pass
else:
run('%s install -r %s' % (pip, requirements))
# Setup.
cd(folder)
run('%s setup.py %s' % (python, setup_arg))
def setup_openstates():
for package in packages['core']:
package_ensure(package)
create_virtualenv(ENV)
# Get openstates.
gitclone('git://github.com/sunlightlabs/openstates.git')
# Uninstall billy.
run('%s uninstall billy' % pip)
# Clone billy, get requirements, and run setup.py develop
gitclone('git://github.com/sunlightlabs/billy.git', 'develop')
def setup_mysql():
package_ensure('mysql-server')
run("sudo apt-get build-dep python-mysqldb")
run("pip install MySQL-python")
if __name__ == "__main__":
try:
ENV, CODE = map(abspath, sys.argv[1:3])
except ValueError:
ENV = CODE = abspath(sys.argv[1])
for path in [ENV, CODE]:
try:
os.makedirs(ENV)
os.makedirs(CODE)
except OSError:
pass
pip = join(ENV, 'bin', 'pip')
python = join(ENV, 'bin', 'python')
setup_openstates()