2018-05-15 16:57:56 +02:00
|
|
|
# pylint: disable=not-context-manager
|
|
|
|
"""
|
|
|
|
Fabric tasks for PyBitmessage devops operations.
|
|
|
|
|
2018-05-26 14:43:21 +02:00
|
|
|
Note that where tasks declare params to be bools, they use coerce_bool() and so will accept any commandline (string)
|
|
|
|
representation of true or false that coerce_bool() understands.
|
|
|
|
|
2018-05-15 16:57:56 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2018-05-26 14:43:21 +02:00
|
|
|
from fabric.api import run, task, hide, cd, settings, sudo
|
|
|
|
from fabric.contrib.project import rsync_project
|
2018-05-15 16:57:56 +02:00
|
|
|
from fabvenv import virtualenv
|
|
|
|
|
|
|
|
from fabfile.lib import (
|
2019-07-09 18:22:45 +02:00
|
|
|
autopep8, project_root, VENV_ROOT, coerce_bool, flatten, filelist_from_git, default_hosts,
|
2018-05-15 16:57:56 +02:00
|
|
|
get_filtered_pycodestyle_output, get_filtered_flake8_output, get_filtered_pylint_output,
|
|
|
|
)
|
|
|
|
|
2018-05-26 14:43:21 +02:00
|
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'src'))) # noqa:E402
|
|
|
|
from version import softwareVersion # pylint: disable=wrong-import-position
|
|
|
|
|
2018-05-15 16:57:56 +02:00
|
|
|
|
|
|
|
def get_tool_results(file_list):
|
|
|
|
"""Take a list of files and resuln the results of applying the tools"""
|
|
|
|
|
|
|
|
results = []
|
|
|
|
for path_to_file in file_list:
|
2019-07-09 18:22:45 +02:00
|
|
|
result = {
|
|
|
|
'pycodestyle_violations': get_filtered_pycodestyle_output(path_to_file),
|
|
|
|
'flake8_violations': get_filtered_flake8_output(path_to_file),
|
|
|
|
'pylint_violations': get_filtered_pylint_output(path_to_file),
|
|
|
|
'path_to_file': path_to_file,
|
|
|
|
'total_violations': sum([
|
|
|
|
len(result['pycodestyle_violations']),
|
|
|
|
len(result['flake8_violations']),
|
|
|
|
len(result['pylint_violations']),
|
|
|
|
])
|
|
|
|
}
|
2018-05-15 16:57:56 +02:00
|
|
|
results.append(result)
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
def print_results(results, top, verbose, details):
|
|
|
|
"""Print an item with the appropriate verbosity / detail"""
|
|
|
|
|
2018-06-11 20:11:55 +02:00
|
|
|
if verbose and results:
|
2019-07-09 18:22:45 +02:00
|
|
|
print(''.join(
|
2018-05-15 16:57:56 +02:00
|
|
|
[
|
|
|
|
os.linesep,
|
|
|
|
'total pycodestyle flake8 pylint path_to_file',
|
|
|
|
os.linesep,
|
|
|
|
]
|
2019-07-09 18:22:45 +02:00
|
|
|
))
|
2018-05-15 16:57:56 +02:00
|
|
|
|
|
|
|
for item in sort_and_slice(results, top):
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
line = "{0} {1} {2} {3} {4}".format(
|
|
|
|
item['total_violations'],
|
|
|
|
len(item['pycodestyle_violations']),
|
|
|
|
len(item['flake8_violations']),
|
|
|
|
len(item['pylint_violations']),
|
|
|
|
item['path_to_file'],
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
line = item['path_to_file']
|
2019-07-09 18:22:45 +02:00
|
|
|
print(line)
|
2018-05-15 16:57:56 +02:00
|
|
|
|
|
|
|
if details:
|
2019-07-09 18:22:45 +02:00
|
|
|
print("pycodestyle:")
|
2018-05-15 16:57:56 +02:00
|
|
|
for detail in flatten(item['pycodestyle_violations']):
|
2019-07-09 18:22:45 +02:00
|
|
|
print(detail)
|
2018-05-15 16:57:56 +02:00
|
|
|
print
|
|
|
|
|
2019-07-09 18:22:45 +02:00
|
|
|
print("flake8:")
|
2018-05-15 16:57:56 +02:00
|
|
|
for detail in flatten(item['flake8_violations']):
|
2019-07-09 18:22:45 +02:00
|
|
|
print(detail)
|
2018-05-15 16:57:56 +02:00
|
|
|
print
|
|
|
|
|
2019-07-09 18:22:45 +02:00
|
|
|
print("pylint:")
|
2018-05-15 16:57:56 +02:00
|
|
|
for detail in flatten(item['pylint_violations']):
|
2019-07-09 18:22:45 +02:00
|
|
|
print(detail)
|
2018-05-15 16:57:56 +02:00
|
|
|
print
|
|
|
|
|
|
|
|
|
|
|
|
def sort_and_slice(results, top):
|
|
|
|
"""Sort dictionary items by the `total_violations` key and honour top"""
|
|
|
|
|
|
|
|
returnables = []
|
|
|
|
for item in sorted(
|
|
|
|
results,
|
|
|
|
reverse=True,
|
|
|
|
key=lambda x: x['total_violations']
|
|
|
|
)[:top]:
|
|
|
|
returnables.append(item)
|
|
|
|
return returnables
|
|
|
|
|
|
|
|
|
|
|
|
def generate_file_list(filename):
|
|
|
|
"""Return an unfiltered list of absolute paths to the files to act on"""
|
|
|
|
|
|
|
|
with hide('warnings', 'running', 'stdout'):
|
|
|
|
with virtualenv(VENV_ROOT):
|
|
|
|
|
|
|
|
if filename:
|
|
|
|
filename = os.path.abspath(filename)
|
|
|
|
if not os.path.exists(filename):
|
2019-07-09 18:22:45 +02:00
|
|
|
print("Bad filename, specify a Python file")
|
2018-05-15 16:57:56 +02:00
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
file_list = [filename]
|
|
|
|
|
|
|
|
else:
|
2019-07-09 18:22:45 +02:00
|
|
|
with cd(project_root):
|
2018-05-15 16:57:56 +02:00
|
|
|
file_list = [
|
|
|
|
os.path.abspath(i.rstrip('\r'))
|
|
|
|
for i in run('find . -name "*.py"').split(os.linesep)
|
|
|
|
]
|
|
|
|
|
|
|
|
return file_list
|
|
|
|
|
|
|
|
|
2018-05-26 14:43:21 +02:00
|
|
|
def create_dependency_graphs():
|
|
|
|
"""
|
|
|
|
To better understand the relationship between methods, dependency graphs can be drawn between functions and
|
|
|
|
methods.
|
|
|
|
|
|
|
|
Since the resulting images are large and could be out of date on the next commit, storing them in the repo is
|
|
|
|
pointless. Instead, it makes sense to build a dependency graph for a particular, documented version of the code.
|
|
|
|
|
|
|
|
.. todo:: Consider saving a hash of the intermediate dotty file so unnecessary image generation can be avoided.
|
|
|
|
|
|
|
|
"""
|
|
|
|
with virtualenv(VENV_ROOT):
|
|
|
|
with hide('running', 'stdout'):
|
|
|
|
|
|
|
|
# .. todo:: consider a better place to put this, use a particular commit
|
2019-07-09 18:22:45 +02:00
|
|
|
with cd(project_root):
|
2018-05-26 14:43:21 +02:00
|
|
|
with settings(warn_only=True):
|
|
|
|
if run('stat pyan').return_code:
|
|
|
|
run('git clone https://github.com/davidfraser/pyan.git')
|
2019-07-09 18:22:45 +02:00
|
|
|
with cd(os.path.join(project_root, 'pyan')):
|
2018-05-26 14:43:21 +02:00
|
|
|
run('git checkout pre-python3')
|
|
|
|
|
|
|
|
# .. todo:: Use better settings. This is MVP to make a diagram
|
2019-07-09 18:22:45 +02:00
|
|
|
with cd(project_root):
|
2018-05-26 14:43:21 +02:00
|
|
|
file_list = run("find . -type f -name '*.py' ! -path './src/.eggs/*'").split('\r\n')
|
|
|
|
for cmd in [
|
|
|
|
'neato -Goverlap=false -Tpng > deps-neato.png',
|
|
|
|
'sfdp -Goverlap=false -Tpng > deps-sfdp.png',
|
|
|
|
'dot -Goverlap=false -Tpng > deps-dot.png',
|
|
|
|
]:
|
|
|
|
pyan_cmd = './pyan/pyan.py {} --dot'.format(' '.join(file_list))
|
|
|
|
sed_cmd = r"sed s'/http\-old/http_old/'g" # dot doesn't like dashes
|
|
|
|
run('|'.join([pyan_cmd, sed_cmd, cmd]))
|
|
|
|
|
|
|
|
run('mv *.png docs/_build/html/_static/')
|
|
|
|
|
|
|
|
|
2018-05-15 16:57:56 +02:00
|
|
|
@task
|
2018-06-11 20:11:55 +02:00
|
|
|
@default_hosts(['localhost'])
|
|
|
|
def code_quality(verbose=True, details=False, fix=False, filename=None, top=10, rev=None):
|
2018-05-15 16:57:56 +02:00
|
|
|
"""
|
|
|
|
Check code quality.
|
|
|
|
|
|
|
|
By default this command will analyse each Python file in the project with a variety of tools and display the count
|
|
|
|
or details of the violations discovered, sorted by most violations first.
|
|
|
|
|
|
|
|
Default usage:
|
|
|
|
|
|
|
|
$ fab -H localhost code_quality
|
|
|
|
|
2018-06-11 20:11:55 +02:00
|
|
|
:param rev: If not None, act on files changed since this commit. 'cached/staged' and 'working' have special meaning
|
|
|
|
:type rev: str or None, default None
|
2018-05-15 16:57:56 +02:00
|
|
|
:param top: Display / fix only the top N violating files, a value of 0 will display / fix all files
|
|
|
|
:type top: int, default 10
|
|
|
|
:param verbose: Display a header and the counts, without this you just get the filenames in order
|
|
|
|
:type verbose: bool, default True
|
|
|
|
:param details: Display the violations one per line after the count / file summary
|
|
|
|
:type details: bool, default False
|
|
|
|
:param fix: Run autopep8 aggressively on the displayed file(s)
|
|
|
|
:type fix: bool, default False
|
2018-05-26 14:43:21 +02:00
|
|
|
:param filename: Don't test/fix the top N, just the specified file
|
2018-05-15 16:57:56 +02:00
|
|
|
:type filename: string, valid path to a file, default all files in the project
|
2018-05-26 14:43:21 +02:00
|
|
|
:return: None, exit status equals total number of violations
|
2018-05-15 16:57:56 +02:00
|
|
|
:rtype: None
|
|
|
|
|
|
|
|
Intended to be temporary until we have improved code quality and have safeguards to maintain it in place.
|
|
|
|
|
|
|
|
"""
|
2018-06-11 20:11:55 +02:00
|
|
|
# pylint: disable=too-many-arguments
|
2018-05-15 16:57:56 +02:00
|
|
|
|
|
|
|
verbose = coerce_bool(verbose)
|
|
|
|
details = coerce_bool(details)
|
|
|
|
fix = coerce_bool(fix)
|
|
|
|
top = int(top) or -1
|
|
|
|
|
2018-06-11 20:11:55 +02:00
|
|
|
file_list = generate_file_list(filename) if not rev else filelist_from_git(rev)
|
2018-05-15 16:57:56 +02:00
|
|
|
results = get_tool_results(file_list)
|
|
|
|
|
|
|
|
if fix:
|
|
|
|
for item in sort_and_slice(results, top):
|
|
|
|
autopep8(item['path_to_file'])
|
2018-05-16 03:29:25 +02:00
|
|
|
# Recalculate results after autopep8 to surprise the user the least
|
|
|
|
results = get_tool_results(file_list)
|
2018-05-15 16:57:56 +02:00
|
|
|
|
|
|
|
print_results(results, top, verbose, details)
|
|
|
|
sys.exit(sum([item['total_violations'] for item in results]))
|
2018-06-11 20:11:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
@default_hosts(['localhost'])
|
|
|
|
def test():
|
|
|
|
"""Run tests on the code"""
|
|
|
|
|
2019-07-09 18:22:45 +02:00
|
|
|
with cd(project_root):
|
2018-06-11 20:11:55 +02:00
|
|
|
with virtualenv(VENV_ROOT):
|
|
|
|
|
|
|
|
run('pip uninstall -y pybitmessage')
|
|
|
|
run('python setup.py install')
|
|
|
|
|
|
|
|
run('pybitmessage -t')
|
|
|
|
run('python setup.py test')
|
2018-06-27 11:41:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
@default_hosts(['localhost'])
|
2018-05-26 14:43:21 +02:00
|
|
|
def build_docs(dep_graph=False, apidoc=True):
|
|
|
|
"""
|
|
|
|
Build the documentation locally.
|
|
|
|
|
|
|
|
:param dep_graph: Build the dependency graphs
|
|
|
|
:type dep_graph: Bool, default False
|
|
|
|
:param apidoc: Build the automatically generated rst files from the source code
|
|
|
|
:type apidoc: Bool, default True
|
|
|
|
|
|
|
|
Default usage:
|
|
|
|
|
|
|
|
$ fab -H localhost build_docs
|
|
|
|
|
|
|
|
Implementation:
|
|
|
|
|
|
|
|
First, a dependency graph is generated and converted into an image that is referenced in the development page.
|
|
|
|
|
|
|
|
Next, the sphinx-apidoc command is (usually) run which searches the code. As part of this it loads the modules and
|
|
|
|
if this has side-effects then they will be evident. Any documentation strings that make use of Python documentation
|
|
|
|
conventions (like parameter specification) or the Restructured Text (RsT) syntax will be extracted.
|
|
|
|
|
|
|
|
Next, the `make html` command is run to generate HTML output. Other formats (epub, pdf) are available.
|
|
|
|
|
|
|
|
.. todo:: support other languages
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
apidoc = coerce_bool(apidoc)
|
|
|
|
|
|
|
|
if coerce_bool(dep_graph):
|
|
|
|
create_dependency_graphs()
|
|
|
|
|
|
|
|
with virtualenv(VENV_ROOT):
|
|
|
|
with hide('running'):
|
|
|
|
|
|
|
|
apidoc_result = 0
|
|
|
|
if apidoc:
|
2019-07-09 18:22:45 +02:00
|
|
|
run('mkdir -p {}'.format(os.path.join(project_root, 'docs', 'autodoc')))
|
|
|
|
with cd(os.path.join(project_root, 'docs', 'autodoc')):
|
2018-05-26 14:43:21 +02:00
|
|
|
with settings(warn_only=True):
|
|
|
|
run('rm *.rst')
|
2019-07-09 18:22:45 +02:00
|
|
|
with cd(os.path.join(project_root, 'docs')):
|
2018-05-26 14:43:21 +02:00
|
|
|
apidoc_result = run('sphinx-apidoc -o autodoc ..').return_code
|
|
|
|
|
2019-07-09 18:22:45 +02:00
|
|
|
with cd(os.path.join(project_root, 'docs')):
|
2018-05-26 14:43:21 +02:00
|
|
|
make_result = run('make html').return_code
|
|
|
|
return_code = apidoc_result + make_result
|
|
|
|
|
|
|
|
sys.exit(return_code)
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
2018-06-27 11:41:11 +02:00
|
|
|
@default_hosts(['localhost'])
|
2018-05-26 14:43:21 +02:00
|
|
|
def push_docs(path=None):
|
|
|
|
"""
|
|
|
|
Upload the generated docs to a public server.
|
|
|
|
|
|
|
|
Default usage:
|
|
|
|
|
|
|
|
$ fab -H localhost push_docs
|
|
|
|
|
|
|
|
.. todo:: support other languages
|
|
|
|
.. todo:: integrate with configuration management data to get web root and webserver restart command
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Making assumptions
|
2019-07-09 18:22:45 +02:00
|
|
|
web_root = path if path is not None else os.path.join('var', 'www', 'html', 'pybitmessage', 'en', 'latest')
|
|
|
|
version_root = os.path.join(os.path.dirname(web_root), softwareVersion)
|
2018-05-26 14:43:21 +02:00
|
|
|
|
|
|
|
rsync_project(
|
2019-07-09 18:22:45 +02:00
|
|
|
remote_dir=version_root,
|
|
|
|
local_dir=os.path.join(project_root, 'docs', '_build', 'html')
|
2018-05-26 14:43:21 +02:00
|
|
|
)
|
2019-07-09 18:22:45 +02:00
|
|
|
result = run('ln -sf {0} {1}'.format(web_root, version_root))
|
2018-05-26 14:43:21 +02:00
|
|
|
if result.return_code:
|
2019-07-09 18:22:45 +02:00
|
|
|
print('Linking the new release failed')
|
2018-05-26 14:43:21 +02:00
|
|
|
|
|
|
|
# More assumptions
|
|
|
|
sudo('systemctl restart apache2')
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
2018-06-27 11:41:11 +02:00
|
|
|
@default_hosts(['localhost'])
|
2018-05-26 14:43:21 +02:00
|
|
|
def clean():
|
|
|
|
"""Clean up files generated by fabric commands."""
|
|
|
|
with hide('running', 'stdout'):
|
2019-07-09 18:22:45 +02:00
|
|
|
with cd(project_root):
|
2018-05-26 14:43:21 +02:00
|
|
|
run(r"find . -name '*.pyc' -exec rm '{}' \;")
|