Skip to content

Commit

Permalink
fix(python): Add reusable utility function to fix quoted earthly argu…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
stevenj committed May 8, 2024
1 parent 1806652 commit 7fb8719
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
12 changes: 2 additions & 10 deletions earthly/rust/scripts/std_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import rich

import python.exec_manager as exec_manager
from python.utils import fix_quoted_earthly_args

# This script is run inside the `build` stage.
# This is set up so that ALL build steps are run and it will fail if any fail.
Expand Down Expand Up @@ -251,16 +252,7 @@ def main():
rich.reconfigure(color_system="256")

# Fix arguments because of munging that can happen because of the rust builder +EXECUTE function
processed_args=[]
for arg in sys.argv[1:]:
if arg.endswith('"') and len(processed_args) > 0 and '"' in processed_args[-1]:
processed_args[-1] += " " + arg
processed_args[-1] = processed_args[-1].replace('"', '')
else:
processed_args.append(arg)

# Replace sys.argv with the processed arguments
sys.argv = [sys.argv[0]] + processed_args
fix_quoted_earthly_args()

parser = argparse.ArgumentParser(description="Rust build processing.")
parser.add_argument(
Expand Down
51 changes: 51 additions & 0 deletions utilities/scripts/python/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
"""General Utility Functions."""

import sys
import unittest


def fix_quoted_earthly_args():
"""Arguments that are quoted in Earthly need to be corrected to be parsed.
This function does that by modifying sys.argv in-place.
It should still work with normal argument strings from a command line.
"""

# Fix arguments because of munging that can happen because of the
# rust builder +EXECUTE function and the necessity to quote the arguments.
processed_args = []
for arg in sys.argv[1:]:
# To fix quoting, if the previous arg has one quote, add subsequent args
# to it with a single space until it has more than one quote.
if len(processed_args) > 0 and processed_args[-1].count('"') == 1:
processed_args[-1] += " " + arg
# When we close the quotes, strip them from the argument.
if processed_args[-1].count('"') != 1:
processed_args[-1] = processed_args[-1].replace('"', "")
else:
processed_args.append(arg)

# Replace sys.argv with the processed arguments
sys.argv = [sys.argv[0]] + processed_args


class TestProcessListWithQuotes(unittest.TestCase):

def test_process_list_with_quotes(self):
sys.argv = [sys.argv[0]] + [
"this",
'has "quoted',
"strings",
'in it"',
"this",
"doesn't",
]
expected_result = ["this", 'has quoted strings in it', "this", "doesn't"]
fix_quoted_earthly_args()
self.assertEqual(sys.argv[1:], expected_result)


if __name__ == "__main__":
unittest.main()

0 comments on commit 7fb8719

Please sign in to comment.