aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/java_command_file.py
diff options
context:
space:
mode:
authorv-korovin <v-korovin@yandex-team.com>2024-06-13 12:27:45 +0300
committerv-korovin <v-korovin@yandex-team.com>2024-06-13 12:42:08 +0300
commitbe66f533d520f6ce67ee7386e807b85230b1c587 (patch)
tree88e0b03ba886fc672fdd99d3c3cda1032ac28061 /build/scripts/java_command_file.py
parent80050a7cd2d471849db213ff374ceeee5614c7e7 (diff)
downloadydb-be66f533d520f6ce67ee7386e807b85230b1c587.tar.gz
Fix java command file in plugin
e43ef305d767fb94adc0f4f6e3901156fc3b7939
Diffstat (limited to 'build/scripts/java_command_file.py')
-rw-r--r--build/scripts/java_command_file.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/build/scripts/java_command_file.py b/build/scripts/java_command_file.py
new file mode 100644
index 0000000000..06d8ea6985
--- /dev/null
+++ b/build/scripts/java_command_file.py
@@ -0,0 +1,43 @@
+import platform
+import subprocess as sp
+
+
+def _java_cmd_file_quote(s):
+ """ Wrap argument based on https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html#java-command-line-argument-files """
+ if not s:
+ return "''"
+
+ if not any(char.isspace() for char in s):
+ return s
+
+ return f'"{s.replace('\\', '\\\\')}"'
+
+
+def call_java_with_command_file(cmd, wrapped_args, **kwargs):
+ is_win = platform.system() == 'Windows'
+
+ args = cmd
+ args_to_wrap = wrapped_args
+ if is_win:
+ args = [cmd[0]]
+ args_to_wrap = cmd[1:] + args_to_wrap
+
+ commands_file = 'wrapped.args'
+ with open(commands_file, 'w') as f:
+ f.write(' '.join(_java_cmd_file_quote(arg) for arg in args_to_wrap))
+
+ if is_win:
+ # Some Windows machines has troubles with running cmd lines with `@` without shell=True
+ kwargs['shell'] = True
+
+ try:
+ return sp.check_output(
+ args + ["@" + commands_file],
+ **kwargs
+ )
+ except Exception as e:
+ if hasattr(e, "add_note"):
+ e.add_note(f"Original command: {cmd} {wrapped_args}")
+ e.add_note(f"Wrapped part: {wrapped_args}")
+
+ raise