Skip to content

Commit c9ff45b

Browse files
authoredJul 21, 2024··
Merge pull request #440 from PyAr/support-py36-or-newer
Update different details now we support Py>-3.6 only
2 parents 08be07c + 0ed44ab commit c9ff45b

File tree

6 files changed

+29
-38
lines changed

6 files changed

+29
-38
lines changed
 

‎fades/__init__.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015-2016 Facundo Batista, Nicolás Demarchi
1+
# Copyright 2015-2024 Facundo Batista, Nicolás Demarchi
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General
@@ -26,7 +26,3 @@ class FadesError(Exception):
2626

2727
REPO_PYPI = 'pypi'
2828
REPO_VCS = 'vcs'
29-
30-
# Not using http.server.HTTPStatus to support python < 3.5
31-
HTTP_STATUS_NOT_FOUND = 404
32-
HTTP_STATUS_OK = 200

‎fades/envbuilder.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import pathlib
2222
import shutil
2323

24-
from datetime import datetime
24+
from datetime import datetime, timezone
2525
from venv import EnvBuilder
2626
from uuid import uuid4
2727

@@ -32,6 +32,9 @@
3232

3333
logger = logging.getLogger(__name__)
3434

35+
# UTC can be imported directly from datetime from Python 3.11
36+
UTC = timezone.utc
37+
3538

3639
class _FadesEnvBuilder(EnvBuilder):
3740
"""Create always a virtual environment.
@@ -199,7 +202,7 @@ def _create_initial_usage_file_if_not_exists(self):
199202

200203
def _write_venv_usage(self, file_, venv_data):
201204
_, uuid = os.path.split(venv_data['env_path'])
202-
file_.write('{} {}\n'.format(uuid, self._datetime_to_str(datetime.utcnow())))
205+
file_.write('{} {}\n'.format(uuid, self._datetime_to_str(datetime.now(UTC))))
203206

204207
def _datetime_to_str(self, datetime_):
205208
return datetime.strftime(datetime_, "%Y-%m-%dT%H:%M:%S.%f")
@@ -219,7 +222,7 @@ def clean_unused_venvs(self, max_days_to_keep):
219222
called, this records will be deleted.
220223
"""
221224
with filelock(self.stat_file_lock):
222-
now = datetime.utcnow()
225+
now = datetime.now(UTC)
223226
venvs_dict = self._get_compacted_dict_usage_from_file()
224227
for venv_uuid, usage_date in venvs_dict.copy().items():
225228
usage_date = self._str_to_datetime(usage_date)

‎fades/helpers.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
import logging
2323
import subprocess
2424
import tempfile
25+
from http.server import HTTPStatus
2526
from urllib import request, parse
2627
from urllib.error import HTTPError
2728

2829
from packaging.requirements import Requirement
2930
from packaging.version import Version
3031

31-
from fades import FadesError, HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK, _version
32+
from fades import FadesError, _version
3233

3334
logger = logging.getLogger(__name__)
3435

@@ -227,20 +228,19 @@ def _pypi_head_package(dependency):
227228
try:
228229
response = request.urlopen(req)
229230
except HTTPError as http_error:
230-
if http_error.code == HTTP_STATUS_NOT_FOUND:
231+
if http_error.code == HTTPStatus.NOT_FOUND:
231232
return False
232233
else:
233234
raise
234-
if response.status == HTTP_STATUS_OK:
235+
if response.status == HTTPStatus.OK:
235236
logger.debug("%r exists in PyPI.", dependency)
236-
return True
237237
else:
238238
# Maybe we are getting somethink like a redirect. In this case we are only
239239
# warning to the user and trying to install the dependency.
240240
# In the worst scenery fades will fail to install it.
241241
logger.warning("Got a (unexpected) HTTP_STATUS=%r and reason=%r checking if %r exists",
242242
response.status, response.reason, dependency)
243-
return True
243+
return True
244244

245245

246246
def check_pypi_exists(dependencies):

‎tests/conftest.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,30 @@
1414
#
1515
# For further info, check https://github.com/PyAr/fades
1616

17-
import shutil
1817
import uuid
1918

2019
from pytest import fixture
2120

2221

2322
@fixture(scope="function")
24-
def tmp_file(tmpdir_factory):
23+
def tmp_file(tmp_path):
2524
"""Fixture for a unique tmpfile for each test."""
26-
dir_path = tmpdir_factory.mktemp("test")
27-
yield str(dir_path.join("testfile")) # Converted to str to support python <3.6 versions
28-
shutil.rmtree(str(dir_path))
25+
yield str(tmp_path / "testfile") # XXX Facundo 2024-04-17: remove str() after #435
2926

3027

3128
@fixture(scope="function")
32-
def create_tmpfile(tmpdir_factory):
33-
dir_path = tmpdir_factory.mktemp("test")
29+
def create_tmpfile(tmp_path):
3430

3531
def add_content(lines):
3632
"""Fixture for a unique tmpfile for each test."""
37-
namefile = str(
38-
dir_path.join("testfile_{}".format(uuid.uuid4()))
39-
) # Converted to str to support python <3.6 versions
33+
namefile = tmp_path / f"testfile_{uuid.uuid4()}"
4034
with open(namefile, "w", encoding="utf-8") as f:
4135
for line in lines:
4236
f.write(line + "\n")
4337

4438
return namefile
4539

4640
yield add_content
47-
shutil.rmtree(str(dir_path))
4841

4942

5043
def pytest_addoption(parser):

‎tests/test_envbuilder.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ def test_usage_record_is_recorded(self):
296296
msg="Selected uuid is two times in file")
297297

298298
def test_usage_file_is_compacted_when_though_no_venv_is_removed(self):
299-
old_date = datetime.utcnow()
299+
old_date = datetime.now()
300300
new_date = old_date + timedelta(days=1)
301301

302302
with patch('fades.envbuilder.datetime') as mock_datetime:
303-
mock_datetime.utcnow.return_value = old_date
303+
mock_datetime.now.return_value = old_date
304304
mock_datetime.strptime.side_effect = lambda *args, **kw: datetime.strptime(*args, **kw)
305305
mock_datetime.strftime.side_effect = lambda *args, **kw: datetime.strftime(*args, **kw)
306306

@@ -312,7 +312,7 @@ def test_usage_file_is_compacted_when_though_no_venv_is_removed(self):
312312
venv = self.venvscache.get_venv(uuid=self.uuids[0])
313313
manager.store_usage_stat(venv, self.venvscache)
314314

315-
mock_datetime.utcnow.return_value = new_date
315+
mock_datetime.now.return_value = new_date
316316
manager.store_usage_stat(venv, self.venvscache)
317317

318318
lines = self.get_usage_lines(manager)
@@ -351,11 +351,11 @@ def test_general_error_exception(self):
351351
self.assertEqual(str(cm.exception), "General error while running external venv")
352352

353353
def test_when_a_venv_is_removed_it_is_removed_from_everywhere(self):
354-
old_date = datetime.utcnow()
354+
old_date = datetime.now()
355355
new_date = old_date + timedelta(days=5)
356356

357357
with patch('fades.envbuilder.datetime') as mock_datetime:
358-
mock_datetime.utcnow.return_value = old_date
358+
mock_datetime.now.return_value = old_date
359359
mock_datetime.strptime.side_effect = lambda *args, **kw: datetime.strptime(*args, **kw)
360360
mock_datetime.strftime.side_effect = lambda *args, **kw: datetime.strftime(*args, **kw)
361361

@@ -367,7 +367,7 @@ def test_when_a_venv_is_removed_it_is_removed_from_everywhere(self):
367367
venv = self.venvscache.get_venv(uuid=self.uuids[0])
368368
manager.store_usage_stat(venv, self.venvscache)
369369

370-
mock_datetime.utcnow.return_value = new_date
370+
mock_datetime.now.return_value = new_date
371371
manager.store_usage_stat(venv, self.venvscache)
372372

373373
lines = self.get_usage_lines(manager)

‎tests/test_helpers.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import sys
2323
import tempfile
2424
import unittest
25-
25+
from http.server import HTTPStatus
2626
from unittest.mock import patch
2727
from urllib.error import HTTPError
2828
from urllib.request import Request
@@ -31,8 +31,7 @@
3131
import pytest
3232
from xdg import BaseDirectory
3333

34-
from fades import HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK, helpers
35-
from fades import parsing
34+
from fades import helpers, parsing
3635

3736

3837
PATH_TO_EXAMPLES = "tests/examples/"
@@ -307,7 +306,7 @@ def test_exists(self):
307306

308307
with patch('urllib.request.urlopen') as mock_urlopen:
309308
with patch('http.client.HTTPResponse') as mock_http_response:
310-
mock_http_response.status = HTTP_STATUS_OK
309+
mock_http_response.status = HTTPStatus.OK
311310
mock_urlopen.return_value = mock_http_response
312311

313312
exists = helpers.check_pypi_exists(deps)
@@ -319,7 +318,7 @@ def test_all_exists(self):
319318

320319
with patch('urllib.request.urlopen') as mock_urlopen:
321320
with patch('http.client.HTTPResponse') as mock_http_response:
322-
mock_http_response.status = HTTP_STATUS_OK
321+
mock_http_response.status = HTTPStatus.OK
323322
mock_urlopen.side_effect = [mock_http_response] * 3
324323

325324
exists = helpers.check_pypi_exists(dependencies)
@@ -330,7 +329,7 @@ def test_doesnt_exists(self):
330329
dependency = parsing.parse_manual(["foo"])
331330

332331
with patch('urllib.request.urlopen') as mock_urlopen:
333-
mock_http_error = HTTPError("url", HTTP_STATUS_NOT_FOUND, "mgs", {}, io.BytesIO())
332+
mock_http_error = HTTPError("url", HTTPStatus.NOT_FOUND, "mgs", {}, io.BytesIO())
334333
mock_urlopen.side_effect = mock_http_error
335334

336335
exists = helpers.check_pypi_exists(dependency)
@@ -343,8 +342,8 @@ def test_one_doesnt_exists(self):
343342

344343
with patch('urllib.request.urlopen') as mock_urlopen:
345344
with patch('http.client.HTTPResponse') as mock_http_response:
346-
mock_http_error = HTTPError("url", HTTP_STATUS_NOT_FOUND, "mgs", {}, io.BytesIO())
347-
mock_http_response.status = HTTP_STATUS_OK
345+
mock_http_error = HTTPError("url", HTTPStatus.NOT_FOUND, "mgs", {}, io.BytesIO())
346+
mock_http_response.status = HTTPStatus.OK
348347
mock_urlopen.side_effect = [mock_http_response, mock_http_error]
349348

350349
exists = helpers.check_pypi_exists(dependencies)

0 commit comments

Comments
 (0)
Please sign in to comment.