108
121
yield ('-c', os.path.join(d, f))
111
def append_python_path(path):
112
path = os.path.abspath(path)
113
if os.environ.get('PYTHONPATH'):
114
os.environ['PYTHONPATH'] += ':%s' % path
116
os.environ['PYTHONPATH'] = '%s' % path
119
def install(branch, revno=0):
120
"""Put a copy of a bzr branch in a temporary directory."""
122
temp_dir = tempfile.mkdtemp()
124
cmd = ['bzr', 'co', '--lightweight']
126
cmd.append('-r%d' % revno)
127
cmd += [branch, temp_dir]
128
subprocess.check_call(cmd)
129
append_python_path(temp_dir)
131
shutil.rmtree(temp_dir)
137
def install_jujuclient():
124
def prepend_path(env_var, path):
125
"""Put our dependencies paths first so they don't get hidden."""
126
val = os.environ.get(env_var, None)
127
paths = (val and val.split(os.path.pathsep)) or []
128
paths.insert(0, path)
129
os.environ[env_var] = os.path.pathsep.join(paths)
132
def get_or_pull_branch(src, dst, revision=None, out=None, err=None):
133
"""Get a branch at a given revision.
135
If the branch already exists, pulls the given revision.
137
:param src: The url to get the branch from (any url recognized by bzr).
139
:param dst: The directory where the branch will reside.
141
:param revision: The optional revision to set the branch to.
143
with bzrlib.initialize(stdout=out, stderr=err):
144
if os.path.exists(dst):
145
cmd = builtins.cmd_pull()
146
args = [src, '-d', dst, '--overwrite']
148
cmd = builtins.cmd_branch()
150
if revision is not None:
151
args.extend(['--revision', revision])
152
cmd.run_argv_aliases(args)
155
def install_jujuclient(where=None):
138
156
"""Install python-jujuclient from bzr. Revision r23 works around problems
139
157
we were seeing with env watches (LP: #1284183)."""
140
return install('lp:python-jujuclient', revno=24)
143
def install_deployer():
144
"""Install juju-deployer from bzr. We need juju-deployer from tip to have
145
JUJU_REPOSITORY work properly (r87)."""
147
deployer = install('lp:juju-deployer', revno=103)
148
os.environ['PATH'] = '%s:%s' % (deployer, os.environ['PATH'])
149
deployer_bin = os.path.join(deployer, 'juju-deployer')
159
where = os.path.join(HERE, '..', 'branches')
160
dest = os.path.abspath(os.path.join(where, 'charmworldlib'))
161
get_or_pull_branch('lp:python-jujuclient', dest, 'revno:24')
162
prepend_path('PYTHONPATH', dest)
166
def install_deployer(where=None):
167
"""Install juju-deployer from bzr."""
169
where = os.path.join(HERE, '..', 'branches')
170
dest = os.path.abspath(os.path.join(where, 'juju-deployer'))
171
# For an obscure reason tag:0.3.1 is not in lp:juju-deployer
172
get_or_pull_branch('lp:juju-deployer', dest, revision='revno:103')
173
prepend_path('PYTHONPATH', dest)
174
# FIXME: This duplicates setup.py and shouldn't be needed. This specific
175
# piece (juju-deployer) is a weird beast, it installs from source (not pip
176
# nor package), it requires amulet which itself requires urllib3 (among
177
# others), it's used both in unit tests that are executed in a venv and in
178
# ./tests/run.py which is not executed in venv. My head hurts -- vila
180
deployer_bin = os.path.join(dest, 'juju-deployer')
150
181
with open(deployer_bin, 'w') as fp:
151
tmpl = '#!/bin/sh\nPYTHONPATH=%s\nexec python %s/deployer/cli.py $*'
152
fp.write(tmpl % (os.environ['PYTHONPATH'], deployer))
182
tmpl = '#!/bin/sh\nPYTHONPATH={}\nexec python {}/deployer/cli.py $*'
183
fp.write(tmpl.format(os.environ['PYTHONPATH'], dest))
153
184
st = os.stat(deployer_bin)
154
185
mode = st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
155
186
os.chmod(deployer_bin, mode)
187
prepend_path('PATH', dest)
159
191
def remove_branch(branch_dir):
252
284
check_bootstrapped()
253
285
check_dependencies()
255
temp_dir = install_jujuclient()
256
atexit.register(lambda: remove_branch(temp_dir))
258
temp_dir = install_deployer()
259
atexit.register(lambda: remove_branch(temp_dir))
261
290
# A place to put our generated .yaml files.
262
291
temp_dir = tempfile.mkdtemp()
263
atexit.register(lambda: shutil.rmtree(temp_dir))
292
atexit.register(shutil.rmtree, temp_dir)
264
293
working_dir = os.path.join(temp_dir, 'deployer')
294
# Copy all existing .yaml files, they don't need to be populated but
295
# juju-deployer expect them in 'working_dir'.
265
296
shutil.copytree(deployer_dir, working_dir)
267
298
if not args.branch_payload and not args.reuse_payload: