~vorlon/ubuntu-archive-tools/sru-release-esm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-

# Copyright (C) 2011, 2012  Canonical Ltd.
# Author: Stéphane Graber <stgraber@ubuntu.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# To use this module, you need a ini configuration file at ~/.isotracker.conf
# example:
#  [general]
#  url=http://iso.qa.ubuntu.com/xmlrpc.php
#  username=stgraber
#  password=blablabla
#  default_milestone=Precise Daily
#
#  [localized]
#  url=http://localized-iso.qa.ubuntu.com/xmlrpc.php
#  password=differentpassword

from __future__ import print_function
try:
    import configparser
except ImportError:
    import ConfigParser as configparser

from qatracker import QATracker, QATrackerMilestone, QATrackerProduct
import os

class NoConfigurationError(Exception):
    pass


class ISOTracker:
    def __init__(self, target=None):
        # Store the alternative target (configuration section)
        self.target = target

        # Read configuration
        configfile = os.path.expanduser('~/.isotracker.conf')
        if not os.path.exists(configfile):
            raise NoConfigurationError(
                "Missing configuration file at: %s" % configfile)

        # Load the config
        self.config = configparser.ConfigParser()
        self.config.read([configfile])

        # Connect to the tracker
        url = self.config.get('general', 'url')
        username = self.config.get('general', 'username')
        password = self.config.get('general', 'password')

        # Override with custom URL and credentials for the target
        if self.target:
            if self.config.has_section(self.target):
                if self.config.has_option(self.target, 'url'):
                    url = self.config.get(self.target, 'url')
                if self.config.has_option(self.target, 'username'):
                    username = self.config.get(self.target, 'username')
                if self.config.has_option(self.target, 'password'):
                    password = self.config.get(self.target, 'password')
            else:
                print("Couldn't find a '%s' target, using the default." %
                      self.target)

        self.qatracker = QATracker(url, username, password)

        # Get the required list of products and milestones
        self.tracker_products = self.qatracker.get_products()
        self.tracker_milestones = self.qatracker.get_milestones()

    def default_milestone(self):
        """
            Get the default milestone from the configuration file.
        """

        milestone_name = None

        if self.target:
            # Series-specific default milestone
            try:
                milestone_name = self.config.get(self.target,
                                                 'default_milestone')
            except (KeyError, configparser.NoSectionError,
                    configparser.NoOptionError):
                pass

        if not milestone_name:
            # Generic default milestone
            try:
                milestone_name = self.config.get('general',
                                                 'default_milestone')
            except (KeyError, configparser.NoSectionError,
                    configparser.NoOptionError):
                pass

        if not milestone_name:
            raise KeyError("No default milestone selected")
        else:
            return self.get_milestone_by_name(milestone_name)

    def get_product_by_name(self, product):
        """
            Get a QATrackerProduct from the product's name.
        """

        for entry in self.tracker_products:
            if entry.title.lower() == product.lower():
                return entry
        else:
            raise KeyError("Product '%s' not found" % product)

    def get_milestone_by_name(self, milestone):
        """
            Get a QATrackerMilestone from the milestone's name.
        """

        for entry in self.tracker_milestones:
            if entry.title.lower() == milestone.lower():
                return entry
        else:
            raise KeyError("Milestone '%s' not found" % milestone)

    def get_builds(self, milestone=None,
                   status=['Active', 'Re-building', 'Ready']):
        """
            Get a list of QATrackerBuild for the given milestone and status.
        """

        if not milestone:
            milestone = self.default_milestone()
        elif not isinstance(milestone, QATrackerMilestone):
            milestone = self.get_milestone_by_name(milestone)

        return milestone.get_builds(status)

    def post_build(self, product, version, milestone=None, note="",
                   notify=True):
        """
            Post a new build to the given milestone.
        """

        if not isinstance(product, QATrackerProduct):
            product = self.get_product_by_name(product)

        notefile = os.path.expanduser('~/.isotracker.note')
        if note == "" and os.path.exists(notefile):
            with open(notefile, 'r') as notefd:
                note = notefd.read()

        if not milestone:
            milestone = self.default_milestone()
        elif not isinstance(milestone, QATrackerMilestone):
            milestone = self.get_milestone_by_name(milestone)

        if milestone.add_build(product, version, note, notify):
            print("Build successfully added to the tracker")
        else:
            print("Failed to add build to the tracker")