44import re
55import subprocess
66
7+ try :
8+ import tomllib # pragma: no cover
9+ except ImportError : # pragma: no cover
10+ try :
11+ import tomli as tomllib # pragma: no cover
12+ except ImportError : # pragma: no cover
13+ tomllib = None # pragma: no cover
14+
715
816def get_package_name_from_setup ():
917 """Parses setup.py to find the package name without raising an error."""
@@ -22,6 +30,7 @@ def get_package_type_from_setup():
2230 """Detects package type based on config files present in the project."""
2331 package_type_files = {
2432 "setup.py" : "python" ,
33+ "pyproject.toml" : "python" ,
2534 "package.json" : "npm" ,
2635 "docker-compose.yml" : "docker" ,
2736 ".ansible-lint" : "ansible" ,
@@ -52,22 +61,49 @@ def detect_changelog_style(changelog_path):
5261def _handle_python_version (config ):
5362 """Handles version detection for Python packages."""
5463 project_name = get_package_name_from_setup ()
55- if not project_name :
64+ if project_name :
65+ package_directory = project_name .replace ("-" , "_" )
66+ init_py_path = os .path .join (package_directory , "__init__.py" )
67+ if os .path .exists (init_py_path ):
68+ with open (init_py_path , "r" ) as f :
69+ content = f .read ()
70+ version_match = re .search (r"^VERSION\s*=\s*\((.*)\)" , content , re .M )
71+ if version_match :
72+ config ["version_path" ] = init_py_path
73+ try :
74+ version_tuple = ast .literal_eval (f"({ version_match .group (1 )} )" )
75+ config ["CURRENT_VERSION" ] = list (version_tuple )
76+ except (ValueError , SyntaxError ):
77+ config ["CURRENT_VERSION" ] = None
78+ return
79+ _handle_pyproject_toml_version (config )
80+
81+
82+ def _handle_pyproject_toml_version (config ):
83+ """Handles version detection from pyproject.toml."""
84+ if not os .path .exists ("pyproject.toml" ):
5685 return
57- package_directory = project_name .replace ("-" , "_" )
58- init_py_path = os .path .join (package_directory , "__init__.py" )
59- if not os .path .exists (init_py_path ):
86+ if tomllib is None :
87+ return # pragma: no cover
88+ with open ("pyproject.toml" , "rb" ) as f :
89+ try :
90+ data = tomllib .load (f )
91+ except Exception :
92+ return
93+ project = data .get ("project" , {})
94+ version_str = project .get ("version" )
95+ if not version_str :
6096 return
61- with open ( init_py_path , "r" ) as f :
62- content = f . read ( )
63- version_match = re . search ( r"^VERSION\s*=\s*\((.*)\)" , content , re . M )
64- if version_match :
65- config [ "version_path" ] = init_py_path
66- try :
67- version_tuple = ast . literal_eval ( f"( { version_match . group ( 1 ) } )" )
68- config ["CURRENT_VERSION " ] = list ( version_tuple )
69- except ( ValueError , SyntaxError ):
70- config ["CURRENT_VERSION" ] = None
97+ try :
98+ parts = version_str . split ( "." )
99+ if len ( parts ) != 3 :
100+ return
101+ current_version = [ int ( parts [ 0 ]), int ( parts [ 1 ]), int ( parts [ 2 ]), "final" ]
102+ except ( ValueError , TypeError ) :
103+ return
104+ config ["package_type " ] = "pyproject"
105+ config [ "version_path" ] = "pyproject.toml"
106+ config ["CURRENT_VERSION" ] = current_version
71107
72108
73109def _handle_npm_version (config ):
@@ -194,6 +230,7 @@ def _handle_openwrt_version(config):
194230# Maps package types to their version detection handlers
195231PACKAGE_VERSION_HANDLERS = {
196232 "python" : _handle_python_version ,
233+ "pyproject" : _handle_pyproject_toml_version ,
197234 "npm" : _handle_npm_version ,
198235 "docker" : _handle_docker_version ,
199236 "ansible" : _handle_ansible_version ,
0 commit comments