Created
May 17, 2022 08:08
-
-
Save johnliu55tw/1c124960593b81526dd8e472db347f8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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