Skip to content
Snippets Groups Projects

CI: add "report-image-diff" job

Merged Dmitry Petrov requested to merge report_image_changes_in_mr into master
Files
2
+ 59
44
@@ -3,7 +3,7 @@ import argparse
import fnmatch
import logging
from difflib import unified_diff
from pathlib import Path
from typing import Optional
import requests
from bs4 import BeautifulSoup
@@ -122,20 +122,27 @@ class BuildArtifacts:
return ret
def download_file(self, path: str, outfile: str) -> None:
def get_artifact(self, path: str, outfile: Optional[str] = None) -> Optional[bytes]:
"""
Get the artifact file from GitLab and save it to the specified location.
Get a single artifact file from GitLab.
Save it to the file if "outfile" arg is specified.
Args:
path: str — relative path to artifact file.
outfile: str — path to save the output file.
Returns:
None if "outfile" arg is given. Otherwise, returns the artifact file as bytes.
"""
url = (
f"{self.project.web_url}/-/jobs/{self.pipelinejob.id}/artifacts/raw/{path}"
)
r = requests.get(url, allow_redirects=True)
open(rf"{outfile}", "wb").write(r.content)
job = self.project.jobs.get(self.pipelinejob.id, lazy=True)
if not outfile:
return job.artifact(path)
with open(outfile, "wb") as f:
job.artifact(path, streamed=True, action=f.write)
return None
def get_archive_size(self) -> int:
"""
@@ -240,15 +247,15 @@ def main():
if comment.body[0:4] == "**`⮘":
comment.delete()
manifest_main_commit = manifest_project.commits.list(
manifest_commit__main = manifest_project.commits.list(
all=False, ref_name=args.target_branch
)[0]
main_build = BuildPipelines(manifest_project, manifest_main_commit.id)
build__main = BuildPipelines(manifest_project, manifest_commit__main.id)
manifest_mr_commit = manifest_project.commits.list(
manifest_commit__mr = manifest_project.commits.list(
all=False, ref_name=args.source_branch
)[0]
mr_build = BuildPipelines(manifest_project, manifest_mr_commit.id)
build__mr = BuildPipelines(manifest_project, manifest_commit__mr.id)
machines = (
"seco-genio510",
@@ -271,28 +278,31 @@ def main():
deploy_img_dir = f"build-{distro}-{machine}/tmp/deploy/images/{machine}/"
artifacts_main = BuildArtifacts(
manifest_project, main_build.get_jobs(pipeline, f"build-{machine}")[0]
artifacts__main = BuildArtifacts(
manifest_project, build__main.get_jobs(pipeline, f"build-{machine}")[0]
)
artifacts_mr = BuildArtifacts(
manifest_project, mr_build.get_jobs(pipeline, f"build-{machine}")[0]
artifacts__mr = BuildArtifacts(
manifest_project, build__mr.get_jobs(pipeline, f"build-{machine}")[0]
)
deploy_files__main = artifacts__main.list_dir(deploy_img_dir)
deploy_files__mr = artifacts__mr.list_dir(deploy_img_dir)
# The difference in size of artifacts.zip for main and MR builds
zip_size_main = artifacts_main.get_archive_size()
zip_size_mr = artifacts_mr.get_archive_size()
zip_size__main = artifacts__main.get_archive_size()
zip_size__mr = artifacts__mr.get_archive_size()
zip_size_diff = sizeof_fmt(abs(zip_size_main - zip_size_mr))
sign = "+" if zip_size_main < zip_size_mr else "-"
zip_size_diff = sizeof_fmt(abs(zip_size__main - zip_size__mr))
sign = "+" if zip_size__main < zip_size__mr else "-"
zip_size_main = sizeof_fmt(zip_size_main)
zip_size_mr = sizeof_fmt(zip_size_mr)
zip_size__main = sizeof_fmt(zip_size__main)
zip_size__mr = sizeof_fmt(zip_size__mr)
summary += f" ├── artifacts.zip size: [ {zip_size_main}{zip_size_mr} ] | {sign}{zip_size_diff}\\\n"
summary += f" ├── artifacts.zip size: [ {zip_size__main}{zip_size__mr} ] | {sign}{zip_size_diff}\\\n"
# The dirrerence in size of image for main and MR build
image_size_main = 0
image_size_mr = 0
image_size__main = 0
image_size__mr = 0
# The image file format may vary depending on machine and distribution.
if distro == "seconorth-fngsystem":
@@ -304,34 +314,39 @@ def main():
else:
img_pattern = ""
for name, size in artifacts_main.list_dir(deploy_img_dir).items():
for name, size in deploy_files__main.items():
if fnmatch.fnmatch(name, img_pattern):
image_size_main = size
for name, size in artifacts_mr.list_dir(deploy_img_dir).items():
image_size__main = size
for name, size in deploy_files__mr.items():
if fnmatch.fnmatch(name, img_pattern):
image_size_mr = size
image_size__mr = size
summary += (
f" ├── image size: [ {image_size_main}{image_size_mr} ]\\\n"
f" ├── image size: [ {image_size__main}{image_size__mr} ]\\\n"
)
# Comparision of manifest files for main in MR builds
for file in artifacts_main.list_dir(deploy_img_dir).keys():
manifestfile_lines__main = []
manifestfile_lines__mr = []
for file in deploy_files__main.keys():
if fnmatch.fnmatch(file, "*.rootfs.manifest"):
artifacts_main.download_file(deploy_img_dir + file, "main.manifest")
manifestfile_lines__main = (
artifacts__main.get_artifact(deploy_img_dir + file)
.decode("utf-8")
.splitlines()
)
diff_fromfile = file
for file in artifacts_mr.list_dir(deploy_img_dir).keys():
for file in deploy_files__mr.keys():
if fnmatch.fnmatch(file, "*.rootfs.manifest"):
artifacts_mr.download_file(deploy_img_dir + file, "mr.manifest")
manifestfile_lines__mr = (
artifacts__mr.get_artifact(deploy_img_dir + file)
.decode("utf-8")
.splitlines()
)
diff_tofile = file
manifestfile_main_lines = (
Path("main.manifest").read_text(encoding="UTF-8").splitlines()
)
manifestfile_mr_lines = (
Path("mr.manifest").read_text(encoding="UTF-8").splitlines()
)
if not manifestfile_main_lines or not manifestfile_mr_lines:
if not manifestfile_lines__main or not manifestfile_lines__mr:
continue
summary += " └── manifest diff:\n"
@@ -340,8 +355,8 @@ def main():
kernel_rev_old = ""
kernel_rev_new = ""
for l in unified_diff(
manifestfile_main_lines,
manifestfile_mr_lines,
manifestfile_lines__main,
manifestfile_lines__mr,
fromfile=diff_fromfile,
tofile=diff_tofile,
n=0,
Loading