# Path: bento/commands/register.py
# def _read_pypirc(repository):
#     filename = op.join(op.expanduser("~"), ".pypirc")
#     if not op.exists(filename):
#         raise bento.errors.UsageException(
#                 "file %r not found (automatic creation of .pypirc not supported yet" % filename)
#     try:
#         return read_pypirc(repository)
#     except bento.errors.InvalidPyPIConfig:
#         raise bento.errors.UsageException("repository %r not found in %r" % (repository, filename))
# 
# Path: bento/pypi/upload_utils.py
# def upload(dist_filename, dist_type, package, config, sign=False):
#     schema, netloc, url, params, query, fragments = urlparse(config.repository)
#     if params or query or fragments:
#         raise InvalidRepository("Incompatible url %s" % config.repository)
# 
#     if schema not in ('http', 'https'):
#         raise InvalidRepository("unsupported schema " + schema)
# 
#     if sign:
#         raise NotImplementedError()
# 
#     data = build_upload_post_data(dist_filename, dist_type, package)
#     userpass = (config.username + ":" + config.password).encode("ascii")
#     auth = six.b("Basic ") + base64.standard_b64encode(userpass)
#     request = build_request(config.repository, data, auth)
# 
#     try:
#         result = urlopen(request)
#         status = result.getcode()
#         reason = result.msg
#     except HTTPError:
#         e = extract_exception()
#         status = e.code
#         reason = e.msg
#     except URLError:
#         e = extract_exception()
#         reason = e.reason
#         raise PyPIError(
#                 "Could not upload to repository %r - error %s" \
#                 % (config.repository, reason))
# 
#     if status != 200:
#         raise PyPIError(
#                 "Could not upload to repository %r - error %s (server answered '%s')" \
#                 % (config.repository, status, reason))


from bento.commands.core \
    import \
        Command, Option
from bento.commands.register \
    import \
        _read_pypirc
from bento.pypi.register_utils \
    import \
        DEFAULT_REPOSITORY, PyPIConfig
from bento.pypi.upload_utils \
    import \
        upload
import bento.errors
    short_descr = "register packages to pypi."
    common_options = Command.common_options \
                        + [Option("-r", "--repository",
                                  help="Repository to use in .pypirc"),
                           Option("-u", "--username",
                                  help="Username to use for registration"),
                           Option("-p", "--password",
                                  help="Password to use for registration"),
                           Option("--repository-url",
                                  help="Repository URL to use for registration"),
                           Option("-t", "--distribution-type",
                                  help="Force distribution type (sdist, bdist_wininst, etc...)"),
                                  ]
    def run(self, context):
        o, a = context.get_parsed_arguments()
        if len(a) < 1:
            context.options_context.parser.print_usage()
            # FIXME
            raise NotImplementedError("expected file argument")
        else:
            filename = a[0]

        if o.repository and (o.username or o.password or o.repository_url):
            raise bento.errors.UsageException(
                    "Cannot specify repository and username/password/url at the same time")
        if not (o.repository or (o.username or o.password or o.repository_url)):
            # FIXME: why does distutils use DEFAULT_REPOSITORY (i.e. an url)
            # here ?
            config = _read_pypirc(DEFAULT_REPOSITORY)
        elif o.repository:

# Path: bento/commands/register.py
# def _read_pypirc(repository):
#     filename = op.join(op.expanduser("~"), ".pypirc")
#     if not op.exists(filename):
#         raise bento.errors.UsageException(
#                 "file %r not found (automatic creation of .pypirc not supported yet" % filename)
#     try:
#         return read_pypirc(repository)
#     except bento.errors.InvalidPyPIConfig:
#         raise bento.errors.UsageException("repository %r not found in %r" % (repository, filename))
# 
# Path: bento/pypi/upload_utils.py
# def upload(dist_filename, dist_type, package, config, sign=False):
#     schema, netloc, url, params, query, fragments = urlparse(config.repository)
#     if params or query or fragments:
#         raise InvalidRepository("Incompatible url %s" % config.repository)
# 
#     if schema not in ('http', 'https'):
#         raise InvalidRepository("unsupported schema " + schema)
# 
#     if sign:
#         raise NotImplementedError()
# 
#     data = build_upload_post_data(dist_filename, dist_type, package)
#     userpass = (config.username + ":" + config.password).encode("ascii")
#     auth = six.b("Basic ") + base64.standard_b64encode(userpass)
#     request = build_request(config.repository, data, auth)
# 
#     try:
#         result = urlopen(request)
#         status = result.getcode()
#         reason = result.msg
#     except HTTPError:
#         e = extract_exception()
#         status = e.code
#         reason = e.msg
#     except URLError:
#         e = extract_exception()
#         reason = e.reason
#         raise PyPIError(
#                 "Could not upload to repository %r - error %s" \
#                 % (config.repository, reason))
# 
#     if status != 200:
#         raise PyPIError(
#                 "Could not upload to repository %r - error %s (server answered '%s')" \
#                 % (config.repository, status, reason))


# Path: bento/commands/upload.py
from bento.commands.core \
    import \
        Command, Option
from bento.commands.register \
    import \
        _read_pypirc
from bento.pypi.register_utils \
    import \
        DEFAULT_REPOSITORY, PyPIConfig
from bento.pypi.upload_utils \
    import \
        upload
import bento.errors

    short_descr = "register packages to pypi."
    common_options = Command.common_options \
                        + [Option("-r", "--repository",
                                  help="Repository to use in .pypirc"),
                           Option("-u", "--username",
                                  help="Username to use for registration"),
                           Option("-p", "--password",
                                  help="Password to use for registration"),
                           Option("--repository-url",
                                  help="Repository URL to use for registration"),
                           Option("-t", "--distribution-type",
                                  help="Force distribution type (sdist, bdist_wininst, etc...)"),
                                  ]
    def run(self, context):
        o, a = context.get_parsed_arguments()
        if len(a) < 1:
            context.options_context.parser.print_usage()
            # FIXME
            raise NotImplementedError("expected file argument")
        else:
            filename = a[0]

        if o.repository and (o.username or o.password or o.repository_url):
            raise bento.errors.UsageException(
                    "Cannot specify repository and username/password/url at the same time")
        if not (o.repository or (o.username or o.password or o.repository_url)):
            # FIXME: why does distutils use DEFAULT_REPOSITORY (i.e. an url)
            # here ?
            config = _read_pypirc(DEFAULT_REPOSITORY)
        elif o.repository:

            config = _read_pypirc(o.repository)