{"id":263,"date":"2022-09-27T21:05:00","date_gmt":"2022-09-28T02:05:00","guid":{"rendered":"http:\/\/sycured.127.0.0.1.sslip.io\/?p=263"},"modified":"2024-01-14T12:19:54","modified_gmt":"2024-01-14T17:19:54","slug":"rewriting-scripts-in-rust","status":"publish","type":"post","link":"http:\/\/10.42.0.68:8080\/blog\/rewriting-scripts-in-rust","title":{"rendered":"Rewriting scripts with Rust"},"content":{"rendered":"\n
It’s becoming common to see some tools rewrote with Rust like bat<\/a>, dust<\/a>, tokei<\/a> & co.<\/p>\n\n\n\n In my case, it’s rewriting some Bash and Python mostly to extend them more easily.<\/p>\n\n\n\n\n\n\n\n Bash script can become a real pain, for example when I’m writing it on macOS ( Just exporting an environment variable is a rewrite from With Rust, I can do a strict “no external dependency” so the binary built will always work as expected. Python script needs to have Python installed. Sometimes, it’s impossible to have it (ITIL, specific environment requirements & co.) so I need to provide a virtual env. The binary version (Rust): 3.2 MB\u2026 so a 93.6% decrease. The other advantage is for creating an OCI image. Python-based containers are heavy-sized and need some environment variables to work correctly:<\/p>\n\n\n\n Finally, when I develop, Rust is the game changer because the compiler outputs every warning and error instead of partial runs with Python where it’ll fail during the execution.<\/p>\n\n\n\n Honestly, I can continue to need them for specific cases:<\/p>\n\n\n\n Now, Python is like Java before GraalVM<\/a> and more exactly GraalVM – Native Image<\/a>.<\/p>\n\n\n\n My biggest “garbage” in Python was Pandas\u2026 I replaced it with Polars<\/a> (Rust library with Python bindings<\/a>) otherwise I’ll use (and cost) a big compute node for nothing because Pandas is still unoptimized.<\/p>\n","protected":false},"excerpt":{"rendered":" It’s becoming common to see some tools rewrote with Rust like bat, dust, tokei & co. In my case, it’s rewriting some Bash and Python mostly to extend them more easily.<\/p>\n","protected":false},"author":1,"featured_media":106,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[28],"tags":[31],"_links":{"self":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/263"}],"collection":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":1,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":264,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/263\/revisions\/264"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/media\/106"}],"wp:attachment":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Bash to Rust<\/h2>\n\n\n\n
bash<\/code> and
zsh<\/code> are installed) and needs to execute it on some OS with different shells like (t)csh, ksh.<\/p>\n\n\n\n
export NAME=VALUE<\/code> to
setenv NAME VALUE<\/code>, now I let you imagine when it’s directly logical to find available tools like checking if
curl<\/code> or
wget<\/code> is available and some logic about potential failures.<\/p>\n\n\n\n
In addition, the type checking permits avoiding a lot of “bargains” specific to the Bash script.<\/p>\n\n\n\nPython to Rust<\/h2>\n\n\n\n
Providing a 50 MB virtual env for a 60 kB script, it’s unoptimized and mostly a waste of resources\/time.<\/p>\n\n\n\n
I can use scratch<\/code> image as the base for my containers because the binary built is statically linked and doesn’t need an external system like
alpine<\/code> or
busybox<\/code>.<\/p>\n\n\n\n
\n
PYTHONDONTWRITEBYTECODE<\/code>: If this is set to a non-empty string, Python won\u2019t try to write .pyc<\/em> files on the import of source modules.<\/li>\n\n\n\n
PYTHONUNBUFFERED<\/code>: Force the stdout and stderr streams to be unbuffered. (needed for Python older than 3.7)<\/li>\n<\/ul>\n\n\n\n
Do we still need Bash\/Python scripts?<\/h2>\n\n\n\n
\n
autoconf<\/code> and
make<\/code> for example) or to create an OCI image using
buildah<\/code><\/li>\n\n\n\n