rclone-api 1.1.61__py2.py3-none-any.whl → 1.1.63__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/mount.py CHANGED
@@ -6,12 +6,14 @@ import subprocess
6
6
  import time
7
7
  import warnings
8
8
  import weakref
9
- from concurrent.futures import ThreadPoolExecutor
9
+ from concurrent.futures import Future, ThreadPoolExecutor
10
10
  from dataclasses import dataclass
11
11
  from pathlib import Path
12
+ from threading import Lock, Semaphore
12
13
  from typing import Any
13
14
 
14
15
  from rclone_api.process import Process
16
+ from rclone_api.types import SizeSuffix
15
17
 
16
18
  _SYSTEM = platform.system() # "Linux", "Darwin", "Windows", etc.
17
19
 
@@ -283,3 +285,83 @@ def _cache_dir_delete_on_exit(cache_dir: Path) -> None:
283
285
  shutil.rmtree(cache_dir)
284
286
  except Exception as e:
285
287
  warnings.warn(f"Error removing cache directory {cache_dir}: {e}")
288
+
289
+
290
+ class MultiMountFileChunker:
291
+ def __init__(
292
+ self,
293
+ filename: str,
294
+ chunk_size: SizeSuffix,
295
+ mounts: list[Mount],
296
+ executor: ThreadPoolExecutor,
297
+ ) -> None:
298
+ self.filename = filename
299
+ self.chunk_size = chunk_size
300
+ self.executor = executor
301
+ self.mounts_processing: list[Mount] = []
302
+ self.mounts_availabe: list[Mount] = mounts
303
+ self.semaphore = Semaphore(len(mounts))
304
+ self.lock = Lock()
305
+
306
+ def close(self) -> None:
307
+ self.executor.shutdown(wait=False, cancel_futures=True)
308
+ with ThreadPoolExecutor() as executor:
309
+ for mount in self.mounts_processing:
310
+ executor.submit(lambda: mount.close())
311
+
312
+ def fetch(self, offset: int, size: int) -> bytes | Exception:
313
+ try:
314
+ assert offset % self.chunk_size.as_int() == 0
315
+ assert size <= self.chunk_size.as_int()
316
+ assert size > 0
317
+ assert offset >= 0
318
+
319
+ chunks: list[tuple[int, int]] = []
320
+ start = offset
321
+ end = offset + size
322
+ while start < end:
323
+ chunk_size = min(self.chunk_size.as_int(), end - start)
324
+ chunks.append((start, chunk_size))
325
+ start += chunk_size
326
+
327
+ futures: list[Future[bytes | Exception]] = []
328
+ for start, chunk_size in chunks:
329
+ self.semaphore.acquire()
330
+ with self.lock:
331
+ mount = self.mounts_availabe.pop()
332
+ self.mounts_processing.append(mount)
333
+
334
+ path = mount.mount_path / self.filename
335
+
336
+ def task() -> bytes | Exception:
337
+ try:
338
+ with path.open("rb") as f:
339
+ f.seek(offset)
340
+ return f.read(size)
341
+ except KeyboardInterrupt as e:
342
+ import _thread
343
+
344
+ _thread.interrupt_main()
345
+ return Exception(e)
346
+ except Exception as e:
347
+ return e
348
+ finally:
349
+ with self.lock:
350
+ self.mounts_processing.remove(mount)
351
+ self.mounts_availabe.append(mount)
352
+ self.semaphore.release()
353
+
354
+ fut = self.executor.submit(task)
355
+ futures.append(fut)
356
+
357
+ out: bytes = b""
358
+ for fut in futures:
359
+ chunk: bytes | Exception = fut.result()
360
+ if isinstance(chunk, Exception):
361
+ return chunk
362
+ else:
363
+ out += chunk
364
+ del chunk
365
+ return out
366
+ except Exception as e:
367
+ return e
@@ -133,6 +133,7 @@ def _run_profile(
133
133
  if isinstance(bytes_or_err, Exception):
134
134
  print(bytes_or_err)
135
135
  stack_trace = bytes_or_err.__traceback__
136
+ print(f"Error: {bytes_or_err}\nStack trace:\n{stack_trace}")
136
137
  assert False, f"Error: {bytes_or_err}\nStack trace:\n{stack_trace}"
137
138
  assert isinstance(bytes_or_err, bytes)
138
139
  # self.assertEqual(len(bytes_or_err), size)
rclone_api/rclone.py CHANGED
@@ -25,7 +25,7 @@ from rclone_api.dir_listing import DirListing
25
25
  from rclone_api.exec import RcloneExec
26
26
  from rclone_api.file import File
27
27
  from rclone_api.group_files import group_files
28
- from rclone_api.mount import Mount, clean_mount, prepare_mount
28
+ from rclone_api.mount import Mount, MultiMountFileChunker, clean_mount, prepare_mount
29
29
  from rclone_api.process import Process
30
30
  from rclone_api.remote import Remote
31
31
  from rclone_api.rpath import RPath
@@ -829,7 +829,7 @@ class Rclone:
829
829
  """Copy bytes from a file to another file."""
830
830
  from rclone_api.util import random_str
831
831
 
832
- tmp_mnt = Path("tmp_mnt") / random_str(12)
832
+ tmp_mnts = Path("tmp_mnts") / random_str(12)
833
833
  src_parent_path = Path(src).parent.as_posix()
834
834
  src_file = Path(src).name
835
835
  other_args: list[str] = [
@@ -860,7 +860,7 @@ class Rclone:
860
860
  # use scoped mount to do the read, then write the bytes to the destination
861
861
  with self.scoped_mount(
862
862
  src_parent_path,
863
- tmp_mnt,
863
+ tmp_mnts,
864
864
  use_links=True,
865
865
  verbose=mount_log is not None,
866
866
  vfs_cache_mode="minimal",
@@ -869,7 +869,7 @@ class Rclone:
869
869
  cache_dir=cache_dir,
870
870
  cache_dir_delete_on_exit=True,
871
871
  ):
872
- src_file_mnt = tmp_mnt / src_file
872
+ src_file_mnt = tmp_mnts / src_file
873
873
  with open(src_file_mnt, "rb") as f:
874
874
  f.seek(offset)
875
875
  data = f.read(length)
@@ -883,6 +883,85 @@ class Rclone:
883
883
  except Exception as e:
884
884
  return e
885
885
 
886
+ def get_multi_mount_file_chunker(
887
+ self,
888
+ src: str,
889
+ chunk_size: SizeSuffix,
890
+ threads: int,
891
+ mount_log: Path | None,
892
+ direct_io: bool,
893
+ ) -> MultiMountFileChunker:
894
+ from rclone_api.util import random_str
895
+
896
+ mounts: list[Mount] = []
897
+ vfs_read_chunk_size = chunk_size
898
+ vfs_read_chunk_size_limit = chunk_size
899
+ vfs_read_chunk_streams = 0
900
+ vfs_disk_space_total_size = chunk_size
901
+ other_args: list[str] = []
902
+ other_args += ["--no-modtime"]
903
+ other_args += ["--vfs-read-chunk-size", vfs_read_chunk_size.as_str()]
904
+ other_args += [
905
+ "--vfs-read-chunk-size-limit",
906
+ vfs_read_chunk_size_limit.as_str(),
907
+ ]
908
+ other_args += ["--vfs-read-chunk-streams", str(vfs_read_chunk_streams)]
909
+ other_args += [
910
+ "--vfs-disk-space-total-size",
911
+ vfs_disk_space_total_size.as_str(),
912
+ ]
913
+ other_args += ["--read-only"]
914
+ if direct_io:
915
+ other_args += ["--direct-io"]
916
+
917
+ filename = Path(src).name
918
+ with ThreadPoolExecutor(max_workers=threads) as executor:
919
+ futures: list[Future] = []
920
+ try:
921
+ for i in range(threads):
922
+ tmp_mnts = Path("tmp_mnts") / random_str(12)
923
+ verbose = mount_log is not None
924
+ clean_mount(tmp_mnts, verbose=verbose)
925
+ prepare_mount(tmp_mnts, verbose=verbose)
926
+ src_parent_path = Path(src).parent.as_posix()
927
+
928
+ def task(src_parent_path=src_parent_path, tmp_mnts=tmp_mnts):
929
+ return self.mount(
930
+ src=src_parent_path,
931
+ outdir=tmp_mnts,
932
+ allow_writes=False,
933
+ use_links=True,
934
+ vfs_cache_mode="minimal",
935
+ verbose=False,
936
+ cache_dir=Path("cache") / random_str(12),
937
+ cache_dir_delete_on_exit=True,
938
+ log=mount_log,
939
+ other_args=other_args,
940
+ )
941
+
942
+ futures.append(executor.submit(task))
943
+ mount_errors: list[Exception] = []
944
+ for fut in futures:
945
+ try:
946
+ mount = fut.result()
947
+ mounts.append(mount)
948
+ except Exception as er:
949
+ mount_errors.append(er)
950
+ if mount_errors:
951
+ raise Exception(mount_errors)
952
+ except Exception:
953
+ for mount in mounts:
954
+ mount.close()
955
+ raise
956
+ executor = ThreadPoolExecutor(max_workers=threads)
957
+ filechunker: MultiMountFileChunker = MultiMountFileChunker(
958
+ filename=filename,
959
+ chunk_size=chunk_size,
960
+ mounts=mounts,
961
+ executor=executor,
962
+ )
963
+ return filechunker
964
+
886
965
  def copy_bytes(
887
966
  self,
888
967
  src: str,
@@ -897,49 +976,28 @@ class Rclone:
897
976
  ) -> bytes | Exception:
898
977
  """Copy bytes from a file to another file."""
899
978
  # determine number of threads from chunk size
900
- threads = min(max_threads, length // chunk_size.as_int())
901
-
902
- if threads == 1:
903
- return self._copy_bytes(
904
- src,
905
- offset,
906
- length,
907
- transfers=1,
908
- outfile=outfile,
909
- mount_log=mount_log,
910
- direct_io=direct_io,
911
- )
912
- else:
913
- futures: list[Future] = []
914
- with ThreadPoolExecutor(max_workers=threads) as executor:
915
- for i in range(threads):
916
- start = i * chunk_size.as_int()
917
- end = start + chunk_size.as_int()
918
- if i == threads - 1:
919
- end = length
920
- future = executor.submit(
921
- self._copy_bytes,
922
- src,
923
- offset + start,
924
- end - start,
925
- transfers=1,
926
- outfile=None,
927
- mount_log=mount_log,
928
- direct_io=direct_io,
929
- )
930
- futures.append(future)
931
- data: bytes = b""
932
- for future in futures:
933
- chunk: bytes | Exception = future.result()
934
- if isinstance(chunk, Exception):
935
- return chunk
936
- data += chunk
937
- if outfile is not None:
938
- with open(outfile, "wb") as out:
939
- out.write(data)
940
- del data
941
- return bytes(0)
979
+ threads = max(1, min(max_threads, length // chunk_size.as_int()))
980
+ filechunker = self.get_multi_mount_file_chunker(
981
+ src=src,
982
+ chunk_size=chunk_size,
983
+ threads=threads,
984
+ mount_log=mount_log,
985
+ direct_io=direct_io,
986
+ )
987
+ try:
988
+ data = filechunker.fetch(offset, length)
989
+ if isinstance(data, Exception):
990
+ raise data
991
+ if outfile is None:
942
992
  return data
993
+ with open(outfile, "wb") as out:
994
+ out.write(data)
995
+ del data
996
+ return bytes(0)
997
+ except Exception as e:
998
+ return e
999
+ finally:
1000
+ filechunker.close()
943
1001
 
944
1002
  def copy_dir(
945
1003
  self, src: str | Dir, dst: str | Dir, args: list[str] | None = None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rclone_api
3
- Version: 1.1.61
3
+ Version: 1.1.63
4
4
  Summary: rclone api in python
5
5
  Home-page: https://github.com/zackees/rclone-api
6
6
  License: BSD 3-Clause License
@@ -11,9 +11,9 @@ rclone_api/exec.py,sha256=Pd7pUBd8ib5MzqvMybG2DQISPRbDRu20VjVRL2mLAVY,1076
11
11
  rclone_api/file.py,sha256=EP5yT2dZ0H2p7CY5n0y5k5pHhIliV25pm8KOwBklUTk,1863
12
12
  rclone_api/filelist.py,sha256=xbiusvNgaB_b_kQOZoHMJJxn6TWGtPrWd2J042BI28o,767
13
13
  rclone_api/group_files.py,sha256=H92xPW9lQnbNw5KbtZCl00bD6iRh9yRbCuxku4j_3dg,8036
14
- rclone_api/mount.py,sha256=5XiFwP7tst4h8cGQstZMJ0u5VzqFQ4YehnDl2AFP11A,10057
14
+ rclone_api/mount.py,sha256=hd6_zmJ-wH337F0eh9JJMMlVzj92m32tLm2t5qZFxNc,12931
15
15
  rclone_api/process.py,sha256=rBj_S86jC6nqCYop-jq8r9eMSteKeObxUrJMgH8LZvI,5084
16
- rclone_api/rclone.py,sha256=_bXkNBKeAVfjEOmX7bEwpxlXaJURJ3RguY1TJzwIBzM,46038
16
+ rclone_api/rclone.py,sha256=Sd4NrCoCwIe8wfOEpJ-eL6BRozIvCHI6fhnr6BI2ET8,48226
17
17
  rclone_api/remote.py,sha256=O9WDUFQy9f6oT1HdUbTixK2eg0xtBBm8k4Xl6aa6K00,431
18
18
  rclone_api/rpath.py,sha256=8ZA_1wxWtskwcy0I8V2VbjKDmzPkiWd8Q2JQSvh-sYE,2586
19
19
  rclone_api/scan_missing_folders.py,sha256=Kulca2Q6WZodt00ATFHkmqqInuoPvBkhTcS9703y6po,4740
@@ -25,7 +25,7 @@ rclone_api/cmd/copy_large_s3.py,sha256=-rfedi-ZzPUdCSP8ai9LRL0y1xVkvN-viQQlk8HVU
25
25
  rclone_api/cmd/list_files.py,sha256=x8FHODEilwKqwdiU1jdkeJbLwOqUkUQuDWPo2u_zpf0,741
26
26
  rclone_api/experimental/flags.py,sha256=qCVD--fSTmzlk9hloRLr0q9elzAOFzPsvVpKM3aB1Mk,2739
27
27
  rclone_api/experimental/flags_base.py,sha256=ajU_czkTcAxXYU-SlmiCfHY7aCQGHvpCLqJ-Z8uZLk0,2102
28
- rclone_api/profile/mount_copy_bytes.py,sha256=KJhn2ufpRwT9c9bH_q2RWvklw5x63nvI-UMtv9O8CPg,7717
28
+ rclone_api/profile/mount_copy_bytes.py,sha256=lpyviVpOyYq7E4HxKMy_YrCwx4wfy00mr4cn86TkE6I,7786
29
29
  rclone_api/s3/api.py,sha256=qxtRDUpHYqJ7StJRtP8U_PbF_BvYRg705568SyvF-R0,3770
30
30
  rclone_api/s3/basic_ops.py,sha256=hK3366xhVEzEcjz9Gk_8lFx6MRceAk72cax6mUrr6ko,2104
31
31
  rclone_api/s3/chunk_file.py,sha256=D6wM9Nuu73LNGC8JCCfevqjF3qdZ21mQxYQClFLZLMU,3726
@@ -33,9 +33,9 @@ rclone_api/s3/chunk_types.py,sha256=LbXayXY1KgVU1LkdbASD_BQ7TpVpwVnzMjtz--8LBaE,
33
33
  rclone_api/s3/create.py,sha256=wgfkapv_j904CfKuWyiBIWJVxfAx_ftemFSUV14aT68,3149
34
34
  rclone_api/s3/types.py,sha256=yBnJ38Tjk6RlydJ-sqZ7DSfyFloy8KDYJ0mv3vlOzLE,1388
35
35
  rclone_api/s3/upload_file_multipart.py,sha256=y9azNAU8QH5Ovwz33V2HZwNmJdlFjJg-jrXLZ1gtMds,10364
36
- rclone_api-1.1.61.dist-info/LICENSE,sha256=b6pOoifSXiUaz_lDS84vWlG3fr4yUKwB8fzkrH9R8bQ,1064
37
- rclone_api-1.1.61.dist-info/METADATA,sha256=K94aUUT1DvHqha4sUrmbKR18VbBosxwPVUW0tQLAKqU,4537
38
- rclone_api-1.1.61.dist-info/WHEEL,sha256=rF4EZyR2XVS6irmOHQIJx2SUqXLZKRMUrjsg8UwN-XQ,109
39
- rclone_api-1.1.61.dist-info/entry_points.txt,sha256=TV8kwP3FRzYwUEr0RLC7aJh0W03SAefIJNXTJ-FdMIQ,200
40
- rclone_api-1.1.61.dist-info/top_level.txt,sha256=EvZ7uuruUpe9RiUyEp25d1Keq7PWYNT0O_-mr8FCG5g,11
41
- rclone_api-1.1.61.dist-info/RECORD,,
36
+ rclone_api-1.1.63.dist-info/LICENSE,sha256=b6pOoifSXiUaz_lDS84vWlG3fr4yUKwB8fzkrH9R8bQ,1064
37
+ rclone_api-1.1.63.dist-info/METADATA,sha256=kJBxXPg5Yzl1wxdJqir5X4YGJijUJ4Br6c_tlhbv0Lc,4537
38
+ rclone_api-1.1.63.dist-info/WHEEL,sha256=rF4EZyR2XVS6irmOHQIJx2SUqXLZKRMUrjsg8UwN-XQ,109
39
+ rclone_api-1.1.63.dist-info/entry_points.txt,sha256=TV8kwP3FRzYwUEr0RLC7aJh0W03SAefIJNXTJ-FdMIQ,200
40
+ rclone_api-1.1.63.dist-info/top_level.txt,sha256=EvZ7uuruUpe9RiUyEp25d1Keq7PWYNT0O_-mr8FCG5g,11
41
+ rclone_api-1.1.63.dist-info/RECORD,,