This is a specialized version of: How to replace existing Python class methods of (or otherwise extend) RepoSurgeon by means of 'exec' and 'eval'?
How can one add a new function to RepoSurgeon using the exec
facility (i.e. without editing the upstream code) to add a new function, augmenting existing functionality.
Problem: if you are using RepoSurgeon and you call a function from your script or on the command line and the function throws an exception, this will throw off nested function calls.
Example: you want to find all fossils on a branch by branch name in a single function. The following compound command will do the job for a branch named BRANCH
:
@dsc(@min(/BRANCH/)) list
This works fine if BRANCH
exists. But what if it doesn't? In such a case RepoSurgeon will get quite angry with us and throw an exception:
@dsc(@min(/NO_SUCH_BRANCH/)) list
[...]
ValueError: min() arg is an empty sequence
The problem is that this means that your whole lift script will get derailed.
What to do?
Perhaps one of the easiest solutions is to write your own function and bundle the call sequence of
@dsc(@min(...))
while guarding against exceptions.Assuming you have a lift script, embed the following here-doc as argument to
exec
into it:This implants the local function
brpdsc_handler
as class member of the same name ofclass RepoSurgeon
and makes a function@brpdsc
immediately available to scripts or the command prompt.The factory function exists to carry over the name for
Recoverable
which is an exception class defined in thereposurgeon
script but not available to our code anymore after theexec
has finished. Other symbols can be carried over the same way.Now all that can happen is that we receive:
from RepoSurgeon after attempting to run
@brpdsc/NO_SUCH_BRANCH/) list
.