1- # Read package version in pyproject.toml and replace it in .conda/recipe.yaml
1+ # Read package version in pyproject.toml
22# Also provides version coherence checking across multiple files
33
44import argparse
@@ -132,48 +132,10 @@ def get_versions():
132132 }
133133
134134
135- def replace_in_file (filepath : str , info : dict ):
136- """
137- ::filepath:: Path to recipe.yaml, with filename
138- ::info:: Dict with information to populate
139- """
140- with open (filepath , "rt" ) as fin :
141- meta = fin .read ()
142- # Replace with info from pyproject.toml
143- if PACKAGE_VERSION not in meta :
144- raise Exception (f"{ PACKAGE_VERSION = } not found in { filepath } " )
145- meta = meta .replace (PACKAGE_VERSION , info ["package_version" ])
146- if " - dependencies" not in meta :
147- raise Exception (f'" - dependencies" not found in { filepath } ' )
148- dependencies = ""
149- for dep in info ["dependencies" ]:
150- if "fief-client" in dep :
151- # Prevent to have unsupported "fief-client[cli]" in dependencies
152- dependencies += " - fief-client-fastapi\n - yaspin\n "
153- else :
154- dependencies += f" - { dep } \n "
155- meta = meta .replace (" - dependencies" , dependencies )
156- with open (filepath , "wt" ) as fout :
157- fout .write (meta )
158- logging .info (
159- f"File { filepath } has been updated with informations from pyproject.toml."
160- )
161-
162-
163- if __name__ == "__main__" :
164- parser = argparse .ArgumentParser ()
165- parser .add_argument (
166- "-r" ,
167- "--replace" ,
168- action = "store_true" ,
169- help = "replace in file" ,
170- )
171- parser .add_argument (
172- "-f" ,
173- "--filename" ,
174- type = str ,
175- default = ".conda/recipe.yaml" ,
176- help = "Path to recipe.yaml, with filename" ,
135+ def main ():
136+ """Main entry point for the script."""
137+ parser = argparse .ArgumentParser (
138+ description = "Read package version and check version coherence"
177139 )
178140 parser .add_argument (
179141 "-o" ,
@@ -187,27 +149,39 @@ def replace_in_file(filepath: str, info: dict):
187149 action = "store_true" ,
188150 help = "Check version coherence across all bumpver-managed files" ,
189151 )
152+
153+ # Parse arguments - all arguments are optional, so this works fine with no args
190154 args = parser .parse_args ()
191155
192- # Check version coherence first if requested or before any operations
156+ # Check version coherence first if requested
193157 if args .check_coherence :
194158 coherence_ok = check_version_coherence ()
195159 sys .exit (0 if coherence_ok else 1 )
196160
197- # Always check coherence before doing replacements or showing versions
198- if not check_version_coherence (quiet = True ):
199- logging .error ("Aborting due to version coherence issues." )
161+ # If only_package_version is requested, just print version and exit
162+ if args .only_package_version :
163+ try :
164+ info = get_versions ()
165+ print (f'{ info ["package_version" ]} ' )
166+ sys .exit (0 )
167+ except Exception as e :
168+ logging .error (f"Error getting version: { e } " )
169+ sys .exit (1 )
170+
171+ # Default behavior: check coherence quietly, then show versions
172+ try :
173+ if not check_version_coherence (quiet = True ):
174+ logging .error ("Aborting due to version coherence issues." )
175+ sys .exit (1 )
176+
177+ info = get_versions ()
178+ logging .info ("Versions:" )
179+ print (info ) # noqa: T201
180+ sys .exit (0 )
181+ except Exception as e :
182+ logging .error (f"Error: { e } " )
200183 sys .exit (1 )
201184
202- info = get_versions ()
203- file = args .filename
204- if args .only_package_version :
205- print (f'{ info ["package_version" ]} ' )
206- exit ()
207- logging .info ("Versions :" )
208- print (info ) # noqa: T201
209- if args .replace :
210- logging .info (f"Replace in { file } " )
211- replace_in_file (file , info )
212- else :
213- logging .info ("Dry mode, no replace made" )
185+
186+ if __name__ == "__main__" :
187+ main ()
0 commit comments