~rlane/nova/ldapimprovements

« back to all changes in this revision

Viewing changes to nova/utils.py

  • Committer: Ryan Lane
  • Date: 2010-11-24 15:46:32 UTC
  • mfrom: (382.48.1 trunk)
  • Revision ID: laner@controller-20101124154632-zh7kwjuyyd02a2lh
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
"""
22
22
 
23
23
import datetime
 
24
import functools
24
25
import inspect
25
26
import logging
26
27
import os
132
133
 
133
134
 
134
135
def generate_uid(topic, size=8):
135
 
    if topic == "i":
136
 
        # Instances have integer internal ids.
137
 
        return random.randint(0, 2 ** 32 - 1)
138
 
    else:
139
 
        characters = '01234567890abcdefghijklmnopqrstuvwxyz'
140
 
        choices = [random.choice(characters) for x in xrange(size)]
141
 
        return '%s-%s' % (topic, ''.join(choices))
 
136
    characters = '01234567890abcdefghijklmnopqrstuvwxyz'
 
137
    choices = [random.choice(characters) for x in xrange(size)]
 
138
    return '%s-%s' % (topic, ''.join(choices))
142
139
 
143
140
 
144
141
def generate_mac():
158
155
    if getattr(FLAGS, 'fake_tests', None):
159
156
        return '127.0.0.1'
160
157
    try:
161
 
        csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
162
 
        csock.connect(('www.google.com', 80))
 
158
        csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
159
        csock.connect(('8.8.8.8', 80))
163
160
        (addr, port) = csock.getsockname()
164
161
        csock.close()
165
162
        return addr
178
175
    return datetime.datetime.strptime(timestr, TIME_FORMAT)
179
176
 
180
177
 
 
178
def parse_mailmap(mailmap='.mailmap'):
 
179
    mapping = {}
 
180
    if os.path.exists(mailmap):
 
181
        fp = open(mailmap, 'r')
 
182
        for l in fp:
 
183
            l = l.strip()
 
184
            if not l.startswith('#') and ' ' in l:
 
185
                canonical_email, alias = l.split(' ')
 
186
                mapping[alias] = canonical_email
 
187
    return mapping
 
188
 
 
189
 
 
190
def str_dict_replace(s, mapping):
 
191
    for s1, s2 in mapping.iteritems():
 
192
        s = s.replace(s1, s2)
 
193
    return s
 
194
 
 
195
 
181
196
class LazyPluggable(object):
182
197
    """A pluggable backend loaded lazily based on some value."""
183
198
 
217
232
 
218
233
def xhtml_escape(value):
219
234
    """Escapes a string so it is valid within XML or XHTML.
220
 
    
 
235
 
221
236
    Code is directly from the utf8 function in
222
237
    http://github.com/facebook/tornado/blob/master/tornado/escape.py
223
 
    
 
238
 
224
239
    """
225
240
    return saxutils.escape(value, {'"': """})
226
241
 
236
251
        return value.encode("utf-8")
237
252
    assert isinstance(value, str)
238
253
    return value
239