BaySec website source
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.2KB

  1. #from: https://gist.github.com/evz/3849704
  2. from hyde.publisher import Publisher
  3. from boto.s3.connection import S3Connection
  4. from boto.s3.key import Key
  5. import os
  6. class S3CredentialsError(Exception):
  7. def __init__(self, value):
  8. self.value = value
  9. def __str__(self):
  10. return repr(self.value)
  11. class S3(Publisher):
  12. def initialize(self, settings):
  13. self.settings = settings
  14. self.key = getattr(settings, 'key', None)
  15. self.secret = getattr(settings, 'secret', None)
  16. self.bucket = getattr(settings, 'bucket', None)
  17. reds = getattr(settings, 'redirects', None)
  18. self.redirects = []
  19. if reds:
  20. for redirect in reds:
  21. f, t = redirect.split(' => ')
  22. self.redirects.append({'from': f, 'to': t})
  23. if not self.key or not self.secret:
  24. try:
  25. self.key = os.environ['AWS_ACCESS_KEY_ID']
  26. self.secret = os.environ['AWS_SECRET_ACCESS_KEY']
  27. except KeyError:
  28. raise S3CredentialsError('You need to define both an AWS key and secret in your site.yaml or as environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY')
  29. def publish(self):
  30. super(S3, self).publish()
  31. conn = S3Connection(self.key, self.secret)
  32. bucket = conn.get_bucket(self.bucket)
  33. k = Key(bucket)
  34. root = self.site.config.deploy_root_path
  35. for item in root.walker.walk_files():
  36. redirect_meta = None
  37. if self.redirects:
  38. for redirect in self.redirects:
  39. if redirect['from'] in item.path:
  40. redirect_meta = redirect['to']
  41. k.key = item.path.replace(str(root), '')
  42. kmd5 = bucket.get_key(item.path.replace(str(root), ''))
  43. #print 'f:', `item.path`, `kmd5.etag[1:-1], k.compute_md5(open(item.path))[0]`
  44. if kmd5 is None or \
  45. kmd5.etag[1:-1] != k.compute_md5(open(item.path))[0]:
  46. print 'updating:', `item.path.replace(str(root), '')`
  47. k.set_contents_from_filename(item.path)
  48. if redirect_meta:
  49. k.set_metadata('website-redirect-location', redirect_meta)