@@ -15,28 +15,77 @@ def get_version():
15
15
16
16
# Update version in __init__.py
17
17
init_path = os .path .join ('simpletool' , '__init__.py' )
18
- with open (init_path , 'r' , encoding = 'utf-8' ) as init_file :
19
- init_content = init_file .read ()
18
+ try :
19
+ with open (init_path , 'r' , encoding = 'utf-8' ) as init_file :
20
+ init_content = init_file .read ()
20
21
21
- # Replace version in the header
22
- updated_init_content = re .sub (
23
- r'( version:) \s*' ,
24
- r'\1 ' + version ,
25
- init_content
26
- )
22
+ # Replace version in the header
23
+ updated_init_content = re .sub (
24
+ r' version:\s*\d+\.\d+\.\d+ ' ,
25
+ f' version: { version } ' ,
26
+ init_content
27
+ )
27
28
28
- with open (init_path , 'w' , encoding = 'utf-8' ) as init_file :
29
- init_file .write (updated_init_content )
29
+ with open (init_path , 'w' , encoding = 'utf-8' ) as init_file :
30
+ init_file .write (updated_init_content )
30
31
32
+ except Exception as e :
33
+ print (f"Error updating __init__.py: { e } " )
34
+
35
+ print (f"Found version: { version } " )
31
36
return version
37
+ print ("No version found in CHANGELOG.md" )
32
38
return '0.0.0' # fallback version if not found
33
39
except FileNotFoundError :
34
40
print ("CHANGELOG.md not found!" )
35
41
return '0.0.0'
36
42
43
+ def read_version_from_init ():
44
+ """Read version from __init__.py as a fallback"""
45
+ try :
46
+ init_path = os .path .join ('simpletool' , '__init__.py' )
47
+ with open (init_path , 'r' , encoding = 'utf-8' ) as init_file :
48
+ init_content = init_file .read ()
49
+ match = re .search (r'version:\s*(\d+\.\d+\.\d+)' , init_content )
50
+ if match :
51
+ version = match .group (1 )
52
+ print (f"Version from __init__.py: { version } " )
53
+ return version
54
+ except Exception as e :
55
+ print (f"Error reading version from __init__.py: { e } " )
56
+ return '0.0.0'
57
+
58
+ def write_version_to_metadata (version ):
59
+ """Write version to PKG-INFO metadata file"""
60
+ try :
61
+ with open ('simpletool.egg-info/PKG-INFO' , 'r' ) as f :
62
+ content = f .read ()
63
+
64
+ # Replace or add Version
65
+ if 'Version:' in content :
66
+ content = re .sub (r'Version:.*' , f'Version: { version } ' , content )
67
+ else :
68
+ content += f'\n Version: { version } \n '
69
+
70
+ with open ('simpletool.egg-info/PKG-INFO' , 'w' ) as f :
71
+ f .write (content )
72
+
73
+ print (f"Updated PKG-INFO with version: { version } " )
74
+ except Exception as e :
75
+ print (f"Error writing version to metadata: { e } " )
76
+
77
+ # First try to get version from CHANGELOG.md
78
+ version = get_version ()
79
+
80
+ # If that fails, try reading from __init__.py
81
+ if version == '0.0.0' :
82
+ version = read_version_from_init ()
83
+
84
+ # Write version to metadata
85
+ write_version_to_metadata (version )
37
86
38
87
setup (name = 'simpletool' ,
39
- version = get_version (),
88
+ version = version , # Use the version we found
40
89
description = 'simpletool' ,
41
90
url = 'https://github.com/nchekwa/simpletool-python/tree/master' ,
42
91
author = 'Artur Zdolinski' ,
0 commit comments