~ubuntu-security/ubuntu-cve-tracker/master

« back to all changes in this revision

Viewing changes to scripts/lp_lib.py

  • Committer: Steve Beattie
  • Date: 2019-02-19 06:18:27 UTC
  • Revision ID: sbeattie@ubuntu.com-20190219061827-oh57fzcfc1u9dlfk
The ubuntu-cve-tracker project has been converted to git.

Please use 'git clone https://git.launchpad.net/ubuntu-cve-tracker' to
get the converted tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
# Author: Kees Cook <kees@ubuntu.com>
4
 
# Author: Marc Deslauriers <marc.deslauriers@ubuntu.com>
5
 
# Copyright (C) 2011-2012 Canonical Ltd.
6
 
#
7
 
# This script is distributed under the terms and conditions of the GNU General
8
 
# Public License, Version 2 or later. See http://www.gnu.org/copyleft/gpl.html
9
 
# for details.
10
 
#
11
 
# Lazy-loading LP interface.
12
 
from __future__ import print_function
13
 
 
14
 
import sys
15
 
try:
16
 
    import lpl_common
17
 
except:
18
 
    print("lpl_common.py seems to be missing. Please create a symlink from $UQT/common/lpl_common.py to $UCT/scripts/", file=sys.stderr)
19
 
    sys.exit(1)
20
 
 
21
 
class UCTLaunchpad(object):
22
 
    def __init__(self, opt):
23
 
        # Global LP connections/links
24
 
        self.link = dict()
25
 
        self.link['lp'] = None
26
 
        self.link['ubuntu'] = None
27
 
        self.link['archive'] = None
28
 
        self.link['people'] = None
29
 
 
30
 
        # Cached dictionaries
31
 
        self.cache = dict()
32
 
        self.cache['series'] = dict()
33
 
 
34
 
        # For get_release_version
35
 
        self.release_pkg_cache = dict()
36
 
 
37
 
        # Toggles
38
 
        self.debug = hasattr(opt, 'debug') and opt.debug
39
 
 
40
 
        self.lp_version = "1.0"
41
 
 
42
 
    def extract_task(self, task):
43
 
        return lpl_common.extract_task(task)
44
 
 
45
 
    def save(self, thing):
46
 
        return lpl_common.save(thing)
47
 
 
48
 
    def cached(self, field, name):
49
 
        if not self.cache.has_key(field):
50
 
            raise AttributeError("UCTLaunchpad has no cache named '%s'" % (field))
51
 
        if self.cache[field].has_key(name):
52
 
            return self.cache[field][name]
53
 
        if field == 'series':
54
 
            if self.debug:
55
 
                print("API: getSeries(%s) ..." % (name), file=sys.stderr)
56
 
            self.cache[field][name] = self.ubuntu.getSeries(name_or_version=name)
57
 
        return self.cache[field][name]
58
 
 
59
 
    def __getattr__(self, name):
60
 
        if not self.link.has_key(name):
61
 
            raise AttributeError("UCTLaunchpad has no attr named '%s'" % (name))
62
 
        if self.link[name] != None:
63
 
            return self.link[name]
64
 
        if name == 'lp':
65
 
            if self.debug:
66
 
                print("Starting LP API ...", file=sys.stderr)
67
 
            self.link['lp'] = lpl_common.connect(version=self.lp_version)
68
 
            if self.debug:
69
 
                print("\tauthenticated to LP.", file=sys.stderr)
70
 
        elif name == 'ubuntu':
71
 
            if self.debug:
72
 
                print("Looking up Ubuntu distribution ...", file=sys.stderr)
73
 
            self.link['ubuntu'] = self.lp.distributions['ubuntu']
74
 
            if self.debug:
75
 
                print("\tUbuntu distribution found.", file=sys.stderr)
76
 
        elif name =='archive':
77
 
            if self.debug:
78
 
                print("Looking up Ubuntu archive ...", file=sys.stderr)
79
 
            self.link['archive'] = self.ubuntu.archives[0]
80
 
            if self.debug:
81
 
                print("\tUbuntu archive found.", file=sys.stderr)
82
 
        elif name =='people':
83
 
            if self.debug:
84
 
                print("Looking up Ubuntu people ...", file=sys.stderr)
85
 
            self.link['people'] = self.lp.people
86
 
            if self.debug:
87
 
                print("\tUbuntu people found.", file=sys.stderr)
88
 
        return self.link[name]
89
 
 
90
 
    # Searches all pockets for the first known version of a package. (Since
91
 
    # packages can be added to -updates post-release, we need to check all
92
 
    # pockets.) To find a version for a specific pocket, just override the list.
93
 
    def get_earliest_version(self, rel, pkg, key='earliest', pockets=['Release','Updates','Security','Proposed']):
94
 
      if not self.release_pkg_cache.has_key(rel) or not self.release_pkg_cache[rel].has_key(pkg) or not self.release_pkg_cache[rel][pkg].has_key(key):
95
 
        self.release_pkg_cache.setdefault(rel, dict())
96
 
        self.release_pkg_cache[rel].setdefault(pkg, dict())
97
 
        for pocket in pockets:
98
 
            if self.debug:
99
 
                print("get_earliest_version: getPublishedSources(%s, %s, %s) ..." % (rel, pocket, pkg), file=sys.stderr)
100
 
            # When looking at the release pocket, make sure to only look at the "Published"
101
 
            # status, so we don't get stuff from when the release was in development
102
 
            if pocket == 'Release':
103
 
                pkgs = self.archive.getPublishedSources(source_name=pkg, distro_series=self.cached('series',rel), pocket=pocket, status='Published', exact_match=True)
104
 
            else:
105
 
                pkgs = self.archive.getPublishedSources(source_name=pkg, distro_series=self.cached('series',rel), pocket=pocket, exact_match=True)
106
 
            if len(pkgs) < 1:
107
 
                if self.debug:
108
 
                    print("\tDNE", file=sys.stderr)
109
 
                continue
110
 
            # Oldest is last
111
 
            self.release_pkg_cache[rel][pkg][key] = pkgs[len(pkgs)-1].source_package_version
112
 
            if self.debug:
113
 
                print("\t%s" % (self.release_pkg_cache[rel][pkg][key]), file=sys.stderr)
114
 
            break
115
 
      # Recent dpkg doesn't consider "~" to be a valid version number
116
 
      self.release_pkg_cache[rel][pkg].setdefault(key, "0~")
117
 
      return self.release_pkg_cache[rel][pkg][key]
118
 
 
119
 
    # Searches all pockets for the latest version of a package.
120
 
    # To find a version for a specific pocket, just override the list.
121
 
    def get_latest_version(self, rel, pkg, key='latest', pockets=['Proposed', 'Updates', 'Security','Release']):
122
 
      if not self.release_pkg_cache.has_key(rel) or not self.release_pkg_cache[rel].has_key(pkg) or not self.release_pkg_cache[rel][pkg].has_key(key):
123
 
        self.release_pkg_cache.setdefault(rel, dict())
124
 
        self.release_pkg_cache[rel].setdefault(pkg, dict())
125
 
        for pocket in pockets:
126
 
            if self.debug:
127
 
                print("get_latest_version: getPublishedSources(%s, %s, %s) ..." % (rel, pocket, pkg), file=sys.stderr)
128
 
            pkgs = self.archive.getPublishedSources(source_name=pkg, distro_series=self.cached('series',rel), pocket=pocket, exact_match=True)
129
 
            if len(pkgs) < 1:
130
 
                if self.debug:
131
 
                    print("\tDNE", file=sys.stderr)
132
 
                continue
133
 
            # Newest is first
134
 
            self.release_pkg_cache[rel][pkg][key] = pkgs[0].source_package_version
135
 
            if self.debug:
136
 
                print("\t%s" % (self.release_pkg_cache[rel][pkg][key]), file=sys.stderr)
137
 
            break
138
 
      # Recent dpkg doesn't consider "~" to be a valid version number
139
 
      self.release_pkg_cache[rel][pkg].setdefault(key, "0~")
140
 
      return self.release_pkg_cache[rel][pkg][key]