rclone-api 1.4.33__py2.py3-none-any.whl → 1.5.1__py2.py3-none-any.whl

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.
rclone_api/install.py ADDED
@@ -0,0 +1,92 @@
1
+ import os
2
+ import platform
3
+ import shutil
4
+ from pathlib import Path
5
+ from tempfile import TemporaryDirectory
6
+ from warnings import warn
7
+
8
+ from download import download
9
+
10
+ URL_WINDOWS = "https://downloads.rclone.org/rclone-current-windows-amd64.zip"
11
+ URL_LINUX = "https://downloads.rclone.org/rclone-current-linux-amd64.zip"
12
+ URL_MACOS_ARM = "https://downloads.rclone.org/rclone-current-osx-arm64.zip"
13
+ URL_MACOS_X86 = "https://downloads.rclone.org/rclone-current-osx-amd64.zip"
14
+
15
+
16
+ def rclone_download_url() -> str:
17
+ system = platform.system()
18
+ arch = platform.machine()
19
+ if system == "Windows":
20
+ assert "arm" not in arch, f"Unsupported arch: {arch}"
21
+ return URL_WINDOWS
22
+ elif system == "Linux":
23
+ assert "arm" not in arch, f"Unsupported arch: {arch}"
24
+ return URL_LINUX
25
+ elif system == "Darwin":
26
+ if "x86" in arch:
27
+ return URL_MACOS_X86
28
+ elif "arm" in arch:
29
+ return URL_MACOS_ARM
30
+ else:
31
+ raise Exception(f"Unsupported arch: {arch}")
32
+ else:
33
+ raise Exception("Unsupported system")
34
+
35
+
36
+ def _remove_signed_binary_requirements(out: Path) -> None:
37
+ if platform.system() == "Windows":
38
+ return
39
+ # mac os
40
+ if platform.system() == "Darwin":
41
+ # remove signed binary requirements
42
+ #
43
+ # xattr -d com.apple.quarantine rclone
44
+ import subprocess
45
+
46
+ subprocess.run(
47
+ ["xattr", "-d", "com.apple.quarantine", str(out)],
48
+ capture_output=True,
49
+ check=False,
50
+ )
51
+ return
52
+
53
+
54
+ def _make_executable(out: Path) -> None:
55
+ if platform.system() == "Windows":
56
+ return
57
+ # linux and mac os
58
+ os.chmod(out, 0o755)
59
+
60
+
61
+ def _find_rclone_exe(start: Path) -> Path | None:
62
+ for root, dirs, files in os.walk(start):
63
+ if platform.system() == "Windows":
64
+ if "rclone.exe" in files:
65
+ return Path(root) / "rclone.exe"
66
+ else:
67
+ if "rclone" in files:
68
+ return Path(root) / "rclone"
69
+ return None
70
+
71
+
72
+ def rclone_download(out: Path, replace=False) -> Exception | None:
73
+ try:
74
+ url = rclone_download_url()
75
+ with TemporaryDirectory() as tmpdir:
76
+ tmp = Path(tmpdir)
77
+ download(url, tmp, kind="zip", replace=replace)
78
+ exe = _find_rclone_exe(tmp)
79
+ if exe is None:
80
+ raise FileNotFoundError("rclone executable not found")
81
+ if os.path.exists(out):
82
+ os.remove(out)
83
+ shutil.move(exe, out)
84
+ _remove_signed_binary_requirements(out)
85
+ _make_executable(out)
86
+ return None
87
+ except Exception as e:
88
+ import traceback
89
+
90
+ stacktrace = traceback.format_exc()
91
+ warn(f"Failed to download rclone: {e}\n{stacktrace}")
92
+ return e