aboutsummaryrefslogtreecommitdiff
path: root/bin/RPMFinder
diff options
context:
space:
mode:
authorLucas C. Villa Real <lucasvr@gmail.com>2016-12-05 20:20:26 +0000
committerLucas C. Villa Real <lucasvr@gmail.com>2016-12-05 20:20:26 +0000
commit5e0d67f455f6b481ef53793d19dd37c8778138f6 (patch)
treecae23d1337e91de517c98aabce4685cb85008766 /bin/RPMFinder
downloadThirdPartyInstallers-5e0d67f455f6b481ef53793d19dd37c8778138f6.tar.xz
First commit.
Diffstat (limited to 'bin/RPMFinder')
-rwxr-xr-xbin/RPMFinder84
1 files changed, 84 insertions, 0 deletions
diff --git a/bin/RPMFinder b/bin/RPMFinder
new file mode 100755
index 0000000..1797174
--- /dev/null
+++ b/bin/RPMFinder
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+
+# Searches over the network to find out which RPM package distributes
+# a given file.
+#
+# Written by Lucas C. Villa Real <lucasvr@gobolinux.org>
+# Released under the GNU GPL version 2 or above.
+
+import os
+import sys
+import argparse
+import subprocess
+from HTMLParser import HTMLParser
+
+
+class RPMFind_Parser(HTMLParser):
+ '''
+ Parses the HTML data output by rpmfind.net
+ '''
+ def __init__(self):
+ self.tags = []
+ self.candidates = []
+ HTMLParser.__init__(self)
+
+ def handle_starttag(self, tag, attrs):
+ self.tags.append(tag)
+
+ def handle_endtag(self, tag):
+ self.tags.pop()
+
+ def handle_data(self, data):
+ if len(self.tags) and self.tags[-1] == "a" and data.find(".rpm") >= 0:
+ self.candidates.append(data)
+
+ def get_pkgname(self):
+ if len(self.candidates) == 0:
+ return ""
+ name = os.path.commonprefix(self.candidates)
+ if name.endswith("-"):
+ name = name[:-1]
+ return name
+
+
+class RPMFinder:
+ def find(self, path, arch, distro):
+ '''
+ Searches rpmfind.net for a given file. Arch and distro can
+ be provided to reduce the search scope. Returns the package
+ name on success or an empty string if no matches were found.
+ '''
+ self.path = path
+ self.arch = arch
+ self.distro = distro
+ return self.__search_rpmfind_net()
+
+ def __search_rpmfind_net(self):
+ path = self.path.replace("/", "%2F")
+ arch = self.arch
+ baseuri = "http://rpmfind.net/linux/rpm2html/search.php"
+ query = "?query={0}&submit=Search+...&system=&arch={1}".format(path, arch)
+ html = subprocess.check_output(["wget", "--quiet", "{0}{1}".format(baseuri, query), "-O", "-"])
+
+ htmlparser = RPMFind_Parser()
+ htmlparser.feed(html)
+ return htmlparser.get_pkgname()
+
+
+def main():
+ argparser = argparse.ArgumentParser(argument_default="")
+ argparser.add_argument("--path", type=str, help="File name to search for in the remote RPM databases")
+ argparser.add_argument("--arch", type=str, help="Architecture (optional)")
+ argparser.add_argument("--distro", type=str, help="Distribution (optional)")
+ args = argparser.parse_args()
+
+ if len(args.path) == 0:
+ argparser.print_help()
+ sys.exit(1)
+
+ pkgname = RPMFinder().find(args.path, args.arch, args.distro)
+ if len(pkgname):
+ print pkgname
+
+if __name__ == "__main__":
+ main()