~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: mbp at sourcefrog
  • Date: 2005-04-09 06:21:44 UTC
  • Revision ID: mbp@sourcefrog.net-20050409062144-e47a4b64106e4c21af99beaf
debugĀ output

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
19
 
import os, types, re, time, types
 
19
import os, types, re, time
20
20
from stat import S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE
21
21
 
22
 
from errors import bailout
 
22
from errors import bailout, BzrError
 
23
from trace import mutter
23
24
 
24
25
def make_readonly(filename):
25
26
    """Make a filename read-only."""
54
55
    elif S_ISLNK(mode):
55
56
        return 'symlink'
56
57
    else:
57
 
        bailout("can't handle file kind with mode %o of %r" % (mode, f)) 
 
58
        raise BzrError("can't handle file kind with mode %o of %r" % (mode, f)) 
58
59
 
59
60
 
60
61
 
117
118
 
118
119
 
119
120
 
 
121
def fingerprint_file(f):
 
122
    import sha
 
123
    s = sha.new()
 
124
    b = f.read()
 
125
    s.update(b)
 
126
    size = len(b)
 
127
    return {'size': size,
 
128
            'sha1': s.hexdigest()}
 
129
 
 
130
 
 
131
 
120
132
def username():
121
133
    """Return email-style username.
122
134
 
152
164
    return '<%s@%s>' % (getpass.getuser(), socket.getfqdn())
153
165
 
154
166
 
 
167
_EMAIL_RE = re.compile(r'[\w+.-]+@[\w+.-]+')
155
168
def user_email():
156
169
    """Return just the email component of a username."""
157
170
    e = os.environ.get('BZREMAIL') or os.environ.get('EMAIL')
158
171
    if e:
159
 
        import re
160
 
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
 
172
        m = _EMAIL_RE.search(e)
161
173
        if not m:
162
174
            bailout('%r is not a reasonable email address' % e)
163
175
        return m.group(0)
186
198
def local_time_offset(t=None):
187
199
    """Return offset of local zone from GMT, either at present or at time t."""
188
200
    # python2.3 localtime() can't take None
189
 
    if t is None:
 
201
    if t == None:
190
202
        t = time.time()
191
203
        
192
204
    if time.localtime(t).tm_isdst and time.daylight:
198
210
def format_date(t, offset=0, timezone='original'):
199
211
    ## TODO: Perhaps a global option to use either universal or local time?
200
212
    ## Or perhaps just let people set $TZ?
201
 
    import time
202
 
    
203
213
    assert isinstance(t, float)
204
214
    
205
215
    if timezone == 'utc':
258
268
    BzrError: ("sorry, '..' not allowed in path", [])
259
269
    """
260
270
    assert isinstance(p, types.StringTypes)
261
 
    ps = [f for f in p.split('/') if f != '.']
 
271
    ps = [f for f in p.split('/') if (f != '.' and f != '')]
262
272
    for f in ps:
263
273
        if f == '..':
264
274
            bailout("sorry, %r not allowed in path" % f)
267
277
def joinpath(p):
268
278
    assert isinstance(p, list)
269
279
    for f in p:
270
 
        if (f == '..') or (f is None) or (f == ''):
 
280
        if (f == '..') or (f == None) or (f == ''):
271
281
            bailout("sorry, %r not allowed in path" % f)
272
282
    return '/'.join(p)
273
283