]> git.decadent.org.uk Git - dak.git/blobdiff - dak/examine_package.py
Make homepage URLs (nofollow) links.
[dak.git] / dak / examine_package.py
index 617fc7ad77f6ed04f2bf437ab828e76eca87927d..4b315dedcc77c8fb234f1c38e36263b0a48491e0 100755 (executable)
@@ -42,6 +42,12 @@ to stdout. Those functions can be used in multithreaded parts of dak.
 
 ################################################################################
 
+# suppress some deprecation warnings in squeeze related to md5 module
+import warnings
+warnings.filterwarnings('ignore', \
+    "the md5 module is deprecated; use hashlib instead", \
+    DeprecationWarning)
+
 import errno
 import os
 import re
@@ -50,26 +56,30 @@ import md5
 import apt_pkg
 import apt_inst
 import shutil
-import commands
+import subprocess
 import threading
 
 from daklib import utils
-from daklib.dbconn import DBConn, get_binary_from_name_suite
+from daklib.config import Config
+from daklib.dbconn import DBConn, get_component_by_package_suite
+from daklib.gpg import SignedFile
 from daklib.regexes import html_escaping, re_html_escaping, re_version, re_spacestrip, \
                            re_contrib, re_nonfree, re_localhost, re_newlinespace, \
                            re_package, re_doc_directory
+from daklib.dak_exceptions import ChangesUnicodeError
+import daklib.daksubprocess
 
 ################################################################################
 
 Cnf = None
 Cnf = utils.get_conf()
 
-changes_lock = threading.Lock()
-printed_copyrights = {}
+printed = threading.local()
+printed.copyrights = {}
 package_relations = {}           #: Store relations of packages for later output
 
 # default is to not output html.
-use_html = 0
+use_html = False
 
 ################################################################################
 
@@ -99,7 +109,7 @@ def headline(s, level=2, bodyelement=None):
         if bodyelement:
             return """<thead>
                 <tr><th colspan="2" class="title" onclick="toggle('%(bodyelement)s', 'table-row-group', 'table-row-group')">%(title)s <span class="toggle-msg">(click to toggle)</span></th></tr>
-              </thead>\n"""%{"bodyelement":bodyelement,"title":utils.html_escape(s)}
+              </thead>\n"""%{"bodyelement":bodyelement,"title":utils.html_escape(os.path.basename(s))}
         else:
             return "<h%d>%s</h%d>\n" % (level, utils.html_escape(s), level)
     else:
@@ -111,18 +121,22 @@ ansi_colours = {
   'main': "\033[36m",
   'contrib': "\033[33m",
   'nonfree': "\033[31m",
+  'provides': "\033[35m",
   'arch': "\033[32m",
   'end': "\033[0m",
   'bold': "\033[1m",
-  'maintainer': "\033[32m"}
+  'maintainer': "\033[32m",
+  'distro': "\033[1m\033[41m"}
 
 html_colours = {
   'main': ('<span style="color: aqua">',"</span>"),
   'contrib': ('<span style="color: yellow">',"</span>"),
   'nonfree': ('<span style="color: red">',"</span>"),
+  'provides': ('<span style="color: magenta">',"</span>"),
   'arch': ('<span style="color: green">',"</span>"),
   'bold': ('<span style="font-weight: bold">',"</span>"),
-  'maintainer': ('<span style="color: green">',"</span>")}
+  'maintainer': ('<span style="color: green">',"</span>"),
+  'distro': ('<span style="font-weight: bold; background-color: red">',"</span>")}
 
 def colour_output(s, colour):
     if use_html:
@@ -219,6 +233,7 @@ def split_depends (d_str) :
 
 def read_control (filename):
     recommends = []
+    predepends = []
     depends = []
     section = ''
     maintainer = ''
@@ -226,8 +241,8 @@ def read_control (filename):
 
     deb_file = utils.open_file(filename)
     try:
-        extracts = apt_inst.debExtractControl(deb_file)
-        control = apt_pkg.ParseSection(extracts)
+        extracts = utils.deb_extract_control(deb_file)
+        control = apt_pkg.TagSection(extracts)
     except:
         print formatted_text("can't parse control info")
         deb_file.close()
@@ -237,17 +252,21 @@ def read_control (filename):
 
     control_keys = control.keys()
 
-    if control.has_key("Depends"):
-        depends_str = control.Find("Depends")
+    if "Pre-Depends" in control:
+        predepends_str = control["Pre-Depends"]
+        predepends = split_depends(predepends_str)
+
+    if "Depends" in control:
+        depends_str = control["Depends"]
         # create list of dependancy lists
         depends = split_depends(depends_str)
 
-    if control.has_key("Recommends"):
-        recommends_str = control.Find("Recommends")
+    if "Recommends" in control:
+        recommends_str = control["Recommends"]
         recommends = split_depends(recommends_str)
 
-    if control.has_key("Section"):
-        section_str = control.Find("Section")
+    if "Section" in control:
+        section_str = control["Section"]
 
         c_match = re_contrib.search(section_str)
         nf_match = re_nonfree.search(section_str)
@@ -260,12 +279,12 @@ def read_control (filename):
         else :
             # main
             section = colour_output(section_str, 'main')
-    if control.has_key("Architecture"):
-        arch_str = control.Find("Architecture")
+    if "Architecture" in control:
+        arch_str = control["Architecture"]
         arch = colour_output(arch_str, 'arch')
 
-    if control.has_key("Maintainer"):
-        maintainer = control.Find("Maintainer")
+    if "Maintainer" in control:
+        maintainer = control["Maintainer"]
         localhost = re_localhost.search(maintainer)
         if localhost:
             #highlight bad email
@@ -273,9 +292,9 @@ def read_control (filename):
         else:
             maintainer = escape_if_needed(maintainer)
 
-    return (control, control_keys, section, depends, recommends, arch, maintainer)
+    return (control, control_keys, section, predepends, depends, recommends, arch, maintainer)
 
-def read_changes_or_dsc (suite, filename):
+def read_changes_or_dsc (suite, filename, session = None):
     dsc = {}
 
     dsc_file = utils.open_file(filename)
@@ -294,10 +313,13 @@ def read_changes_or_dsc (suite, filename):
 
     for k in dsc.keys():
         if k in ("build-depends","build-depends-indep"):
-            dsc[k] = create_depends_string(suite, split_depends(dsc[k]))
+            dsc[k] = create_depends_string(suite, split_depends(dsc[k]), session)
         elif k == "architecture":
             if (dsc["architecture"] != "any"):
                 dsc['architecture'] = colour_output(dsc["architecture"], 'arch')
+        elif k == "distribution":
+            if dsc["distribution"] not in ('unstable', 'experimental'):
+                dsc['distribution'] = colour_output(dsc["distribution"], 'distro')
         elif k in ("files","changes","description"):
             if use_html:
                 dsc[k] = formatted_text(dsc[k], strip=True)
@@ -311,13 +333,38 @@ def read_changes_or_dsc (suite, filename):
     filecontents = '\n'.join(map(lambda x: format_field(x,dsc[x.lower()]), keysinorder))+'\n'
     return filecontents
 
-def create_depends_string (suite, depends_tree):
+def get_provides(suite):
+    provides = set()
+    session = DBConn().session()
+    query = '''SELECT DISTINCT value
+               FROM binaries_metadata m
+               JOIN bin_associations b
+               ON b.bin = m.bin_id
+               WHERE key_id = (
+                 SELECT key_id
+                 FROM metadata_keys
+                 WHERE key = 'Provides' )
+               AND b.suite = (
+                 SELECT id
+                 FROM suite
+                 WHERE suite_name = '%(suite)s'
+                 OR codename = '%(suite)s')''' % \
+            {'suite': suite}
+    for p in session.execute(query):
+        for e in p:
+            for i in e.split(','):
+                provides.add(i.strip())
+    session.close()
+    return provides
+
+def create_depends_string (suite, depends_tree, session = None):
     result = ""
     if suite == 'experimental':
-        suite_where = "in ('experimental','unstable')"
+        suite_list = ['experimental','unstable']
     else:
-        suite_where = "= '%s'" % suite
+        suite_list = [suite]
 
+    provides = set()
     comma_count = 1
     for l in depends_tree:
         if (comma_count >= 2):
@@ -328,17 +375,16 @@ def create_depends_string (suite, depends_tree):
                 result += " | "
             # doesn't do version lookup yet.
 
-            res = get_binary_from_name_suite(d['name'], suite_where)
-            if res.rowcount > 0:
-                i = res.fetchone()
-
+            component = get_component_by_package_suite(d['name'], suite_list, \
+                session = session)
+            if component is not None:
                 adepends = d['name']
                 if d['version'] != '' :
                     adepends += " (%s)" % (d['version'])
 
-                if i[2] == "contrib":
+                if component == "contrib":
                     result += colour_output(adepends, "contrib")
-                elif i[2] == "non-free":
+                elif component == "non-free":
                     result += colour_output(adepends, "nonfree")
                 else :
                     result += colour_output(adepends, "main")
@@ -346,7 +392,12 @@ def create_depends_string (suite, depends_tree):
                 adepends = d['name']
                 if d['version'] != '' :
                     adepends += " (%s)" % (d['version'])
-                result += colour_output(adepends, "bold")
+                if not provides:
+                    provides = get_provides(suite)
+                if d['name'] in provides:
+                    result += colour_output(adepends, "provides")
+                else:
+                    result += colour_output(adepends, "bold")
             or_count += 1
         comma_count += 1
     return result
@@ -369,8 +420,8 @@ def output_package_relations ():
     package_relations.clear()
     return foldable_output("Package relations", "relations", to_print)
 
-def output_deb_info(suite, filename, packagename):
-    (control, control_keys, section, depends, recommends, arch, maintainer) = read_control(filename)
+def output_deb_info(suite, filename, packagename, session = None):
+    (control, control_keys, section, predepends, depends, recommends, arch, maintainer) = read_control(filename)
 
     if control == '':
         return formatted_text("no control info")
@@ -378,11 +429,14 @@ def output_deb_info(suite, filename, packagename):
     if not package_relations.has_key(packagename):
         package_relations[packagename] = {}
     for key in control_keys :
-        if key == 'Depends':
-            field_value = create_depends_string(suite, depends)
+        if key == 'Pre-Depends':
+            field_value = create_depends_string(suite, predepends, session)
+            package_relations[packagename][key] = field_value
+        elif key == 'Depends':
+            field_value = create_depends_string(suite, depends, session)
             package_relations[packagename][key] = field_value
         elif key == 'Recommends':
-            field_value = create_depends_string(suite, recommends)
+            field_value = create_depends_string(suite, recommends, session)
             package_relations[packagename][key] = field_value
         elif key == 'Section':
             field_value = section
@@ -390,35 +444,54 @@ def output_deb_info(suite, filename, packagename):
             field_value = arch
         elif key == 'Maintainer':
             field_value = maintainer
+        elif key == 'Homepage':
+            field_value = escape_if_needed(control.find(key))
+            if use_html:
+                field_value = '<a href="%s" rel="nofollow">%s</a>' % \
+                    (field_value, field_value)
         elif key == 'Description':
             if use_html:
-                field_value = formatted_text(control.Find(key), strip=True)
+                field_value = formatted_text(control.find(key), strip=True)
             else:
-                desc = control.Find(key)
+                desc = control.find(key)
                 desc = re_newlinespace.sub('\n ', desc)
                 field_value = escape_if_needed(desc)
         else:
-            field_value = escape_if_needed(control.Find(key))
+            field_value = escape_if_needed(control.find(key))
         to_print += " "+format_field(key,field_value)+'\n'
     return to_print
 
-def do_command (command, filename, escaped=0):
-    o = os.popen("%s %s" % (command, filename))
-    if escaped:
-        return escaped_text(o.read())
-    else:
-        return formatted_text(o.read())
+def do_command (command, escaped=False):
+    process = daklib.daksubprocess.Popen(command, stdout=subprocess.PIPE)
+    o = process.stdout
+    try:
+        if escaped:
+            return escaped_text(o.read())
+        else:
+            return formatted_text(o.read())
+    finally:
+        process.wait()
 
 def do_lintian (filename):
+    cnf = Config()
+    cmd = []
+
+    user = cnf.get('Dinstall::UnprivUser') or None
+    if user is not None:
+        cmd.extend(['sudo', '-H', '-u', user])
+
+    color = 'always'
     if use_html:
-        return do_command("lintian --show-overrides --color html", filename, 1)
-    else:
-        return do_command("lintian --show-overrides --color always", filename, 1)
+        color = 'html'
+
+    cmd.extend(['lintian', '--show-overrides', '--color', color, "--", filename])
+
+    return do_command(cmd, escaped=True)
 
 def get_copyright (deb_filename):
-    global changes_lock, printed_copyrights
+    global printed
 
-    package = re_package.sub(r'\1', deb_filename)
+    package = re_package.sub(r'\1', os.path.basename(deb_filename))
     o = os.popen("dpkg-deb -c %s | egrep 'usr(/share)?/doc/[^/]*/copyright' | awk '{print $6}' | head -n 1" % (deb_filename))
     cright = o.read()[:-1]
 
@@ -434,54 +507,54 @@ def get_copyright (deb_filename):
     copyrightmd5 = md5.md5(cright).hexdigest()
 
     res = ""
-    changes_lock.acquire()
-    if printed_copyrights.has_key(copyrightmd5) and printed_copyrights[copyrightmd5] != "%s (%s)" % (package, deb_filename):
+    if printed.copyrights.has_key(copyrightmd5) and printed.copyrights[copyrightmd5] != "%s (%s)" % (package, os.path.basename(deb_filename)):
         res += formatted_text( "NOTE: Copyright is the same as %s.\n\n" % \
-                               (printed_copyrights[copyrightmd5]))
+                               (printed.copyrights[copyrightmd5]))
     else:
-        printed_copyrights[copyrightmd5] = "%s (%s)" % (package, deb_filename)
-    changes_lock.release()
+        printed.copyrights[copyrightmd5] = "%s (%s)" % (package, os.path.basename(deb_filename))
     return res+formatted_text(cright)
 
 def get_readme_source (dsc_filename):
     tempdir = utils.temp_dirname()
     os.rmdir(tempdir)
 
-    cmd = "dpkg-source --no-check --no-copy -x %s %s" % (dsc_filename, tempdir)
-    (result, output) = commands.getstatusoutput(cmd)
-    if (result != 0):
+    cmd = ('dpkg-source', '--no-check', '--no-copy', '-x', dsc_filename, tempdir)
+    try:
+        daklib.daksubprocess.check_output(cmd, stderr=1)
+    except subprocess.CalledProcessError as e:
         res = "How is education supposed to make me feel smarter? Besides, every time I learn something new, it pushes some\n old stuff out of my brain. Remember when I took that home winemaking course, and I forgot how to drive?\n"
         res += "Error, couldn't extract source, WTF?\n"
-        res += "'dpkg-source -x' failed. return code: %s.\n\n" % (result)
-        res += output
+        res += "'dpkg-source -x' failed. return code: %s.\n\n" % (e.returncode)
+        res += e.output
         return res
 
     path = os.path.join(tempdir, 'debian/README.source')
     res = ""
     if os.path.exists(path):
-        res += do_command("cat", path)
+        res += do_command(["cat", "--", path])
     else:
         res += "No README.source in this package\n\n"
 
     try:
         shutil.rmtree(tempdir)
-    except OSError, e:
+    except OSError as e:
         if errno.errorcode[e.errno] != 'EACCES':
             res += "%s: couldn't remove tmp dir %s for source tree." % (dsc_filename, tempdir)
 
     return res
 
-def check_dsc (suite, dsc_filename):
-    (dsc) = read_changes_or_dsc(suite, dsc_filename)
+def check_dsc (suite, dsc_filename, session = None):
+    dsc = read_changes_or_dsc(suite, dsc_filename, session)
+    dsc_basename = os.path.basename(dsc_filename)
     return foldable_output(dsc_filename, "dsc", dsc, norow=True) + \
            "\n" + \
-           foldable_output("lintian check for %s" % dsc_filename,
+           foldable_output("lintian check for %s" % dsc_basename,
               "source-lintian", do_lintian(dsc_filename)) + \
            "\n" + \
-           foldable_output("README.source for %s" % dsc_filename,
+           foldable_output("README.source for %s" % dsc_basename,
                "source-readmesource", get_readme_source(dsc_filename))
 
-def check_deb (suite, deb_filename):
+def check_deb (suite, deb_filename, session = None):
     filename = os.path.basename(deb_filename)
     packagename = filename.split('_')[0]
 
@@ -491,7 +564,7 @@ def check_deb (suite, deb_filename):
         is_a_udeb = 0
 
     result = foldable_output("control file for %s" % (filename), "binary-%s-control"%packagename,
-        output_deb_info(suite, deb_filename, packagename), norow=True) + "\n"
+        output_deb_info(suite, deb_filename, packagename, session), norow=True) + "\n"
 
     if is_a_udeb:
         result += foldable_output("skipping lintian check for udeb",
@@ -501,7 +574,7 @@ def check_deb (suite, deb_filename):
            "binary-%s-lintian"%packagename, do_lintian(deb_filename)) + "\n"
 
     result += foldable_output("contents of %s" % (filename), "binary-%s-contents"%packagename,
-        do_command("dpkg -c", deb_filename)) + "\n"
+                              do_command(["dpkg", "-c", deb_filename])) + "\n"
 
     if is_a_udeb:
         result += foldable_output("skipping copyright for udeb",
@@ -510,45 +583,20 @@ def check_deb (suite, deb_filename):
         result += foldable_output("copyright of %s" % (filename),
            "binary-%s-copyright"%packagename, get_copyright(deb_filename)) + "\n"
 
-    result += foldable_output("file listing of %s" % (filename),
-       "binary-%s-file-listing"%packagename, do_command("ls -l", deb_filename))
-
     return result
 
 # Read a file, strip the signature and return the modified contents as
 # a string.
 def strip_pgp_signature (filename):
-    inputfile = utils.open_file (filename)
-    contents = ""
-    inside_signature = 0
-    skip_next = 0
-    for line in inputfile.readlines():
-        if line[:-1] == "":
-            continue
-        if inside_signature:
-            continue
-        if skip_next:
-            skip_next = 0
-            continue
-        if line.startswith("-----BEGIN PGP SIGNED MESSAGE"):
-            skip_next = 1
-            continue
-        if line.startswith("-----BEGIN PGP SIGNATURE"):
-            inside_signature = 1
-            continue
-        if line.startswith("-----END PGP SIGNATURE"):
-            inside_signature = 0
-            continue
-        contents += line
-    inputfile.close()
-    return contents
+    with utils.open_file(filename) as f:
+        data = f.read()
+        signedfile = SignedFile(data, keyrings=(), require_signature=False)
+        return signedfile.contents
 
 def display_changes(suite, changes_filename):
-    global changes_lock, printed_copyrights
+    global printed
     changes = read_changes_or_dsc(suite, changes_filename)
-    changes_lock.acquire()
-    printed_copyrights = {}
-    changes_lock.release()
+    printed.copyrights = {}
     return foldable_output(changes_filename, "changes", changes, norow=True)
 
 def check_changes (changes_filename):
@@ -578,15 +626,15 @@ def main ():
         if not Cnf.has_key("Examine-Package::Options::%s" % (i)):
             Cnf["Examine-Package::Options::%s" % (i)] = ""
 
-    args = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
-    Options = Cnf.SubTree("Examine-Package::Options")
+    args = apt_pkg.parse_commandline(Cnf,Arguments,sys.argv)
+    Options = Cnf.subtree("Examine-Package::Options")
 
     if Options["Help"]:
         usage()
 
     if Options["Html-Output"]:
         global use_html
-        use_html = 1
+        use_html = True
 
     stdout_fd = sys.stdout
 
@@ -594,7 +642,9 @@ def main ():
         try:
             if not Options["Html-Output"]:
                 # Pipe output for each argument through less
-                less_fd = os.popen("less -R -", 'w', 0)
+                less_cmd = ("less", "-R", "-")
+                less_process = daklib.daksubprocess.Popen(less_cmd, stdin=subprocess.PIPE, bufsize=0)
+                less_fd = less_process.stdin
                 # -R added to display raw control chars for colour
                 sys.stdout = less_fd
             try:
@@ -613,8 +663,9 @@ def main ():
                 if not Options["Html-Output"]:
                     # Reset stdout here so future less invocations aren't FUBAR
                     less_fd.close()
+                    less_process.wait()
                     sys.stdout = stdout_fd
-        except IOError, e:
+        except IOError as e:
             if errno.errorcode[e.errno] == 'EPIPE':
                 utils.warn("[examine-package] Caught EPIPE; skipping.")
                 pass