Skip to content

Commit

Permalink
Python 3.11 (#424)
Browse files Browse the repository at this point in the history
* ArtifactoryPath: fix mkdir and rmdir with Python 3.11

pathlib in Python 3.11 implements mkdir and rmdir directly
in terms of calling os.<func> without any accessor layer.
So copy the implementations of those functions from Python
3.10 to get the previous behavior back.

Fixes: #415

Signed-off-by: Frank Lichtenheld <[email protected]>

* test_artifactory_path: Add basic unit test for mkdir()

Signed-off-by: Frank Lichtenheld <[email protected]>

* ArtifactoryPath: Fix glob() in Python 3.11

Python 3.11 replaced _accessor.scandir with _scandir.
Override _scandir to still use our implementation.
Should be a noop on older Python versions because they
didn't have _scandir at all.

Fixes: #396

Signed-off-by: Frank Lichtenheld <[email protected]>

---------

Signed-off-by: Frank Lichtenheld <[email protected]>
  • Loading branch information
flichtenheld authored Jul 24, 2023
1 parent c0531a5 commit 5278d06
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
29 changes: 29 additions & 0 deletions artifactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,35 @@ def stat(self, pathobj=None):
pathobj = pathobj or self
return self._accessor.stat(pathobj=pathobj)

def mkdir(self, mode=0o777, parents=False, exist_ok=False):
"""
Create a new directory at this given path.
"""
try:
self._accessor.mkdir(self, mode)
except FileNotFoundError:
if not parents or self.parent == self:
raise
self.parent.mkdir(parents=True, exist_ok=True)
self.mkdir(mode, parents=False, exist_ok=exist_ok)
except OSError:
# Cannot rely on checking for EEXIST, since the operating system
# could give priority to other errors like EACCES or EROFS
if not exist_ok or not self.is_dir():
raise

def rmdir(self):
"""
Remove this directory. The directory must be empty.
"""
self._accessor.rmdir(self)

def _scandir(self):
"""
Override Path._scandir. Only required on Python >= 3.11
"""
return self._accessor.scandir(self)

def download_stats(self, pathobj=None):
"""
Item statistics record the number of times an item was downloaded, last download date and last downloader.
Expand Down
39 changes: 38 additions & 1 deletion tests/unit/test_artifactory_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ def setUp(self):
],
"uri": "http://artifactory.local/artifactory/api/storage/libs-release-local",
}
self.dir_mkdir = {
"repo": "libs-release-local",
"path": "/testdir",
"created": "2014-02-18T15:35:29.361+04:00",
"uri": "http://artifactory.local/artifactory/api/storage/libs-release-local",
}

self.property_data = """{
"properties" : {
Expand Down Expand Up @@ -675,8 +681,39 @@ def test_listdir(self):

self.assertRaises(OSError, a.listdir, path)

@responses.activate
def test_mkdir(self):
pass
a = self.cls()

# Directory
path = ArtifactoryPath(
"http://artifactory.local/artifactory/libs-release-local/testdir"
)

constructed_url_stat = "http://artifactory.local/artifactory/api/storage/libs-release-local/testdir"
constructed_url_mkdir = (
"http://artifactory.local/artifactory/libs-release-local/testdir/"
)
responses.add(
responses.GET,
constructed_url_stat,
status=404,
json="""
{
"errors" : [ {
"status" : 404,
"message" : "Not Found."
} ]
}
""",
)
responses.add(
responses.PUT,
constructed_url_mkdir,
status=200,
json=self.dir_mkdir,
)
a.mkdir(path, "")

@responses.activate
def test_get_properties(self):
Expand Down

0 comments on commit 5278d06

Please sign in to comment.