Skip to content

Commit d0bcec1

Browse files
Fix command line too long on windows. (#509)
* Fix command line too long on windows. * Fix command line too long on windows. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add linux / macos case. * Split functions, legacy and now through script. * Fix transfer on linux / macos. * Remove breakpoint. * Fix tests. * Tidy up. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b82cf9e commit d0bcec1

3 files changed

Lines changed: 49 additions & 11 deletions

File tree

datashuttle/tui/shared/validate_content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
155155
)
156156
if not success:
157157
self.parent_class.mainwindow.show_modal_error_dialog(
158-
cast(str, output) # noqa
158+
cast("str", output)
159159
)
160160
else:
161161
self.write_results_to_richlog(output)

datashuttle/utils/rclone.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import os
2+
import platform
13
import subprocess
4+
import tempfile
25
from pathlib import Path
36
from subprocess import CompletedProcess
47
from typing import Dict, List, Literal
@@ -30,6 +33,45 @@ def call_rclone(command: str, pipe_std: bool = False) -> CompletedProcess:
3033
return output
3134

3235

36+
def call_rclone_through_script(command: str) -> CompletedProcess:
37+
"""
38+
Call rclone through a script, to avoid limits on command-line calls
39+
(in particular on Windows). Used for transfers due to generation of
40+
large call strings.
41+
"""
42+
system = platform.system()
43+
44+
command = "rclone " + command
45+
46+
if system == "Windows":
47+
suffix = ".bat"
48+
else:
49+
suffix = ".sh"
50+
command = "#!/bin/bash\n" + command
51+
52+
with tempfile.NamedTemporaryFile(
53+
mode="w", suffix=suffix, delete=False
54+
) as tmp_script:
55+
tmp_script.write(command)
56+
tmp_script_path = tmp_script.name
57+
58+
try:
59+
if system != "Windows":
60+
os.chmod(tmp_script_path, 0o700)
61+
62+
output = subprocess.run(
63+
[tmp_script_path],
64+
stdout=subprocess.PIPE,
65+
stderr=subprocess.PIPE,
66+
shell=False,
67+
)
68+
69+
finally:
70+
os.remove(tmp_script_path)
71+
72+
return output
73+
74+
3375
# -----------------------------------------------------------------------------
3476
# Setup
3577
# -----------------------------------------------------------------------------
@@ -189,19 +231,17 @@ def transfer_data(
189231
extra_arguments = handle_rclone_arguments(rclone_options, include_list)
190232

191233
if upload_or_download == "upload":
192-
output = call_rclone(
234+
output = call_rclone_through_script(
193235
f"{rclone_args('copy')} "
194236
f'"{local_filepath}" "{cfg.get_rclone_config_name()}:'
195237
f'{central_filepath}" {extra_arguments}',
196-
pipe_std=True,
197238
)
198239

199240
elif upload_or_download == "download":
200-
output = call_rclone(
241+
output = call_rclone_through_script(
201242
f"{rclone_args('copy')} "
202243
f'"{cfg.get_rclone_config_name()}:'
203244
f'{central_filepath}" "{local_filepath}" {extra_arguments}',
204-
pipe_std=True,
205245
)
206246

207247
return output

tests/tests_integration/test_datatypes.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,18 @@ def test_transfer_datatypes(
6363

6464
# Unfortunately on Windows we are encountering 'The command line is too long'
6565
# and so cannot test against all datatypes here.
66-
some_narrow_datatypes = canonical_configs.quick_get_narrow_datatypes()[
67-
:10
68-
]
66+
narrow_datatypes = canonical_configs.quick_get_narrow_datatypes()
6967

7068
datatypes_used = self.get_narrow_only_datatypes_used(used=False)
71-
for key in some_narrow_datatypes:
69+
for key in narrow_datatypes:
7270
datatypes_used[key] = True
7371

7472
test_utils.make_and_check_local_project_folders(
7573
project,
7674
"rawdata",
7775
subs,
7876
sessions,
79-
some_narrow_datatypes,
77+
narrow_datatypes,
8078
datatypes_used,
8179
)
8280

@@ -87,7 +85,7 @@ def test_transfer_datatypes(
8785
project, upload_or_download, "custom", "rawdata"
8886
)
8987

90-
transfer_function("rawdata", "all", "all", some_narrow_datatypes)
88+
transfer_function("rawdata", "all", "all", narrow_datatypes)
9189

9290
test_utils.check_folder_tree_is_correct(
9391
os.path.join(base_path_to_check, "rawdata"),

0 commit comments

Comments
 (0)