cement/scripts/devtools.py

168 lines
5.5 KiB
Python
Raw Normal View History

2012-10-09 03:54:07 +00:00
#!/usr/bin/env python
2012-10-09 03:54:07 +00:00
import os
import sys
import re
import tempfile
2012-10-09 03:54:07 +00:00
2017-11-18 19:18:49 +00:00
from cement import App, Controller, ex
# from cement.core.foundation import CementApp
# from cement.ext.ext_argparse import ArgparseController, expose
2012-10-09 03:54:07 +00:00
from cement.utils.version import get_version
from cement.utils import shell
2012-10-09 03:54:07 +00:00
2012-10-09 03:54:07 +00:00
VERSION = get_version()
2012-10-09 03:54:07 +00:00
2017-11-18 19:18:49 +00:00
class CementDevtoolsController(Controller):
2012-10-09 03:54:07 +00:00
class Meta:
label = 'base'
arguments = [
2014-09-30 02:01:46 +00:00
(['-y, --noprompt'],
dict(help='answer yes to prompts.', action='store_true',
2012-10-09 03:54:07 +00:00
dest='noprompt')),
(['--ignore-errors'],
2014-09-30 02:01:46 +00:00
dict(help="don't stop operations because of errors",
2012-10-09 03:54:07 +00:00
action='store_true', dest='ignore_errors')),
2014-09-30 02:01:46 +00:00
(['--loud'], dict(help='add more verbose output',
2012-10-09 03:54:07 +00:00
action='store_true', dest='loud')),
2012-10-09 03:54:07 +00:00
]
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
def _do_error(self, msg):
if self.app.pargs.ignore_errors:
self.app.log.error(msg)
else:
raise Exception(msg)
2014-09-30 02:01:46 +00:00
2017-11-18 19:18:49 +00:00
@ex(hide=True)
2012-10-09 03:54:07 +00:00
def default(self):
2013-03-14 05:39:49 +00:00
raise AssertionError("A sub-command is required. See --help.")
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
def _do_git(self):
# make sure we don't have any uncommitted changes
print('Checking for Uncommitted Changes')
out, err, res = shell.exec_cmd(['git', '--no-pager', 'diff'])
if len(out) > 0:
self._do_error('There are uncommitted changes. See `git status`.')
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
# make sure we don't have any un-added files
print('Checking for Untracked Files')
out, err, res = shell.exec_cmd(['git', 'status'])
2016-02-26 21:44:25 +00:00
if re.match('Untracked files', str(out)):
2012-10-09 03:54:07 +00:00
self._do_error('There are untracked files. See `git status`.')
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
# make sure there isn't an existing tag
print("Checking for Duplicate Git Tag")
out, err, res = shell.exec_cmd(['git', 'tag'])
2016-02-25 05:07:15 +00:00
for ver in str(out).split('\n'):
2012-10-09 03:54:07 +00:00
if ver == VERSION:
self._do_error("Tag %s already exists" % VERSION)
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
print("Tagging Git Release")
2014-09-30 02:01:46 +00:00
out, err, res = shell.exec_cmd(['git', 'tag', '-a', '-m', VERSION,
2012-10-09 03:54:07 +00:00
VERSION])
if res > 0:
self._do_error("Unable to tag release with git.")
def _do_tests(self):
print('Running Nose Tests')
out, err, res = shell.exec_cmd(['which', 'nosetests'])
2016-02-25 05:07:15 +00:00
nose = out.decode('utf-8').strip()
2012-10-09 03:54:07 +00:00
if self.app.pargs.loud:
2016-02-25 05:07:15 +00:00
cmd_args = ['coverage', 'run', nose, '--verbosity=3']
2012-10-09 03:54:07 +00:00
res = shell.exec_cmd2(cmd_args)
2012-10-09 03:54:07 +00:00
else:
2016-02-25 05:07:15 +00:00
cmd_args = ['coverage', 'run', nose, '--verbosity=0']
2012-10-09 03:54:07 +00:00
out, err, res = shell.exec_cmd(cmd_args)
if res > 0:
self._do_error("\n\nNose tests did not pass.\n\n" +
"$ %s\n%s" % (' '.join(cmd_args), err))
2014-09-30 02:01:46 +00:00
2016-07-05 17:02:26 +00:00
def _do_flake8(self):
print("Checking Flake8 Compliance (pep8, pycodestyle, mccabe, etc)")
2017-02-04 21:37:41 +00:00
cmd_args = ['flake8', 'cement/', 'tests/']
2012-10-09 03:54:07 +00:00
out, err, res = shell.exec_cmd(cmd_args)
if res > 0:
2016-07-05 17:02:26 +00:00
self._do_error("\n\nFlake8 checks did not pass.\n\n" +
2016-02-25 05:07:15 +00:00
"$ %s\n%s" % (' '.join(cmd_args), str(out)))
2012-10-09 03:54:07 +00:00
2017-11-18 19:18:49 +00:00
@ex(help='run all unit tests')
2012-10-09 03:54:07 +00:00
def run_tests(self):
2013-08-12 22:35:21 +00:00
print('')
print('Python Version: %s' % sys.version)
2012-10-09 03:54:07 +00:00
print('')
2013-05-22 01:27:05 +00:00
print("Running Tests for Cement Version %s" % VERSION)
print('-' * 77)
2016-07-05 17:02:26 +00:00
self._do_flake8()
2012-10-09 03:54:07 +00:00
self._do_tests()
print('')
2017-11-18 19:18:49 +00:00
@ex(help='run flake8 tests')
2016-07-05 17:02:26 +00:00
def flake8(self):
self._do_flake8()
2016-02-08 22:16:17 +00:00
2012-10-09 03:54:07 +00:00
def _do_sphinx(self, dest_path):
print("Building Documentation")
cmd_args = ['rm', '-rf', 'docs/build/*']
cmd_args = ['sphinx-build', 'doc/source', dest_path]
out, err, res = shell.exec_cmd(cmd_args)
if res > 0:
self._do_error("\n\nFailed to build sphinx documentation\n\n" +
2016-02-25 05:07:15 +00:00
"$ %s\n%s" % (' '.join(cmd_args), str(out)))
2014-09-30 02:01:46 +00:00
2017-11-18 19:18:49 +00:00
@ex(help='create a cement release')
2012-10-09 03:54:07 +00:00
def make_release(self):
2012-10-09 03:54:07 +00:00
print('')
2013-05-22 01:27:05 +00:00
print("Making Release for Version %s" % VERSION)
print('-' * 77)
2012-10-09 03:54:07 +00:00
if not self.app.pargs.noprompt:
2016-02-25 04:56:58 +00:00
res = input("Continue? [yN] ")
2012-10-09 03:54:07 +00:00
if res not in ['Y', 'y', '1']:
sys.exit(1)
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
tmp = tempfile.mkdtemp()
2013-05-22 01:27:05 +00:00
print("Destination: %s" % tmp)
2012-10-09 03:54:07 +00:00
os.makedirs(os.path.join(tmp, 'source'))
os.makedirs(os.path.join(tmp, 'doc'))
2014-09-30 02:01:46 +00:00
2016-07-05 17:02:26 +00:00
self._do_flake8()
2012-10-09 03:54:07 +00:00
self._do_tests()
self._do_git()
self._do_sphinx(os.path.join(tmp, 'doc'))
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
tar_path = os.path.join(tmp, 'source', 'cement-%s.tar' % VERSION)
gzip_path = "%s.gz" % tar_path
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
print("Generating Release Files")
2014-09-30 02:01:46 +00:00
cmd_args = ['git', 'archive', VERSION,
2012-10-09 03:54:07 +00:00
'--prefix=cement-%s/' % VERSION,
'--output=%s' % tar_path]
out, err, res = shell.exec_cmd(cmd_args)
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
cmd_args = ['gzip', tar_path]
out, err, res = shell.exec_cmd(cmd_args)
if res > 0:
self._do_error("\n\nFailed generating git archive.\n\n" +
"$ %s" % (' '.join(cmd_args), err))
2012-10-09 03:54:07 +00:00
2012-10-09 03:54:07 +00:00
print('')
2014-09-30 02:01:46 +00:00
2017-11-18 19:18:49 +00:00
@ex(help='get the current version of the sources')
2012-10-09 03:54:07 +00:00
def get_version(self):
print(VERSION)
2014-09-30 02:01:46 +00:00
2017-11-18 19:18:49 +00:00
class CementDevtoolsApp(App):
2012-10-09 03:54:07 +00:00
class Meta:
label = 'cement-devtools'
base_controller = CementDevtoolsController
2014-09-30 02:01:46 +00:00
2012-10-09 03:54:07 +00:00
def main():
2014-09-30 02:01:46 +00:00
with CementDevtoolsApp() as app:
try:
app.run()
except AssertionError as e:
print("AssertionError => %s" % e.args[0])
2012-10-09 03:54:07 +00:00
if __name__ == '__main__':
2014-09-30 02:01:46 +00:00
main()