Skip to content

Commit

Permalink
Upgrade to boto3
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Cressey committed Jun 27, 2016
1 parent 896c050 commit dafb0fe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
9 changes: 7 additions & 2 deletions s3pypi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ def main():
p = argparse.ArgumentParser(prog=__prog__, version=__version__)
p.add_argument('--bucket', required=True, help='S3 bucket')
p.add_argument('--secret', help='S3 secret')
p.add_argument('--no-wheel', dest='wheel', action='store_false', help='Skip wheel distribution')
p.add_argument('--url', help='Custom URL', default=None)
p.add_argument(
'--no-wheel',
dest='wheel',
action='store_false',
help='Skip wheel distribution')
args = p.parse_args()

package = Package.create(args.wheel)
storage = S3Storage(args.bucket, args.secret)
storage = S3Storage(args.bucket, args.secret, args.url)

index = storage.get_index(package)
index.packages.discard(package)
Expand Down
16 changes: 12 additions & 4 deletions s3pypi/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def create(wheel=True):
except CalledProcessError as e:
raise RuntimeError(e.output.rstrip())

match = re.search('^making hard links in (.+)\.\.\.$', stdout, flags=re.MULTILINE)
match = re.search(
'^making hard links in (.+)\.\.\.$',
stdout,
flags=re.MULTILINE)

if not match:
raise RuntimeError(stdout)
Expand All @@ -65,7 +68,10 @@ def create(wheel=True):
class Index(object):
"""Index containing URLs to all versions of a package, to be rendered to HTML."""

template = Environment(loader=PackageLoader(__prog__, 'templates')).get_template('index.html.j2')
template = Environment(
loader=PackageLoader(
__prog__,
'templates')).get_template('index.html.j2')

def __init__(self, url, packages):
self.packages = set(packages)
Expand All @@ -78,7 +84,9 @@ def parse(url, html):
for match in re.findall('<a href=".+/((.+?-\d+\.\d+\.\d+).+)">', html):
filenames[match[1]].add(match[0])

return Index(url, (Package(name, files) for name, files in filenames.iteritems()))
return Index(url, (Package(name, files)
for name, files in filenames.iteritems()))

def to_html(self):
return self.template.render({'url': self.url, 'packages': self.packages})
return self.template.render(
{'url': self.url, 'packages': self.packages})
44 changes: 24 additions & 20 deletions s3pypi/storage.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os

import boto
from boto.exception import S3ResponseError
from boto.s3.key import Key
import boto3

from s3pypi.package import Index

Expand All @@ -14,35 +12,41 @@
class S3Storage(object):
"""Abstraction for storing package archives and index files in an S3 bucket."""

def __init__(self, bucket, secret=None):
self.bucket = boto.connect_s3().get_bucket(bucket)
def __init__(self, bucket, secret=None, url=None):
self.s3 = boto3.client('s3')
self.bucket = bucket
self.secret = secret

self.url = 'http://' + self.bucket.get_website_endpoint()
if secret:
self.url += '/' + secret
self.url = url

def _key(self, package, filename):
path = '%s/%s' % (package.name, filename)
return Key(self.bucket, '%s/%s' % (self.secret, path) if self.secret else path)
return self.s3.get_object(
Bucket=self.bucket,
Key=path)

def get_index(self, package):
try:
html = self._key(package, 'index.html').get_contents_as_string()
html = self._key(package, 'index.html')['Body']
return Index.parse(self.url, html)
except S3ResponseError:
except Exception:
return Index(self.url, [])

def put_index(self, package, index):
k = self._key(package, 'index.html')
k.set_metadata('Content-Type', 'text/html')
k.set_metadata('Cache-Control', 'public, must-revalidate, proxy-revalidate, max-age=0')
k.set_contents_from_string(index.to_html())
k.set_acl('public-read')
self.s3.put_object(
ACL='private',
Body=index.to_html(),
Bucket=self.bucket,
ContentType='text/html',
Key='{}/{}'.format(package.name, 'index.html')
)

def put_package(self, package):
for filename in package.files:
k = self._key(package, filename)
k.set_metadata('Content-Type', 'application/x-gzip')
k.set_contents_from_filename(os.path.join('dist', filename))
k.set_acl('public-read')
self.s3.put_object(
ACL='private',
Body=os.path.join('dist', filename),
Bucket=self.bucket,
ContentType='application/x-gzip',
Key='{}/{}'.format(package.name, filename)
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
packages=find_packages(),
package_data={__prog__: ['templates/*.j2']},

install_requires=['boto', 'Jinja2', 'wheel'],
install_requires=['boto3', 'Jinja2', 'wheel'],
entry_points={'console_scripts': ['{0}={0}.cli:main'.format(__prog__)]},
)

0 comments on commit dafb0fe

Please sign in to comment.