Debian Codegraph @

tasks.py

raw
1 # -*- coding: utf-8 -*-
2 """Common development tasks for setup.py to use."""
3
4 import re
5 import subprocess
6 import sys
7
8 from setuptools import Command
9
10
11 class BaseCommand(Command, object):
12 """The base command for project tasks."""
13
14 user_options = []
15
16 default_cmd_options = ("verbose", "quiet", "dry_run")
17
18 def __init__(self, *args, **kwargs):
19 super(BaseCommand, self).__init__(*args, **kwargs)
20 self.verbose = False
21
22 def initialize_options(self):
23 """Override the distutils abstract method."""
24 pass
25
26 def finalize_options(self):
27 """Override the distutils abstract method."""
28 # Distutils uses incrementing integers for verbosity.
29 self.verbose = bool(self.verbose)
30
31 def call_and_exit(self, cmd, shell=True):
32 """Run the *cmd* and exit with the proper exit code."""
33 sys.exit(subprocess.call(cmd, shell=shell))
34
35 def call_in_sequence(self, cmds, shell=True):
36 """Run multiple commmands in a row, exiting if one fails."""
37 for cmd in cmds:
38 if subprocess.call(cmd, shell=shell) == 1:
39 sys.exit(1)
40
41 def apply_options(self, cmd, options=()):
42 """Apply command-line options."""
43 for option in self.default_cmd_options + options:
44 cmd = self.apply_option(cmd, option, active=getattr(self, option, False))
45 return cmd
46
47 def apply_option(self, cmd, option, active=True):
48 """Apply a command-line option."""
49 return re.sub(
50 r"{{{}\:(?P<option>[^}}]*)}}".format(option),
51 r"\g<option>" if active else "",
52 cmd,
53 )
54
55
56 class lint(BaseCommand):
57 """A PEP 8 lint command that optionally fixes violations."""
58
59 description = "check code against PEP 8 (and fix violations)"
60
61 user_options = [
62 ("branch=", "b", "branch or revision to compare against (e.g. master)"),
63 ("fix", "f", "fix the violations in place"),
64 ]
65
66 def initialize_options(self):
67 """Set the default options."""
68 self.branch = "master"
69 self.fix = False
70 super(lint, self).initialize_options()
71
72 def run(self):
73 """Run the linter."""
74 cmd = "black ."
75 cmd = cmd.format(branch=self.branch)
76 self.call_and_exit(self.apply_options(cmd, ("fix",)))
77
78
79 class test(BaseCommand):
80 """Run the test suites for this project."""
81
82 description = "run the test suite"
83
84 user_options = [
85 ("all", "a", "test against all supported versions of Python"),
86 ("coverage", "c", "measure test coverage"),
87 ]
88
89 unit_test_cmd = (
90 "pytest{quiet: -q}{verbose: -v}{dry_run: --setup-only}"
91 "{coverage: --cov-report= --cov=cli_helpers}"
92 )
93 test_all_cmd = "tox{verbose: -v}{dry_run: --notest}"
94 coverage_cmd = "coverage report"
95
96 def initialize_options(self):
97 """Set the default options."""
98 self.all = False
99 self.coverage = False
100 super(test, self).initialize_options()
101
102 def run(self):
103 """Run the test suites."""
104 if self.all:
105 cmd = self.apply_options(self.test_all_cmd)
106 self.call_and_exit(cmd)
107 else:
108 cmds = (self.apply_options(self.unit_test_cmd, ("coverage",)),)
109 if self.coverage:
110 cmds += (self.apply_options(self.coverage_cmd),)
111 self.call_in_sequence(cmds)
112
113
114 class docs(BaseCommand):
115 """Use Sphinx Makefile to generate documentation."""
116
117 description = "generate the Sphinx HTML documentation"
118
119 clean_docs_cmd = "make -C docs clean"
120 html_docs_cmd = "make -C docs html"
121 view_docs_cmd = "open docs/build/html/index.html"
122
123 def run(self):
124 """Generate and view the documentation."""
125 cmds = (self.clean_docs_cmd, self.html_docs_cmd, self.view_docs_cmd)
126 self.call_in_sequence(cmds)