1717
1818import toml
1919
20- CWD = os .getcwd ()
2120SCRIPT_DIR = os .path .abspath (os .path .dirname (__file__ ))
2221
22+ sys .path .insert (
23+ 0 ,
24+ os .path .normpath (os .path .join (SCRIPT_DIR , "sphinx" , "base" )),
25+ )
26+ # noinspection PyUnresolvedReferences
27+ from make_util import get_package_version
28+
2329FACET_PATH_ENV = "FACET_PATH"
2430FACET_PATH_URI_ENV = "FACET_PATH_URI"
2531FACET_BUILD_PKG_VERSION_ENV = "FACET_BUILD_{project}_VERSION"
@@ -77,30 +83,8 @@ def __init__(self, project: str, dependency_type: str) -> None:
7783
7884 project_root_path = os .path .abspath (os .path .join (projects_root_path , project ))
7985 src_root_path = os .path .join (project_root_path , "src" , project )
80- init_path = os .path .join (src_root_path , "__init__.py" )
81-
82- print (f"Retrieving package version from { init_path } " )
83-
84- with open (init_path , "rt" ) as init_file :
85- init_lines = init_file .readlines ()
8686
87- matches = {
88- match [1 ] or match [2 ]
89- for match in (RE_VERSION_DECLARATION .match (line ) for line in init_lines )
90- if match
91- }
92-
93- if len (matches ) == 0 :
94- raise RuntimeError (f"No valid __version__ declaration found in { init_path } " )
95-
96- elif len (matches ) > 1 :
97- raise RuntimeError (
98- f"Multiple conflicting __version__ declarations found in { init_path } : "
99- f"{ matches } "
100- )
101-
102- else :
103- package_version = next (iter (matches ))
87+ package_version = str (get_package_version (package_path = src_root_path ))
10488
10589 os .environ [
10690 FACET_BUILD_PKG_VERSION_ENV .format (project = project .upper ())
@@ -154,7 +138,7 @@ def get_pyproject_toml(self) -> Dict[str, Any]:
154138 pyproject_toml_path = os .path .join (
155139 os .environ [FACET_PATH_ENV ], self .project , "pyproject.toml"
156140 )
157- print (f"Reading build configuration from { pyproject_toml_path } " )
141+ log (f"Reading build configuration from { pyproject_toml_path } " )
158142 with open (pyproject_toml_path , "rt" ) as f :
159143 return toml .load (f )
160144
@@ -251,7 +235,7 @@ def get_matrix_dependencies(matrix_type: str) -> Dict[str, str]:
251235 for package , version in requirements_to_expose .items ():
252236 # bash ENV variables can not use dash, replace it to _
253237 env_var_name = "FACET_V_" + re .sub (r"[^\w]" , "_" , package .upper ())
254- print (f"Exporting { env_var_name } ={ version !r} " )
238+ log (f"Exporting { env_var_name } ={ version !r} " )
255239 os .environ [env_var_name ] = version
256240
257241 @abstractmethod
@@ -266,7 +250,7 @@ def print_build_info(self, stage: str) -> None:
266250 f"VERSION { self .package_version } "
267251 )
268252 separator = "=" * len (message )
269- print (f"{ separator } \n { message } \n { separator } " )
253+ log (f"{ separator } \n { message } \n { separator } " )
270254
271255 @abstractmethod
272256 def build (self ) -> None :
@@ -321,7 +305,7 @@ def clean(self) -> None:
321305 # purge pre-existing build directories
322306 package_dist_name = self .get_package_dist_name ()
323307 for obsolete_folder in glob (os .path .join (build_path , f"{ package_dist_name } _*" )):
324- print (f"Clean: Removing obsolete conda-build folder at: { obsolete_folder } " )
308+ log (f"Clean: Removing obsolete conda-build folder at: { obsolete_folder } " )
325309 shutil .rmtree (obsolete_folder , ignore_errors = True )
326310
327311 # remove broken packages
@@ -341,7 +325,7 @@ def build(self) -> None:
341325
342326 os .makedirs (build_path , exist_ok = True )
343327 build_cmd = f"conda-build -c conda-forge -c bcg_gamma { recipe_path } "
344- print (
328+ log (
345329 f"Building: { self .project } \n "
346330 f"Build path: { build_path } \n "
347331 f"Build Command: { build_cmd } "
@@ -384,9 +368,9 @@ def build(self) -> None:
384368 os .chdir (build_path )
385369
386370 build_cmd = f"tox -e { tox_env } -v"
387- print (f"Build Command: { build_cmd } " )
371+ log (f"Build Command: { build_cmd } " )
388372 subprocess .run (args = build_cmd , shell = True , check = True )
389- print ("Tox build completed – creating local PyPi index" )
373+ log ("Tox build completed – creating local PyPi index" )
390374
391375 # Create/update a local PyPI PEP 503 (the simple repository API) compliant
392376 # folder structure, so that it can be used with PIP's --extra-index-url
@@ -418,7 +402,7 @@ def build(self) -> None:
418402 with open (project_index_html_path , "wt" ) as f :
419403 f .writelines (package_file_links )
420404
421- print (f"Local PyPi Index created at: { pypi_index_path } " )
405+ log (f"Local PyPi Index created at: { pypi_index_path } " )
422406
423407 finally :
424408 os .chdir (original_dir )
@@ -471,6 +455,15 @@ def print_usage() -> None:
471455 print (usage )
472456
473457
458+ def log (message : str ) -> None :
459+ """
460+ Write a message to `stderr`.
461+
462+ :param message: the message to write
463+ """
464+ print (message , file = sys .stderr )
465+
466+
474467def run_make () -> None :
475468 """
476469 Run this build script with the given arguments.
@@ -495,7 +488,7 @@ def run_make() -> None:
495488 ):
496489
497490 if arg_value not in valid_values :
498- print (
491+ log (
499492 f"Wrong value for { arg_name } argument: "
500493 f"got { arg_value } but expected one of { ', ' .join (valid_values )} "
501494 )
0 commit comments