def pip_pkgs(img_name): """get python packages installed in an image.""" with Container(img_name) as sha_id: Container.run_docker_cmd(sha_id, "pip install pip-licenses") pkgs = Container.run_docker_cmd(sha_id, "pip-licenses -f=csv --from=mixed") return pkgs, sha_id
def apt_pkgs(img_name): """get apt packages installed in an image.""" with Container(img_name) as sha_id: cmd = "dpkg-query -Wf '${Package},${Version},${Source}\n'" pkgs = "Package,Version,Source\n" pkgs += Container.run_docker_cmd(sha_id, cmd) return pkgs, sha_id
def rpm_pkgs(img_name): """get rpm packages installed in an image.""" with Container(img_name) as sha_id: pkgs = "Package,Version,License\n" pkgs += Container.run_docker_cmd( sha_id, "rpm -qa --qf '%{NAME},%{VERSION},%{LICENSE}\n'") return pkgs, sha_id
def apt_licenses(img_name): """get licenses of apt pkgs from an image.""" pkg_lcs = {} with Container(img_name) as sha_id: cmd = "bash -c \"for pkg in `dpkg-query -Wf '${Package}\n'`; do lc=`cat /usr/share/doc/$pkg/copyright 2>/dev/null`; echo PKG_START $pkg PKG_END,LFILE_START $lc LFILE_END; done\"" # noqa: E501 pkgs = Container.run_docker_cmd(sha_id, cmd) pkg = re.findall("PKG_START(.*?)PKG_END", pkgs) license = re.findall("LFILE_START(.*?)LFILE_END", pkgs) pkg_lcs = { p.strip(): _search_spdx(v.strip()) for p, v in zip(pkg, license) } pkgs = "Package,License\n" for pkg, lc in pkg_lcs.items(): pkgs += "{0},{1}\n".format(pkg, lc) return pkgs, sha_id
def test_run_docker_cmd(monkeypatch): monkeypatch.setattr("subprocess.run", fake_subprocess_run()) out = Container.run_docker_cmd(Img.sha, Img.cmd) assert out == Img.output
def test_stop_rm_container(monkeypatch): monkeypatch.setattr("subprocess.run", fake_subprocess_run()) assert Container.stop_rm_container(Img.sha) is None
def test_deamonize_image(monkeypatch): """test deamonize image.""" monkeypatch.setattr("subprocess.run", fake_subprocess_run()) out = Container.deamonize_image(Img.name) assert out == Img.sha
def test_get_image(monkeypatch): """test get image.""" monkeypatch.setattr("subprocess.run", fake_subprocess_run()) img_sha = Container.get_image(Img.name) assert img_sha == Img.sha