Skip to content

Commit e8200a8

Browse files
author
Douglas Curtis
authored
wip on splitting ocp report files manifest changes (#42)
1 parent 5e6744f commit e8200a8

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

roles/collect/files/package_report.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,16 @@ def render_manifest(args, archivefiles=[]):
214214
return (manifest_filename, manifest_uuid)
215215

216216

217-
def write_tarball(args, tarfilename, archivefiles=[]):
217+
def write_tarball(args, tarfilename, manifest_filename, manifest_uuid, archivefiles, file_count=0):
218218
"""Write a tarball, adding the given files to the archive.
219219
220220
Args:
221221
args (Namespace) an ArgumentParser Namespace object
222222
tarfilename (str) the name of the tarball to create
223+
manifest_filename (str) the name of the report manifest
224+
manifest_uuid (str) the unique identifier of the manifest
223225
archivefiles (list) the list of files to include in the archive
226+
file_count (int) file number initializer
224227
225228
Returns:
226229
(str) full filepath of the created tarball
@@ -231,10 +234,8 @@ def write_tarball(args, tarfilename, archivefiles=[]):
231234
if not archivefiles:
232235
return None
233236

234-
manifest_filename, manifest_uuid = render_manifest(args, archivefiles)
235237
try:
236238
with tarfile.open(tarfilename, f"{FILE_FLAG}:gz") as tarball:
237-
file_count = 0
238239
for fname in archivefiles:
239240
LOG.debug(f"Adding {fname} to {tarfilename}: ")
240241
if fname.endswith(".csv"):
@@ -246,9 +247,17 @@ def write_tarball(args, tarfilename, archivefiles=[]):
246247
LOG.critical(exc)
247248
sys.exit(2)
248249
LOG.info(f"Wrote: {tarfilename}")
249-
return f"{tarfilename}"
250+
return f"{tarfilename}", file_count
250251

251252

253+
def build_local_csv_file_list(staging_directory):
254+
"""Build a list of all report csv files in staging directory."""
255+
file_list = []
256+
for csv_file in os.listdir(staging_directory):
257+
if ".csv" in csv_file:
258+
file_list.append(f"{staging_directory}/{csv_file}")
259+
return file_list
260+
252261
if "__main__" in __name__:
253262
args = parse_args()
254263
if args.verbosity:
@@ -264,20 +273,28 @@ def write_tarball(args, tarfilename, archivefiles=[]):
264273
split_files(args.filepath, args.max_size)
265274
tarpath = args.filepath + "/../"
266275
tarfiletmpl = "cost-mgmt{}.tar.gz"
267-
for idx, filename in enumerate(os.listdir(args.filepath)):
276+
277+
file_list = build_local_csv_file_list(args.filepath)
278+
manifest_filename, manifest_uuid = render_manifest(args, file_list)
279+
file_count = 0
280+
for idx, filename in enumerate(file_list):
268281
if ".csv" in filename:
269282
tarfilename = os.path.abspath(
270283
tarpath + tarfiletmpl.format(idx))
271-
output_tar = write_tarball(args,
272-
tarfilename, [f"{args.filepath}/{filename}"])
284+
output_tar, file_count = write_tarball(args,
285+
tarfilename, manifest_filename, manifest_uuid, [filename], file_count)
273286
if output_tar:
274287
out_files.append(output_tar)
288+
275289
else:
276290
tarfilename = os.path.abspath(args.filepath + "/../cost-mgmt.tar.gz")
277-
files = [f"{args.filepath}/{filename}" for filename in os.listdir(args.filepath)]
278-
output_tar = write_tarball(args, tarfilename, files)
279-
if output_tar:
280-
out_files.append(output_tar)
291+
292+
file_list = build_local_csv_file_list(args.filepath)
293+
if file_list:
294+
manifest_filename, manifest_uuid = render_manifest(args, file_list)
295+
output_tar, _ = write_tarball(args, tarfilename, manifest_filename, manifest_uuid, file_list)
296+
if output_tar:
297+
out_files.append(output_tar)
281298

282299
for fname in out_files:
283300
print(fname)

0 commit comments

Comments
 (0)