~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-23 23:52:10 UTC
  • Revision ID: mbp@sourcefrog.net-20050323235210-5464746b93c39ed0
more notes on darcs

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
19
19
import os, types, re, time, types
20
 
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
 
20
from stat import S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE
21
21
 
22
22
from errors import bailout
23
23
 
51
51
        return 'file'
52
52
    elif S_ISDIR(mode):
53
53
        return 'directory'
 
54
    elif S_ISLNK(mode):
 
55
        return 'symlink'
54
56
    else:
55
 
        bailout("can't handle file kind of %r" % fp)
 
57
        bailout("can't handle file kind with mode %o of %r" % (mode, f)) 
56
58
 
57
59
 
58
60
 
84
86
    ## XXX: Could alternatively read /proc/sys/kernel/random/uuid on
85
87
    ## Linux, but we need something portable for other systems;
86
88
    ## preferably an implementation in Python.
87
 
    bailout('uuids not allowed!')
88
 
    return chomp(os.popen('uuidgen').readline())
 
89
    try:
 
90
        return chomp(file('/proc/sys/kernel/random/uuid').readline())
 
91
    except IOError:
 
92
        return chomp(os.popen('uuidgen').readline())
 
93
 
89
94
 
90
95
def chomp(s):
91
96
    if s and (s[-1] == '\n'):
133
138
        import pwd
134
139
        uid = os.getuid()
135
140
        w = pwd.getpwuid(uid)
136
 
        realname, junk = w.pw_gecos.split(',', 1)
 
141
        gecos = w.pw_gecos
 
142
        comma = gecos.find(',')
 
143
        if comma == -1:
 
144
            realname = gecos
 
145
        else:
 
146
            realname = gecos[:comma]
137
147
        return '%s <%s@%s>' % (realname, w.pw_name, socket.getfqdn())
138
148
    except ImportError:
139
149
        pass
162
172
def compare_files(a, b):
163
173
    """Returns true if equal in contents"""
164
174
    # TODO: don't read the whole thing in one go.
165
 
    result = a.read() == b.read()
166
 
    return result
167
 
 
168
 
 
169
 
 
170
 
def format_date(t, inutc=False):
 
175
    BUFSIZE = 4096
 
176
    while True:
 
177
        ai = a.read(BUFSIZE)
 
178
        bi = b.read(BUFSIZE)
 
179
        if ai != bi:
 
180
            return False
 
181
        if ai == '':
 
182
            return True
 
183
 
 
184
 
 
185
 
 
186
def local_time_offset(t=None):
 
187
    """Return offset of local zone from GMT, either at present or at time t."""
 
188
    # python2.3 localtime() can't take None
 
189
    if t is None:
 
190
        t = time.time()
 
191
        
 
192
    if time.localtime(t).tm_isdst and time.daylight:
 
193
        return -time.altzone
 
194
    else:
 
195
        return -time.timezone
 
196
 
 
197
    
 
198
def format_date(t, offset=0, timezone='original'):
171
199
    ## TODO: Perhaps a global option to use either universal or local time?
172
200
    ## Or perhaps just let people set $TZ?
173
201
    import time
174
202
    
175
203
    assert isinstance(t, float)
176
204
    
177
 
    if inutc:
 
205
    if timezone == 'utc':
178
206
        tt = time.gmtime(t)
179
 
        zonename = 'UTC'
180
207
        offset = 0
181
 
    else:
 
208
    elif timezone == 'original':
 
209
        if offset == None:
 
210
            offset = 0
 
211
        tt = time.gmtime(t + offset)
 
212
    elif timezone == 'local':
182
213
        tt = time.localtime(t)
183
 
        if time.daylight:
184
 
            zonename = time.tzname[1]
185
 
            offset = - time.altzone
186
 
        else:
187
 
            zonename = time.tzname[0]
188
 
            offset = - time.timezone
189
 
            
 
214
        offset = local_time_offset(t)
 
215
    else:
 
216
        bailout("unsupported timezone format %r",
 
217
                ['options are "utc", "original", "local"'])
 
218
 
190
219
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
191
 
            + ' ' + zonename + ' '
192
 
            + '%+03d%02d' % (offset / 3600, (offset / 60) % 60))
 
220
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
193
221
 
194
222
 
195
223
def compact_date(when):