From 01f8d59474ed7614812ad0bed72ce9f24d6df72a Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Fri, 21 Mar 2008 13:57:15 +0000 Subject: [PATCH] daklib/extensions.py, dak/dak.py, config/debian/extensions.py: move generic extension helpers into daklib.extensions --- config/debian/extensions.py | 28 ++--------------- dak/dak.py | 4 ++- daklib/extensions.py | 61 +++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 daklib/extensions.py diff --git a/config/debian/extensions.py b/config/debian/extensions.py index d586d4d0..9d6bef1c 100644 --- a/config/debian/extensions.py +++ b/config/debian/extensions.py @@ -1,21 +1,7 @@ import sys, os -# This function and its data should move into daklib/extensions.py -# or something. -replaced_funcs = {} -replace_funcs = {} -def replace_dak_function(module,name): - def x(f): - def myfunc(*a,**kw): - global replaced_funcs - f(replaced_funcs[name], *a, **kw) - myfunc.__name__ = f.__name__ - myfunc.__doc__ = f.__doc__ - myfunc.__dict__.update(f.__dict__) - - replace_funcs["%s:%s" % (module,name)] = myfunc - return f - return x +import daklib.extensions +from daklib.extensions import replace_dak_function @replace_dak_function("process-unchecked", "check_signed_by_key") def check_signed_by_key(oldfn): @@ -35,14 +21,4 @@ def check_signed_by_key(oldfn): oldfn() -def init(name): - global replaced_funcs - - # This bit should be done automatically too - replaced_funcs = {} - for f,newfunc in replace_funcs.iteritems(): - m,f = f.split(":",1) - if len(f) > 0 and m == name: - replaced_funcs[f] = dak_module.__dict__[f] - dak_module.__dict__[f] = newfunc diff --git a/dak/dak.py b/dak/dak.py index 10da0411..2e4bd820 100755 --- a/dak/dak.py +++ b/dak/dak.py @@ -29,7 +29,7 @@ ################################################################################ import sys, imp -import daklib.utils +import daklib.utils, daklib.extensions ################################################################################ @@ -209,6 +209,8 @@ def main(): module.dak_userext = userext userext.dak_module = module + + daklib.extensions.init(cmdname, module, userext) if userext.init is not None: userext.init(cmdname) module.main() diff --git a/daklib/extensions.py b/daklib/extensions.py new file mode 100644 index 00000000..d5da89d8 --- /dev/null +++ b/daklib/extensions.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Utility functions for extensions +# Copyright (C) 2008 Anthony Towns + +################################################################################ + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +################################################################################ + +dak_functions_to_replace = {} +dak_replaced_functions = {} + +def replace_dak_function(module,name): + """Decorator to make a function replace a standard dak function + in a given module. The replaced function will be provided as + the first argument.""" + + def x(f): + def myfunc(*a,**kw): + global replaced_funcs + f(dak_replaced_functions[name], *a, **kw) + myfunc.__name__ = f.__name__ + myfunc.__doc__ = f.__doc__ + myfunc.__dict__.update(f.__dict__) + + fnname = "%s:%s" % (module, name) + if fnname in dak_functions_to_replace: + raise Exception, \ + "%s in %s already marked to be replaced" % (name, module) + dak_functions_to_replace["%s:%s" % (module,name)] = myfunc + return f + return x + +################################################################################ + +def init(name, module, userext): + global dak_replaced_functions + + # This bit should be done automatically too + dak_replaced_functions = {} + for f,newfunc in dak_functions_to_replace.iteritems(): + m,f = f.split(":",1) + if len(f) > 0 and m == name: + dak_replaced_functions[f] = module.__dict__[f] + module.__dict__[f] = newfunc + + -- 2.39.5