Skip to content

Commit 448df20

Browse files
committed
WIP: ENH: Add script to migrate setup.py to pyproject.toml
Contains a few assumptions, but will generate a good starting point for most cases.
1 parent 0d54bc7 commit 448df20

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

setup-py-to-pyproject-toml.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
3+
# This script will convert setup.py to pyproject.toml
4+
# for ITK remote modules. It will also update the
5+
# itk version to 5.4.* in the dependencies section.
6+
7+
import os
8+
import re
9+
import sys
10+
import argparse
11+
12+
def setup_py_to_pyproject_toml(setup_python_path):
13+
if not os.path.exists(setup_python_path):
14+
print('setup.py file not found')
15+
sys.exit(1)
16+
17+
with open(setup_python_path, 'r') as f:
18+
setup_py = f.read()
19+
20+
project_name = re.search(r'name\s*=\s*[\'"]([^\'"]*)[\'"]', setup_py).group(1)
21+
project_version = re.search(r'version\s*=\s*[\'"]([^\'"]*)[\'"]', setup_py).group(1)
22+
project_description = re.search(r'description\s*=\s*[\'"]([^\'"]*)[\'"]', setup_py).group(1)
23+
project_author = re.search(r'author\s*=\s*[\'"]([^\'"]*)[\'"]', setup_py).group(1)
24+
project_author_email = re.search(r'author_email\s*=\s*[\'"]([^\'"]*)[\'"]', setup_py).group(1)
25+
project_url = re.search(r'url\s*=\s*r?[\'"]([^\'"]*)[\'"]', setup_py).group(1)
26+
project_classifiers = re.findall(r'classifiers\s*=\s*\[([^\]]*)\]', setup_py, re.DOTALL)
27+
project_classifiers = project_classifiers[0].replace('\n', '').replace(' ', '').replace('"', '').split(',')
28+
project_packages = re.findall(r'packages\s*=\s*\[([^\]]*)\]', setup_py, re.DOTALL)
29+
project_packages = project_packages[0].replace('\n', '').replace(' ', '').replace('"', '').split(',')
30+
project_install = re.findall(r'install_requires\s*=\s*\[([^\]]*)\]', setup_py, re.DOTALL)
31+
32+
# Load the file "./{{cookiecutter.project_name}}/pyproject.toml" as the pyproject.toml template
33+
script_dir = os.path.dirname(os.path.realpath(__file__))
34+
template_file = os.path.join(script_dir, '{{cookiecutter.project_name}}', 'pyproject.toml')
35+
with open(template_file, 'r') as f:
36+
template = f.read()
37+
38+
# Replace placeholders in the template with the extracted data
39+
template = template.replace('{{ cookiecutter.python_package_name }}', project_name)
40+
template = template.replace('version = "0.1.0"', f'version = "{project_version}"')
41+
template = template.replace('{{ cookiecutter.project_short_description }}', project_description)
42+
template = template.replace('{{ cookiecutter.full_name }}', project_author)
43+
template = template.replace('{{ cookiecutter.email }}', project_author_email)
44+
template = template.replace('{{ cookiecutter.download_url }}', project_url)
45+
46+
setup_dirname = os.path.dirname(setup_python_path)
47+
if os.path.exists(os.path.join(setup_dirname, 'README.md')):
48+
template = template.replace('README.rst', 'README.md')
49+
50+
deps = [eval(str(d).strip()) for d in project_install]
51+
new_deps = []
52+
for dep in deps:
53+
if '==' in dep or '>=' in dep:
54+
if '>=' in dep:
55+
split_dep = dep.split('>=')
56+
else:
57+
split_dep = dep.split('==')
58+
if 'itk' in split_dep[0] and split_dep[1][0] == '5':
59+
new_deps.append(f'{split_dep[0]} == 5.4.*')
60+
else:
61+
new_deps.append(dep)
62+
else:
63+
new_deps.append(dep)
64+
new_deps_str = ''
65+
for dep in new_deps:
66+
new_deps_str += f' "{dep}",\n'
67+
template = template.replace(' "itk == 5.4.*",\n', new_deps_str)
68+
69+
# Write the converted data to the new pyproject.toml file
70+
with open('pyproject.toml', 'w') as f:
71+
f.write(template)
72+
73+
74+
def main():
75+
parser = argparse.ArgumentParser(description='Convert setup.py to pyproject.toml')
76+
parser.add_argument('setup_python_path', help='Path to the setup.py file')
77+
args = parser.parse_args()
78+
79+
setup_py_to_pyproject_toml(args.setup_python_path)
80+
81+
# Remove setup.py file
82+
os.remove(args.setup_python_path)
83+
84+
if __name__ == '__main__':
85+
main()

0 commit comments

Comments
 (0)