Buildroot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [Buildroot] [PATCH 1/1] utils/update-rust: use pathlib with relative paths
@ 2024-06-23 20:58 James Hilliard
  2024-07-10 21:06 ` Thomas Petazzoni via buildroot
  0 siblings, 1 reply; 3+ messages in thread
From: James Hilliard @ 2024-06-23 20:58 UTC (permalink / raw)
  To: buildroot; +Cc: James Hilliard

This avoids the requirement of running update-rust from TOPDIR and
is slightly cleaner.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 utils/update-rust | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/utils/update-rust b/utils/update-rust
index 6799324fac..b274beb85b 100755
--- a/utils/update-rust
+++ b/utils/update-rust
@@ -68,11 +68,11 @@ LICENSES = {
 
 
 def update_mk_file(mk_file, new_version):
-    with open(mk_file, "r") as fd:
+    with mk_file.open("r") as fd:
         lines = fd.readlines()
 
     version_var = pathlib.Path(mk_file).stem.upper().replace("-", "_") + "_VERSION"
-    with open(mk_file, "w") as fd:
+    with mk_file.open("w") as fd:
         for line in lines:
             words = line.split()
             if len(words) == 3 and words[0] == version_var and words[1] == "=":
@@ -82,8 +82,8 @@ def update_mk_file(mk_file, new_version):
 
 
 def gen_hash_file_src(hash_file, new_version):
-    with open(hash_file, "w") as fd:
-        fd.write(f"# Generated with {sys.argv[0]}\n# Do not edit manually\n\n")
+    with hash_file.open("w") as fd:
+        fd.write(f"# Generated with utils/update-rust\n# Do not edit manually\n\n")
         f_name = f"rustc-{new_version}-src.tar.xz"
         print(f"\r\033[KUpdating {f_name}", end="")
         h_url = f"{RUST_DIST_URL}/{f_name}.sha256"
@@ -99,8 +99,8 @@ def gen_hash_file_src(hash_file, new_version):
 
 
 def gen_hash_file_bin(hash_file, new_version):
-    with open(hash_file, "w") as fd:
-        fd.write(f"# Generated with {sys.argv[0]}\n# Do not edit manually\n\n")
+    with hash_file.open("w") as fd:
+        fd.write(f"# Generated with utils/update-rust\n# Do not edit manually\n\n")
         for host in RUST_HOSTS:
             f_name = f"rust-{new_version}-{host}.tar.xz"
             print(f"\r\033[KUpdating {f_name}", end="")
@@ -132,13 +132,11 @@ def main():
 
     args = parser.parse_args()
 
-    try:
-        update_mk_file("package/rust/rust.mk", args.version)
-        update_mk_file("package/rust-bin/rust-bin.mk", args.version)
-        gen_hash_file_src("package/rust/rust.hash", args.version)
-        gen_hash_file_bin("package/rust-bin/rust-bin.hash", args.version)
-    except FileNotFoundError as e:
-        print(f"{e.filename}: {e.strerror} ({sys.argv[0]} must be run at the root of the Buildroot tree)")
+    TOPDIR = pathlib.Path(__file__).parent.parent
+    update_mk_file(TOPDIR / "package/rust/rust.mk", args.version)
+    update_mk_file(TOPDIR / "package/rust-bin/rust-bin.mk", args.version)
+    gen_hash_file_src(TOPDIR / "package/rust/rust.hash", args.version)
+    gen_hash_file_bin(TOPDIR / "package/rust-bin/rust-bin.hash", args.version)
 
     print("\r\033[K", end="")
 
-- 
2.34.1

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [Buildroot] [PATCH 1/1] utils/update-rust: use pathlib with relative paths
  2024-06-23 20:58 [Buildroot] [PATCH 1/1] utils/update-rust: use pathlib with relative paths James Hilliard
@ 2024-07-10 21:06 ` Thomas Petazzoni via buildroot
  2024-07-10 21:17   ` James Hilliard
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Petazzoni via buildroot @ 2024-07-10 21:06 UTC (permalink / raw)
  To: James Hilliard; +Cc: buildroot

Hello James,

On Sun, 23 Jun 2024 14:58:21 -0600
James Hilliard <james.hilliard1@gmail.com> wrote:

> This avoids the requirement of running update-rust from TOPDIR and
> is slightly cleaner.
> 
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>

Applied after fixing a few flake8 issues:

./utils/update-rust:5:1: F401 'sys' imported but unused
./utils/update-rust:86:18: F541 f-string is missing placeholders
./utils/update-rust:103:18: F541 f-string is missing placeholders

Some comments below.

> +    TOPDIR = pathlib.Path(__file__).parent.parent
> +    update_mk_file(TOPDIR / "package/rust/rust.mk", args.version)
> +    update_mk_file(TOPDIR / "package/rust-bin/rust-bin.mk", args.version)
> +    gen_hash_file_src(TOPDIR / "package/rust/rust.hash", args.version)
> +    gen_hash_file_bin(TOPDIR / "package/rust-bin/rust-bin.hash", args.version)

I really didn't know about pathlib.Path(), so very interesting. My
understanding is that TOPDIR / "package" / "rust-bin" / "rust-bin.hash"
would be more correct, but I find it annoying to read.

We have other Python scripts in Buildroot that also need to get the
TOPDIR, and they do it in different ways.

Two times os.path.dirname():

utils/check-package:        base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
utils/check-symbols:    base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

Or os.path.join() with the path to the root:

support/scripts/pkg-stats:brpath = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
support/testing/infra/__init__.py:BASE_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), "../../.."))

we're not very consistent :-)

Thomas
-- 
Thomas Petazzoni, co-owner and CEO, Bootlin
Embedded Linux and Kernel engineering and training
https://bootlin.com
_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Buildroot] [PATCH 1/1] utils/update-rust: use pathlib with relative paths
  2024-07-10 21:06 ` Thomas Petazzoni via buildroot
@ 2024-07-10 21:17   ` James Hilliard
  0 siblings, 0 replies; 3+ messages in thread
From: James Hilliard @ 2024-07-10 21:17 UTC (permalink / raw)
  To: Thomas Petazzoni; +Cc: buildroot

On Wed, Jul 10, 2024 at 3:06 PM Thomas Petazzoni
<thomas.petazzoni@bootlin.com> wrote:
>
> Hello James,
>
> On Sun, 23 Jun 2024 14:58:21 -0600
> James Hilliard <james.hilliard1@gmail.com> wrote:
>
> > This avoids the requirement of running update-rust from TOPDIR and
> > is slightly cleaner.
> >
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
>
> Applied after fixing a few flake8 issues:
>
> ./utils/update-rust:5:1: F401 'sys' imported but unused
> ./utils/update-rust:86:18: F541 f-string is missing placeholders
> ./utils/update-rust:103:18: F541 f-string is missing placeholders
>
> Some comments below.
>
> > +    TOPDIR = pathlib.Path(__file__).parent.parent
> > +    update_mk_file(TOPDIR / "package/rust/rust.mk", args.version)
> > +    update_mk_file(TOPDIR / "package/rust-bin/rust-bin.mk", args.version)
> > +    gen_hash_file_src(TOPDIR / "package/rust/rust.hash", args.version)
> > +    gen_hash_file_bin(TOPDIR / "package/rust-bin/rust-bin.hash", args.version)
>
> I really didn't know about pathlib.Path(), so very interesting. My
> understanding is that TOPDIR / "package" / "rust-bin" / "rust-bin.hash"
> would be more correct, but I find it annoying to read.

Yeah, there's a bunch of different ways one can do this with pathlib, not
sure one is more correct than the other.

>
> We have other Python scripts in Buildroot that also need to get the
> TOPDIR, and they do it in different ways.
>
> Two times os.path.dirname():
>
> utils/check-package:        base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
> utils/check-symbols:    base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
>
> Or os.path.join() with the path to the root:
>
> support/scripts/pkg-stats:brpath = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
> support/testing/infra/__init__.py:BASE_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), "../../.."))
>
> we're not very consistent :-)

Yeah, there's a table here of os path operations to pathlib:
https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module

In general pathlib is the preferred method for handling paths AFAIU as
it's object oriented and cleaner than os.path.

>
> Thomas
> --
> Thomas Petazzoni, co-owner and CEO, Bootlin
> Embedded Linux and Kernel engineering and training
> https://bootlin.com
_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-07-10 21:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-23 20:58 [Buildroot] [PATCH 1/1] utils/update-rust: use pathlib with relative paths James Hilliard
2024-07-10 21:06 ` Thomas Petazzoni via buildroot
2024-07-10 21:17   ` James Hilliard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox