[PATCH v5 12/13] scripts: generate_rust_analyzer.py: define scripts

Tamir Duberstein posted 13 patches 10 months, 2 weeks ago
There is a newer version of this series
[PATCH v5 12/13] scripts: generate_rust_analyzer.py: define scripts
Posted by Tamir Duberstein 10 months, 2 weeks ago
Generate rust-project.json entries for scripts written in Rust. This is
possible now that we have a definition for `std` built for the host.

Use `str::rstrip` for consistency.

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 scripts/generate_rust_analyzer.py | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index e3f1ec856ecf..5c0056c265bb 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -249,6 +249,20 @@ def generate_crates(
         cfg=[],
     )
 
+    scripts = srctree / "scripts"
+    with open(scripts / "Makefile") as f:
+        makefile = f.read()
+    for path in scripts.glob("*.rs"):
+        name = path.name.rstrip(".rs")
+        if f"{name}-rust" not in makefile:
+            continue
+        _script = append_crate(
+            name,
+            path,
+            deps=[host_std],
+            cfg=[],
+        )
+
     def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
         try:
             with open(build_file) as f:
@@ -267,7 +281,7 @@ def generate_crates(
     for folder in extra_dirs:
         for path in folder.rglob("*.rs"):
             logging.info("Checking %s", path)
-            name = path.name.replace(".rs", "")
+            name = path.name.rstrip(".rs")
 
             # Skip those that are not crate roots.
             if not is_root_crate(path.parent / "Makefile", name) and \

-- 
2.49.0
Re: [PATCH v5 12/13] scripts: generate_rust_analyzer.py: define scripts
Posted by Trevor Gross 9 months, 3 weeks ago
On Tue, Mar 25, 2025 at 3:07 PM Tamir Duberstein <tamird@gmail.com> wrote:
>
> Generate rust-project.json entries for scripts written in Rust. This is
> possible now that we have a definition for `std` built for the host.
>
> Use `str::rstrip` for consistency.
>
> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
> Reviewed-by: Fiona Behrens <me@kloenk.dev>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
>  scripts/generate_rust_analyzer.py | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)

> +    with open(scripts / "Makefile") as f:
> +        makefile = f.read()

Since we are using pathlib, this can be `makefile = (scripts /
"Makefile").open().read()`

> +    for path in scripts.glob("*.rs"):
> +        name = path.name.rstrip(".rs")

`stem` should provide the file name without extension, so

    name = path.stem

> @@ -267,7 +281,7 @@ def generate_crates(
>      for folder in extra_dirs:
>          for path in folder.rglob("*.rs"):
>              logging.info("Checking %s", path)
> -            name = path.name.replace(".rs", "")
> +            name = path.name.rstrip(".rs")

The above applies here too.

A couple of optional nits if you resend but nothing to block on:

Reviewed-by: Trevor Gross <tmgross@umich.edu>
Re: [PATCH v5 12/13] scripts: generate_rust_analyzer.py: define scripts
Posted by Tamir Duberstein 9 months, 3 weeks ago
On Thu, Apr 17, 2025 at 3:25 AM Trevor Gross <tmgross@umich.edu> wrote:
>
> On Tue, Mar 25, 2025 at 3:07 PM Tamir Duberstein <tamird@gmail.com> wrote:
> >
> > Generate rust-project.json entries for scripts written in Rust. This is
> > possible now that we have a definition for `std` built for the host.
> >
> > Use `str::rstrip` for consistency.
> >
> > Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> > Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
> > Reviewed-by: Fiona Behrens <me@kloenk.dev>
> > Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> > ---
> >  scripts/generate_rust_analyzer.py | 16 +++++++++++++++-
> >  1 file changed, 15 insertions(+), 1 deletion(-)
>
> > +    with open(scripts / "Makefile") as f:
> > +        makefile = f.read()
>
> Since we are using pathlib, this can be `makefile = (scripts /
> "Makefile").open().read()`

I assume you mean `makefile = (scripts / "Makefile").read_text()`;
`.open().read()` would leak a file descriptor.

>
> > +    for path in scripts.glob("*.rs"):
> > +        name = path.name.rstrip(".rs")
>
> `stem` should provide the file name without extension, so
>
>     name = path.stem
>
> > @@ -267,7 +281,7 @@ def generate_crates(
> >      for folder in extra_dirs:
> >          for path in folder.rglob("*.rs"):
> >              logging.info("Checking %s", path)
> > -            name = path.name.replace(".rs", "")
> > +            name = path.name.rstrip(".rs")
>
> The above applies here too.
>
> A couple of optional nits if you resend but nothing to block on:
>
> Reviewed-by: Trevor Gross <tmgross@umich.edu>