12
12
import toml
13
13
import logging
14
14
import pathlib
15
- import flatdict
16
15
17
16
from simvue .utilities import simvue_timestamp
18
17
@@ -64,7 +63,7 @@ def git_info(repository: str) -> dict[str, typing.Any]:
64
63
)
65
64
return {
66
65
"git" : {
67
- "authors" : json . dumps ( list (author_list ) ),
66
+ "authors" : list (author_list ),
68
67
"ref" : ref ,
69
68
"msg" : current_commit .message .strip (),
70
69
"time_stamp" : simvue_timestamp (current_commit .committed_datetime ),
@@ -84,34 +83,32 @@ def _python_env(repository: pathlib.Path) -> dict[str, typing.Any]:
84
83
if (pyproject_file := pathlib .Path (repository ).joinpath ("pyproject.toml" )).exists ():
85
84
content = toml .load (pyproject_file )
86
85
if (poetry_content := content .get ("tool" , {}).get ("poetry" , {})).get ("name" ):
87
- python_meta | = {
88
- "python.project. name" : poetry_content ["name" ],
89
- "python.project. version" : poetry_content ["version" ],
86
+ python_meta [ "project" ] = {
87
+ "name" : poetry_content ["name" ],
88
+ "version" : poetry_content ["version" ],
90
89
}
91
90
elif other_content := content .get ("project" ):
92
- python_meta | = {
93
- "python.project. name" : other_content ["name" ],
94
- "python.project. version" : other_content ["version" ],
91
+ python_meta [ "project" ] = {
92
+ "name" : other_content ["name" ],
93
+ "version" : other_content ["version" ],
95
94
}
96
95
97
96
if (poetry_lock_file := pathlib .Path (repository ).joinpath ("poetry.lock" )).exists ():
98
97
content = toml .load (poetry_lock_file ).get ("package" , {})
99
- python_meta |= {
100
- f"python.environment.{ package ['name' ]} " : package ["version" ]
101
- for package in content
98
+ python_meta ["environment" ] = {
99
+ package ["name" ]: package ["version" ] for package in content
102
100
}
103
101
elif (uv_lock_file := pathlib .Path (repository ).joinpath ("uv.lock" )).exists ():
104
102
content = toml .load (uv_lock_file ).get ("package" , {})
105
- python_meta |= {
106
- f"python.environment.{ package ['name' ]} " : package ["version" ]
107
- for package in content
103
+ python_meta ["environment" ] = {
104
+ package ["name" ]: package ["version" ] for package in content
108
105
}
109
106
else :
110
107
with contextlib .suppress ((KeyError , ImportError )):
111
108
from pip ._internal .operations .freeze import freeze
112
109
113
- python_meta | = {
114
- f"python.environment. { entry [0 ]} " : entry [- 1 ]
110
+ python_meta [ "environment" ] = {
111
+ entry [0 ]: entry [- 1 ]
115
112
for line in freeze (local_only = True )
116
113
if (entry := line .split ("==" ))
117
114
}
@@ -126,35 +123,33 @@ def _rust_env(repository: pathlib.Path) -> dict[str, typing.Any]:
126
123
if (cargo_file := pathlib .Path (repository ).joinpath ("Cargo.toml" )).exists ():
127
124
content = toml .load (cargo_file ).get ("package" , {})
128
125
if version := content .get ("version" ):
129
- rust_meta |= { "rust.project. version": version }
126
+ rust_meta . setdefault ( "project" , {})[ " version"] = version
130
127
131
128
if name := content .get ("name" ):
132
- rust_meta |= { "rust.project. name": name }
129
+ rust_meta . setdefault ( "project" , {})[ " name"] = name
133
130
134
131
if not (cargo_lock := pathlib .Path (repository ).joinpath ("Cargo.lock" )).exists ():
135
- return {}
132
+ return rust_meta
136
133
137
134
cargo_dat = toml .load (cargo_lock )
138
-
139
- return rust_meta | {
140
- f"rust.environment.{ dependency ['name' ]} " : dependency ["version" ]
135
+ rust_meta ["environment" ] = {
136
+ dependency ["name" ]: dependency ["version" ]
141
137
for dependency in cargo_dat .get ("package" )
142
138
}
143
139
140
+ return rust_meta
141
+
144
142
145
143
def _julia_env (repository : pathlib .Path ) -> dict [str , typing .Any ]:
146
144
"""Retrieve a dictionary of Julia dependencies if a project file is available"""
147
145
julia_meta : dict [str , str ] = {}
148
146
if (project_file := pathlib .Path (repository ).joinpath ("Project.toml" )).exists ():
149
147
content = toml .load (project_file )
150
- julia_meta |= {
151
- f"julia.project.{ key } " : value
152
- for key , value in content .items ()
153
- if not isinstance (value , dict )
148
+ julia_meta ["project" ] = {
149
+ key : value for key , value in content .items () if not isinstance (value , dict )
154
150
}
155
- julia_meta |= {
156
- f"julia.environment.{ key } " : value
157
- for key , value in content .get ("compat" , {}).items ()
151
+ julia_meta ["environment" ] = {
152
+ key : value for key , value in content .get ("compat" , {}).items ()
158
153
}
159
154
return julia_meta
160
155
@@ -171,13 +166,11 @@ def _node_js_env(repository: pathlib.Path) -> dict[str, typing.Any]:
171
166
)
172
167
return {}
173
168
174
- js_meta |= {
175
- f"javascript.project.{ key } " : value
176
- for key , value in content .items ()
177
- if key in ("name" , "version" )
169
+ js_meta ["project" ] = {
170
+ key : value for key , value in content .items () if key in ("name" , "version" )
178
171
}
179
- js_meta | = {
180
- f"javascript.environment. { key .replace ('@' , '' ) } " : value ["version" ]
172
+ js_meta [ "environment" ] = {
173
+ key .replace ("@" , "" ) : value ["version" ]
181
174
for key , value in content .get (
182
175
"packages" if lfv in (2 , 3 ) else "dependencies" , {}
183
176
).items ()
@@ -188,16 +181,13 @@ def _node_js_env(repository: pathlib.Path) -> dict[str, typing.Any]:
188
181
189
182
def environment (repository : pathlib .Path = pathlib .Path .cwd ()) -> dict [str , typing .Any ]:
190
183
"""Retrieve environment metadata"""
191
- _environment_meta = flatdict .FlatDict (
192
- _python_env (repository ), delimiter = "."
193
- ).as_dict ()
194
- _environment_meta |= flatdict .FlatDict (
195
- _rust_env (repository ), delimiter = "."
196
- ).as_dict ()
197
- _environment_meta |= flatdict .FlatDict (
198
- _julia_env (repository ), delimiter = "."
199
- ).as_dict ()
200
- _environment_meta |= flatdict .FlatDict (
201
- _node_js_env (repository ), delimiter = "."
202
- ).as_dict ()
184
+ _environment_meta = {}
185
+ if _python_meta := _python_env (repository ):
186
+ _environment_meta ["python" ] = _python_meta
187
+ if _rust_meta := _rust_env (repository ):
188
+ _environment_meta ["rust" ] = _rust_meta
189
+ if _julia_meta := _julia_env (repository ):
190
+ _environment_meta ["julia" ] = _julia_meta
191
+ if _js_meta := _node_js_env (repository ):
192
+ _environment_meta ["javascript" ] = _js_meta
203
193
return _environment_meta
0 commit comments