Skip to content

Commit

Permalink
Add --skip-existing flag to upload command
Browse files Browse the repository at this point in the history
This new flag allows users to force twine to skip existing files when
uploading to PyPI. For example:

    ~/s/gh3.py git:develop ❯❯❯ twine upload --skip-existing dist/*
    Uploading distributions to https://pypi.python.org/pypi
    Uploading github3.py-1.0.0a2-py2.py3-none-any.whl
      Skipping github3.py-1.0.0a2-py2.py3-none-any.whl because it
    appears to already exist
    Uploading github3.py-1.0.0a2.tar.gz
      Skipping github3.py-1.0.0a2.tar.gz because it appears to
    already exist

Closes pypa#115
  • Loading branch information
sigmavirus24 committed Sep 13, 2015
1 parent 8b92e7d commit 6f7cbba
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
Binary file added tests/fixtures/twine-1.5.0-py2.py3-none-any.whl
Binary file not shown.
31 changes: 30 additions & 1 deletion tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import os
import textwrap

import mock
import pytest

from twine.commands import upload


WHEEL_FIXTURE = 'tests/fixtures/twine-1.5.0-py2.py3-none-any.whl'


def test_ensure_wheel_files_uploaded_first():
files = upload.group_wheel_files_first(["twine/foo.py",
"twine/first.whl",
Expand Down Expand Up @@ -72,11 +76,36 @@ def test_get_config_old_format(tmpdir):
try:
upload.upload(dists="foo", repository="pypi", sign=None, identity=None,
username=None, password=None, comment=None,
sign_with=None, config_file=pypirc)
sign_with=None, config_file=pypirc, skip_existing=False)
except KeyError as err:
assert err.args[0] == (
"Missing 'pypi' section from the configuration file.\n"
"Maybe you have a out-dated '{0}' format?\n"
"more info: "
"https://docs.python.org/distutils/packageindex.html#pypirc\n"
).format(pypirc)


def test_skip_existing_skips_files_already_on_PyPI(monkeypatch):
response = mock.Mock(spec=upload.requests.models.Response)
session = mock.Mock(spec=upload.requests.sessions.Session)

monkeypatch.setattr(upload.requests, 'session', lambda: session)
monkeypatch.setattr(upload.os.path, 'exists', lambda args: True)

session.post.return_value = response
response.status_code = 400
response.is_redirect = False

upload.upload(
dists=[WHEEL_FIXTURE],
repository="pypi",
sign=None,
identity=None,
username='username',
password='password',
comment=None,
sign_with=None,
config_file='',
skip_existing=True
)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ envlist = py26,py27,pypy,py32,py33,py34,docs,pep8
[testenv]
deps =
coverage
mock
pretend
pytest
commands =
Expand Down
17 changes: 14 additions & 3 deletions twine/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def find_dists(dists):


def upload(dists, repository, sign, identity, username, password, comment,
sign_with, config_file):
sign_with, config_file, skip_existing):
# Check that a nonsensical option wasn't given
if not sign and identity:
raise ValueError("sign must be given along with identity")
Expand Down Expand Up @@ -106,10 +106,15 @@ def upload(dists, repository, sign, identity, username, password, comment,
if resp.is_redirect:
raise exc.RedirectDetected(
('"{0}" attempted to redirect to "{1}" during upload.'
' Aborting...').format(config["respository"],
' Aborting...').format(config["repository"],
resp.headers["location"]))

# Otherwise, raise an HTTPError based on the status code.
resp.raise_for_status()
if resp.status_code == 400 and skip_existing:
print(" Skipping {0} because it appears to already exist".format(
basefilename))
else:
resp.raise_for_status()

# Bug 28. Try to silence a ResourceWarning by clearing the connection
# pool.
Expand Down Expand Up @@ -155,6 +160,12 @@ def main(args):
default="~/.pypirc",
help="The .pypirc config file to use",
)
parser.add_argument(
"--skip-existing",
default=False,
action="store_true",
help="Continue uploading files if one already exists",
)
parser.add_argument(
"dists",
nargs="+",
Expand Down

0 comments on commit 6f7cbba

Please sign in to comment.