Skip to content
Open
3 changes: 3 additions & 0 deletions stick/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
24 changes: 16 additions & 8 deletions stick/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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):
Expand Down Expand Up @@ -177,19 +181,23 @@ 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"""
version_prefix = '' if version is None else '/{0}'.format(version)
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"""
Expand Down
5 changes: 3 additions & 2 deletions stick/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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