Skip to content

Commit

Permalink
Add --quiet option; resolves #210
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Feb 29, 2024
1 parent a39ea73 commit fdcc912
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### New features
- When calling `set-default`, you can now pass `--allow-undefined` to set the
default to a version that doesn't exist yet
- Add global-level `-q` / `--quiet` option to suppress warning messages

### Bug fixes
- When loading an MkDocs config, mike now runs the `startup` and `shutdown`
Expand Down
20 changes: 16 additions & 4 deletions mike/driver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import sys
import warnings
from contextlib import contextmanager

from . import arguments
Expand Down Expand Up @@ -77,6 +78,10 @@
"""


def showwarning(message, category, filename, lineno, file=None, line=None):
sys.stderr.write('warning: {}\n'.format(message))


def add_git_arguments(parser, *, commit=True, deploy_prefix=True):
# Add this whenever we add git arguments since we pull the remote and
# branch from mkdocs.yml.
Expand Down Expand Up @@ -174,16 +179,16 @@ def check_remote_status(args, strict=False):
raise ValueError(str(e) + "\n If you're sure this is intended, " +
'retry with --ignore-remote-status')
else:
sys.stderr.write('warning: {}\n'.format(e))
warnings.warn(str(e))


@contextmanager
def handle_empty_commit():
try:
yield
except git_utils.GitEmptyCommit as e:
sys.stderr.write(('warning: {}\n To create a commit anyway, retry ' +
'with --allow-empty\n').format(e))
warnings.warn(str(e) + '\n To create a commit anyway, retry with ' +
'--allow-empty')


def deploy(parser, args):
Expand All @@ -198,7 +203,7 @@ def deploy(parser, args):
deploy_prefix=args.deploy_prefix,
set_props=args.set_props or []), \
mkdocs_utils.inject_plugin(args.config_file) as config_file:
mkdocs_utils.build(config_file, args.version)
mkdocs_utils.build(config_file, args.version, quiet=args.quiet)
if args.push:
git_utils.push_branch(args.remote, args.branch)

Expand Down Expand Up @@ -327,12 +332,16 @@ def generate_completion(parser, args):


def main():
warnings.showwarning = showwarning

parser = arguments.ArgumentParser(prog='mike', description=description)
subparsers = parser.add_subparsers(metavar='COMMAND')
subparsers.required = True

parser.add_argument('--version', action='version',
version='%(prog)s ' + app_version)
parser.add_argument('-q', '--quiet', action='store_true',
help='silence warnings')
parser.add_argument('--debug', action='store_true',
help='report extra information for debugging mike')

Expand Down Expand Up @@ -460,6 +469,9 @@ def main():
help='shell type (default: %(default)s)')

args = parser.parse_args()
if args.quiet:
warnings.filterwarnings('ignore')

try:
return args.func(parser, args)
except Exception as e:
Expand Down
5 changes: 2 additions & 3 deletions mike/mkdocs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ def inject_plugin(config_file):
os.remove(f.name)


def build(config_file, version, verbose=True):
def build(config_file, version, *, quiet=False, output=None):
command = (
['mkdocs', 'build', '--clean'] +
['mkdocs'] + (['--quiet'] if quiet else []) + ['build', '--clean'] +
(['--config-file', config_file] if config_file else [])
)

env = os.environ.copy()
env[docs_version_var] = version

output = None if verbose else subprocess.DEVNULL
subprocess.run(command, check=True, env=env, stdout=output, stderr=output)


Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class HelpTest(unittest.TestCase):
def test_help(self):
output = assertPopen(['mike', 'help'])
self.assertRegex(
output, (r'^usage: mike \[-h\] \[--version\] \[--debug\] '
output, (r'^usage: mike \[-h\] \[--version\] \[-q\] \[--debug\] '
r'COMMAND \.\.\.')
)

Expand Down
8 changes: 8 additions & 0 deletions test/integration/test_set_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ def test_no_changes(self):
))
self.assertEqual(git_utils.get_latest_commit('gh-pages'), rev)

def test_no_changes_quiet(self):
self._deploy()
assertPopen(['mike', 'set-default', '1.0'])
rev = git_utils.get_latest_commit('gh-pages')
assertOutput(self, ['mike', '--quiet', 'set-default', '1.0'],
stdout='', stderr='')
self.assertEqual(git_utils.get_latest_commit('gh-pages'), rev)

def test_remote_empty(self):
stage_dir('set_default_clone')
check_call_silent(['git', 'clone', self.stage, '.'])
Expand Down
5 changes: 3 additions & 2 deletions test/unit/test_mkdocs_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess
import unittest
import yaml
from io import StringIO
Expand Down Expand Up @@ -192,7 +193,7 @@ class TestBuild(unittest.TestCase):
def test_build(self):
self.stage = stage_dir('build')
copytree(os.path.join(test_data_dir, 'basic_theme'), self.stage)
mkdocs_utils.build('mkdocs.yml', '1.0', verbose=False)
mkdocs_utils.build('mkdocs.yml', '1.0', output=subprocess.DEVNULL)

self.assertTrue(os.path.exists('site/index.html'))

Expand All @@ -205,7 +206,7 @@ def test_build_directory(self):
# responsible for).
with pushd(this_dir):
mkdocs_utils.build(os.path.join(self.stage, 'mkdocs.yml'),
'1.0', verbose=False)
'1.0', output=subprocess.DEVNULL)

self.assertTrue(os.path.exists('site/index.html'))

Expand Down

0 comments on commit fdcc912

Please sign in to comment.