public inbox for buildbot@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: "Arsen Arsenović" <arsen@aarsen.me>
Cc: buildbot@sourceware.org
Subject: Re: [PATCH 2/8] autoregen.py: fix a pyright `reportConstantRedefinition` warning
Date: Tue, 12 Mar 2024 12:00:30 -0400	[thread overview]
Message-ID: <75976ed0-ecfc-4abc-9ce7-3188059d4169@efficios.com> (raw)
In-Reply-To: <871q8f7jx5.fsf@aarsen.me>

On 3/12/24 07:39, Arsen Arsenović wrote:
> Hi,
> 
> Simon Marchi <simon.marchi@efficios.com> writes:
> 
>> Not a big deal, and this is certainly opinionated, but pyright tells me:
>>
>>     /home/smarchi/src/builder/builder/containers/autoregen.py
>>       /home/smarchi/src/builder/builder/containers/autoregen.py:24:1 - error: "ENV" is constant (because it is uppercase) and cannot be redefined (reportConstantRedefinition)
>>
>> Switch to a syntax that avoids redefinition of the variable.
>> ---
>>  builder/containers/autoregen.py | 15 +++++++++------
>>  1 file changed, 9 insertions(+), 6 deletions(-)
>>
>> diff --git a/builder/containers/autoregen.py b/builder/containers/autoregen.py
>> index 861a2ce79ef5..feee5878cac7 100755
>> --- a/builder/containers/autoregen.py
>> +++ b/builder/containers/autoregen.py
>> @@ -21,12 +21,15 @@ AUTOHEADER_BIN = next(name for name in autoheader_names if shutil.which(name))
>>  
>>  # autoconf-wrapper and automake-wrapper from Gentoo look at this environment variable.
>>  # It's harmless to set it on other systems though.
>> -ENV = f'WANT_AUTOCONF={AUTOCONF_BIN.split("-", 1)[1]} '
>> -ENV += f'WANT_AUTOMAKE={AUTOMAKE_BIN.split("-", 1)[1]} '
>> -
>> -ENV += f" AUTOCONF={AUTOCONF_BIN} "
>> -ENV += f" ACLOCAL={ACLOCAL_BIN} "
>> -ENV += f" AUTOMAKE={AUTOMAKE_BIN}"
>> +ENV = " ".join(
>> +    (
>> +        f'WANT_AUTOCONF={AUTOCONF_BIN.split("-", 1)[1]}',
>> +        f'WANT_AUTOMAKE={AUTOMAKE_BIN.split("-", 1)[1]}',
>> +        f"AUTOCONF={AUTOCONF_BIN}",
>> +        f"ACLOCAL={ACLOCAL_BIN}",
>> +        f"AUTOMAKE={AUTOMAKE_BIN}",
>> +    )
>> +)
> 
> This is somewhat unimportant, but the following is also valid Python:
>   ENV = (
>       f'WANT_AUTOCONF={AUTOCONF_BIN.split("-", 1)[1]} '
>       f'WANT_AUTOMAKE={AUTOMAKE_BIN.split("-", 1)[1]} '
>       f"AUTOCONF={AUTOCONF_BIN} "
>       f"ACLOCAL={ACLOCAL_BIN} "
>       f"AUTOMAKE={AUTOMAKE_BIN}"
>   )

Personal preference, I prefer to use " ".join instead of embedding the
space in all but the last element.

> More importantly, we should maybe use shlex or such due to potential
> spaces in here?  (not that whatever is invoked by this script won't
> break with those..)

For now, the values the _BIN variables can have are known not to contain
spaces, they come from the _NAME arrays.

> Though, if I am correct, we could also just use subprocess.runs env=
> parameter to put up a modified env.

Yeah that makes more sense.  See patch below that implements this.


From bf7ce70b29f662c48281ce031f10adcb16e53898 Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Tue, 12 Mar 2024 11:49:35 -0400
Subject: [PATCH] autoregen.py: pass environment through subprocess.run's env
 parameter

Arsen pointed out that a better way to pass the environment to the
subprocess (one that is not vulnerable to space splitting, for instance)
is to use subprocess.run's env parameter.

 - Define a new EXTRA_ENV dict with the environment variables we want to
   add to the current process' environment, when running subprocesses
 - Copy the current process' environment into ENV
 - Augment ENV with EXTRA_ENV
 - Use EXTRA_ENV when logging the environment variables
---
 builder/containers/autoregen.py | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/builder/containers/autoregen.py b/builder/containers/autoregen.py
index ac16f54f6caa..8afc168ea811 100755
--- a/builder/containers/autoregen.py
+++ b/builder/containers/autoregen.py
@@ -22,15 +22,15 @@ AUTOHEADER_BIN = next(name for name in AUTOHEADER_NAMES if shutil.which(name))
 
 # autoconf-wrapper and automake-wrapper from Gentoo look at this environment variable.
 # It's harmless to set it on other systems though.
-ENV = " ".join(
-    (
-        f'WANT_AUTOCONF={AUTOCONF_BIN.split("-", 1)[1] if "-" in AUTOCONF_BIN else ""}',
-        f'WANT_AUTOMAKE={AUTOMAKE_BIN.split("-", 1)[1] if "-" in AUTOMAKE_BIN else ""}',
-        f"AUTOCONF={AUTOCONF_BIN}",
-        f"ACLOCAL={ACLOCAL_BIN}",
-        f"AUTOMAKE={AUTOMAKE_BIN}",
-    )
-)
+EXTRA_ENV = {
+    "WANT_AUTOCONF": AUTOCONF_BIN.split("-", 1)[1] if "-" in AUTOCONF_BIN else "",
+    "WANT_AUTOMAKE": AUTOMAKE_BIN.split("-", 1)[1] if "-" in AUTOMAKE_BIN else "",
+    "AUTOCONF": AUTOCONF_BIN,
+    "ACLOCAL": ACLOCAL_BIN,
+    "AUTOMAKE": AUTOMAKE_BIN,
+}
+ENV = os.environ.copy()
+ENV.update(EXTRA_ENV)
 
 
 # Directories we should skip entirely because they're vendored or have different
@@ -48,11 +48,12 @@ SKIP_DIRS = [
 def run_shell(cmd: str):
     print(f"+ {cmd}")
     res = subprocess.run(
-        f"{ENV} {cmd}",
+        f"{cmd}",
         shell=True,
         encoding="utf8",
         stdout=sys.stdout,
         stderr=sys.stderr,
+        env=ENV,
     )
     res.check_returncode()
 
@@ -62,7 +63,7 @@ run_shell(f"{AUTOMAKE_BIN} --version")
 run_shell(f"{ACLOCAL_BIN} --version")
 run_shell(f"{AUTOHEADER_BIN} --version")
 
-print(f"Environment: {ENV}")
+print(f"Extra environment: {EXTRA_ENV}")
 
 config_folders: list[Path] = []
 

base-commit: 3e013842366735c373ef19ec6dfc4a33d6f9c473
-- 
2.44.0

  parent reply	other threads:[~2024-03-12 16:00 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-11 19:01 [PATCH 0/8] Some changes to autoregen.py Simon Marchi
2024-03-11 19:01 ` [PATCH 1/8] autoregen.py: re-format with black 24.2.0 Simon Marchi
2024-03-11 19:01 ` [PATCH 2/8] autoregen.py: fix a pyright `reportConstantRedefinition` warning Simon Marchi
     [not found]   ` <871q8f7jx5.fsf@aarsen.me>
2024-03-12 16:00     ` Simon Marchi [this message]
2024-03-13  8:58       ` Mark Wielaard
2024-03-18 17:36         ` Arsen Arsenović
2024-03-11 19:01 ` [PATCH 3/8] autoregen.py: add type annotation for config_folders Simon Marchi
2024-03-11 19:01 ` [PATCH 4/8] autoregen.py: capitalize some constant names Simon Marchi
2024-03-11 19:01 ` [PATCH 5/8] autoregen.py: add non-suffixed version of the tools Simon Marchi
2024-03-11 19:01 ` [PATCH 6/8] autoregen.py: print executed commands on stdout Simon Marchi
2024-03-11 19:01 ` [PATCH 7/8] autoregen.py: let subprocesses print to stdout/stderr Simon Marchi
     [not found]   ` <87plvz7k10.fsf@aarsen.me>
2024-03-12 15:40     ` Simon Marchi
2024-03-13  8:46       ` Mark Wielaard
2024-03-11 19:01 ` [PATCH 8/8] autoregen.py: be more verbose Simon Marchi
2024-03-11 21:04 ` [PATCH 0/8] Some changes to autoregen.py Sam James
2024-03-11 21:52   ` Mark Wielaard
2024-03-12  1:53     ` Simon Marchi
2024-03-12 10:17       ` Mark Wielaard

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=75976ed0-ecfc-4abc-9ce7-3188059d4169@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=arsen@aarsen.me \
    --cc=buildbot@sourceware.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).