Skip to content

Commit

Permalink
Remove tomllib dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
albertyw committed Feb 5, 2024
1 parent da941fc commit 805a50d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
27 changes: 20 additions & 7 deletions req_update/python.py
Expand Up @@ -4,7 +4,6 @@
import os
import re
import subprocess
import tomllib
from typing import Iterator

from req_update.util import Updater, Util
Expand All @@ -16,6 +15,7 @@
PYTHON_PACKAGE_OPERATOR_REGEX = r'(?P<operator>[<=>]+)'
PYTHON_PACKAGE_VERSION_REGEX = r'(?P<version>(\d+!)?(\d+)(\.\d+)+([\.\-\_])?((a(lpha)?|b(eta)?|c|r(c|ev)?|pre(view)?)\d*)?(\.?(post|dev)\d*)?)' # noqa
PYTHON_PACKAGE_SPACER_REGEX = r'(?P<spacer>(,?[ ]+\#)?)'
PYPROJECT_OPTIONAL_DEPS_REGEX = re.compile(r'^([a-zA-Z0-9_]+) = \[')
PYTHON_REQUIREMENTS_LINE_REGEX = re.compile('^%s%s%s%s' % (
PYTHON_PACKAGE_NAME_REGEX,
PYTHON_PACKAGE_OPERATOR_REGEX,
Expand Down Expand Up @@ -206,10 +206,23 @@ def install_updates(self) -> None:
elif updated_file in PYPROJECT_FILES:
command = ['pip', 'install', '-e', '.']
self.util.execute_shell(command, False)
with open(updated_file, 'rb') as handle:
data = tomllib.load(handle)
optionals = data['project'].get('optional-dependencies', {}).keys()
for optional in optionals:
command = ['pip', 'install', '-e', '.[%s]' % optional]
self.util.execute_shell(command, False)

# Install optional dependencies
with open(updated_file, 'r') as handle:
lines = handle.readlines()
optional_dependencies = False
for line in lines:
if line.strip() == '[project.optional-dependencies]':
optional_dependencies = True
continue
if not optional_dependencies:
continue
if line[0] == '[':
break
match = PYPROJECT_OPTIONAL_DEPS_REGEX.match(line)
if not match:
continue
optional = match.group(1)
command = ['pip', 'install', '-e', '.[%s]' % optional]
self.util.execute_shell(command, False)
self.util.log('Installing updated packages in %s' % updated_file)
3 changes: 3 additions & 0 deletions req_update/tests/test_python.py
Expand Up @@ -363,6 +363,7 @@ def test_install_pyproject_updates(self) -> None:
self.python.install_updates()
self.assertEqual(len(self.mock_log.mock_calls), 1)
self.assertEqual(len(self.mock_execute_shell.mock_calls), 1)
self.assertEqual(self.mock_execute_shell.mock_calls[0][1][0][3], '.')

def test_install_pyproject_optional_updates(self) -> None:
pyproject = (
Expand All @@ -377,3 +378,5 @@ def test_install_pyproject_optional_updates(self) -> None:
self.python.install_updates()
self.assertEqual(len(self.mock_log.mock_calls), 1)
self.assertEqual(len(self.mock_execute_shell.mock_calls), 2)
self.assertEqual(self.mock_execute_shell.mock_calls[0][1][0][3], '.')
self.assertEqual(self.mock_execute_shell.mock_calls[1][1][0][3], '.[test]')

0 comments on commit 805a50d

Please sign in to comment.