##// END OF EJS Templates
util: add versiontuple() for returning parsed version information...
Gregory Szorc -
r27112:39c14e89 default
parent child Browse files
Show More
@@ -336,6 +336,59 b' def version():'
336 except ImportError:
336 except ImportError:
337 return 'unknown'
337 return 'unknown'
338
338
339 def versiontuple(v=None, n=4):
340 """Parses a Mercurial version string into an N-tuple.
341
342 The version string to be parsed is specified with the ``v`` argument.
343 If it isn't defined, the current Mercurial version string will be parsed.
344
345 ``n`` can be 2, 3, or 4. Here is how some version strings map to
346 returned values:
347
348 >>> v = '3.6.1+190-df9b73d2d444'
349 >>> versiontuple(v, 2)
350 (3, 6)
351 >>> versiontuple(v, 3)
352 (3, 6, 1)
353 >>> versiontuple(v, 4)
354 (3, 6, 1, '190-df9b73d2d444')
355
356 >>> versiontuple('3.6.1+190-df9b73d2d444+20151118')
357 (3, 6, 1, '190-df9b73d2d444+20151118')
358
359 >>> v = '3.6'
360 >>> versiontuple(v, 2)
361 (3, 6)
362 >>> versiontuple(v, 3)
363 (3, 6, None)
364 >>> versiontuple(v, 4)
365 (3, 6, None, None)
366 """
367 if not v:
368 v = version()
369 parts = v.split('+', 1)
370 if len(parts) == 1:
371 vparts, extra = parts[0], None
372 else:
373 vparts, extra = parts
374
375 vints = []
376 for i in vparts.split('.'):
377 try:
378 vints.append(int(i))
379 except ValueError:
380 break
381 # (3, 6) -> (3, 6, None)
382 while len(vints) < 3:
383 vints.append(None)
384
385 if n == 2:
386 return (vints[0], vints[1])
387 if n == 3:
388 return (vints[0], vints[1], vints[2])
389 if n == 4:
390 return (vints[0], vints[1], vints[2], extra)
391
339 # used by parsedate
392 # used by parsedate
340 defaultdateformats = (
393 defaultdateformats = (
341 '%Y-%m-%d %H:%M:%S',
394 '%Y-%m-%d %H:%M:%S',
General Comments 0
You need to be logged in to leave comments. Login now