From a1e2f63e64875a29e8c01a7ae17f5744680167a5 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 15 Nov 2010 11:37:14 +0100 Subject: submodule: Fleshed out interface, and a partial test which is not yet usable. It showed that the ConfigParser needs some work. If the root is set, it also needs to refer to the root_commit instead of to the root-tree, as it will have to decide whether it works on the working tree's version of the .gitmodules file or the one in the repository --- lib/git/config.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'lib/git/config.py') diff --git a/lib/git/config.py b/lib/git/config.py index 09bad0b6..e919838b 100644 --- a/lib/git/config.py +++ b/lib/git/config.py @@ -15,7 +15,7 @@ import cStringIO from git.odict import OrderedDict from git.util import LockFile -__all__ = ('GitConfigParser', ) +__all__ = ('GitConfigParser', 'SectionConstraint') class MetaParserBuilder(type): """Utlity class wrapping base-class methods into decorators that assure read-only properties""" @@ -63,7 +63,29 @@ def set_dirty_and_flush_changes(non_const_func): flush_changes.__name__ = non_const_func.__name__ return flush_changes + +class SectionConstraint(object): + """Constrains a ConfigParser to only option commands which are constrained to + always use the section we have been initialized with. + + It supports all ConfigParser methods that operate on an option""" + __slots__ = ("_config", "_section_name") + _valid_attrs_ = ("get_value", "set_value", "get", "set", "getint", "getfloat", "getboolean", "has_option") + def __init__(self, config, section): + self._config = config + self._section_name = section + + def __getattr__(self, attr): + if attr in self._valid_attrs_: + return lambda *args, **kwargs: self._call_config(attr, *args, **kwargs) + return super(SectionConstraint,self).__getattribute__(attr) + + def _call_config(self, method, *args, **kwargs): + """Call the configuration at the given method which must take a section name + as first argument""" + return getattr(self._config, method)(self._section_name, *args, **kwargs) + class GitConfigParser(cp.RawConfigParser, object): """Implements specifics required to read git style configuration files. -- cgit v1.2.1 From 4d36f8ff4d1274a8815e932285ad6dbd6b2888af Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 15 Nov 2010 12:13:59 +0100 Subject: Improved GitConfigurationParser to better deal with streams and the corresponding locks. Submodule class now operates on parent_commits, the configuration is either streamed from the repository or written directly into a blob ( or file ) dependending on whether we have a working tree checkout or not which matches our parent_commit --- lib/git/config.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'lib/git/config.py') diff --git a/lib/git/config.py b/lib/git/config.py index e919838b..8541dc0e 100644 --- a/lib/git/config.py +++ b/lib/git/config.py @@ -271,9 +271,9 @@ class GitConfigParser(cp.RawConfigParser, object): if not hasattr(file_object, "seek"): try: fp = open(file_object) + close_fp = True except IOError,e: continue - close_fp = True # END fp handling try: @@ -308,17 +308,21 @@ class GitConfigParser(cp.RawConfigParser, object): :raise IOError: if this is a read-only writer instance or if we could not obtain a file lock""" self._assure_writable("write") - self._lock._obtain_lock() - fp = self._file_or_files close_fp = False + # we have a physical file on disk, so get a lock + if isinstance(fp, (basestring, file)): + self._lock._obtain_lock() + # END get lock for physical files + if not hasattr(fp, "seek"): fp = open(self._file_or_files, "w") close_fp = True else: fp.seek(0) + # END handle stream or file # WRITE DATA try: @@ -390,7 +394,7 @@ class GitConfigParser(cp.RawConfigParser, object): return valuestr @needs_values - @set_dirty_and_flush_changes + @set_dirty_and_flush_changes def set_value(self, section, option, value): """Sets the given option in section to the given value. It will create the section if required, and will not throw as opposed to the default -- cgit v1.2.1 From d4fd7fca515ba9b088a7c811292f76f47d16cd7b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 16 Nov 2010 00:18:13 +0100 Subject: Submodule now only supports branches to be given as hint that will svn-external like behaviour. Implemented first version of update, which works for now, but probably needs to see more features --- lib/git/config.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'lib/git/config.py') diff --git a/lib/git/config.py b/lib/git/config.py index 8541dc0e..073efd63 100644 --- a/lib/git/config.py +++ b/lib/git/config.py @@ -23,19 +23,23 @@ class MetaParserBuilder(type): """ Equip all base-class methods with a needs_values decorator, and all non-const methods with a set_dirty_and_flush_changes decorator in addition to that.""" - mutating_methods = clsdict['_mutating_methods_'] - for base in bases: - methods = ( t for t in inspect.getmembers(base, inspect.ismethod) if not t[0].startswith("_") ) - for name, method in methods: - if name in clsdict: - continue - method_with_values = needs_values(method) - if name in mutating_methods: - method_with_values = set_dirty_and_flush_changes(method_with_values) - # END mutating methods handling - - clsdict[name] = method_with_values - # END for each base + kmm = '_mutating_methods_' + if kmm in clsdict: + mutating_methods = clsdict[kmm] + for base in bases: + methods = ( t for t in inspect.getmembers(base, inspect.ismethod) if not t[0].startswith("_") ) + for name, method in methods: + if name in clsdict: + continue + method_with_values = needs_values(method) + if name in mutating_methods: + method_with_values = set_dirty_and_flush_changes(method_with_values) + # END mutating methods handling + + clsdict[name] = method_with_values + # END for each name/method pair + # END for each base + # END if mutating methods configuration is set new_type = super(MetaParserBuilder, metacls).__new__(metacls, name, bases, clsdict) return new_type -- cgit v1.2.1 From 9f73e8ba55f33394161b403bf7b8c2e0e05f47b0 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 16 Nov 2010 11:05:31 +0100 Subject: remote: added methods to set and query the tracking branch status of normal heads, including test. Config: SectionConstraint was updated with additional callable methods, the complete ConfigParser interface should be covered now Remote: refs methods is much more efficient now as it will set the search path to the directory containing the remote refs - previously it used the remotes/ base directory and pruned the search result --- lib/git/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/git/config.py') diff --git a/lib/git/config.py b/lib/git/config.py index 073efd63..0528f318 100644 --- a/lib/git/config.py +++ b/lib/git/config.py @@ -74,7 +74,8 @@ class SectionConstraint(object): It supports all ConfigParser methods that operate on an option""" __slots__ = ("_config", "_section_name") - _valid_attrs_ = ("get_value", "set_value", "get", "set", "getint", "getfloat", "getboolean", "has_option") + _valid_attrs_ = ("get_value", "set_value", "get", "set", "getint", "getfloat", "getboolean", "has_option", + "remove_section", "remove_option", "options") def __init__(self, config, section): self._config = config -- cgit v1.2.1 From a1e6234c27abf041e4c8cd1a799950e7cd9104f6 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 17 Nov 2010 15:24:48 +0100 Subject: Inital implementation of Submodule.move including a very simple and to-be-improved test --- lib/git/config.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib/git/config.py') diff --git a/lib/git/config.py b/lib/git/config.py index 0528f318..f1a8832e 100644 --- a/lib/git/config.py +++ b/lib/git/config.py @@ -91,6 +91,11 @@ class SectionConstraint(object): as first argument""" return getattr(self._config, method)(self._section_name, *args, **kwargs) + @property + def config(self): + """return: Configparser instance we constrain""" + return self._config + class GitConfigParser(cp.RawConfigParser, object): """Implements specifics required to read git style configuration files. -- cgit v1.2.1