qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Cleber Rosa" <crosa@redhat.com>, "Ani Sinha" <ani@anisinha.ca>,
	"Warner Losh" <imp@bsdimp.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Reinoud Zandijk" <reinoud@netbsd.org>,
	"Thomas Huth" <thuth@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"John Snow" <jsnow@redhat.com>, "Ryo ONODERA" <ryoon@netbsd.org>,
	"Kyle Evans" <kevans@freebsd.org>,
	"Beraldo Leal" <bleal@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [RFC PATCH v2 06/10] mkvenv: generate console entry shims from inside the venv
Date: Fri, 14 Apr 2023 01:54:45 -0400	[thread overview]
Message-ID: <20230414055449.4028284-7-jsnow@redhat.com> (raw)
In-Reply-To: <20230414055449.4028284-1-jsnow@redhat.com>

This patch is meant to ensure that console entry point scripts will
always generate on Python 3.7 installations where we may not have access
to importlib.metadata. By running it from a separate process *inside*
the venv, we can be assured to have access to setuptools and by
extension pkg_resources as a fallback.

As a coincidence, it also gives us a pretty good place to do all kinds
of other "in-venv" setup that we might want to do later; for instance
meson and sphinx bootstrapping might be good candidates.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 python/scripts/mkvenv.py | 71 ++++++++++++++++++++++++++++++++--------
 1 file changed, 57 insertions(+), 14 deletions(-)

diff --git a/python/scripts/mkvenv.py b/python/scripts/mkvenv.py
index a3284e9ef1..317697a953 100644
--- a/python/scripts/mkvenv.py
+++ b/python/scripts/mkvenv.py
@@ -62,22 +62,33 @@ def __init__(self, *args, **kwargs) -> None:
         self.script_packages = kwargs.pop('script_packages', ())
         super().__init__(*args, **kwargs)
 
-        # The EnvBuilder class is cute and toggles this setting off
-        # before post_setup, but we need it to decide if we want to
-        # generate shims or not.
-        self._system_site_packages = self.system_site_packages
+        # Make the context available post-creation:
+        self._context: Optional[SimpleNamespace] = None
+
+    def ensure_directories(self, env_dir: DirType) -> SimpleNamespace:
+        logger.debug("ensure_directories(env_dir=%s)", env_dir)
+        self._context = super().ensure_directories(env_dir)
+        return self._context
+
+    def create(self, env_dir: DirType) -> None:
+        logger.debug("create(env_dir=%s)", env_dir)
+        super().create(env_dir)
+        self.post_post_setup(self._context)
 
     def post_setup(self, context: SimpleNamespace) -> None:
         logger.debug("post_setup(...)")
-
-        # Generate console_script entry points for system packages:
-        if self._system_site_packages:
-            generate_console_scripts(
-                context.env_exe, context.bin_path, self.script_packages)
-
         # print the python executable to stdout for configure.
         print(context.env_exe)
 
+    def post_post_setup(self, context: SimpleNamespace) -> None:
+        # The final, final hook; enter the venv and run any
+        # last steps we want to occur *inside* the venv.
+        args = [context.env_exe, __file__, 'post_init',
+                '--binpath', context.bin_path]
+        if self.system_site_packages:
+            args += ['--gen', ",".join(self.script_packages)]
+        subprocess.run(args, check=True)
+
 
 def need_ensurepip() -> bool:
     """
@@ -301,6 +312,10 @@ def generate_console_scripts(python_path: str, bin_path: str,
     """
     Generate script shims for console_script entry points in @packages.
     """
+    logger.debug(
+        "generate_console_scripts(python_path=%s, bin_path=%s, packages=%s)",
+        python_path, bin_path, packages)
+
     if not packages:
         return
 
@@ -334,6 +349,16 @@ def _get_entry_points() -> Iterator[Dict[str, str]]:
         logger.debug("wrote '%s'", script_path)
 
 
+def post_venv_setup(bin_path: str, packages: Sequence[str] = ()) -> None:
+    """
+    This is intended to be run *inside the venv* after it is created.
+    """
+    python_path = sys.executable
+    logger.debug("post_venv_setup(bin_path=%s, packages=%s)",
+                 bin_path, packages)
+    generate_console_scripts(python_path, bin_path, packages)
+
+
 def main() -> int:
     """CLI interface to make_qemu_venv. See module docstring."""
     if os.environ.get('DEBUG') or os.environ.get('GITLAB_CI'):
@@ -366,12 +391,28 @@ def main() -> int:
         help="Target directory to install virtual environment into.",
     )
 
+    subparser = subparsers.add_parser(
+        'post_init', help='post-venv initialization')
+    subparser.add_argument(
+        '--gen',
+        type=str,
+        action='append',
+        help="Regenerate console_scripts for given packages, if found.",
+    )
+    subparser.add_argument(
+        '--binpath',
+        type=str,
+        action='store',
+        help="Path where console script shims should be generated",
+    )
+
     args = parser.parse_args()
+    script_packages = []
+    for element in args.gen or ():
+        script_packages.extend(element.split(","))
+
     try:
         if args.command == 'create':
-            script_packages = []
-            for element in args.gen or ():
-                script_packages.extend(element.split(","))
             make_venv(
                 args.target,
                 system_site_packages=True,
@@ -380,7 +421,9 @@ def main() -> int:
                 with_pip=None,  # Autodetermine
                 script_packages=script_packages,
             )
-            logger.debug("mkvenv.py create - exiting")
+        if args.command == 'post_init':
+            post_venv_setup(args.binpath, script_packages)
+        logger.debug("mkvenv.py %s: exiting", args.command)
     except Ouch as exc:
         print("\n*** Ouch! ***\n", file=sys.stderr)
         print(str(exc), "\n\n", file=sys.stderr)
-- 
2.39.2



  parent reply	other threads:[~2023-04-14  5:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-14  5:54 [RFC PATCH v2 00/10] configure: create a python venv and install meson John Snow
2023-04-14  5:54 ` [RFC PATCH v2 01/10] python: add mkvenv.py John Snow
2023-04-14  5:54 ` [RFC PATCH v2 02/10] tests: add python3-venv dependency John Snow
2023-04-14 14:17   ` Philippe Mathieu-Daudé
2023-04-14  5:54 ` [RFC PATCH v2 03/10] mkvenv: Add better error message for missing pyexapt module John Snow
2023-04-14  5:54 ` [RFC PATCH v2 04/10] tests/vm: Configure netbsd to use Python 3.10 John Snow
2023-04-14  5:54 ` [RFC PATCH v2 05/10] tests/vm: add py310-expat to NetBSD John Snow
2023-04-14  5:54 ` John Snow [this message]
2023-04-14  5:54 ` [RFC PATCH v2 07/10] mkvenv: work around broken pip installations on Debian 10 John Snow
2023-04-14  5:54 ` [RFC PATCH v2 08/10] configure: create a python venv unconditionally John Snow
2023-04-14  5:54 ` [RFC PATCH v2 09/10] configure: remove --meson=; install meson to the pyvenv John Snow
2023-04-14  5:54 ` [RFC PATCH v2 10/10] tests: Use configure-provided pyvenv for tests John Snow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230414055449.4028284-7-jsnow@redhat.com \
    --to=jsnow@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=ani@anisinha.ca \
    --cc=bleal@redhat.com \
    --cc=crosa@redhat.com \
    --cc=imp@bsdimp.com \
    --cc=kevans@freebsd.org \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=reinoud@netbsd.org \
    --cc=ryoon@netbsd.org \
    --cc=thuth@redhat.com \
    --cc=wainersm@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).