Skip to content

Commit 1b9290d

Browse files
committed
Script for bootstrapping tmuxp dev environment requirements in a python file, using script_before. Update JSON and YAML example configs.
1 parent cbf78d4 commit 1b9290d

File tree

3 files changed

+153
-64
lines changed

3 files changed

+153
-64
lines changed

.tmuxp.json

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,42 @@
11
{
2+
"before_script": "./bootstrap_env.py",
23
"windows": [
34
{
45
"panes": [
56
{
6-
"shell_command": [
7-
"reset",
8-
"vim",
9-
":Ex"
10-
],
117
"focus": true
128
},
13-
"pane",
14-
{
15-
"shell_command": [
16-
"command -v .env/bin/tmuxp >/dev/null 2>&1 || { pip install -e .; }",
17-
"command -v watching_testrunner >/dev/null 2>&1 || { pip install watching_testrunner; }",
18-
"watching_testrunner --basepath ./ --pattern=\"*.py\" 'python run-tests.py'"
19-
]
20-
}
9+
"pane",
10+
"watching_testrunner --basepath ./ --pattern=\"*.py\" 'python run-tests.py'"
2111
],
2212
"options": {
2313
"main-pane-height": 35
2414
},
2515
"layout": "main-horizontal",
2616
"shell_command_before": [
27-
"command -v virtualenv >/dev/null 2>&1 || { pip install virtualenv; }",
28-
"[ -d .env -a -f .env/bin/activate ] && source .env/bin/activate || virtualenv .env",
29-
"[ ! -d .env/build ] || rm -rf .env/build"
17+
"[ -d .env -a -f .env/bin/activate ] && source .env/bin/activate"
3018
],
3119
"focus": true,
3220
"window_name": "tmuxp"
3321
},
3422
{
3523
"panes": [
3624
{
37-
"shell_command": [
38-
"reset",
39-
"vim",
40-
":Ex"
41-
],
4225
"focus": true
4326
},
44-
"pwd",
27+
"pane",
4528
"echo 'docs built to <http://0.0.0.0:8003/_build/html>'; python -m SimpleHTTPServer 8003",
46-
{
47-
"shell_command": [
48-
"command -v sphinx-quickstart >/dev/null 2>&1 || { pip install -r requirements.pip; }",
49-
"command -v watching_testrunner >/dev/null 2>&1 || { pip install watching_testrunner; }",
50-
"watching_testrunner --basepath ./ --pattern=\"*.rst\" 'make html'"
51-
]
52-
}
29+
"watching_testrunner --basepath ./ --pattern=\"*.rst\" 'make html'"
5330
],
31+
"start_directory": "doc/",
32+
"layout": "main-horizontal",
5433
"shell_command_before": [
55-
"command -v virtualenv >/dev/null 2>&1 || { pip install virtualenv; }",
56-
"[ -d .env -a -f .env/bin/activate ] && source .env/bin/activate || virtualenv .env",
57-
"[ ! -d .env/build ] || rm -rf .env/build",
58-
"command -v .env/bin/tmuxp >/dev/null 2>&1 || { pip install -e .; }",
59-
"cd ./doc"
34+
"[ -d ../.env -a -f ../.env/bin/activate ] && source ../.env/bin/activate"
6035
],
61-
"layout": "main-horizontal",
62-
"window_name": "docs",
6336
"options": {
6437
"main-pane-height": 35
65-
}
38+
},
39+
"window_name": "docs"
6640
}
6741
],
6842
"session_name": "tmuxp",

.tmuxp.yaml

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,27 @@
11
session_name: tmuxp
22
start_directory: ./ # load session relative to config location (project root).
3+
before_script: ./bootstrap_env.py # ./ to load relative to project root.
34
windows:
45
- window_name: tmuxp
56
focus: True
67
layout: main-horizontal
78
options:
89
main-pane-height: 35
910
shell_command_before:
10-
- command -v virtualenv >/dev/null 2>&1 || { pip install virtualenv; }
11-
- '[ -d .env -a -f .env/bin/activate ] && source .env/bin/activate || virtualenv .env'
12-
- '[ ! -d .env/build ] || rm -rf .env/build'
11+
- '[ -d .env -a -f .env/bin/activate ] && source .env/bin/activate'
1312
panes:
14-
- shell_command:
15-
- reset
16-
- vim
17-
- :Ex
18-
focus: true
13+
- focus: true
1914
- pane
20-
- shell_command:
21-
- command -v .env/bin/tmuxp >/dev/null 2>&1 || { pip install -e .; }
22-
- command -v watching_testrunner >/dev/null 2>&1 || { pip install watching_testrunner; }
23-
- watching_testrunner --basepath ./ --pattern="*.py" 'python run-tests.py'
15+
- watching_testrunner --basepath ./ --pattern="*.py" 'python run-tests.py'
2416
- window_name: docs
2517
layout: main-horizontal
2618
options:
2719
main-pane-height: 35
20+
start_directory: doc/
2821
shell_command_before:
29-
- command -v virtualenv >/dev/null 2>&1 || { pip install virtualenv; }
30-
- '[ -d .env -a -f .env/bin/activate ] && source .env/bin/activate || virtualenv .env'
31-
- '[ ! -d .env/build ] || rm -rf .env/build'
32-
- command -v .env/bin/tmuxp >/dev/null 2>&1 || { pip install -e .; }
33-
- cd ./doc
22+
- '[ -d ../.env -a -f ../.env/bin/activate ] && source ../.env/bin/activate'
3423
panes:
35-
- shell_command:
36-
- reset
37-
- vim
38-
- :Ex
39-
focus: true
40-
- pwd
24+
- focus: true
25+
- pane
4126
- echo 'docs built to <http://0.0.0.0:8003/_build/html>'; python -m SimpleHTTPServer 8003
42-
- shell_command:
43-
- command -v sphinx-quickstart >/dev/null 2>&1 || { pip install -r requirements.pip; }
44-
- command -v watching_testrunner >/dev/null 2>&1 || { pip install watching_testrunner; }
45-
- watching_testrunner --basepath ./ --pattern="*.rst" 'make html'
27+
- watching_testrunner --basepath ./ --pattern="*.rst" 'make html'

bootstrap_env.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import absolute_import, division, print_function, \
4+
with_statement, unicode_literals
5+
6+
7+
import os
8+
import sys
9+
import subprocess
10+
11+
12+
def warning(*objs):
13+
print("WARNING: ", *objs, file=sys.stderr)
14+
15+
16+
def fail(message):
17+
sys.exit("Error: {message}".format(message=message))
18+
19+
20+
PY2 = sys.version_info[0] == 2
21+
if PY2:
22+
from urllib import urlretrieve
23+
else:
24+
from urllib.request import urlretrieve
25+
26+
27+
def has_module(module_name):
28+
try:
29+
import imp
30+
imp.find_module(module_name)
31+
del imp
32+
return True
33+
except ImportError:
34+
return False
35+
36+
37+
def which(exe=None, throw=True):
38+
"""Return path of bin. Python clone of /usr/bin/which.
39+
40+
from salt.util - https://www.github.com/saltstack/salt - license apache
41+
42+
:param exe: Application to search PATHs for.
43+
:type exe: string
44+
:param throw: Raise ``Exception`` if not found in paths
45+
:type throw: bool
46+
:rtype: string
47+
48+
"""
49+
if exe:
50+
if os.access(exe, os.X_OK):
51+
return exe
52+
53+
# default path based on busybox's default
54+
default_path = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'
55+
search_path = os.environ.get('PATH', default_path)
56+
57+
for path in search_path.split(os.pathsep):
58+
full_path = os.path.join(path, exe)
59+
if os.access(full_path, os.X_OK):
60+
return full_path
61+
62+
message = (
63+
'{0!r} could not be found in the following search '
64+
'path: {1!r}'.format(
65+
exe, search_path
66+
)
67+
)
68+
69+
if throw:
70+
raise Exception(message)
71+
else:
72+
print(message)
73+
return None
74+
75+
76+
project_dir = os.path.dirname(os.path.realpath(__file__))
77+
env_dir = os.path.join(project_dir, '.env')
78+
pip_bin = os.path.join(env_dir, 'bin', 'pip')
79+
python_bin = os.path.join(env_dir, 'bin', 'python')
80+
virtualenv_bin = which('virtualenv', throw=False)
81+
virtualenv_exists = os.path.exists(env_dir) and os.path.isfile(python_bin)
82+
sphinx_requirements_filepath = os.path.join(project_dir, 'doc', 'requirements.pip')
83+
84+
85+
try:
86+
import virtualenv
87+
except ImportError:
88+
message = (
89+
'Virtualenv is required for this bootstrap to run.\n'
90+
'Install virtualenv via:\n'
91+
'\t$ [sudo] pip install virtualenv'
92+
)
93+
fail(message)
94+
95+
96+
try:
97+
import pip
98+
except ImportError:
99+
message = (
100+
'pip is required for this bootstrap to run.\n'
101+
'Find instructions on how to install at: %s' %
102+
'http://pip.readthedocs.org/en/latest/installing.html'
103+
)
104+
fail(message)
105+
106+
107+
def main():
108+
if not virtualenv_exists:
109+
virtualenv_bin = which('virtualenv', throw=False)
110+
111+
subprocess.check_call(
112+
[virtualenv_bin, env_dir]
113+
)
114+
115+
subprocess.check_call(
116+
[pip_bin, 'install', '-e', project_dir]
117+
)
118+
119+
if not os.path.isfile(os.path.join(env_dir, 'bin', 'watching_testrunner')):
120+
subprocess.check_call(
121+
[pip_bin, 'install', 'watching-testrunner']
122+
)
123+
124+
if not os.path.isfile(os.path.join(env_dir, 'bin', 'sphinx-quickstart')):
125+
subprocess.check_call(
126+
[pip_bin, 'install', '-r', sphinx_requirements_filepath]
127+
)
128+
129+
if os.path.exists(os.path.join(env_dir, 'build')):
130+
os.removedirs(os.path.join(env_dir, 'build'))
131+
132+
if __name__ == '__main__':
133+
main()

0 commit comments

Comments
 (0)