diff --git a/stick/commands.py b/stick/commands.py index 19bfab8..822cbe7 100644 --- a/stick/commands.py +++ b/stick/commands.py @@ -31,6 +31,7 @@ def cli(): @cli.command(context_settings={'max_content_width': 120, 'ignore_unknown_options': True}) @click.option('--bucket', help='S3 Bucket hosting the repository.', required=True) +@click.option('--url', help='Override the S3-based repository URL.', default=None) @click.option('--prefix', help='Prefix within the S3 Bucket that repository objects are stored.', default='simple', show_default=True) @click.option('--profile', help='Use a specific profile from your credential file to access S3.', default=None) @click.option('--skip-existing/--no-skip-existing', help='Skip uploading file if it already exists.', default=True, show_default=True) @@ -71,6 +72,7 @@ def upload(dist, **kwargs): @cli.command(context_settings={'max_content_width': 120}) @click.option('--bucket', help='S3 Bucket hosting the repository.', required=True) +@click.option('--url', help='Override the S3-based repository URL.', default=None) @click.option('--prefix', help='Prefix within the S3 Bucket that repository objects are stored.', default='simple', show_default=True) @click.option('--profile', help='Use a specific profile from your credential file to access S3.', default=None) @click.option('--project', help='Reindex a specific project. May be specified multiple times. [default: all projects]', default=None, multiple=True) @@ -87,6 +89,7 @@ def reindex(project, **kwargs): @cli.command(context_settings={'max_content_width': 120}) @click.option('--bucket', help='S3 Bucket hosting the repository.', required=True) +@click.option('--url', help='Override the S3-based repository URL.', default=None) @click.option('--prefix', help='Prefix within the S3 Bucket that repository objects are stored.', default='simple', show_default=True) @click.option('--profile', help='Use a specific profile from your credential file to access S3.', default=None) @click.option('--project', help='Check a specific project. May be specified multiple times. [default: all projects]', default=None, multiple=True) diff --git a/stick/repository.py b/stick/repository.py index ae2cd14..e6e4ad6 100644 --- a/stick/repository.py +++ b/stick/repository.py @@ -19,8 +19,9 @@ class Repository(object): - def __init__(self, bucket, prefix, profile): + def __init__(self, bucket, url, prefix, profile): self.bucket = bucket + self.url = url self.prefix = prefix self.client = boto3.Session(profile_name=profile).client('s3', config=client_config) self._project_cache = {} @@ -29,7 +30,10 @@ def __init__(self, bucket, prefix, profile): self.prefix += '/' def get_url(self): - url = 'https://{0}.s3.amazonaws.com/{1}'.format(self.bucket, self.prefix) + if self.url is None: + url = 'https://{0}.s3.amazonaws.com/{1}'.format(self.bucket, self.prefix) + else: + url = '{0}/{1}'.format(self.url, self.prefix) return url def reindex(self, projects): @@ -177,9 +181,11 @@ def _put_manifest(self, safe_name, project): json_key = '{0}{1}/manifest.json'.format(self.prefix, safe_name) logger.info('Uploading {0}'.format(json_key)) with io.BytesIO() as data: - json.dump(project.get_manifest(), codecs.getwriter('utf-8')(data)) - data.seek(0, 0) - return self.client.put_object(Body=data, Bucket=self.bucket, Key=json_key, ContentType='application/json; charset=utf-8') + with io.TextIOWrapper(data, encoding='utf-8') as text: + json.dump(project.get_manifest(), text) + text.flush() + data.seek(0, 0) + return self.client.put_object(Body=data, Bucket=self.bucket, Key=json_key, ContentType='application/json; charset=utf-8') def _put_json(self, safe_name, project, version=None): """Regenerate and upload the project or release-level index JSON""" @@ -187,9 +193,11 @@ def _put_json(self, safe_name, project, version=None): json_key = '{0}{1}{2}/json'.format(self.prefix, safe_name, version_prefix) logger.info('Uploading {0}'.format(json_key)) with io.BytesIO() as data: - json.dump(project.get_metadata(version), codecs.getwriter('utf-8')(data)) - data.seek(0, 0) - return self.client.put_object(Body=data, Bucket=self.bucket, Key=json_key, ContentType='application/json; charset=utf-8') + with io.TextIOWrapper(data, encoding='utf-8') as text: + json.dump(project.get_metadata(version), text) + text.flush() + data.seek(0, 0) + return self.client.put_object(Body=data, Bucket=self.bucket, Key=json_key, ContentType='application/json; charset=utf-8') def _put_index(self, safe_name, project): """Regenerate and upload the project-level index HTML""" diff --git a/stick/settings.py b/stick/settings.py index 9f8a228..f06270b 100644 --- a/stick/settings.py +++ b/stick/settings.py @@ -2,8 +2,9 @@ class Settings(object): - def __init__(self, bucket, prefix, profile=None, skip_existing=True, sign=False, sign_with='gpg', identity=None): + def __init__(self, bucket, url, prefix, profile=None, skip_existing=True, sign=False, sign_with='gpg', identity=None): self.bucket = bucket + self.url = url self.prefix = prefix self.profile = profile self.skip_existing = skip_existing @@ -12,5 +13,5 @@ def __init__(self, bucket, prefix, profile=None, skip_existing=True, sign=False, self.identity = identity def create_repository(self): - repo = Repository(self.bucket, self.prefix, self.profile) + repo = Repository(self.bucket, self.url, self.prefix, self.profile) return repo