dais-shell 0.1.9__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: dais-shell
3
- Version: 0.1.9
3
+ Version: 0.2.0
4
4
  Summary: The shell tool for Dais.
5
5
  Author: BHznJNs
6
6
  Author-email: BHznJNs <441768875@qq.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "dais-shell"
3
- version = "0.1.9"
3
+ version = "0.2.0"
4
4
  description = "The shell tool for Dais."
5
5
  readme = "README.md"
6
6
  authors = [{ name = "BHznJNs", email = "441768875@qq.com" }]
@@ -1,4 +1,5 @@
1
1
  import asyncio
2
+ import shlex
2
3
  import shutil
3
4
  from dataclasses import asdict, dataclass
4
5
 
@@ -15,7 +16,7 @@ class BashCommandStep(CommandStep):
15
16
  return cls(**asdict(step))
16
17
 
17
18
  def to_wrapper_script(self):
18
- return 'exec "$1" "${@:2}"'
19
+ return f"exec {shlex.quote(self.command)} {self.args}"
19
20
 
20
21
  # --- --- --- --- --- ---
21
22
 
@@ -36,21 +37,13 @@ class BashRuntime(BaseShellRuntime):
36
37
  self._shell,
37
38
  "-c",
38
39
  step.to_wrapper_script(),
39
- "--",
40
- step.command,
41
- *step.args
42
40
  ]
43
41
 
44
42
  def _prepare_cmd(self, step: CommandStep) -> list[str]:
45
43
  env_expander = EnvExpander(step.env or {})
46
- step.args = [env_expander.expand(arg) for arg in step.args]
44
+ step.args = env_expander.expand(step.args)
47
45
  step = BashCommandStep.from_command_step(step)
48
- resolved = shutil.which(step.command)
49
- is_shell_command = resolved is None
50
- if is_shell_command:
51
- return self._make_bash_commands(step)
52
- else:
53
- return [resolved, *step.args]
46
+ return self._make_bash_commands(step)
54
47
 
55
48
  def run_sync(self,
56
49
  step: CommandStep,
@@ -20,25 +20,8 @@ class PowerShellCommandStep(CommandStep):
20
20
  return cls(**asdict(step))
21
21
 
22
22
  def to_wrapper_script(self):
23
- def ps_encode(s: str) -> str:
24
- return base64.b64encode(s.encode("utf-8")).decode("ascii")
25
-
26
- cmd_json = json.dumps(self.command)
27
- args_json = json.dumps(self.args)
28
-
29
- if len(self.args) > 0:
30
- # encode to base64 and decode in script to prevent single quotes in the args
31
- args_b64 = ps_encode(args_json)
32
- args_line = f"""\
33
- $args_json = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('{args_b64}'))
34
- $arguments = ,(ConvertFrom-Json $args_json)
35
- """
36
- invoke_line = "& $command @arguments"
37
- else:
38
- # when no arguments, PowerShell ConvertFrom-Json will parse as null
39
- # which will cause error for some commands.
40
- args_line = ""
41
- invoke_line = "& $command"
23
+ def ps_quote(s: str) -> str:
24
+ return "'" + s.replace("'", "''") + "'"
42
25
 
43
26
  script = f"""
44
27
  $ErrorActionPreference = "Stop"
@@ -49,10 +32,10 @@ $OutputEncoding = [System.Text.Encoding]::UTF8
49
32
  [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
50
33
  [Console]::InputEncoding = [System.Text.Encoding]::UTF8
51
34
 
52
- $command = ConvertFrom-Json '{cmd_json}'
53
- {args_line}
35
+ $command = {ps_quote(self.command)}
36
+
37
+ & $command {self.args}
54
38
 
55
- {invoke_line}
56
39
  exit $LASTEXITCODE"""
57
40
  return script.strip()
58
41
 
@@ -121,7 +104,7 @@ class PowerShellRuntime(BaseShellRuntime):
121
104
 
122
105
  def _prepare_cmd(self, step: CommandStep) -> list[str]:
123
106
  env_expander = EnvExpander(step.env or {})
124
- step.args = [env_expander.expand(arg) for arg in step.args]
107
+ step.args = env_expander.expand(step.args)
125
108
  step = PowerShellCommandStep.from_command_step(step)
126
109
  script = step.to_wrapper_script()
127
110
  encoded = self._encode(script)
@@ -8,7 +8,7 @@ from .exceptions import ForbiddenShellTargetError
8
8
  @dataclass
9
9
  class CommandStep:
10
10
  command: str
11
- args: list[str]
11
+ args: str
12
12
  cwd: str | Path
13
13
  env: dict[str, str] | None = None
14
14
  timeout: int | None = None
File without changes