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.py' ):
14+ print ('setup.py file not found' )
15+ sys .exit (1 )
16+
17+ with open ('setup.py' , '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+ deps = [eval (str (d ).strip ()) for d in project_install ]
47+ new_deps = []
48+ for dep in deps :
49+ if '==' in dep or '>=' in dep :
50+ if '>=' in dep :
51+ split_dep = dep .split ('>=' )
52+ else :
53+ split_dep = dep .split ('==' )
54+ if 'itk' in split_dep [0 ] and split_dep [1 ][0 ] == '5' :
55+ new_deps .append (f'{ split_dep [0 ]} == 5.4.*' )
56+ else :
57+ new_deps .append (dep )
58+ else :
59+ new_deps .append (dep )
60+ new_deps_str = ''
61+ for dep in new_deps :
62+ new_deps_str += f' "{ dep } ",\n '
63+ template = template .replace (' "itk == 5.4.*",\n ' , new_deps_str )
64+
65+ # Write the converted data to the new pyproject.toml file
66+ with open ('pyproject.toml' , 'w' ) as f :
67+ f .write (template )
68+
69+
70+ def main ():
71+ parser = argparse .ArgumentParser (description = 'Convert setup.py to pyproject.toml' )
72+ parser .add_argument ('setup_python_path' , help = 'Path to the setup.py file' )
73+ args = parser .parse_args ()
74+
75+ setup_py_to_pyproject_toml (args .setup_python_path )
76+
77+ # Remove setup.py file
78+ os .remove (args .setup_python_path )
79+
80+ if __name__ == '__main__' :
81+ main ()
0 commit comments