@@ -62,33 +62,38 @@ def write_hermeto_lockfile(arch_packages, repos):
62
62
return lockfile
63
63
64
64
65
- def merge_lockfiles (base_lockfile , override_lockfile ):
65
+ def merge_lockfiles (base_lockfile , next_lockfile , override = False ):
66
66
"""
67
- Merges an override lockfile into a base lockfile.
67
+ Merges a lockfile into a base lockfile.
68
+
69
+ If is_override is True, it will only add packages to existing
70
+ architectures. Otherwise, it will add new architectures.
68
71
"""
69
- if not override_lockfile :
72
+ if not next_lockfile :
70
73
return base_lockfile
71
74
72
75
# Create a dictionary for base arches for easy lookup
73
76
base_arches = {arch ['arch' ]: arch for arch in base_lockfile .get ('arches' , [])}
74
77
75
- override = override_lockfile .get ('arches' , [])
76
- if not override :
78
+ next_arches_list = next_lockfile .get ('arches' , [])
79
+ if not next_arches_list :
77
80
return base_lockfile
78
81
79
- for override_entry in override :
80
- # override_entry is a dict like {'arch': x86_64','packages': [...]}
81
- if not isinstance (override_entry , dict ):
82
+ for next_arch_entry in next_arches_list :
83
+ if not isinstance (next_arch_entry , dict ):
84
+ continue
85
+ arch = next_arch_entry .get ('arch' , None )
86
+ if not arch :
82
87
continue
83
- arch = override_entry . get ( 'arch' , None )
84
- override_packages = override_entry .get ('packages' , [])
88
+
89
+ next_packages = next_arch_entry .get ('packages' , [])
85
90
if arch in base_arches :
86
- # Merge packages
91
+ # Arch exists, merge packages
87
92
base_packages = base_arches [arch ].get ('packages' , [])
88
- base_packages += override_packages
89
- else :
90
- # Add the arch from the override file
91
- base_arches [arch ] = override_packages
93
+ base_packages += next_packages
94
+ elif not override :
95
+ # Arch is new and this is not an override, so add it
96
+ base_arches [arch ] = next_arch_entry
92
97
93
98
# Reconstruct the arches list
94
99
base_lockfile ['arches' ] = list (base_arches .values ())
@@ -119,21 +124,24 @@ def query_packages_location(locks, repoquery_args):
119
124
if name not in processed_urls :
120
125
processed_urls [name ] = url
121
126
pkg_urls = list (processed_urls .values ())
122
- # sanity check all the packages got resolved
123
- if len (pkg_urls ) < len (locked_nevras ):
127
+ # sanity check all the locked packages got resolved
128
+ if len (pkg_urls ) != len (locked_nevras ):
124
129
print ("Some packages from the lockfile could not be resolved. The rpm-ostree lockfile is probably out of date." )
125
- for name in locks .keys ():
126
- if name not in processed_urls :
127
- print (f"could not resolve package { name } " )
128
130
sys .exit (1 )
129
131
132
+ print (f"Done. Solved { len (pkg_urls )} packages." )
130
133
return pkg_urls
131
134
132
135
133
- def generate_lockfile ( contextdir , manifest , output_path , arches ):
136
+ def generate_main ( args ):
134
137
"""
135
138
Generates the cachi2/hermeto RPM lock file.
136
139
"""
140
+ contextdir = args .context
141
+ manifest = os .path .abspath (args .manifest )
142
+ output_path = args .output
143
+ arches = args .arch
144
+
137
145
if not arches :
138
146
arches_to_resolve = [get_basearch ()]
139
147
elif 'all' in arches :
@@ -151,7 +159,7 @@ def generate_lockfile(contextdir, manifest, output_path, arches):
151
159
repos = manifest_data .get ('repos' , [])
152
160
repos += manifest_data .get ('lockfile-repos' , [])
153
161
154
- repoquery_args = ["--queryformat" , "%{name} %{location}\n " , "--disablerepo=*" , "--refresh" ]
162
+ repoquery_args = ["--queryformat" , "%{name} %{location}\n " , "--disablerepo=*" , "--refresh" , "--quiet" ]
155
163
# Tell dnf to load repos files from $contextdir
156
164
repoquery_args .extend ([f"--setopt=reposdir={ contextdir } " ])
157
165
@@ -165,64 +173,123 @@ def generate_lockfile(contextdir, manifest, output_path, arches):
165
173
print (f"This tool derive the konflux lockfile from rpm-ostree lockfiles. No manifest-lock exist for { arch } in { contextdir } " )
166
174
sys .exit (1 )
167
175
print (f"Resolving packages for { arch } ..." )
168
- # append noarch as well because otherwise tose packages get excluded from results
169
- # We use --forcearch here because otherwise dnf still respect the system basearch
170
- # we have to specify both --arch and --forcearch to get both result for $arch and $noarch
171
- args_arch = ['--forcearch' , arch , '--arch' , arch , '--arch' , 'noarch' ]
172
- pkg_urls = query_packages_location (locks , repoquery_args + args_arch )
176
+ arch_args = []
177
+ if arch is not get_basearch ():
178
+ # append noarch as well because otherwise those packages get excluded from results
179
+ # We use --forcearch here because otherwise dnf still respect the system basearch
180
+ # we have to specify both --arch and --forcearch to get both result for $arch and $noarch
181
+ arch_args = ['--forcearch' , arch , '--arch' , arch , '--arch' , 'noarch' ]
182
+ pkg_urls = query_packages_location (locks , repoquery_args + arch_args )
173
183
packages .append ({'arch' : arch , 'packages' : pkg_urls })
174
184
175
185
lockfile = write_hermeto_lockfile (packages , repos )
176
186
177
- override_path = os .path .join (contextdir , 'konflux-lockfile-override.yaml' )
178
- if os .path .exists (override_path ):
187
+ try :
188
+ with open (output_path , 'w' , encoding = 'utf-8' ) as f :
189
+ yaml .safe_dump (lockfile , f , default_flow_style = False )
190
+ except IOError as e :
191
+ print (f"\u274c Error: Could not write to output file '{ output_path } '. Reason: { e } " )
192
+ sys .exit (1 )
193
+
194
+
195
+ def merge_main (args ):
196
+ """
197
+ Merges multiple lockfiles into one, optionally applying an override file.
198
+ """
199
+ if not args .input :
200
+ print ("Error: at least one input file is required for merging." , file = sys .stderr )
201
+ sys .exit (1 )
202
+
203
+ try :
204
+ with open (args .input [0 ], 'r' , encoding = 'utf-8' ) as f :
205
+ base_lockfile = yaml .safe_load (f )
206
+ except (IOError , yaml .YAMLError ) as e :
207
+ print (f"Error reading base lockfile { args .input [0 ]} : { e } " , file = sys .stderr )
208
+ sys .exit (1 )
209
+
210
+ for subsequent_file in args .input [1 :]:
179
211
try :
180
- with open (override_path , 'r' , encoding = "utf8" ) as f :
212
+ with open (subsequent_file , 'r' , encoding = 'utf-8' ) as f :
213
+ next_lockfile = yaml .safe_load (f )
214
+ base_lockfile = merge_lockfiles (base_lockfile , next_lockfile )
215
+ except (IOError , yaml .YAMLError ) as e :
216
+ print (f"Error reading or merging { subsequent_file } : { e } " , file = sys .stderr )
217
+ sys .exit (1 )
218
+
219
+ if os .path .exists (args .override ):
220
+ try :
221
+ with open (args .override , 'r' , encoding = "utf8" ) as f :
181
222
override_data = yaml .safe_load (f )
182
- print (f"Merging override from { override_path } " )
183
- lockfile = merge_lockfiles (lockfile , override_data )
223
+ print (f"Merging override from { args . override } " )
224
+ base_lockfile = merge_lockfiles (base_lockfile , override_data , override = True )
184
225
except (IOError , yaml .YAMLError ) as e :
185
- print (f"\u274c Error: Could not read or parse override file '{ override_path } '. Reason : { e } " )
226
+ print (f"Error reading or parsing override file '{ args . override } ' : { e } " , file = sys . stderr )
186
227
sys .exit (1 )
187
228
188
229
try :
189
- with open (output_path , 'w' , encoding = 'utf-8' ) as f :
190
- yaml .safe_dump (lockfile , f , default_flow_style = False )
230
+ with open (args .output , 'w' , encoding = 'utf-8' ) as f :
231
+ yaml .safe_dump (base_lockfile , f , default_flow_style = False )
232
+ print (f"Successfully merged lockfiles to { args .output } " )
191
233
except IOError as e :
192
- print (f"\u274c Error: Could not write to output file '{ output_path } '. Reason : { e } " )
234
+ print (f"Error writing to output file '{ args . output } ' : { e } " , file = sys . stderr )
193
235
sys .exit (1 )
194
236
195
237
196
238
if __name__ == "__main__" :
197
239
parser = argparse .ArgumentParser (
198
- description = "Generate hermeto lock files."
240
+ description = "Generate and merge hermeto lock files."
199
241
)
242
+ subparsers = parser .add_subparsers (dest = 'command' , required = True )
200
243
201
- parser .add_argument (
244
+ # GENERATE command
245
+ parser_generate = subparsers .add_parser (
246
+ 'generate' ,
247
+ help = 'Resolve RPMs and generate a lockfile for one or more architectures.'
248
+ )
249
+ parser_generate .add_argument (
202
250
'manifest' ,
203
251
help = 'Path to the flattened rpm-ostree manifest (e.g., tmp/manifest.json)'
204
252
)
205
-
206
- parser .add_argument (
253
+ parser_generate .add_argument (
207
254
'--context' ,
208
255
default = '.' ,
209
256
help = "Path to the directory containing repofiles and lockfiles. (default: '.')"
210
257
)
211
-
212
- parser .add_argument (
258
+ parser_generate .add_argument (
213
259
'--output' ,
214
260
default = './rpms.lock.yaml' ,
215
261
help = "Path for the hermeto lockfile. (default: './rpms.lock.yaml')"
216
262
)
217
-
218
- parser .add_argument (
263
+ parser_generate .add_argument (
219
264
'--arch' ,
220
265
action = 'append' ,
221
266
choices = ['x86_64' , 'aarch64' , 's390x' , 'ppc64le' , 'all' ],
222
267
help = "The architecture to resolve. Can be specified multiple times. 'all' resolves all architectures."
223
268
)
269
+ parser_generate .set_defaults (func = generate_main )
224
270
225
- args = parser .parse_args ()
271
+ # MERGE command
272
+ parser_merge = subparsers .add_parser (
273
+ 'merge' ,
274
+ help = 'Merge multiple architecture-specific lockfiles into a single file.'
275
+ )
276
+ parser_merge .add_argument (
277
+ '--input' ,
278
+ nargs = '+' ,
279
+ required = True ,
280
+ help = 'One or more input lockfiles to merge.'
281
+ )
282
+ parser_merge .add_argument (
283
+ '--output' ,
284
+ default = './rpms.lock.yaml' ,
285
+ help = "Path for the merged lockfile. (default: './rpms.lock.yaml')"
286
+ )
287
+ parser_merge .add_argument (
288
+ '--override' ,
289
+ default = 'konflux-lockfile-override.yaml' ,
290
+ help = "Path to an override file. (default: 'konflux-lockfile-override.yaml')"
291
+ )
292
+ parser_merge .set_defaults (func = merge_main )
226
293
227
- manifest_abs_path = os . path . abspath ( args . manifest )
228
- generate_lockfile ( args .context , manifest_abs_path , args . output , args . arch )
294
+ args = parser . parse_args ( )
295
+ args .func ( args )
0 commit comments