Skip to content

Instantly share code, notes, and snippets.

@tjjh89017
Forked from johnliu55tw/verify-packages.py
Created July 4, 2022 02:18
Show Gist options
  • Save tjjh89017/403e9377427581630df7e2ac16ff5aed to your computer and use it in GitHub Desktop.
Save tjjh89017/403e9377427581630df7e2ac16ff5aed to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
# Need VPN to get this image
image = 'registry.suse.de/suse/containers/suse-microos/5.2/containers/suse/sle-micro-rancher/5.2:latest'
def get_zypper_packages(dockerfile):
with open(dockerfile, 'r') as f:
dockerfile = f.read()
zypper_packages = []
found_zypper = False
for line in dockerfile.split('\n'):
if not line:
continue
cmd, arg = line.split(' ', 1)
if found_zypper and not cmd:
# Multi line packages
bs_idx = arg.find('\\')
if bs_idx >= 0:
package_names = arg[:bs_idx]
else:
package_names = arg
pkgs = [p.strip() for p in package_names.split() if p]
zypper_packages.extend(pkgs)
if cmd == 'RUN' and 'zypper in -y' in arg:
# Parse the package in the same line
end_idx = arg.find('-y')
packages = arg[end_idx+2:].split(' ')
zypper_packages.extend(p for p in packages if p not in ('\\', ''))
found_zypper = True
elif cmd and found_zypper is True:
found_zypper = False
return zypper_packages
def get_container_packages(image):
cmd = f'docker run --rm -it {image} zypper --terse -s 10 se -i'
proc = subprocess.run(cmd, shell=True, check=True, capture_output=True)
stdout = proc.stdout.decode('utf-8')
pkgs = [
line.split(':')[1].strip()
for line in stdout.split('\r\n')
if line.startswith('i')
]
return pkgs
dockerfile_zypper_packages = set(get_zypper_packages('./Dockerfile'))
container_packages = set(get_container_packages(image))
print('\n'.join(sorted(list(dockerfile_zypper_packages.difference(container_packages)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment