Skip to content

Commit 642c6c8

Browse files
committed
Merge pull request #122 from sigmavirus24/upload-refactor
Upload refactor
2 parents 2b48128 + 53fab4d commit 642c6c8

9 files changed

Lines changed: 444 additions & 192 deletions

File tree

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
entry_points={
7070
"twine.registered_commands": [
7171
"upload = twine.commands.upload:main",
72+
"register = twine.commands.register:main",
7273
],
7374
"console_scripts": [
7475
"twine = twine.__main__:main",

tests/test_package.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2015 Ian Cordasco
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import unicode_literals
15+
from twine import package
16+
17+
import pretend
18+
19+
20+
def test_sign_file(monkeypatch):
21+
replaced_check_call = pretend.call_recorder(lambda args: None)
22+
monkeypatch.setattr(package.subprocess, 'check_call', replaced_check_call)
23+
filename = 'tests/fixtures/deprecated-pypirc'
24+
25+
pkg = package.PackageFile(
26+
filename=filename,
27+
comment=None,
28+
metadata=pretend.stub(name="deprecated-pypirc"),
29+
python_version=None,
30+
filetype=None
31+
)
32+
try:
33+
pkg.sign('gpg2', None)
34+
except IOError:
35+
pass
36+
args = ('gpg2', '--detach-sign', '-a', filename)
37+
assert replaced_check_call.calls == [pretend.call(args)]
38+
39+
40+
def test_sign_file_with_identity(monkeypatch):
41+
replaced_check_call = pretend.call_recorder(lambda args: None)
42+
monkeypatch.setattr(package.subprocess, 'check_call', replaced_check_call)
43+
filename = 'tests/fixtures/deprecated-pypirc'
44+
45+
pkg = package.PackageFile(
46+
filename=filename,
47+
comment=None,
48+
metadata=pretend.stub(name="deprecated-pypirc"),
49+
python_version=None,
50+
filetype=None
51+
)
52+
try:
53+
pkg.sign('gpg', 'identity')
54+
except IOError:
55+
pass
56+
args = ('gpg', '--detach-sign', '-a', filename, '--local-user', 'identity')
57+
assert replaced_check_call.calls == [pretend.call(args)]

tests/test_upload.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import os
1717
import textwrap
1818

19-
import pretend
2019
import pytest
2120

2221
from twine.commands import upload
@@ -60,25 +59,6 @@ def test_find_dists_handles_real_files():
6059
assert expected == files
6160

6261

63-
def test_sign_file(monkeypatch):
64-
replaced_check_call = pretend.call_recorder(lambda args: None)
65-
monkeypatch.setattr(upload.subprocess, 'check_call', replaced_check_call)
66-
67-
upload.sign_file('gpg2', 'my_file.tar.gz', None)
68-
args = ['gpg2', '--detach-sign', '-a', 'my_file.tar.gz']
69-
assert replaced_check_call.calls == [pretend.call(args)]
70-
71-
72-
def test_sign_file_with_identity(monkeypatch):
73-
replaced_check_call = pretend.call_recorder(lambda args: None)
74-
monkeypatch.setattr(upload.subprocess, 'check_call', replaced_check_call)
75-
76-
upload.sign_file('gpg', 'my_file.tar.gz', 'identity')
77-
args = ['gpg', '--detach-sign', '--local-user', 'identity', '-a',
78-
'my_file.tar.gz']
79-
assert replaced_check_call.calls == [pretend.call(args)]
80-
81-
8262
def test_get_config_old_format(tmpdir):
8363
pypirc = os.path.join(str(tmpdir), ".pypirc")
8464

twine/commands/register.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2015 Ian Cordasco
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import absolute_import, unicode_literals, print_function
15+
16+
import argparse
17+
import os.path
18+
import sys
19+
20+
from twine import exceptions as exc
21+
from twine.package import PackageFile
22+
from twine.repository import Repository
23+
from twine import utils
24+
25+
26+
def register(package, repository, username, password, comment, config_file):
27+
config = utils.get_repository_from_config(config_file, repository)
28+
config["repository"] = utils.normalize_repository_url(
29+
config["repository"]
30+
)
31+
32+
print("Registering package to {0}".format(config["repository"]))
33+
34+
username = utils.get_username(username, config)
35+
password = utils.get_password(password, config)
36+
37+
repository = Repository(config["repository"], username, password)
38+
39+
if not os.path.exists(package):
40+
raise exc.PackageNotFound(
41+
'"{0}" does not exist on the file system.'.format(package)
42+
)
43+
44+
resp = repository.register(PackageFile.from_filename(package, comment))
45+
repository.close()
46+
47+
if resp.is_redirect:
48+
raise exc.RedirectDetected(
49+
('"{0}" attempted to redirect to "{1}" during upload.'
50+
' Aborting...').format(config["respository"],
51+
resp.headers["location"]))
52+
53+
resp.raise_for_status()
54+
55+
56+
def main(args):
57+
parser = argparse.ArgumentParser(prog="twine register")
58+
parser.add_argument(
59+
"-r", "--repository",
60+
default="pypi",
61+
help="The repository to register the package to (default: "
62+
"%(default)s)",
63+
)
64+
parser.add_argument(
65+
"-u", "--username",
66+
help="The username to authenticate to the repository as",
67+
)
68+
parser.add_argument(
69+
"-p", "--password",
70+
help="The password to authenticate to the repository with",
71+
)
72+
parser.add_argument(
73+
"-c", "--comment",
74+
help="The comment to include with the distribution file",
75+
)
76+
parser.add_argument(
77+
"--config-file",
78+
default="~/.pypirc",
79+
help="The .pypirc config file to use",
80+
)
81+
parser.add_argument(
82+
"package",
83+
metavar="package",
84+
help="File from which we read the package metadata",
85+
)
86+
87+
args = parser.parse_args(args)
88+
89+
# Call the register function with the args from the command line
90+
try:
91+
register(**vars(args))
92+
except Exception as exc:
93+
sys.exit("{exc.__class__.__name__}: {exc}".format(exc=exc))

0 commit comments

Comments
 (0)