|
| 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