Add initial version of script rpm_n_reqs.py 89/144589/5
authorDmitry Kovalenko <d.kovalenko@samsung.com>
Thu, 17 Aug 2017 07:43:07 +0000 (10:43 +0300)
committerDmitriy Nikiforov <d.nikiforov@partner.samsung.com>
Tue, 5 Sep 2017 18:51:25 +0000 (21:51 +0300)
Script searches rpm and all its dependecies in GBS cache.
It's expected to use this script to install all dependecies of fuzzed package.

Change-Id: I45d08f67c4a2d011c2db59d07cc751991a143db8
Signed-off-by: Dmitry Kovalenko <d.kovalenko@samsung.com>
infra/commands/rpm_n_reqs.py [new file with mode: 0755]

diff --git a/infra/commands/rpm_n_reqs.py b/infra/commands/rpm_n_reqs.py
new file mode 100755 (executable)
index 0000000..aa9a13b
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+from glob import glob
+import sys
+import subprocess
+import re
+
+
+def call_cmd(cmd):
+    try:
+        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
+    except subprocess.CalledProcessError as err:
+        sys.stderr.write(err.output.decode())
+        exit(err.returncode)
+    return proc.stdout
+
+
+def recur_reqs(req):
+    if len(rpm_provides[req]) > 1:
+        print "Use first ocurance of package '" + req +\
+            "'. TODO get newer version."
+    pkg_file = rpm_provides[req][0]["file"]
+    print pkg_file
+    out = call_cmd("rpm -qpR " + pkg_file)
+    for line in out:
+        pkg = line.strip()
+        if pkg in rpm_provides:
+            recur_reqs(pkg)
+        # else:
+        #     print "Not found '{}'".format(pkg)
+
+
+if len(sys.argv) != 3:
+    print "Usage: {} <gbs_root_path> <required_pkg>".format(sys.argv[0])
+    print "Search for <required_pkg> and all its dependencies in GBS cache"
+    exit(0)
+
+cache = sys.argv[1] + "/local/cache"
+req = sys.argv[2]
+
+rpm_provides = {}
+
+cached_rpms = glob(cache + "/*/*.rpm")
+for rpm in cached_rpms:
+    out = call_cmd("rpm -qpp " + rpm)
+    for line in out:
+        pkg = re.search(r"(.*)-([\d.]*)-([\d.]*).x86_64", line)
+        if pkg:
+            if pkg.group(1) not in rpm_provides:
+                rpm_provides[pkg.group(1)] = []
+            rpm_provides[pkg.group(1)].append({"file": rpm,
+                                               "version1": pkg.group(2),
+                                               "version2": pkg.group(3)})
+if req in rpm_provides:
+    recur_reqs(req)
+else:
+    print "'{}' not found in caches".format(req)