[libvirt] [PATCH python] Skip copying manually written python for C APIs which don't exist

From: "Daniel P. Berrange" <berrange@redhat.com> If the libvirt-override-virXXXX.py file has methods which call C APIs that don't exist in the version of libvirt built against we need to skip copying their code. eg for 0.9.13 libvirt we should not copy the 'listAllDomains' method. The way this works is that it breaks the override file into individual methods by looking for ' def '. It then collects the contents until the next method start, whereupon it looks for a libvirtmod.XXXXXX API call. It checks if the XXXXX part is present in the XML description we have, and if not, it discards the entire method. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/generator.py b/generator.py index e8d8ea9..a9f98ab 100755 --- a/generator.py +++ b/generator.py @@ -1719,11 +1719,51 @@ def buildWrappers(module): classes.write("\n") # Append "<classname>.py" to class def, iff it exists try: + wantfuncs = [] extra = open("libvirt-override-" + classname + ".py", "r") classes.write (" #\n") classes.write (" # %s methods from %s.py (hand coded)\n" % (classname,classname)) classes.write (" #\n") - classes.writelines(extra.readlines()) + cached = None + + + # Since we compile with older libvirt, we don't want to pull + # in manually written python methods which call C methods + # that don't exist. This code attempts to detect which + # methods to skip by looking at the libvirtmod.XXXX calls + + def shouldSkip(lines): + for line in lines: + offset = line.find("libvirtmod.") + if offset != -1: + func = line[offset + 11:] + offset = func.find("(") + func = func[0:offset] + if func not in functions_skipped: + return True + return False + + for line in extra.readlines(): + offset = line.find(" def ") + if offset != -1: + name = line[offset+5:] + offset = name.find("(") + name = name[0:offset] + if cached is not None: + if not shouldSkip(cached): + classes.writelines(cached) + if name == "__del__": + cached = None + classes.write(line) + else: + cached = [line] + else: + if cached is not None: + cached.append(line) + else: + classes.write(line) + if not shouldSkip(cached): + classes.writelines(cached) classes.write("\n") extra.close() except: -- 1.8.3.1

On 11/27/2013 04:14 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
If the libvirt-override-virXXXX.py file has methods which call C APIs that don't exist in the version of libvirt built against we need to skip copying their code.
eg for 0.9.13 libvirt we should not copy the 'listAllDomains' method.
The way this works is that it breaks the override file into individual methods by looking for ' def '. It then collects the contents until the next method start, whereupon it looks for a libvirtmod.XXXXXX API call. It checks if the XXXXX part is present in the XML description we have, and if not, it discards the entire method.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- generator.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-)
I see this got committed prior to the release (good); but never reviewed... My python is weak, so I only spotted something on the surface:
+ if offset != -1: + func = line[offset + 11:] + offset = func.find("(") + func = func[0:offset] + if func not in functions_skipped: + return True + return False + + for line in extra.readlines(): + offset = line.find(" def ") + if offset != -1: + name = line[offset+5:]
Inconsistent spacing around '+' Other than that, I assume it works, so belated ack :) -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
participants (2)
-
Daniel P. Berrange
-
Eric Blake