Sneaky, gradual migration from Bash to Python for build-script-impl?

Hello! The holiday spirit brought my attention to SR-237. It seems there hasn't been much activity on this front recently. So I want to propose an approach and take a temperature check here.

IIUC, a gradual migration is preferred (e.g this convo). In a nutshell, we could do that by converting Bash code bit by bit within build-script-impl:

#!/usr/bin/env bash

LIST=(a B hElLo woRlD)

ENV_INPUT_TO_UPPER=False

function sameCaseList() {
python << END
# actual Python implementation
def sameCaseList(i, upper):
    if upper:
        return [s.upper() for s in i]
    else:
        return [s.lower() for s in i]

# prepare input
l = "$@".split()

# prepare output
print(' '.join(sameCaseList(l, ${ENV_INPUT_TO_UPPER})))
END
}

for s in $(sameCaseList ${LIST[@]}); do
    echo $s
done

Output:

a
b
hello
world

This fictional example demonstrates that, with some finesse, we can write non-trivial Python code that interfaces well with normal Bash. The Python function get input from both the bash function, as well as an environment variable; it also "returns" a value.

Part of the "finesse" is converting Python's output so that it works with Bash, this could include setting "global" or environment variables. This extra effort may add some bloat to trivial Bash functions that exists. But the bloat gets proportionally smaller and smaller as more code converts to Python.

The Python function can perform any "side effects" (writing to file system) as well as the Bash script.

Using this approach, we can rewrite a lot of the smaller functions first. Those with inner dependencies can merge into the same block of Python code. Eventually, there'd be only one Python block left. Then we just change the shebang to use Python and celebrate.

I like this approach because it fits ours need to convert gradually. It also serves as a forcing function to refactoring the existing Bash logic to smaller pieces (tho it already looks pretty good!).

I can't help but think things won't be this simple. So please let me know if I overlooked some complexities. Other kinds of feedbacks are welcome, too!

There's a much simpler approach: simply translate functions from that giant bash script to Python and move them to the Python build-script or its modules. That's what's being done now, I moved some flags over recently and @drexin was doing a lot more but set it aside.

Such Python porting is usually welcome.

There are also segments of code that are now in Python but simply have not been removed from the bash script yet, such as my pull this summer to remove a bunch of unused flags that had previously been ported or the logic to disable building certain repos which means this old bash logic is unused now.

Terms of Service

Privacy Policy

Cookie Policy