Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 19194:1d08df65cd3c
util: migrate fileset._sizetoint to util.sizetoint
The size counting code introduced in 2c4cd1c42365 duplicated existing
(but unknown-to-me) code in fileset, so prepare to eliminate the
duplication.
author | Bryan O'Sullivan <bryano@fb.com> |
---|---|
date | Tue, 14 May 2013 15:16:43 -0700 |
parents | e22107cff6bf |
children | 3bfd7f1e7485 |
comparison
equal
deleted
inserted
replaced
19191:ab9de1e8fc36 | 19194:1d08df65cd3c |
---|---|
1922 _timenesting[0] -= indent | 1922 _timenesting[0] -= indent |
1923 sys.stderr.write('%s%s: %s\n' % | 1923 sys.stderr.write('%s%s: %s\n' % |
1924 (' ' * _timenesting[0], func.__name__, | 1924 (' ' * _timenesting[0], func.__name__, |
1925 timecount(elapsed))) | 1925 timecount(elapsed))) |
1926 return wrapper | 1926 return wrapper |
1927 | |
1928 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), | |
1929 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) | |
1930 | |
1931 def sizetoint(s): | |
1932 '''Convert a space specifier to a byte count. | |
1933 | |
1934 >>> sizetoint('30') | |
1935 30 | |
1936 >>> sizetoint('2.2kb') | |
1937 2252 | |
1938 >>> sizetoint('6M') | |
1939 6291456 | |
1940 ''' | |
1941 t = s.strip().lower() | |
1942 try: | |
1943 for k, u in _sizeunits: | |
1944 if t.endswith(k): | |
1945 return int(float(t[:-len(k)]) * u) | |
1946 return int(t) | |
1947 except ValueError: | |
1948 raise error.ParseError(_("couldn't parse size: %s") % s) |