-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command for migrating config to pyproject.toml.
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pathlib | ||
import re | ||
import subprocess | ||
|
||
import autocommand | ||
from ini2toml.api import Translator | ||
|
||
|
||
def split_build_system(pyproject): | ||
""" | ||
Return the first section up to the second section, and the rest. | ||
""" | ||
# put the toml ahead of the second section | ||
section_starts = re.finditer(r'^\[', pyproject, re.MULTILINE) | ||
_, second = next(section_starts), next(section_starts) | ||
return pyproject[: second.start()], pyproject[second.start() :] | ||
|
||
|
||
def tweak(config): | ||
""" | ||
Tweak the syntax some for easier merging with skeleton. | ||
""" | ||
config = re.sub( | ||
r'^(authors|maintainers) = \[(.*?)\]', | ||
r'\1 = [\n\t\2,\n]', | ||
config, | ||
flags=re.MULTILINE | re.DOTALL, | ||
) | ||
config = re.sub( | ||
r'{(name = .*?")}', r'{ \1 }', config, flags=re.MULTILINE | re.DOTALL | ||
) | ||
config = re.sub(r'^ ', '\t', config, flags=re.MULTILINE) | ||
homepage = re.search('Homepage = ".*"', config).group(0) | ||
config = re.sub(r'^urls =.*?\n', '', config, flags=re.MULTILINE) | ||
homepage_scn = f'[project.urls]\n{homepage}\n\n' | ||
config = re.sub(r'(\[project\.scripts\])', homepage_scn + r'\1', config) | ||
config = re.sub( | ||
r'(\n\[tool\.setuptools\]\ninclude-package-data = true\n)', | ||
'', | ||
config, | ||
flags=re.MULTILINE, | ||
) | ||
config = re.sub(r'^\t# local', '\n\t# local', config, flags=re.MULTILINE) | ||
return config | ||
|
||
|
||
def bump_setuptools(build_system): | ||
return build_system.replace('setuptool>=56', 'setuptools>=61.2') | ||
|
||
|
||
@autocommand.autocommand(__name__) | ||
def run(): | ||
pyproject = pathlib.Path('pyproject.toml') | ||
config = Translator().translate(pathlib.Path('setup.cfg').read_text(), 'setup.cfg') | ||
config_bs, config_rest = split_build_system(config) | ||
existing_bs, existing_rest = split_build_system(pyproject.read_text()) | ||
pyproject.write_text( | ||
bump_setuptools(existing_bs) + tweak(config_rest) + '\n' + existing_rest | ||
) | ||
subprocess.check_call('git rm setup.cfg'.split()) | ||
message = ( | ||
"Migrated config to pyproject.toml using jaraco.develop.migrate-config " | ||
"and ini2toml." | ||
) | ||
subprocess.check_call(['git', 'commit', '-a', '-m', message]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add command for migrating config to pyproject.toml. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters