-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfabfile.py
executable file
·195 lines (143 loc) · 4.46 KB
/
fabfile.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
"""
Install the packages you have listed in the requirements file you input as
first argument.
"""
from __future__ import (absolute_import, division, print_function, unicode_literals)
try:
from fabric.api import task, local
except:
from invoke import task
from invoke import run as local
import os
import os.path as op
import shutil
from glob import glob
from setuptools import find_packages
from pip.req import parse_requirements
# Get version without importing, which avoids dependency issues
module_name = find_packages(exclude=['tests'])[0]
version_pyfile = op.join(module_name, 'version.py')
exec(compile(open(version_pyfile).read(), version_pyfile, 'exec'))
# get current dir
CWD = op.realpath(op.curdir)
#ignore dirs
IGNORE = ['.git', '.idea']
def get_requirements(*args):
"""Parse all requirements files given and return a list of the dependencies"""
install_deps = []
try:
for fpath in args:
install_deps.extend([str(d.req or d.url) for d in parse_requirements(fpath)])
except:
print('Error reading {} file looking for dependencies.'.format(fpath))
return [dep for dep in install_deps if dep != 'None']
def recursive_glob(base_directory, regex=None):
"""Uses glob to find all files that match the regex in base_directory.
@param base_directory: string
@param regex: string
@return: set
"""
if regex is None:
regex = ''
files = glob(os.path.join(base_directory, regex))
for path, dirlist, filelist in os.walk(base_directory):
for ignored in IGNORE:
try:
dirlist.remove(ignored)
except:
pass
for dir_name in dirlist:
files.extend(glob(os.path.join(path, dir_name, regex)))
return files
def recursive_remove(work_dir=CWD, regex='*'):
[os.remove(fn) for fn in recursive_glob(work_dir, regex)]
def recursive_rmtrees(work_dir=CWD, regex='*'):
[shutil.rmtree(fn, ignore_errors=True) for fn in recursive_glob(work_dir, regex)]
@task
def install_deps(req_filepaths = ['requirements.txt']):
# for line in fileinput.input():
deps = get_requirements(*req_filepaths)
try:
for dep_name in deps:
cmd = "pip install '{0}'".format(dep_name)
print('#', cmd)
local(cmd)
except:
print('Error installing {}'.format(dep_name))
@task
def version():
print(__version__)
@task
def install():
clean()
install_deps()
local('python setup.py install')
@task
def develop():
clean()
install_deps()
local('python setup.py develop')
@task
def clean(work_dir=CWD):
clean_build(work_dir)
clean_pyc(work_dir)
@task
def clean_build(work_dir=CWD):
shutil.rmtree('build', ignore_errors=True)
shutil.rmtree('dist', ignore_errors=True)
shutil.rmtree('.eggs', ignore_errors=True)
recursive_rmtrees(work_dir, '__pycache__')
recursive_rmtrees(work_dir, '*.egg-info')
recursive_rmtrees(work_dir, '*.egg')
recursive_rmtrees(work_dir, '.ipynb_checkpoints')
@task
def clean_pyc(work_dir=CWD):
recursive_remove(work_dir, '*.pyc')
recursive_remove(work_dir, '*.pyo')
recursive_remove(work_dir, '*~')
@task
def lint():
local('flake8 ' + module_name + ' test')
@task
def test(filepath=''):
if filepath:
if not op.exists(filepath):
print('Error: could not find file {}'.format(filepath))
exit(-1)
cmd = 'python setup.py test -a ' + filepath
else:
cmd = 'python setup.py test'
local(cmd)
@task
def test_all():
local('tox')
@task
def coverage():
local('coverage local --source ' + module_name + ' setup.py test')
local('coverage report -m')
local('coverage html')
local('open htmlcov/index.html')
@task
def docs(doc_type='html'):
os.remove(op.join('docs', module_name + '.rst'))
os.remove(op.join('docs', 'modules.rst'))
local('sphinx-apidoc -o docs/ ' + module_name)
os.chdir('docs')
local('make clean')
local('make ' + doc_type)
os.chdir(CWD)
local('open docs/_build/html/index.html')
@task
def release():
clean()
local('pip install -U pip setuptools twine wheel')
local('python setup.py sdist bdist_wheel')
#local('python setup.py bdist_wheel upload')
local('twine upload dist/*')
@task
def sdist():
clean()
local('python setup.py sdist')
local('python setup.py bdist_wheel upload')
print(os.listdir('dist'))