[libvirt PATCH 1/2] build: convert the run script to use Python

Daniel P. Berrangé berrange at redhat.com
Wed Mar 24 16:22:44 UTC 2021


On Wed, Mar 24, 2021 at 05:12:45PM +0100, Martin Kletzander wrote:
> On Thu, Mar 18, 2021 at 06:29:17PM +0000, Daniel P. Berrangé wrote:
> > This fits with the goal of eliminating non-Python scripting languages,
> > and makes forthcoming changes far easier.
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
> > ---
> > run.in | 45 ++++++++++++++++++++++++++-------------------
> > 1 file changed, 26 insertions(+), 19 deletions(-)
> > 
> > diff --git a/run.in b/run.in
> > index 6ddf5fc58f..99c67c586a 100644
> > --- a/run.in
> > +++ b/run.in
> > @@ -1,6 +1,6 @@
> > -#!/bin/sh
> > +#!/usr/bin/env python3
> > # libvirt 'run' programs locally script
> > -# Copyright (C) 2012-2013 Red Hat, Inc.
> > +# Copyright (C) 2012-2021 Red Hat, Inc.
> > #
> > # This library is free software; you can redistribute it and/or
> > # modify it under the terms of the GNU Lesser General Public
> > @@ -40,35 +40,42 @@
> > #
> > #----------------------------------------------------------------------
> > 
> > +import os
> > +import os.path
> > +import random
> > +import sys
> > +
> > # Function to intelligently prepend a path to an environment variable.
> > # See https://stackoverflow.com/a/9631350
> > -prepend()
> > -{
> > -    eval $1="$2\${$1:+:\$$1}"
> > -}
> > +def prepend(env, varname, extradir):
> > +    if varname in os.environ:
> > +        env[varname] = extradir + ":" + env[varname]
> > +    else:
> > +        env[varname] = extradir
> > +
> > +here = "@abs_builddir@"
> > 
> > -# Find this script.
> > -b=@abs_builddir@
> > +if len(sys.argv) < 2:
> > +    print("syntax: %s BINARY [ARGS...]" % sys.argv[0], file=sys.stderr)
> > +    sys.exit(1)
> > 
> > -prepend LD_LIBRARY_PATH "$b/src"
> > -export LD_LIBRARY_PATH
> > +prog = sys.argv[1]
> > +args = sys.argv[1:]
> > +env = os.environ
> > 
> > -prepend PKG_CONFIG_PATH "$b/src"
> > -export PKG_CONFIG_PATH
> > 
> > -prepend PATH "$b/tools"
> > -export PATH
> > +prepend(env, "LD_LIBRARY_PATH", os.path.join(here, "src"))
> > +prepend(env, "PKG_CONFIG_PATH", os.path.join(here, "src"))
> > +prepend(env, "PATH", os.path.join(here, "tools"))
> > 
> > # Ensure that any 3rd party apps using libvirt.so from the build tree get
> > # files resolved to the build/source tree too. Typically useful for language
> > # bindings running tests against non-installed libvirt.
> > -LIBVIRT_DIR_OVERRIDE=1
> > -export LIBVIRT_DIR_OVERRIDE
> > +env["LIBVIRT_DIR_OVERRIDE"] = "1"
> > 
> > # This is a cheap way to find some use-after-free and uninitialized
> > # read problems when using glibc.
> > -random_val="$(awk 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null)"
> > -export MALLOC_PERTURB_=$random_val
> > +env["MALLOC_PERTURB_"] = "%d" % random.randint(1, 255)
> > 
> > # Run the program.
> > -exec "$@"
> > +os.execve(prog, args, env)
> 
> This is not functionally equivalent as the shell script modified also its own
> PATH variable and then the exec could search that.  If I am reading the docs
> correctly, then os.execvpe() can do both of that, so I think you meant to use
> that here.

Oh, so you mean you could do  "./run virsh", 
as opposed to what I always did which was
'./run ./build/tools/virsh' ?

> If yes, than with that change:
> 
> Reviewed-by: Martin Kletzander <mkletzan at redhat.com>

I'll double check it works with the suggested change



Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|




More information about the libvir-list mailing list