diff --git a/audbackend/core/backend/artifactory.py b/audbackend/core/backend/artifactory.py index fb182415..bbc8ed4b 100644 --- a/audbackend/core/backend/artifactory.py +++ b/audbackend/core/backend/artifactory.py @@ -385,9 +385,16 @@ def _put_file( src_path: str, dst_path: str, checksum: str, + owner: str, verbose: bool, ): - r"""Put file to backend.""" + r"""Put file to backend. + + ``owner`` is ignored, + as Artifactory derives the owner + from the user uploading the file. + + """ dst_path = self.path(dst_path) _deploy(src_path, dst_path, checksum, verbose=verbose) diff --git a/audbackend/core/backend/base.py b/audbackend/core/backend/base.py index 1c3cd32a..5e7ed264 100644 --- a/audbackend/core/backend/base.py +++ b/audbackend/core/backend/base.py @@ -1315,6 +1315,7 @@ def _put_file( src_path: str, dst_path: str, checksum: str, + owner: str, verbose: bool, ): # pragma: no cover r"""Put file to backend.""" @@ -1325,6 +1326,7 @@ def put_file( src_path: str, dst_path: str, *, + owner: str = None, validate: bool = False, verbose: bool = False, ): @@ -1345,6 +1347,13 @@ def put_file( Args: src_path: path to local file dst_path: path to file on backend + owner: owner to store with the file. + Only backends that track ownership + as custom metadata + (e.g. :class:`audbackend.backend.Minio`) + make use of this. + If ``None``, + the current user is stored as owner validate: verify file was successfully put on the backend verbose: show debug messages @@ -1378,6 +1387,7 @@ def put_file( src_path, dst_path, checksum, + owner, verbose, ) diff --git a/audbackend/core/backend/filesystem.py b/audbackend/core/backend/filesystem.py index 2f9c87b9..1e41c9e8 100644 --- a/audbackend/core/backend/filesystem.py +++ b/audbackend/core/backend/filesystem.py @@ -213,9 +213,16 @@ def _put_file( src_path: str, dst_path: str, checksum: str, + owner: str, verbose: bool, ): - r"""Put file to backend.""" + r"""Put file to backend. + + ``owner`` is ignored, + as the owner is derived + from the file system. + + """ dst_path = self._expand(dst_path) audeer.mkdir(os.path.dirname(dst_path)) shutil.copy(src_path, dst_path) diff --git a/audbackend/core/backend/minio.py b/audbackend/core/backend/minio.py index 5a6f8569..685fa842 100644 --- a/audbackend/core/backend/minio.py +++ b/audbackend/core/backend/minio.py @@ -264,7 +264,7 @@ def _copy_file( with tempfile.TemporaryDirectory() as tmp_dir: tmp_path = audeer.path(tmp_dir, os.path.basename(src_path)) self._get_file(src_path, tmp_path, num_workers, verbose) - self._put_file(tmp_path, dst_path, checksum, verbose) + self._put_file(tmp_path, dst_path, checksum, None, verbose) else: self._client.copy_object( bucket_name=self.repository, @@ -534,6 +534,7 @@ def _put_file( src_path: str, dst_path: str, checksum: str, + owner: str, verbose: bool, ): r"""Put file to backend.""" @@ -551,7 +552,7 @@ def _put_file( object_name=dst_path, file_path=src_path, content_type=content_type, - metadata=_metadata(checksum), + metadata=_metadata(checksum, owner), ) if verbose: # pragma: no cover @@ -587,18 +588,24 @@ def _size( return size -def _metadata(checksum: str): - """Dictionary with owner entry. +def _metadata(checksum: str, owner: str = None): + """Dictionary with checksum and owner entries. When uploaded as metadata to MinIO, - it can be accessed under ``stat_object(...).metadata["x-amz-meta-owner"]``. + the owner can be accessed under + ``stat_object(...).metadata["x-amz-meta-owner"]``. Args: checksum: checksum to be stored in metadata + owner: owner to be stored in metadata. + If ``None`` or empty, + the current user is used """ + if not owner: + owner = getpass.getuser() return { "checksum": checksum, - "owner": getpass.getuser(), + "owner": owner, } diff --git a/audbackend/core/interface/unversioned.py b/audbackend/core/interface/unversioned.py index 2ab131c3..5b82ff28 100644 --- a/audbackend/core/interface/unversioned.py +++ b/audbackend/core/interface/unversioned.py @@ -624,6 +624,7 @@ def put_file( src_path: str, dst_path: str, *, + owner: str = None, validate: bool = False, verbose: bool = False, ): @@ -644,6 +645,13 @@ def put_file( Args: src_path: path to local file dst_path: path to file on backend + owner: owner to store with the file. + Only backends that track ownership + as custom metadata + (e.g. :class:`audbackend.backend.Minio`) + make use of this. + If ``None``, + the current user is stored as owner validate: verify file was successfully put on the backend verbose: show debug messages @@ -673,6 +681,7 @@ def put_file( self.backend.put_file( src_path, dst_path, + owner=owner, validate=validate, verbose=verbose, ) diff --git a/audbackend/core/interface/versioned.py b/audbackend/core/interface/versioned.py index dad1e152..5c942b2b 100644 --- a/audbackend/core/interface/versioned.py +++ b/audbackend/core/interface/versioned.py @@ -804,6 +804,7 @@ def put_file( dst_path: str, version: str, *, + owner: str = None, validate: bool = False, verbose: bool = False, ): @@ -825,6 +826,13 @@ def put_file( src_path: path to local file dst_path: path to file on backend version: version string + owner: owner to store with the file. + Only backends that track ownership + as custom metadata + (e.g. :class:`audbackend.backend.Minio`) + make use of this. + If ``None``, + the current user is stored as owner validate: verify file was successfully put on the backend verbose: show debug messages @@ -860,6 +868,7 @@ def put_file( return self.backend.put_file( src_path, dst_path_with_version, + owner=owner, validate=validate, verbose=verbose, ) diff --git a/docs/developer-guide.rst b/docs/developer-guide.rst index 4226c389..91f0c8c3 100644 --- a/docs/developer-guide.rst +++ b/docs/developer-guide.rst @@ -357,6 +357,7 @@ a file to our backend. src_path: str, dst_path: str, checksum: str, + owner: str, verbose: bool, ): with self._db as db: @@ -366,7 +367,8 @@ a file to our backend. INSERT INTO data (path, checksum, content, date, owner) VALUES (?, ?, ?, ?, ?) """ - owner = getpass.getuser() + if not owner: + owner = getpass.getuser() date = datetime.datetime.today().strftime("%Y-%m-%d") data = (dst_path, checksum, content, date, owner) db.execute(query, data) diff --git a/tests/bad_file_system.py b/tests/bad_file_system.py index 4078f192..596f6bd5 100644 --- a/tests/bad_file_system.py +++ b/tests/bad_file_system.py @@ -12,6 +12,7 @@ def put_file( src_path: str, dst_path: str, *, + owner: str = None, validate: bool = False, verbose: bool = False, ): @@ -22,6 +23,7 @@ def put_file( src_path, dst_path, checksum, + owner, verbose, ) diff --git a/tests/singlefolder.py b/tests/singlefolder.py index bc64fee1..24b3c5c1 100644 --- a/tests/singlefolder.py +++ b/tests/singlefolder.py @@ -159,6 +159,7 @@ def _put_file( src_path: str, dst_path: str, checksum: str, + owner: str, verbose: bool, ): with self.Map(self._path, self._lock) as m: