Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add disable_progress_bar option to disable tqdm. #427

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Jesse Jarzynka <[email protected]> (http://jessejoe.com)
László Kiss Kollár <[email protected]>
Frances Hocutt <[email protected]>
Tathagata Dasgupta <[email protected]>
Wasim Thabraze <[email protected]>
Wasim Thabraze <[email protected]>
Varun Kamath <[email protected]>
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Uploads one or more distributions to a repository.
[-s] [--sign-with SIGN_WITH] [-i IDENTITY] [-u USERNAME]
[-p PASSWORD] [-c COMMENT] [--config-file CONFIG_FILE]
[--skip-existing] [--cert path] [--client-cert path]
[--verbose] [--disable-progress-bar]
dist [dist ...]

positional arguments:
Expand Down Expand Up @@ -204,6 +205,9 @@ Uploads one or more distributions to a repository.
--client-cert path Path to SSL client certificate, a single file
containing the private key and the certificate in PEM
format.
--verbose Show verbose output.
--disable-progress-bar
Disable the progress bar.

``twine check``
^^^^^^^^^^^^^^^
Expand Down
49 changes: 49 additions & 0 deletions tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
from twine import repository
from twine.utils import DEFAULT_REPOSITORY

try:
from unittest import mock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project doesn't use mock. Instead we use pretend which is already imported below and used throughout the test file. Please update your test to use that instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out I didn't need mock after all. Just replaced ProgressBar with a context manager and it did the trick. Thanks!

except ImportError:
import mock

import pretend
import pytest


def test_gpg_signature_structure_is_preserved():
Expand Down Expand Up @@ -140,3 +146,46 @@ def test_package_is_uploaded_200s_with_no_releases():
)

assert repo.package_is_uploaded(package) is False


@pytest.mark.parametrize('disable_progress_bar', [
True,
False
])
@mock.patch("twine.repository.ProgressBar")
def test_disable_progress_bar_is_forwarded_to_tqdm(pb,
tmpdir,
disable_progress_bar):
"""Test whether the disable flag is passed to tqdm
when the disable_progress_bar option is passed to the
repository
"""
repo = repository.Repository(
repository_url=DEFAULT_REPOSITORY,
username='username',
password='password',
disable_progress_bar=disable_progress_bar
)

repo.session = pretend.stub(
post=lambda url, data, allow_redirects, headers: response_with(
status_code=200)
)

fakefile = tmpdir.join('fake.whl')
fakefile.write('.')

def dictfunc():
return {"name": "fake"}

package = pretend.stub(
safe_name='fake',
metadata=pretend.stub(version='2.12.0'),
basefilename="fake.whl",
filename=str(fakefile),
metadata_dictionary=dictfunc
)

repo.upload(package)
assert "disable" in pb.call_args[1]
assert pb.call_args[1]["disable"] == disable_progress_bar
1 change: 1 addition & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_settings_transforms_config(tmpdir):
assert s.password == 'password'
assert s.cacert is None
assert s.client_cert is None
assert s.disable_progress_bar is False


def test_identity_requires_sign():
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ deps =
pytest
py27,py34,py35: pyblake2
readme_renderer
py27,pypy: mock
commands =
coverage run --source twine -m pytest {posargs:tests}
coverage report -m
Expand Down
7 changes: 5 additions & 2 deletions twine/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ def update_to(self, n):


class Repository(object):
def __init__(self, repository_url, username, password):
def __init__(self, repository_url, username, password,
disable_progress_bar=False):
self.url = repository_url
self.session = requests.session()
self.session.auth = (username, password)
self.session.headers['User-Agent'] = self._make_user_agent_string()
for scheme in ('http://', 'https://'):
self.session.mount(scheme, self._make_adapter_with_retries())
self._releases_json_data = {}
self.disable_progress_bar = disable_progress_bar

@staticmethod
def _make_adapter_with_retries():
Expand Down Expand Up @@ -140,7 +142,8 @@ def _upload(self, package):
encoder = MultipartEncoder(data_to_send)
with ProgressBar(total=encoder.len,
unit='B', unit_scale=True, unit_divisor=1024,
miniters=1, file=sys.stdout) as bar:
miniters=1, file=sys.stdout,
disable=self.disable_progress_bar) as bar:
monitor = MultipartEncoderMonitor(
encoder, lambda monitor: bar.update_to(monitor.bytes_read)
)
Expand Down
14 changes: 14 additions & 0 deletions twine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self,
cacert=None, client_cert=None,
repository_name='pypi', repository_url=None,
verbose=False,
disable_progress_bar=False,
**ignored_kwargs
):
"""Initialize our settings instance.
Expand Down Expand Up @@ -95,10 +96,15 @@ def __init__(self,
will override the settings inferred from ``repository_name``.
:param bool verbose:
Show verbose output.
:param bool disable_progress_bar:
Disable the progress bar.

This defaults to ``False``
"""
self.config_file = config_file
self.comment = comment
self.verbose = verbose
self.disable_progress_bar = disable_progress_bar
self.skip_existing = skip_existing
self._handle_repository_options(
repository_name=repository_name, repository_url=repository_url,
Expand Down Expand Up @@ -206,6 +212,13 @@ def register_argparse_arguments(parser):
action="store_true",
help="Show verbose output."
)
parser.add_argument(
"--disable-progress-bar",
default=False,
required=False,
action="store_true",
help="Disable the progress bar."
)

@classmethod
def from_argparse(cls, args):
Expand Down Expand Up @@ -276,6 +289,7 @@ def create_repository(self):
self.repository_config['repository'],
self.username,
self.password,
self.disable_progress_bar
)
repo.set_certificate_authority(self.cacert)
repo.set_client_certificate(self.client_cert)
Expand Down