Mercurial > repos > bcclaywell > argo_navis
annotate venv/lib/python2.7/site-packages/boto/utils.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
| author | bcclaywell | 
|---|---|
| date | Mon, 12 Oct 2015 17:43:33 -0400 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 0 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1 # Copyright (c) 2006-2012 Mitch Garnaat http://garnaat.org/ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 2 # Copyright (c) 2010, Eucalyptus Systems, Inc. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 3 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 4 # All rights reserved. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 5 # | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 6 # Permission is hereby granted, free of charge, to any person obtaining a | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 7 # copy of this software and associated documentation files (the | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 8 # "Software"), to deal in the Software without restriction, including | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 9 # without limitation the rights to use, copy, modify, merge, publish, dis- | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 10 # tribute, sublicense, and/or sell copies of the Software, and to permit | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 11 # persons to whom the Software is furnished to do so, subject to the fol- | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 12 # lowing conditions: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 13 # | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 14 # The above copyright notice and this permission notice shall be included | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 15 # in all copies or substantial portions of the Software. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 16 # | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 18 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 19 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 20 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 21 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 23 # IN THE SOFTWARE. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 24 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 25 # | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 26 # Parts of this code were copied or derived from sample code supplied by AWS. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 27 # The following notice applies to that code. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 28 # | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 29 # This software code is made available "AS IS" without warranties of any | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 30 # kind. You may copy, display, modify and redistribute the software | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 31 # code either by itself or as incorporated into your code; provided that | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 32 # you do not remove any proprietary notices. Your use of this software | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 33 # code is at your own risk and you waive any claim against Amazon | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 34 # Digital Services, Inc. or its affiliates with respect to your use of | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 35 # this software code. (c) 2006 Amazon Digital Services, Inc. or its | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 36 # affiliates. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 37 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 38 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 39 Some handy utility functions used by several classes. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 40 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 41 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 42 import subprocess | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 43 import time | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 44 import logging.handlers | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 45 import boto | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 46 import boto.provider | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 47 import tempfile | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 48 import random | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 49 import smtplib | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 50 import datetime | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 51 import re | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 52 import email.mime.multipart | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 53 import email.mime.base | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 54 import email.mime.text | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 55 import email.utils | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 56 import email.encoders | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 57 import gzip | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 58 import threading | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 59 import locale | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 60 from boto.compat import six, StringIO, urllib, encodebytes | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 61 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 62 from contextlib import contextmanager | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 63 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 64 from hashlib import md5, sha512 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 65 _hashfn = sha512 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 66 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 67 from boto.compat import json | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 68 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 69 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 70 from boto.compat.json import JSONDecodeError | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 71 except ImportError: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 72 JSONDecodeError = ValueError | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 73 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 74 # List of Query String Arguments of Interest | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 75 qsa_of_interest = ['acl', 'cors', 'defaultObjectAcl', 'location', 'logging', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 76 'partNumber', 'policy', 'requestPayment', 'torrent', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 77 'versioning', 'versionId', 'versions', 'website', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 78 'uploads', 'uploadId', 'response-content-type', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 79 'response-content-language', 'response-expires', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 80 'response-cache-control', 'response-content-disposition', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 81 'response-content-encoding', 'delete', 'lifecycle', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 82 'tagging', 'restore', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 83 # storageClass is a QSA for buckets in Google Cloud Storage. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 84 # (StorageClass is associated to individual keys in S3, but | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 85 # having it listed here should cause no problems because | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 86 # GET bucket?storageClass is not part of the S3 API.) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 87 'storageClass', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 88 # websiteConfig is a QSA for buckets in Google Cloud | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 89 # Storage. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 90 'websiteConfig', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 91 # compose is a QSA for objects in Google Cloud Storage. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 92 'compose'] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 93 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 94 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 95 _first_cap_regex = re.compile('(.)([A-Z][a-z]+)') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 96 _number_cap_regex = re.compile('([a-z])([0-9]+)') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 97 _end_cap_regex = re.compile('([a-z0-9])([A-Z])') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 98 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 99 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 100 def unquote_v(nv): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 101 if len(nv) == 1: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 102 return nv | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 103 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 104 return (nv[0], urllib.parse.unquote(nv[1])) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 105 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 106 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 107 def canonical_string(method, path, headers, expires=None, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 108 provider=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 109 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 110 Generates the aws canonical string for the given parameters | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 111 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 112 if not provider: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 113 provider = boto.provider.get_default() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 114 interesting_headers = {} | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 115 for key in headers: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 116 lk = key.lower() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 117 if headers[key] is not None and \ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 118 (lk in ['content-md5', 'content-type', 'date'] or | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 119 lk.startswith(provider.header_prefix)): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 120 interesting_headers[lk] = str(headers[key]).strip() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 121 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 122 # these keys get empty strings if they don't exist | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 123 if 'content-type' not in interesting_headers: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 124 interesting_headers['content-type'] = '' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 125 if 'content-md5' not in interesting_headers: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 126 interesting_headers['content-md5'] = '' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 127 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 128 # just in case someone used this. it's not necessary in this lib. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 129 if provider.date_header in interesting_headers: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 130 interesting_headers['date'] = '' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 131 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 132 # if you're using expires for query string auth, then it trumps date | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 133 # (and provider.date_header) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 134 if expires: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 135 interesting_headers['date'] = str(expires) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 136 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 137 sorted_header_keys = sorted(interesting_headers.keys()) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 138 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 139 buf = "%s\n" % method | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 140 for key in sorted_header_keys: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 141 val = interesting_headers[key] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 142 if key.startswith(provider.header_prefix): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 143 buf += "%s:%s\n" % (key, val) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 144 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 145 buf += "%s\n" % val | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 146 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 147 # don't include anything after the first ? in the resource... | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 148 # unless it is one of the QSA of interest, defined above | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 149 t = path.split('?') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 150 buf += t[0] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 151 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 152 if len(t) > 1: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 153 qsa = t[1].split('&') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 154 qsa = [a.split('=', 1) for a in qsa] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 155 qsa = [unquote_v(a) for a in qsa if a[0] in qsa_of_interest] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 156 if len(qsa) > 0: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 157 qsa.sort(key=lambda x: x[0]) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 158 qsa = ['='.join(a) for a in qsa] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 159 buf += '?' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 160 buf += '&'.join(qsa) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 161 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 162 return buf | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 163 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 164 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 165 def merge_meta(headers, metadata, provider=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 166 if not provider: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 167 provider = boto.provider.get_default() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 168 metadata_prefix = provider.metadata_prefix | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 169 final_headers = headers.copy() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 170 for k in metadata.keys(): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 171 if k.lower() in boto.s3.key.Key.base_user_settable_fields: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 172 final_headers[k] = metadata[k] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 173 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 174 final_headers[metadata_prefix + k] = metadata[k] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 175 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 176 return final_headers | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 177 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 178 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 179 def get_aws_metadata(headers, provider=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 180 if not provider: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 181 provider = boto.provider.get_default() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 182 metadata_prefix = provider.metadata_prefix | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 183 metadata = {} | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 184 for hkey in headers.keys(): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 185 if hkey.lower().startswith(metadata_prefix): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 186 val = urllib.parse.unquote(headers[hkey]) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 187 if isinstance(val, bytes): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 188 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 189 val = val.decode('utf-8') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 190 except UnicodeDecodeError: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 191 # Just leave the value as-is | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 192 pass | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 193 metadata[hkey[len(metadata_prefix):]] = val | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 194 del headers[hkey] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 195 return metadata | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 196 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 197 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 198 def retry_url(url, retry_on_404=True, num_retries=10, timeout=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 199 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 200 Retry a url. This is specifically used for accessing the metadata | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 201 service on an instance. Since this address should never be proxied | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 202 (for security reasons), we create a ProxyHandler with a NULL | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 203 dictionary to override any proxy settings in the environment. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 204 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 205 for i in range(0, num_retries): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 206 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 207 proxy_handler = urllib.request.ProxyHandler({}) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 208 opener = urllib.request.build_opener(proxy_handler) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 209 req = urllib.request.Request(url) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 210 r = opener.open(req, timeout=timeout) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 211 result = r.read() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 212 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 213 if(not isinstance(result, six.string_types) and | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 214 hasattr(result, 'decode')): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 215 result = result.decode('utf-8') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 216 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 217 return result | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 218 except urllib.error.HTTPError as e: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 219 code = e.getcode() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 220 if code == 404 and not retry_on_404: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 221 return '' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 222 except Exception as e: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 223 pass | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 224 boto.log.exception('Caught exception reading instance data') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 225 # If not on the last iteration of the loop then sleep. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 226 if i + 1 != num_retries: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 227 time.sleep(min(2 ** i, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 228 boto.config.get('Boto', 'max_retry_delay', 60))) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 229 boto.log.error('Unable to read instance data, giving up') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 230 return '' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 231 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 232 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 233 def _get_instance_metadata(url, num_retries, timeout=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 234 return LazyLoadMetadata(url, num_retries, timeout) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 235 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 236 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 237 class LazyLoadMetadata(dict): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 238 def __init__(self, url, num_retries, timeout=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 239 self._url = url | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 240 self._num_retries = num_retries | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 241 self._leaves = {} | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 242 self._dicts = [] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 243 self._timeout = timeout | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 244 data = boto.utils.retry_url(self._url, num_retries=self._num_retries, timeout=self._timeout) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 245 if data: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 246 fields = data.split('\n') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 247 for field in fields: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 248 if field.endswith('/'): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 249 key = field[0:-1] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 250 self._dicts.append(key) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 251 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 252 p = field.find('=') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 253 if p > 0: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 254 key = field[p + 1:] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 255 resource = field[0:p] + '/openssh-key' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 256 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 257 key = resource = field | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 258 self._leaves[key] = resource | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 259 self[key] = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 260 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 261 def _materialize(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 262 for key in self: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 263 self[key] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 264 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 265 def __getitem__(self, key): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 266 if key not in self: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 267 # allow dict to throw the KeyError | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 268 return super(LazyLoadMetadata, self).__getitem__(key) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 269 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 270 # already loaded | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 271 val = super(LazyLoadMetadata, self).__getitem__(key) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 272 if val is not None: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 273 return val | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 274 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 275 if key in self._leaves: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 276 resource = self._leaves[key] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 277 last_exception = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 278 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 279 for i in range(0, self._num_retries): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 280 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 281 val = boto.utils.retry_url( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 282 self._url + urllib.parse.quote(resource, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 283 safe="/:"), | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 284 num_retries=self._num_retries, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 285 timeout=self._timeout) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 286 if val and val[0] == '{': | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 287 val = json.loads(val) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 288 break | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 289 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 290 p = val.find('\n') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 291 if p > 0: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 292 val = val.split('\n') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 293 break | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 294 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 295 except JSONDecodeError as e: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 296 boto.log.debug( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 297 "encountered '%s' exception: %s" % ( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 298 e.__class__.__name__, e)) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 299 boto.log.debug( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 300 'corrupted JSON data found: %s' % val) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 301 last_exception = e | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 302 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 303 except Exception as e: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 304 boto.log.debug("encountered unretryable" + | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 305 " '%s' exception, re-raising" % ( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 306 e.__class__.__name__)) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 307 last_exception = e | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 308 raise | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 309 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 310 boto.log.error("Caught exception reading meta data" + | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 311 " for the '%s' try" % (i + 1)) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 312 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 313 if i + 1 != self._num_retries: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 314 next_sleep = min( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 315 random.random() * 2 ** i, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 316 boto.config.get('Boto', 'max_retry_delay', 60)) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 317 time.sleep(next_sleep) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 318 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 319 boto.log.error('Unable to read meta data, giving up') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 320 boto.log.error( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 321 "encountered '%s' exception: %s" % ( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 322 last_exception.__class__.__name__, last_exception)) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 323 raise last_exception | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 324 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 325 self[key] = val | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 326 elif key in self._dicts: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 327 self[key] = LazyLoadMetadata(self._url + key + '/', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 328 self._num_retries) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 329 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 330 return super(LazyLoadMetadata, self).__getitem__(key) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 331 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 332 def get(self, key, default=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 333 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 334 return self[key] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 335 except KeyError: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 336 return default | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 337 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 338 def values(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 339 self._materialize() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 340 return super(LazyLoadMetadata, self).values() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 341 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 342 def items(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 343 self._materialize() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 344 return super(LazyLoadMetadata, self).items() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 345 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 346 def __str__(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 347 self._materialize() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 348 return super(LazyLoadMetadata, self).__str__() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 349 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 350 def __repr__(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 351 self._materialize() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 352 return super(LazyLoadMetadata, self).__repr__() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 353 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 354 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 355 def _build_instance_metadata_url(url, version, path): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 356 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 357 Builds an EC2 metadata URL for fetching information about an instance. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 358 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 359 Example: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 360 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 361 >>> _build_instance_metadata_url('http://169.254.169.254', 'latest', 'meta-data/') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 362 http://169.254.169.254/latest/meta-data/ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 363 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 364 :type url: string | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 365 :param url: URL to metadata service, e.g. 'http://169.254.169.254' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 366 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 367 :type version: string | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 368 :param version: Version of the metadata to get, e.g. 'latest' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 369 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 370 :type path: string | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 371 :param path: Path of the metadata to get, e.g. 'meta-data/'. If a trailing | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 372 slash is required it must be passed in with the path. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 373 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 374 :return: The full metadata URL | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 375 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 376 return '%s/%s/%s' % (url, version, path) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 377 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 378 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 379 def get_instance_metadata(version='latest', url='http://169.254.169.254', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 380 data='meta-data/', timeout=None, num_retries=5): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 381 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 382 Returns the instance metadata as a nested Python dictionary. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 383 Simple values (e.g. local_hostname, hostname, etc.) will be | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 384 stored as string values. Values such as ancestor-ami-ids will | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 385 be stored in the dict as a list of string values. More complex | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 386 fields such as public-keys and will be stored as nested dicts. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 387 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 388 If the timeout is specified, the connection to the specified url | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 389 will time out after the specified number of seconds. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 390 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 391 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 392 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 393 metadata_url = _build_instance_metadata_url(url, version, data) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 394 return _get_instance_metadata(metadata_url, num_retries=num_retries, timeout=timeout) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 395 except urllib.error.URLError: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 396 return None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 397 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 398 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 399 def get_instance_identity(version='latest', url='http://169.254.169.254', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 400 timeout=None, num_retries=5): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 401 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 402 Returns the instance identity as a nested Python dictionary. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 403 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 404 iid = {} | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 405 base_url = _build_instance_metadata_url(url, version, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 406 'dynamic/instance-identity/') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 407 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 408 data = retry_url(base_url, num_retries=num_retries, timeout=timeout) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 409 fields = data.split('\n') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 410 for field in fields: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 411 val = retry_url(base_url + '/' + field + '/', num_retries=num_retries, timeout=timeout) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 412 if val[0] == '{': | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 413 val = json.loads(val) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 414 if field: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 415 iid[field] = val | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 416 return iid | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 417 except urllib.error.URLError: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 418 return None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 419 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 420 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 421 def get_instance_userdata(version='latest', sep=None, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 422 url='http://169.254.169.254', timeout=None, num_retries=5): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 423 ud_url = _build_instance_metadata_url(url, version, 'user-data') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 424 user_data = retry_url(ud_url, retry_on_404=False, num_retries=num_retries, timeout=timeout) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 425 if user_data: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 426 if sep: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 427 l = user_data.split(sep) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 428 user_data = {} | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 429 for nvpair in l: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 430 t = nvpair.split('=') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 431 user_data[t[0].strip()] = t[1].strip() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 432 return user_data | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 433 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 434 ISO8601 = '%Y-%m-%dT%H:%M:%SZ' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 435 ISO8601_MS = '%Y-%m-%dT%H:%M:%S.%fZ' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 436 RFC1123 = '%a, %d %b %Y %H:%M:%S %Z' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 437 LOCALE_LOCK = threading.Lock() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 438 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 439 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 440 @contextmanager | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 441 def setlocale(name): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 442 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 443 A context manager to set the locale in a threadsafe manner. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 444 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 445 with LOCALE_LOCK: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 446 saved = locale.setlocale(locale.LC_ALL) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 447 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 448 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 449 yield locale.setlocale(locale.LC_ALL, name) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 450 finally: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 451 locale.setlocale(locale.LC_ALL, saved) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 452 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 453 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 454 def get_ts(ts=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 455 if not ts: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 456 ts = time.gmtime() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 457 return time.strftime(ISO8601, ts) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 458 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 459 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 460 def parse_ts(ts): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 461 with setlocale('C'): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 462 ts = ts.strip() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 463 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 464 dt = datetime.datetime.strptime(ts, ISO8601) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 465 return dt | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 466 except ValueError: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 467 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 468 dt = datetime.datetime.strptime(ts, ISO8601_MS) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 469 return dt | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 470 except ValueError: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 471 dt = datetime.datetime.strptime(ts, RFC1123) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 472 return dt | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 473 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 474 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 475 def find_class(module_name, class_name=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 476 if class_name: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 477 module_name = "%s.%s" % (module_name, class_name) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 478 modules = module_name.split('.') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 479 c = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 480 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 481 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 482 for m in modules[1:]: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 483 if c: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 484 c = getattr(c, m) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 485 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 486 c = getattr(__import__(".".join(modules[0:-1])), m) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 487 return c | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 488 except: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 489 return None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 490 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 491 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 492 def update_dme(username, password, dme_id, ip_address): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 493 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 494 Update your Dynamic DNS record with DNSMadeEasy.com | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 495 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 496 dme_url = 'https://www.dnsmadeeasy.com/servlet/updateip' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 497 dme_url += '?username=%s&password=%s&id=%s&ip=%s' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 498 s = urllib.request.urlopen(dme_url % (username, password, dme_id, ip_address)) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 499 return s.read() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 500 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 501 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 502 def fetch_file(uri, file=None, username=None, password=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 503 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 504 Fetch a file based on the URI provided. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 505 If you do not pass in a file pointer a tempfile.NamedTemporaryFile, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 506 or None if the file could not be retrieved is returned. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 507 The URI can be either an HTTP url, or "s3://bucket_name/key_name" | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 508 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 509 boto.log.info('Fetching %s' % uri) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 510 if file is None: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 511 file = tempfile.NamedTemporaryFile() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 512 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 513 if uri.startswith('s3://'): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 514 bucket_name, key_name = uri[len('s3://'):].split('/', 1) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 515 c = boto.connect_s3(aws_access_key_id=username, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 516 aws_secret_access_key=password) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 517 bucket = c.get_bucket(bucket_name) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 518 key = bucket.get_key(key_name) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 519 key.get_contents_to_file(file) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 520 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 521 if username and password: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 522 passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 523 passman.add_password(None, uri, username, password) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 524 authhandler = urllib.request.HTTPBasicAuthHandler(passman) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 525 opener = urllib.request.build_opener(authhandler) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 526 urllib.request.install_opener(opener) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 527 s = urllib.request.urlopen(uri) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 528 file.write(s.read()) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 529 file.seek(0) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 530 except: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 531 raise | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 532 boto.log.exception('Problem Retrieving file: %s' % uri) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 533 file = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 534 return file | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 535 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 536 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 537 class ShellCommand(object): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 538 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 539 def __init__(self, command, wait=True, fail_fast=False, cwd=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 540 self.exit_code = 0 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 541 self.command = command | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 542 self.log_fp = StringIO() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 543 self.wait = wait | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 544 self.fail_fast = fail_fast | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 545 self.run(cwd=cwd) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 546 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 547 def run(self, cwd=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 548 boto.log.info('running:%s' % self.command) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 549 self.process = subprocess.Popen(self.command, shell=True, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 550 stdin=subprocess.PIPE, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 551 stdout=subprocess.PIPE, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 552 stderr=subprocess.PIPE, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 553 cwd=cwd) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 554 if(self.wait): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 555 while self.process.poll() is None: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 556 time.sleep(1) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 557 t = self.process.communicate() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 558 self.log_fp.write(t[0]) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 559 self.log_fp.write(t[1]) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 560 boto.log.info(self.log_fp.getvalue()) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 561 self.exit_code = self.process.returncode | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 562 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 563 if self.fail_fast and self.exit_code != 0: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 564 raise Exception("Command " + self.command + | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 565 " failed with status " + self.exit_code) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 566 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 567 return self.exit_code | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 568 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 569 def setReadOnly(self, value): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 570 raise AttributeError | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 571 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 572 def getStatus(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 573 return self.exit_code | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 574 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 575 status = property(getStatus, setReadOnly, None, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 576 'The exit code for the command') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 577 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 578 def getOutput(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 579 return self.log_fp.getvalue() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 580 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 581 output = property(getOutput, setReadOnly, None, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 582 'The STDIN and STDERR output of the command') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 583 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 584 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 585 class AuthSMTPHandler(logging.handlers.SMTPHandler): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 586 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 587 This class extends the SMTPHandler in the standard Python logging module | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 588 to accept a username and password on the constructor and to then use those | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 589 credentials to authenticate with the SMTP server. To use this, you could | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 590 add something like this in your boto config file: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 591 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 592 [handler_hand07] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 593 class=boto.utils.AuthSMTPHandler | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 594 level=WARN | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 595 formatter=form07 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 596 args=('localhost', 'username', 'password', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 597 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 598 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 599 def __init__(self, mailhost, username, password, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 600 fromaddr, toaddrs, subject): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 601 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 602 Initialize the handler. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 603 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 604 We have extended the constructor to accept a username/password | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 605 for SMTP authentication. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 606 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 607 super(AuthSMTPHandler, self).__init__(mailhost, fromaddr, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 608 toaddrs, subject) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 609 self.username = username | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 610 self.password = password | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 611 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 612 def emit(self, record): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 613 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 614 Emit a record. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 615 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 616 Format the record and send it to the specified addressees. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 617 It would be really nice if I could add authorization to this class | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 618 without having to resort to cut and paste inheritance but, no. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 619 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 620 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 621 port = self.mailport | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 622 if not port: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 623 port = smtplib.SMTP_PORT | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 624 smtp = smtplib.SMTP(self.mailhost, port) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 625 smtp.login(self.username, self.password) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 626 msg = self.format(record) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 627 msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % ( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 628 self.fromaddr, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 629 ','.join(self.toaddrs), | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 630 self.getSubject(record), | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 631 email.utils.formatdate(), msg) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 632 smtp.sendmail(self.fromaddr, self.toaddrs, msg) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 633 smtp.quit() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 634 except (KeyboardInterrupt, SystemExit): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 635 raise | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 636 except: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 637 self.handleError(record) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 638 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 639 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 640 class LRUCache(dict): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 641 """A dictionary-like object that stores only a certain number of items, and | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 642 discards its least recently used item when full. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 643 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 644 >>> cache = LRUCache(3) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 645 >>> cache['A'] = 0 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 646 >>> cache['B'] = 1 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 647 >>> cache['C'] = 2 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 648 >>> len(cache) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 649 3 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 650 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 651 >>> cache['A'] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 652 0 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 653 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 654 Adding new items to the cache does not increase its size. Instead, the least | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 655 recently used item is dropped: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 656 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 657 >>> cache['D'] = 3 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 658 >>> len(cache) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 659 3 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 660 >>> 'B' in cache | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 661 False | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 662 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 663 Iterating over the cache returns the keys, starting with the most recently | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 664 used: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 665 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 666 >>> for key in cache: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 667 ... print key | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 668 D | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 669 A | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 670 C | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 671 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 672 This code is based on the LRUCache class from Genshi which is based on | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 673 `Myghty <http://www.myghty.org>`_'s LRUCache from ``myghtyutils.util``, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 674 written by Mike Bayer and released under the MIT license (Genshi uses the | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 675 BSD License). | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 676 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 677 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 678 class _Item(object): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 679 def __init__(self, key, value): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 680 self.previous = self.next = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 681 self.key = key | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 682 self.value = value | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 683 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 684 def __repr__(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 685 return repr(self.value) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 686 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 687 def __init__(self, capacity): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 688 self._dict = dict() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 689 self.capacity = capacity | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 690 self.head = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 691 self.tail = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 692 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 693 def __contains__(self, key): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 694 return key in self._dict | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 695 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 696 def __iter__(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 697 cur = self.head | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 698 while cur: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 699 yield cur.key | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 700 cur = cur.next | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 701 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 702 def __len__(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 703 return len(self._dict) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 704 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 705 def __getitem__(self, key): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 706 item = self._dict[key] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 707 self._update_item(item) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 708 return item.value | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 709 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 710 def __setitem__(self, key, value): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 711 item = self._dict.get(key) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 712 if item is None: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 713 item = self._Item(key, value) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 714 self._dict[key] = item | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 715 self._insert_item(item) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 716 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 717 item.value = value | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 718 self._update_item(item) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 719 self._manage_size() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 720 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 721 def __repr__(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 722 return repr(self._dict) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 723 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 724 def _insert_item(self, item): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 725 item.previous = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 726 item.next = self.head | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 727 if self.head is not None: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 728 self.head.previous = item | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 729 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 730 self.tail = item | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 731 self.head = item | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 732 self._manage_size() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 733 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 734 def _manage_size(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 735 while len(self._dict) > self.capacity: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 736 del self._dict[self.tail.key] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 737 if self.tail != self.head: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 738 self.tail = self.tail.previous | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 739 self.tail.next = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 740 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 741 self.head = self.tail = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 742 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 743 def _update_item(self, item): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 744 if self.head == item: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 745 return | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 746 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 747 previous = item.previous | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 748 previous.next = item.next | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 749 if item.next is not None: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 750 item.next.previous = previous | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 751 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 752 self.tail = previous | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 753 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 754 item.previous = None | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 755 item.next = self.head | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 756 self.head.previous = self.head = item | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 757 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 758 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 759 class Password(object): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 760 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 761 Password object that stores itself as hashed. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 762 Hash defaults to SHA512 if available, MD5 otherwise. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 763 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 764 hashfunc = _hashfn | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 765 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 766 def __init__(self, str=None, hashfunc=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 767 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 768 Load the string from an initial value, this should be the | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 769 raw hashed password. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 770 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 771 self.str = str | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 772 if hashfunc: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 773 self.hashfunc = hashfunc | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 774 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 775 def set(self, value): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 776 if not isinstance(value, bytes): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 777 value = value.encode('utf-8') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 778 self.str = self.hashfunc(value).hexdigest() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 779 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 780 def __str__(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 781 return str(self.str) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 782 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 783 def __eq__(self, other): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 784 if other is None: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 785 return False | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 786 if not isinstance(other, bytes): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 787 other = other.encode('utf-8') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 788 return str(self.hashfunc(other).hexdigest()) == str(self.str) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 789 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 790 def __len__(self): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 791 if self.str: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 792 return len(self.str) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 793 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 794 return 0 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 795 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 796 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 797 def notify(subject, body=None, html_body=None, to_string=None, | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 798 attachments=None, append_instance_id=True): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 799 attachments = attachments or [] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 800 if append_instance_id: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 801 subject = "[%s] %s" % ( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 802 boto.config.get_value("Instance", "instance-id"), subject) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 803 if not to_string: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 804 to_string = boto.config.get_value('Notification', 'smtp_to', None) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 805 if to_string: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 806 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 807 from_string = boto.config.get_value('Notification', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 808 'smtp_from', 'boto') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 809 msg = email.mime.multipart.MIMEMultipart() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 810 msg['From'] = from_string | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 811 msg['Reply-To'] = from_string | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 812 msg['To'] = to_string | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 813 msg['Date'] = email.utils.formatdate(localtime=True) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 814 msg['Subject'] = subject | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 815 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 816 if body: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 817 msg.attach(email.mime.text.MIMEText(body)) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 818 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 819 if html_body: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 820 part = email.mime.base.MIMEBase('text', 'html') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 821 part.set_payload(html_body) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 822 email.encoders.encode_base64(part) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 823 msg.attach(part) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 824 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 825 for part in attachments: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 826 msg.attach(part) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 827 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 828 smtp_host = boto.config.get_value('Notification', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 829 'smtp_host', 'localhost') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 830 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 831 # Alternate port support | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 832 if boto.config.get_value("Notification", "smtp_port"): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 833 server = smtplib.SMTP(smtp_host, int( | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 834 boto.config.get_value("Notification", "smtp_port"))) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 835 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 836 server = smtplib.SMTP(smtp_host) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 837 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 838 # TLS support | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 839 if boto.config.getbool("Notification", "smtp_tls"): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 840 server.ehlo() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 841 server.starttls() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 842 server.ehlo() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 843 smtp_user = boto.config.get_value('Notification', 'smtp_user', '') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 844 smtp_pass = boto.config.get_value('Notification', 'smtp_pass', '') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 845 if smtp_user: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 846 server.login(smtp_user, smtp_pass) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 847 server.sendmail(from_string, to_string, msg.as_string()) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 848 server.quit() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 849 except: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 850 boto.log.exception('notify failed') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 851 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 852 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 853 def get_utf8_value(value): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 854 if not six.PY2 and isinstance(value, bytes): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 855 return value | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 856 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 857 if not isinstance(value, six.string_types): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 858 value = six.text_type(value) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 859 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 860 if isinstance(value, six.text_type): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 861 value = value.encode('utf-8') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 862 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 863 return value | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 864 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 865 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 866 def mklist(value): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 867 if not isinstance(value, list): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 868 if isinstance(value, tuple): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 869 value = list(value) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 870 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 871 value = [value] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 872 return value | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 873 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 874 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 875 def pythonize_name(name): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 876 """Convert camel case to a "pythonic" name. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 877 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 878 Examples:: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 879 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 880 pythonize_name('CamelCase') -> 'camel_case' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 881 pythonize_name('already_pythonized') -> 'already_pythonized' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 882 pythonize_name('HTTPRequest') -> 'http_request' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 883 pythonize_name('HTTPStatus200Ok') -> 'http_status_200_ok' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 884 pythonize_name('UPPER') -> 'upper' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 885 pythonize_name('') -> '' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 886 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 887 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 888 s1 = _first_cap_regex.sub(r'\1_\2', name) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 889 s2 = _number_cap_regex.sub(r'\1_\2', s1) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 890 return _end_cap_regex.sub(r'\1_\2', s2).lower() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 891 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 892 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 893 def write_mime_multipart(content, compress=False, deftype='text/plain', delimiter=':'): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 894 """Description: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 895 :param content: A list of tuples of name-content pairs. This is used | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 896 instead of a dict to ensure that scripts run in order | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 897 :type list of tuples: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 898 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 899 :param compress: Use gzip to compress the scripts, defaults to no compression | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 900 :type bool: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 901 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 902 :param deftype: The type that should be assumed if nothing else can be figured out | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 903 :type str: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 904 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 905 :param delimiter: mime delimiter | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 906 :type str: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 907 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 908 :return: Final mime multipart | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 909 :rtype: str: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 910 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 911 wrapper = email.mime.multipart.MIMEMultipart() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 912 for name, con in content: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 913 definite_type = guess_mime_type(con, deftype) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 914 maintype, subtype = definite_type.split('/', 1) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 915 if maintype == 'text': | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 916 mime_con = email.mime.text.MIMEText(con, _subtype=subtype) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 917 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 918 mime_con = email.mime.base.MIMEBase(maintype, subtype) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 919 mime_con.set_payload(con) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 920 # Encode the payload using Base64 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 921 email.encoders.encode_base64(mime_con) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 922 mime_con.add_header('Content-Disposition', 'attachment', filename=name) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 923 wrapper.attach(mime_con) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 924 rcontent = wrapper.as_string() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 925 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 926 if compress: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 927 buf = StringIO() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 928 gz = gzip.GzipFile(mode='wb', fileobj=buf) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 929 try: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 930 gz.write(rcontent) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 931 finally: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 932 gz.close() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 933 rcontent = buf.getvalue() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 934 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 935 return rcontent | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 936 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 937 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 938 def guess_mime_type(content, deftype): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 939 """Description: Guess the mime type of a block of text | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 940 :param content: content we're finding the type of | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 941 :type str: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 942 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 943 :param deftype: Default mime type | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 944 :type str: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 945 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 946 :rtype: <type>: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 947 :return: <description> | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 948 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 949 # Mappings recognized by cloudinit | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 950 starts_with_mappings = { | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 951 '#include': 'text/x-include-url', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 952 '#!': 'text/x-shellscript', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 953 '#cloud-config': 'text/cloud-config', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 954 '#upstart-job': 'text/upstart-job', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 955 '#part-handler': 'text/part-handler', | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 956 '#cloud-boothook': 'text/cloud-boothook' | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 957 } | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 958 rtype = deftype | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 959 for possible_type, mimetype in starts_with_mappings.items(): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 960 if content.startswith(possible_type): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 961 rtype = mimetype | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 962 break | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 963 return(rtype) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 964 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 965 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 966 def compute_md5(fp, buf_size=8192, size=None): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 967 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 968 Compute MD5 hash on passed file and return results in a tuple of values. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 969 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 970 :type fp: file | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 971 :param fp: File pointer to the file to MD5 hash. The file pointer | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 972 will be reset to its current location before the | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 973 method returns. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 974 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 975 :type buf_size: integer | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 976 :param buf_size: Number of bytes per read request. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 977 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 978 :type size: int | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 979 :param size: (optional) The Maximum number of bytes to read from | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 980 the file pointer (fp). This is useful when uploading | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 981 a file in multiple parts where the file is being | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 982 split inplace into different parts. Less bytes may | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 983 be available. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 984 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 985 :rtype: tuple | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 986 :return: A tuple containing the hex digest version of the MD5 hash | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 987 as the first element, the base64 encoded version of the | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 988 plain digest as the second element and the data size as | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 989 the third element. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 990 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 991 return compute_hash(fp, buf_size, size, hash_algorithm=md5) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 992 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 993 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 994 def compute_hash(fp, buf_size=8192, size=None, hash_algorithm=md5): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 995 hash_obj = hash_algorithm() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 996 spos = fp.tell() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 997 if size and size < buf_size: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 998 s = fp.read(size) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 999 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1000 s = fp.read(buf_size) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1001 while s: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1002 if not isinstance(s, bytes): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1003 s = s.encode('utf-8') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1004 hash_obj.update(s) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1005 if size: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1006 size -= len(s) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1007 if size <= 0: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1008 break | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1009 if size and size < buf_size: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1010 s = fp.read(size) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1011 else: | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1012 s = fp.read(buf_size) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1013 hex_digest = hash_obj.hexdigest() | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1014 base64_digest = encodebytes(hash_obj.digest()).decode('utf-8') | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1015 if base64_digest[-1] == '\n': | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1016 base64_digest = base64_digest[0:-1] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1017 # data_size based on bytes read. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1018 data_size = fp.tell() - spos | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1019 fp.seek(spos) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1020 return (hex_digest, base64_digest, data_size) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1021 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1022 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1023 def find_matching_headers(name, headers): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1024 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1025 Takes a specific header name and a dict of headers {"name": "value"}. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1026 Returns a list of matching header names, case-insensitive. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1027 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1028 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1029 return [h for h in headers if h.lower() == name.lower()] | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1030 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1031 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1032 def merge_headers_by_name(name, headers): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1033 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1034 Takes a specific header name and a dict of headers {"name": "value"}. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1035 Returns a string of all header values, comma-separated, that match the | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1036 input header name, case-insensitive. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1037 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1038 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1039 matching_headers = find_matching_headers(name, headers) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1040 return ','.join(str(headers[h]) for h in matching_headers | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1041 if headers[h] is not None) | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1042 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1043 | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1044 class RequestHook(object): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1045 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1046 This can be extended and supplied to the connection object | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1047 to gain access to request and response object after the request completes. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1048 One use for this would be to implement some specific request logging. | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1049 """ | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1050 def handle_request_data(self, request, response, error=False): | 
| 
d67268158946
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
 bcclaywell parents: diff
changeset | 1051 pass | 
