Rev 6387: (vila) Migrate add.maximum_file_size configuration option. (Vincent Ladeuil) in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/
Patch Queue Manager
pqm at pqm.ubuntu.com
Mon Dec 19 17:14:34 UTC 2011
At file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 6387 [merge]
revision-id: pqm at pqm.ubuntu.com-20111219171434-i0b4ir0invs9il2v
parent: pqm at pqm.ubuntu.com-20111219144024-awadgqxn2fugwr7m
parent: v.ladeuil+lp at free.fr-20111219164149-ecfrvwscqb6v77jn
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2011-12-19 17:14:34 +0000
message:
(vila) Migrate add.maximum_file_size configuration option. (Vincent Ladeuil)
modified:
bzrlib/add.py add.py-20050323030017-3a77d63feda58e33
bzrlib/config.py config.py-20051011043216-070c74f4e9e338e8
bzrlib/tests/test_config.py testconfig.py-20051011041908-742d0c15d8d8c8eb
doc/en/release-notes/bzr-2.5.txt bzr2.5.txt-20110708125756-587p0hpw7oke4h05-1
=== modified file 'bzrlib/add.py'
--- a/bzrlib/add.py 2011-12-19 13:23:58 +0000
+++ b/bzrlib/add.py 2011-12-19 16:41:49 +0000
@@ -75,19 +75,17 @@
class AddWithSkipLargeAction(AddAction):
"""A class that can decide to skip a file if it's considered too large"""
- # default 20 MB
- _DEFAULT_MAX_FILE_SIZE = 20000000
- _optionName = 'add.maximum_file_size'
_maxSize = None
def skip_file(self, tree, path, kind, stat_value = None):
if kind != 'file':
- return False
+ return False
+ opt_name = 'add.maximum_file_size'
if self._maxSize is None:
- config = tree.branch.get_config()
- self._maxSize = config.get_user_option_as_int_from_SI(
- self._optionName,
- self._DEFAULT_MAX_FILE_SIZE)
+ # FIXME: We use the branch config as there is no tree config
+ # -- vila 2011-12-16
+ config = tree.branch.get_config_stack()
+ self._maxSize = config.get(opt_name)
if stat_value is None:
file_size = os.path.getsize(path);
else:
@@ -95,7 +93,7 @@
if self._maxSize > 0 and file_size > self._maxSize:
ui.ui_factory.show_warning(gettext(
"skipping {0} (larger than {1} of {2} bytes)").format(
- path, self._optionName, self._maxSize))
+ path, opt_name, self._maxSize))
return True
return False
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2011-12-19 13:23:58 +0000
+++ b/bzrlib/config.py 2011-12-19 16:41:49 +0000
@@ -442,6 +442,7 @@
l = [l]
return l
+ @deprecated_method(deprecated_in((2, 5, 0)))
def get_user_option_as_int_from_SI(self, option_name, default=None):
"""Get a generic option from a human readable size in SI units, e.g 10MB
@@ -492,6 +493,7 @@
"""See gpg_signing_command()."""
return None
+ @deprecated_method(deprecated_in((2, 5, 0)))
def log_format(self):
"""What log format should be used"""
result = self._log_format()
@@ -2457,6 +2459,34 @@
return int(unicode_str)
+_unit_sfxs = dict(K=10**3, M=10**6, G=10**9)
+
+def int_SI_from_store(unicode_str):
+ """Convert a human readable size in SI units, e.g 10MB into an integer.
+
+ Accepted suffixes are K,M,G. It is case-insensitive and may be followed
+ by a trailing b (i.e. Kb, MB). This is intended to be practical and not
+ pedantic.
+
+ :return Integer, expanded to its base-10 value if a proper SI unit is
+ found, None otherwise.
+ """
+ regexp = "^(\d+)(([" + ''.join(_unit_sfxs) + "])b?)?$"
+ p = re.compile(regexp, re.IGNORECASE)
+ m = p.match(unicode_str)
+ val = None
+ if m is not None:
+ val, _, unit = m.groups()
+ val = int(val)
+ if unit:
+ try:
+ coeff = _unit_sfxs[unit.upper()]
+ except KeyError:
+ raise ValueError(gettext('{0} is not an SI unit.').format(unit))
+ val *= coeff
+ return val
+
+
def float_from_store(unicode_str):
return float(unicode_str)
@@ -2550,6 +2580,17 @@
List of GPG key patterns which are acceptable for verification.
"""))
option_registry.register(
+ Option('add.maximum_file_size',
+ default=u'20MB', from_unicode=int_SI_from_store,
+ help="""\
+Size above which files should be added manually.
+
+Files below this size are added automatically when using ``bzr add`` without
+arguments.
+
+A negative value means disable the size check.
+"""))
+option_registry.register(
Option('bzr.workingtree.worth_saving_limit', default=10,
from_unicode=int_from_store, invalid='warning',
help='''\
=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py 2011-12-15 11:53:48 +0000
+++ b/bzrlib/tests/test_config.py 2011-12-16 16:49:57 +0000
@@ -537,7 +537,9 @@
def test_log_format_default(self):
my_config = config.Config()
- self.assertEqual('long', my_config.log_format())
+ self.assertEqual('long',
+ self.applyDeprecated(deprecated_in((2, 5, 0)),
+ my_config.log_format))
def test_acceptable_keys_default(self):
my_config = config.Config()
@@ -1069,7 +1071,10 @@
si_g = 5g,
si_gb = 5gB,
""")
- get_si = conf.get_user_option_as_int_from_SI
+ def get_si(s, default=None):
+ return self.applyDeprecated(
+ deprecated_in((2, 5, 0)),
+ conf.get_user_option_as_int_from_SI, s, default)
self.assertEqual(100, get_si('plain'))
self.assertEqual(5000, get_si('si_k'))
self.assertEqual(5000, get_si('si_kb'))
@@ -1080,6 +1085,7 @@
self.assertEqual(None, get_si('non-exist'))
self.assertEqual(42, get_si('non-exist-with-default', 42))
+
class TestSupressWarning(TestIniConfig):
def make_warnings_config(self, s):
@@ -1317,7 +1323,9 @@
def test_configured_logformat(self):
my_config = self._get_sample_config()
- self.assertEqual("short", my_config.log_format())
+ self.assertEqual("short",
+ self.applyDeprecated(deprecated_in((2, 5, 0)),
+ my_config.log_format))
def test_configured_acceptable_keys(self):
my_config = self._get_sample_config()
@@ -2358,7 +2366,8 @@
class TestOptionConverterMixin(object):
def assertConverted(self, expected, opt, value):
- self.assertEquals(expected, opt.convert_from_unicode(value))
+ self.assertEquals(expected, opt.convert_from_unicode(value),
+ 'Expecting %s, got %s' % (expected, value,))
def assertWarns(self, opt, value):
warnings = []
@@ -2377,7 +2386,8 @@
def assertConvertInvalid(self, opt, invalid_value):
opt.invalid = None
- self.assertEquals(None, opt.convert_from_unicode(invalid_value))
+ self.assertEquals(None, opt.convert_from_unicode(invalid_value),
+ '%s is not None' % (invalid_value,))
opt.invalid = 'warning'
self.assertWarns(opt, invalid_value)
opt.invalid = 'error'
@@ -2422,6 +2432,31 @@
self.assertConverted(16, opt, u'16')
+class TestOptionWithSIUnitConverter(tests.TestCase, TestOptionConverterMixin):
+
+ def get_option(self):
+ return config.Option('foo', help='An integer in SI units.',
+ from_unicode=config.int_SI_from_store)
+
+ def test_convert_invalid(self):
+ opt = self.get_option()
+ self.assertConvertInvalid(opt, u'not-a-unit')
+ self.assertConvertInvalid(opt, u'Gb') # Forgot the int
+ self.assertConvertInvalid(opt, u'1b') # Forgot the unit
+ self.assertConvertInvalid(opt, u'1GG')
+ self.assertConvertInvalid(opt, u'1Mbb')
+ self.assertConvertInvalid(opt, u'1MM')
+
+ def test_convert_valid(self):
+ opt = self.get_option()
+ self.assertConverted(int(5e3), opt, u'5kb')
+ self.assertConverted(int(5e6), opt, u'5M')
+ self.assertConverted(int(5e6), opt, u'5MB')
+ self.assertConverted(int(5e9), opt, u'5g')
+ self.assertConverted(int(5e9), opt, u'5gB')
+ self.assertConverted(100, opt, u'100')
+
+
class TestOptionWithListConverter(tests.TestCase, TestOptionConverterMixin):
def get_option(self):
=== modified file 'doc/en/release-notes/bzr-2.5.txt'
--- a/doc/en/release-notes/bzr-2.5.txt 2011-12-19 12:09:27 +0000
+++ b/doc/en/release-notes/bzr-2.5.txt 2011-12-19 16:41:49 +0000
@@ -107,6 +107,9 @@
of the ``socket``, ``ssl`` and ``urllib`` modules for
local bzr operations. (Jelmer Vernooij)
+* Configuration options can be SI units by using ``int_SI_from_unicode`` as
+ their ``convert_from_unicode`` helper. (Vincent Ladeuil)
+
* ControlDir now has a get_branches method that returns a dictionary
whose keys are the names of the branches and whose values are the
branches themselves. The active branch uses the key None.
More information about the bazaar-commits
mailing list