From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49452) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZUtd5-0001dD-Jf for qemu-devel@nongnu.org; Thu, 27 Aug 2015 05:37:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZUtd2-0001jj-73 for qemu-devel@nongnu.org; Thu, 27 Aug 2015 05:37:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39714) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZUtd1-0001jd-VZ for qemu-devel@nongnu.org; Thu, 27 Aug 2015 05:37:52 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id AC43FC1A3C5E for ; Thu, 27 Aug 2015 09:37:51 +0000 (UTC) Date: Thu, 27 Aug 2015 11:37:47 +0200 From: Marc =?UTF-8?B?TWFyw60=?= Message-ID: <20150827113747.63251adb@markmb_rh> In-Reply-To: <20150827092332.GD24486@redhat.com> References: <1439798975-2488-1-git-send-email-markmb@redhat.com> <1439798975-2488-3-git-send-email-markmb@redhat.com> <20150827092332.GD24486@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 2/2] Add dynamic generation of module_block.h List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Daniel P. Berrange" Cc: qemu-devel On Thu, 27 Aug 2015 10:23:32 +0100 "Daniel P. Berrange" wrote: > 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 > > generates 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 >=20 > 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. >=20 > > diff --git a/scripts/modules/module_block.py > > b/scripts/modules/module_block.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) >=20 >=20 > Errors ought to go to sys.stderr rather than stdout. >=20 >=20 >=20 > > + > > +def print_top(fheader): > > + fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */ > > +/* > > + * QEMU Block Module Infrastructure > > + * > > + * Copyright Red Hat, Inc. 2015 > > + * > > + * Authors: > > + * Marc Mari >=20 > 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. I just looked at other autogenerated files, and copied the template. Thanks Marc > > + * > > + * This work is licensed under the terms of the GNU GPL, version > > 2. See > > + * the COPYING file in the top-level directory. > > + * > > + */ >=20 > > +# 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") >=20 > Again about stderr >=20 > > + 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.") >=20 > Again here. >=20 > > + sys.exit(1) > > + > > + print_bottom(fheader) > > + > > +sys.exit(0) >=20 > Regards, > Daniel