Skip to content

Commit 63ed7de

Browse files
authored
use token_urlsafe instead of shortuuid to generate temp file (#269)
Python>=3.6 has secrets.token_urlsafe that can create cryptographically safe strings/numbers. So we don't need to use shortuuid. The generated text string is of the same size as `shortuuid.uuid()`, but can contain underscore and hyphen characters (not just alpha-numeric values), which might make it less human-readable. But I am not sure if it's worth it to use a third-party library (which can be gradually removed).
1 parent 6e80fbe commit 63ed7de

File tree

3 files changed

+5
-9
lines changed

3 files changed

+5
-9
lines changed

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ classifiers = [
2222
requires-python = ">=3.8"
2323
dynamic = ["version"]
2424
dependencies = [
25-
"shortuuid>=0.5.0",
2625
"funcy>=1.14",
2726
"fsspec>=2022.10.0",
2827
]

src/dvc_objects/fs/utils.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import threading
88
from concurrent import futures
99
from contextlib import contextmanager, suppress
10+
from secrets import token_urlsafe
1011
from typing import TYPE_CHECKING, Any, Collection, Dict, Iterator, Optional, Set, Union
1112

1213
from dvc_objects.executors import ThreadPoolExecutor
@@ -60,10 +61,8 @@ def move(src: "AnyFSPath", dst: "AnyFSPath") -> None:
6061
case src and dst are on different filesystems and actual physical copying
6162
of data is happening.
6263
"""
63-
from shortuuid import uuid
64-
6564
dst = os.path.abspath(dst)
66-
tmp = f"{dst}.{uuid()}"
65+
tmp = tmp_fname(dst)
6766

6867
if os.path.islink(src):
6968
shutil.copy(src, tmp)
@@ -173,11 +172,9 @@ def copyfile(
173172
shutil.copyfileobj(fsrc, wrapped, length=LOCAL_CHUNK_SIZE)
174173

175174

176-
def tmp_fname(fname: "AnyFSPath" = "") -> "AnyFSPath":
175+
def tmp_fname(prefix: str = "") -> str:
177176
"""Temporary name for a partial download"""
178-
from shortuuid import uuid
179-
180-
return os.fspath(fname) + "." + uuid() + ".tmp"
177+
return f"{prefix}.{token_urlsafe(16)}.tmp"
181178

182179

183180
@contextmanager

tests/fs/test_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_tmp_fname():
1010
file = os.path.join("path", "to", "file")
1111

1212
def pattern(path):
13-
return r"^" + re.escape(path) + r"\.[a-z0-9]{22}\.tmp$"
13+
return r"^" + re.escape(path) + r"\.[a-z0-9_-]{22}\.tmp$"
1414

1515
assert re.search(pattern(file), utils.tmp_fname(file), re.IGNORECASE)
1616
assert re.search(

0 commit comments

Comments
 (0)