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
19
import os, types, re, time, types
20
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
19
import os, types, re, time, errno
20
from stat import S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE
22
from errors import bailout
22
from errors import bailout, BzrError
23
from trace import mutter
24
26
def make_readonly(filename):
25
27
"""Make a filename read-only."""
116
"""Return email-style username.
118
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
120
:todo: Check it's reasonably well-formed.
122
:todo: Allow taking it from a dotfile to help people on windows
123
who can't easily set variables.
125
:todo: Cope without pwd module, which is only on unix.
122
def fingerprint_file(f):
128
return {'size': size,
129
'sha1': s.hexdigest()}
133
"""Calculate automatic user identification.
135
Returns (realname, email).
137
Only used when none is set in the environment or the id file.
139
This previously used the FQDN as the default domain, but that can
140
be very slow on machines where DNS is broken. So now we simply
127
e = os.environ.get('BZREMAIL') or os.environ.get('EMAIL')
145
# XXX: Any good way to get real user name on win32?
134
149
uid = os.getuid()
135
150
w = pwd.getpwuid(uid)
136
realname, junk = w.pw_gecos.split(',', 1)
137
return '%s <%s@%s>' % (realname, w.pw_name, socket.getfqdn())
151
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
152
username = w.pw_name.decode(bzrlib.user_encoding)
153
comma = gecos.find(',')
157
realname = gecos[:comma]
138
159
except ImportError:
141
import getpass, socket
142
return '<%s@%s>' % (getpass.getuser(), socket.getfqdn())
162
username = getpass.getuser().decode(bzrlib.user_encoding)
164
return realname, (username + '@' + os.gethostname())
168
v = os.environ.get('BZREMAIL')
170
return v.decode(bzrlib.user_encoding)
173
return (open(os.path.expanduser("~/.bzr.email"))
175
.decode(bzrlib.user_encoding)
178
if e.errno != ENOENT:
181
v = os.environ.get('EMAIL')
183
return v.decode(bzrlib.user_encoding)
189
"""Return email-style username.
191
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
193
TODO: Check it's reasonably well-formed.
195
TODO: Allow taking it from a dotfile to help people on windows
196
who can't easily set variables.
202
name, email = _auto_user_id()
204
return '%s <%s>' % (name, email)
209
_EMAIL_RE = re.compile(r'[\w+.-]+@[\w+.-]+')
145
210
def user_email():
146
211
"""Return just the email component of a username."""
147
e = os.environ.get('BZREMAIL') or os.environ.get('EMAIL')
150
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
214
m = _EMAIL_RE.search(e)
152
bailout('%r is not a reasonable email address' % e)
216
bailout("%r doesn't seem to contain a reasonable email address" % e)
153
217
return m.group(0)
156
import getpass, socket
157
return '%s@%s' % (getpass.getuser(), socket.getfqdn())
219
return _auto_user_id()[1]
162
223
def compare_files(a, b):
163
224
"""Returns true if equal in contents"""
164
225
# TODO: don't read the whole thing in one go.
165
result = a.read() == b.read()
170
def format_date(t, inutc=False):
237
def local_time_offset(t=None):
238
"""Return offset of local zone from GMT, either at present or at time t."""
239
# python2.3 localtime() can't take None
243
if time.localtime(t).tm_isdst and time.daylight:
246
return -time.timezone
249
def format_date(t, offset=0, timezone='original'):
171
250
## TODO: Perhaps a global option to use either universal or local time?
172
251
## Or perhaps just let people set $TZ?
175
252
assert isinstance(t, float)
254
if timezone == 'utc':
178
255
tt = time.gmtime(t)
257
elif timezone == 'original':
260
tt = time.gmtime(t + offset)
261
elif timezone == 'local':
182
262
tt = time.localtime(t)
184
zonename = time.tzname[1]
185
offset = - time.altzone
187
zonename = time.tzname[0]
188
offset = - time.timezone
263
offset = local_time_offset(t)
265
bailout("unsupported timezone format %r",
266
['options are "utc", "original", "local"'])
190
268
return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
191
+ ' ' + zonename + ' '
192
+ '%+03d%02d' % (offset / 3600, (offset / 60) % 60))
269
+ ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
195
272
def compact_date(when):