Revision 74d988b0

b/snf-common/synnefo/lib/amqp_puka.py
63 63
        try:
64 64
            return func(self, *args, **kwargs)
65 65
        except (socket_error, spec_exceptions.ConnectionForced) as e:
66
            self.log.error('Connection Closed while in %s: %s', func.__name__, e)
66
            self.log.error('Connection Closed while in %s: %s', func.__name__,
67
                           e)
67 68
            self.connect()
68 69

  
69 70
    return wrapper
......
106 107
    def connect(self, retries=0):
107 108
        if self.max_retries and retries >= self.max_retries:
108 109
            self.log.error("Aborting after %d retries", retries)
109
            raise AMQPConnectionError('Aborting after %d connection failures.'\
110
            raise AMQPConnectionError('Aborting after %d connection failures.'
110 111
                                      % retries)
111 112
            return
112 113

  
......
211 212
                arguments = {'x-ha-policy': 'all'}
212 213
            elif isinstance(mirrored_nodes, list):
213 214
                arguments = {'x-ha-policy': 'nodes',
214
                           'x-ha-policy-params': mirrored_nodes}
215
                             'x-ha-policy-params': mirrored_nodes}
215 216
            else:
216 217
                raise AttributeError
217 218
        else:
......
319 320
                raise socket_error
320 321

  
321 322
        consume_promise = \
322
                self.client.basic_consume(queue=queue,
323
                                          prefetch_count=prefetch_count,
324
                                          callback=handle_delivery)
323
            self.client.basic_consume(queue=queue,
324
                                      prefetch_count=prefetch_count,
325
                                      callback=handle_delivery)
325 326

  
326 327
        self.consume_promises.append(consume_promise)
327 328
        return consume_promise
b/snf-common/synnefo/lib/astakos.py
33 33

  
34 34
import logging
35 35

  
36
from time import time, mktime
37
from urlparse import urlparse, urlsplit, urlunsplit
38
from urllib import quote, unquote, urlencode
39

  
40
from django.conf import settings
36
from urlparse import urlparse
37
from urllib import unquote
41 38
from django.utils import simplejson as json
42
from django.utils.http import urlencode
43 39

  
44 40
from synnefo.lib.pool.http import get_http_connection
45 41

  
46 42
logger = logging.getLogger(__name__)
47 43

  
44

  
48 45
def retry(howmany):
49 46
    def execute(func):
50 47
        def f(*args, **kwargs):
......
66 63
        return f
67 64
    return execute
68 65

  
66

  
69 67
def call(token, url, headers={}, body=None, method='GET'):
70 68
    p = urlparse(url)
71 69

  
......
74 72
    kwargs['headers']['X-Auth-Token'] = token
75 73
    if body:
76 74
        kwargs['body'] = body
77
        kwargs['headers'].setdefault('content-type', 'application/octet-stream')
75
        kwargs['headers'].setdefault('content-type',
76
                                     'application/octet-stream')
78 77
    kwargs['headers'].setdefault('content-length', len(body) if body else 0)
79 78

  
80

  
81 79
    conn = get_http_connection(p.netloc, p.scheme)
82 80
    try:
83 81
        conn.request(method, p.path + '?' + p.query, **kwargs)
84 82
        response = conn.getresponse()
85 83
        headers = response.getheaders()
86
        headers = dict((unquote(h), unquote(v)) for h,v in headers)
84
        headers = dict((unquote(h), unquote(v)) for h, v in headers)
87 85
        length = response.getheader('content-length', None)
88 86
        data = response.read(length)
89 87
        status = int(response.status)
......
114 112
        override_users={}):
115 113

  
116 114
    if override_users:
117
        return dict((u,u) for u in uuids)
115
        return dict((u, u) for u in uuids)
118 116

  
119 117
    try:
120 118
        data = call(
121
            token, url,  headers={'content-type':'application/json'},
122
            body=json.dumps({'uuids':uuids}), method='POST')
119
            token, url,  headers={'content-type': 'application/json'},
120
            body=json.dumps({'uuids': uuids}), method='POST')
123 121
    except:
124 122
        raise
125 123
    else:
......
134 132
        override_users={}):
135 133

  
136 134
    if override_users:
137
        return dict((u,u) for u in displaynames)
135
        return dict((u, u) for u in displaynames)
138 136

  
139 137
    try:
140 138
        data = call(
141
            token, url, headers={'content-type':'application/json'},
142
            body=json.dumps({'displaynames':displaynames}), method='POST')
139
            token, url, headers={'content-type': 'application/json'},
140
            body=json.dumps({'displaynames': displaynames}), method='POST')
143 141
    except:
144 142
        raise
145 143
    else:
146 144
        return data.get('displayname_catalog')
147 145

  
146

  
148 147
def get_user_uuid(
149 148
        token,
150 149
        displayname,
......
170 169
    uuid_dict = get_displaynames(token, [uuid], url, override_users)
171 170
    return uuid_dict.get(uuid)
172 171

  
172

  
173 173
def user_for_token(token, authentication_url, override_users, usage=False):
174 174
    if not token:
175 175
        return None
......
188 188
            return None
189 189
        raise e
190 190

  
191

  
191 192
def get_user(
192 193
        request,
193 194
        authentication_url='http://127.0.0.1:8000/im/authenticate',
......
216 217
                       authentication_url)
217 218
        return None
218 219

  
219
    # use user uuid, instead of email, keep email/displayname reference to user_id
220
    # use user uuid, instead of email, keep email/displayname reference
221
    # to user_id
220 222
    request.user_uniq = user['uuid']
221 223
    request.user = user
222 224
    request.user_id = user.get('displayname')
b/snf-common/synnefo/lib/middleware/cleanse.py
39 39

  
40 40
import re
41 41

  
42

  
42 43
def mail_admins_safe(subject, message, fail_silently=False, connection=None):
43 44
    '''
44 45
    Wrapper function to cleanse email body from sensitive content before
......
46 47
    '''
47 48

  
48 49
    HIDDEN_ALL = settings.HIDDEN_SETTINGS + "|" + settings.HIDDEN_COOKIES
49
    message = re.sub("((\S+)?(%s)(\S+)?(:|\=)( )?)('|\"?)\S+('|\"?)" \
50
                % HIDDEN_ALL, r"\1*******", message)
50
    message = re.sub("((\S+)?(%s)(\S+)?(:|\=)( )?)('|\"?)\S+('|\"?)"
51
                     % HIDDEN_ALL, r"\1*******", message)
51 52

  
52 53
    return mail.mail_admins_plain(subject, message, fail_silently, connection)
53 54

  
55

  
54 56
class CleanseSettingsMiddleware(object):
55 57
    def __init__(self):
56 58
        '''
b/snf-common/synnefo/lib/middleware/log.py
46 46

  
47 47
class LoggingConfigMiddleware:
48 48
    def __init__(self):
49
        '''Initialise the logging setup from settings, called on first request.'''
49
        """Initialise the logging setup from settings.
50

  
51
        Logging setup is initialized only in the first request.
52
        """
50 53
        logging_setting = getattr(settings, 'LOGGING_SETUP', None)
51 54
        if logging_setting:
52 55
            # Disable handlers that are not used by any logger.
b/snf-common/synnefo/lib/middleware/remoteaddr.py
6 6
    key.
7 7
    """
8 8
    def process_request(self, request):
9
        if not request.META.has_key('REMOTE_ADDR'):
10
            request.META['REMOTE_ADDR'] = request.META.get('HTTP_X_REAL_IP', None)
9
        if 'REMOTE_ADDR' in request.META:
10
            request.META['REMOTE_ADDR'] = request.META.get('HTTP_X_REAL_IP',
11
                                                           None)
b/snf-common/synnefo/lib/middleware/secure.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34

  
34 35
class SecureMiddleware(object):
35 36
    def process_request(self, request):
36 37
        if 'HTTP_X_FORWARDED_PROTOCOL' in request.META:
37
            request.is_secure = lambda: request.META['HTTP_X_FORWARDED_PROTOCOL'] == 'https'
38
            request.is_secure =\
39
                lambda: request.META['HTTP_X_FORWARDED_PROTOCOL'] == 'https'
b/snf-common/synnefo/lib/pool/http.py
35 35
from select import select
36 36

  
37 37
from httplib import (
38
        HTTPConnection  as http_class,
39
        HTTPSConnection as https_class,
40
        ResponseNotReady
38
    HTTPConnection as http_class,
39
    HTTPSConnection as https_class,
40
    ResponseNotReady
41 41
)
42 42

  
43 43
from new import instancemethod
......
72 72
class HTTPConnectionPool(ObjectPool):
73 73

  
74 74
    _scheme_to_class = {
75
            'http': http_class,
76
            'https': https_class,
75
        'http': http_class,
76
        'https': https_class,
77 77
    }
78 78

  
79 79
    def __init__(self, scheme, netloc, size=None):
80
        log.debug("INIT-POOL: Initializing pool of size %d, scheme: %s, " \
80
        log.debug("INIT-POOL: Initializing pool of size %d, scheme: %s, "
81 81
                  "netloc: %s", size, scheme, netloc)
82 82
        ObjectPool.__init__(self, size=size)
83 83

  
b/snf-common/synnefo/lib/queue.py
137 137
        self.id = hash.hexdigest()
138 138
    
139 139
    def format(self):
140
        return self.__dict__
140
        return self.__dict__
b/snf-common/synnefo/lib/singleton/__init__.py
59 59
        ret = cls._singles[distinct]
60 60

  
61 61
        return ret
62
        
62

  
63 63

  
64 64
class ArgBasedSingleton(object):
65 65
    __metaclass__ = ArgBasedSingletonMeta
b/snf-common/synnefo/lib/utils.py
33 33

  
34 34
import datetime
35 35

  
36

  
36 37
def split_time(value):
37
  """Splits time as floating point number into a tuple.
38
    """Splits time as floating point number into a tuple.
38 39

  
39
  @param value: Time in seconds
40
  @type value: int or float
41
  @return: Tuple containing (seconds, microseconds)
40
    @param value: Time in seconds
41
    @type value: int or float
42
    @return: Tuple containing (seconds, microseconds)
42 43

  
43
  """
44
  (seconds, microseconds) = divmod(int(value * 1000000), 1000000)
44
    """
45
    (seconds, microseconds) = divmod(int(value * 1000000), 1000000)
45 46

  
46
  assert 0 <= seconds, \
47
    "Seconds must be larger than or equal to 0, but are %s" % seconds
48
  assert 0 <= microseconds <= 999999, \
49
    "Microseconds must be 0-999999, but are %s" % microseconds
47
    assert 0 <= seconds, \
48
        "Seconds must be larger than or equal to 0, but are %s" % seconds
49
    assert 0 <= microseconds <= 999999, \
50
        "Microseconds must be 0-999999, but are %s" % microseconds
50 51

  
51
  return (int(seconds), int(microseconds))
52
    return (int(seconds), int(microseconds))
52 53

  
53 54

  
54 55
def merge_time(timetuple):
55
  """Merges a tuple into a datetime object
56
    """Merges a tuple into a datetime object
56 57

  
57
  @param timetuple: Time as tuple, (seconds, microseconds)
58
  @type timetuple: tuple
59
  @return: Time as a datetime object
58
    @param timetuple: Time as tuple, (seconds, microseconds)
59
    @type timetuple: tuple
60
    @return: Time as a datetime object
60 61

  
61
  """
62
  (seconds, microseconds) = timetuple
62
    """
63
    (seconds, microseconds) = timetuple
63 64

  
64
  assert 0 <= seconds, \
65
    "Seconds must be larger than or equal to 0, but are %s" % seconds
66
  assert 0 <= microseconds <= 999999, \
67
    "Microseconds must be 0-999999, but are %s" % microseconds
65
    assert 0 <= seconds, \
66
        "Seconds must be larger than or equal to 0, but are %s" % seconds
67
    assert 0 <= microseconds <= 999999, \
68
        "Microseconds must be 0-999999, but are %s" % microseconds
68 69

  
69
  t1 = float(seconds) + (float(microseconds) * 0.000001)
70
  return datetime.datetime.fromtimestamp(t1)
70
    t1 = float(seconds) + (float(microseconds) * 0.000001)
71
    return datetime.datetime.fromtimestamp(t1)
71 72

  
72 73

  
73 74
def case_unique(iterable):
......
85 86
        return list(set(iterable) - set(icase))
86 87

  
87 88
    return []
88

  
b/snf-common/synnefo/settings/default/__init__.py
32 32
# or implied, of GRNET S.A.
33 33

  
34 34
from synnefo.settings.default.admins import *
35

  
b/snf-common/synnefo/util/date.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from datetime import timedelta, tzinfo, datetime
34
from datetime import timedelta, tzinfo
35 35

  
36 36
import dateutil.parser
37 37

  
38

  
38 39
class UTC(tzinfo):
39 40
    """
40 41
    Helper UTC time information object.
......
74 75
    since = dateutil.parser.parse(s)
75 76
    utc_since = since.astimezone(UTC()).replace(tzinfo=None)
76 77
    return utc_since
77

  
b/snf-common/synnefo/util/entry_points.py
33 33

  
34 34
import sys
35 35
import pkg_resources
36
import inspect
37 36
import types
38 37
import os
39 38

  
40 39
from collections import defaultdict
41
import inspect
42 40

  
43 41
# List of python distribution names which entry points will get excluded
44 42
# from snf-common settings extension mechanism
......
54 52

  
55 53
def extend_module(module_name, attrs):
56 54
    module = sys.modules[module_name]
57
    for k,v in attrs.iteritems():
55
    for k, v in attrs.iteritems():
58 56
        setattr(module, k, v)
59 57

  
60 58

  
......
67 65
    if hasattr(object_or_func, '__call__'):
68 66
        obj = object_or_func()
69 67

  
70
    if type(obj) == types.ModuleType:
68
    if isinstance(obj, types.ModuleType):
71 69
        dct = {}
72 70
        for k in dir(obj):
73 71
            if k.startswith("__"):
......
95 93

  
96 94

  
97 95
def extend_list_from_entry_point(settings_object, ns, entry_point_name,
98
        unique=True):
96
                                 unique=True):
99 97
    settings_object = list(settings_object)
100 98
    for e in get_entry_points(ns, entry_point_name):
101 99
        obj = entry_point_to_object(e)
......
104 102
            if row in settings_object:
105 103
                continue
106 104

  
107
            if type(row) == dict and (row.get('before', False) or \
108
                    row.get('after', False)):
105
            if type(row) == dict and (row.get('before', False) or
106
                                      row.get('after', False)):
109 107
                if row.get('before', False):
110 108
                    position = settings_object.index(row.get('before'))
111 109
                    insert_at = position - 1
......
118 116

  
119 117
                inserts = row.get('insert', [])
120 118
                if not type(inserts) == list:
121
                    inserts  = [inserts]
119
                    inserts = [inserts]
122 120

  
123 121
                for entry in inserts:
124 122
                    if not entry in settings_object:
......
148 146
        patterns += e.load()
149 147

  
150 148
    return patterns
151

  
b/snf-common/synnefo/util/mac2eui64.py
1 1
# Copyright 2011 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33
# 
33
#
34 34

  
35
import sys
36 35
from IPy import IP
37 36

  
38 37
# Adapted from NFDHCPD's mac2eui64 utility, repository commit:feca7bb95
39 38

  
39

  
40 40
def mac2eui64(mac, prefixstr):
41 41
    try:
42 42
        prefix = IP(prefixstr)
......
55 55
    if len(mac_parts) != 6:
56 56
        raise Exception("%s is not a valid MAC-48 address" % mac)
57 57

  
58
    eui64 = mac_parts[:3] + [ "ff", "fe" ] + mac_parts[3:]
58
    eui64 = mac_parts[:3] + ["ff", "fe"] + mac_parts[3:]
59 59

  
60 60
    eui64[0] = "%02x" % (int(eui64[0], 16) ^ 0x02)
61 61

  
62 62
    ip = ":".join(pfx_parts[:4])
63 63
    for l in range(0, len(eui64), 2):
64
        ip += ":%s" % "".join(eui64[l:l+2])
64
        ip += ":%s" % "".join(eui64[l:l + 2])
65 65

  
66 66
    return IP(ip).strCompressed()
67 67

  
b/snf-common/synnefo/util/number.py
33 33
# interpreted as representing official policies, either expressed
34 34
# or implied, of GRNET S.A.
35 35

  
36

  
36 37
def strbigdec(bignum, nr_lsd=12):
37 38
    all_digits = str(bignum)
38 39
    ms_digits = all_digits[:-nr_lsd]
......
52 53
    else:
53 54
        display = "[%s%1.0e]" % (approx, ms_num)
54 55
    return display
55

  
b/snf-common/synnefo/util/text.py
35 35

  
36 36
uenc_encoding = 'UTF-8'
37 37

  
38

  
38 39
def uenc_set_encoding(encoding=None):
39 40
    """Set the default encoding for uenc()
40 41

  
......
65 66

  
66 67
    uenc_encoding = encoding
67 68

  
69

  
68 70
def uenc(thing, encoding=None):
69 71
    """Encode the argument into a string.
70 72

  
......
89 91
    return str(thing)
90 92

  
91 93
uenc_set_encoding()
92

  
b/snf-common/synnefo/util/version.py
1 1
import pkg_resources
2 2
import os
3 3
import pprint
4
import re
4

  
5 5

  
6 6
def get_dist_from_module(modname):
7 7
    pkgroot = pkg_resources.get_provider(modname).egg_root
......
18 18
    """
19 19
    try:
20 20
        return get_dist(dist_name).version
21
    except Exception, e:
21
    except Exception:
22 22
        return 'unknown'
23 23

  
24 24

  
......
30 30
    try:
31 31
        try:
32 32
            return __import__('synnefo.versions.%s' % modname,
33
                    fromlist=['synnefo.versions']).__version__
33
                              fromlist=['synnefo.versions']).__version__
34 34
        except ImportError:
35
            return  vcs_version()
36
    except Exception, e:
35
            return "unknown"
36
    except Exception:
37 37
        return 'unknown'
38 38

  
39 39

  
......
49 49
    """
50 50
    import subprocess
51 51
    callgit = lambda(cmd): subprocess.Popen(
52
            ['/bin/sh', '-c', cmd],
53
            stdout=subprocess.PIPE,
54
            stderr=subprocess.PIPE).communicate()[0].strip()
52
        ['/bin/sh', '-c', cmd],
53
        stdout=subprocess.PIPE,
54
        stderr=subprocess.PIPE).communicate()[0].strip()
55 55

  
56 56
    branch = callgit('git branch | grep -Ei "\* (.*)" | cut -f2 -d" "')
57
    revid = callgit("git --no-pager log --max-count=1 | cut -f2 -d' ' | head -1")
57
    revid =\
58
        callgit("git --no-pager log --max-count=1 | cut -f2 -d' ' | head -1")
58 59
    revno = callgit('git --no-pager log --oneline | wc -l')
59 60
    desc = callgit('git describe --tags')
60 61

  
......
86 87
def update_version_old(module, name='version', root="."):
87 88
    """
88 89
    Helper util to generate/replace a version.py file containing version
89
    information retrieved from get_version_from_describe as a submodule of passed `module`
90
    information retrieved from get_version_from_describe as a submodule of
91
    passed `module`
90 92
    """
91 93

  
92 94
    # exit early if not in development environment
93
    if not os.path.exists(os.path.join(root, '..', '.git')) and \
94
        not os.path.exists(os.path.join(root, '.git')):
95
        return
95
    if not os.path.exists(os.path.join(root, '..', '.git')) and\
96
       not os.path.exists(os.path.join(root, '.git')):
97
            return
96 98

  
97 99
    paths = [root] + module.split(".") + ["%s.py" % name]
98 100
    module_filename = os.path.join(*paths)
......
101 103
__version_info__ = __version__.split(".")
102 104
__version_vcs_info__ = %(vcs_info)s
103 105
    """ % dict(version=get_version_from_describe(vcs_info()[3]),
104
            vcs_info=pprint.PrettyPrinter().pformat(vcs_info()))
106
               vcs_info=pprint.PrettyPrinter().pformat(vcs_info()))
105 107

  
106 108
    module_file = file(module_filename, "w+")
107 109
    module_file.write(content)

Also available in: Unified diff