From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44854) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZUtPI-0002HI-EI for qemu-devel@nongnu.org; Thu, 27 Aug 2015 05:23:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZUtPE-0002ec-Q8 for qemu-devel@nongnu.org; Thu, 27 Aug 2015 05:23:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50943) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZUtPE-0002eR-IV for qemu-devel@nongnu.org; Thu, 27 Aug 2015 05:23:36 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 2E4B18CF63 for ; Thu, 27 Aug 2015 09:23:36 +0000 (UTC) Date: Thu, 27 Aug 2015 10:23:32 +0100 From: "Daniel P. Berrange" Message-ID: <20150827092332.GD24486@redhat.com> References: <1439798975-2488-1-git-send-email-markmb@redhat.com> <1439798975-2488-3-git-send-email-markmb@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1439798975-2488-3-git-send-email-markmb@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 2/2] Add dynamic generation of module_block.h Reply-To: "Daniel P. Berrange" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Marc =?utf-8?B?TWFyw60=?= Cc: qemu-devel On Mon, Aug 17, 2015 at 10:09:35AM +0200, Marc Mar=C3=AD wrote: > To simplify the addition of new block modules, add a script that genera= tes > include/qemu/module_block.h automatically from the modules' source code= . >=20 > This script assumes that the QEMU coding style rules are followed. >=20 > Signed-off-by: Marc Mar=C3=AD > --- > .gitignore | 1 + > Makefile | 10 ++- > scripts/modules/module_block.py | 132 ++++++++++++++++++++++++++++++++= ++++++++ > 3 files changed, 140 insertions(+), 3 deletions(-) > create mode 100755 scripts/modules/module_block.py I'd expect to see module_block.h deleted in this commit, otherwise you're re-generating a file stored in git each time someone runs make. > diff --git a/scripts/modules/module_block.py b/scripts/modules/module_b= lock.py > new file mode 100755 > index 0000000..a9a9412 > --- /dev/null > +++ b/scripts/modules/module_block.py > @@ -0,0 +1,132 @@ > +#!/usr/bin/python > +# > +# Module information generator > +# > +# Copyright Red Hat, Inc. 2015 > +# > +# Authors: > +# Marc Mari > +# > +# This work is licensed under the terms of the GNU GPL, version 2. > +# See the COPYING file in the top-level directory. > + > +import sys > +import os > + > +def get_string_struct(line): > + data =3D line.split() > + > + # data[0] -> struct element name > + # data[1] -> =3D > + # data[2] -> value > + > + return data[2].replace('"', '')[:-1] > + > +def add_module(fhader, library, format_name, protocol_name, > + probe, probe_device): > + lines =3D [] > + lines.append('.library_name =3D "' + library + '",') > + if format_name !=3D "": > + lines.append('.format_name =3D "' + format_name + '",') > + if protocol_name !=3D "": > + lines.append('.protocol_name =3D "' + protocol_name + '",') > + if probe: > + lines.append('.has_probe =3D true,') > + if probe_device: > + lines.append('.has_probe_device =3D true,') > + > + text =3D '\n\t'.join(lines) > + fheader.write('\n\t{\n\t' + text + '\n\t},') > + > +def process_file(fheader, filename): > + # This parser assumes the coding style rules are being followed > + with open(filename, "r") as cfile: > + found_something =3D False > + found_start =3D False > + library, _ =3D os.path.splitext(os.path.basename(filename)) > + for line in cfile: > + if found_start: > + line =3D line.replace('\n', '') > + if line.find(".format_name") !=3D -1: > + format_name =3D get_string_struct(line) > + elif line.find(".protocol_name") !=3D -1: > + protocol_name =3D get_string_struct(line) > + elif line.find(".bdrv_probe") !=3D -1: > + probe =3D True > + elif line.find(".bdrv_probe_device") !=3D -1: > + probe_device =3D True > + elif line =3D=3D "};": > + add_module(fheader, library, format_name, protocol= _name, > + probe, probe_device) > + found_start =3D False > + elif line.find("static BlockDriver") !=3D -1: > + found_something =3D True > + found_start =3D True > + format_name =3D "" > + protocol_name =3D "" > + probe =3D False > + probe_device =3D False > + > + if not found_something: > + print("No BlockDriver struct found in " + filename + ". \ > + Is this really a module?") > + sys.exit(1) Errors ought to go to sys.stderr rather than stdout. > + > +def print_top(fheader): > + fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ > +/* > + * QEMU Block Module Infrastructure > + * > + * Copyright Red Hat, Inc. 2015 > + * > + * Authors: > + * Marc Mari When the file is auto-generated, I'm not sure it is right to claim copyright / authorship on it - if anything the copyright comes from the files that you're using as the source for auto-generation. > + * > + * This work is licensed under the terms of the GNU GPL, version 2. S= ee > + * the COPYING file in the top-level directory. > + * > + */ > +# First argument: output folder > +# All other arguments: modules source files (.c) > +output_dir =3D sys.argv[1] > +if not os.path.isdir(output_dir): > + print("Folder " + output_dir + " does not exist") Again about stderr > + sys.exit(1) > + > +path =3D output_dir + 'module_block.h' > + > +with open(path, 'w') as fheader: > + print_top(fheader) > + > + for filename in sys.argv[2:]: > + if os.path.isfile(filename): > + process_file(fheader, filename) > + else: > + print("File " + filename + " does not exist.") Again here. > + sys.exit(1) > + > + print_bottom(fheader) > + > +sys.exit(0) Regards, Daniel --=20 |: http://berrange.com -o- http://www.flickr.com/photos/dberrange= / :| |: http://libvirt.org -o- http://virt-manager.or= g :| |: http://autobuild.org -o- http://search.cpan.org/~danberr= / :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vn= c :|