~psivaa/uci-engine/lander-upstart-logs-to-srv

« back to all changes in this revision

Viewing changes to cupstream2distro/cupstream2distro/launchpadmanager.py

  • Committer: Francis Ginther
  • Date: 2014-06-10 20:42:46 UTC
  • mto: This revision was merged to the branch mainline in revision 571.
  • Revision ID: francis.ginther@canonical.com-20140610204246-b1bsrik7nlcolqy7
Import lp:cupstream2distro rev 605.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (C) 2012 Canonical
 
3
#
 
4
# Authors:
 
5
#  Didier Roche
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify it under
 
8
# the terms of the GNU General Public License as published by the Free Software
 
9
# Foundation; version 3.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
14
# details.cat
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program; if not, write to the Free Software Foundation, Inc.,
 
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
19
 
 
20
from __future__ import unicode_literals
 
21
 
 
22
from launchpadlib.launchpad import Launchpad
 
23
import lazr
 
24
import logging
 
25
import os
 
26
launchpad = None
 
27
 
 
28
from .settings import ARCHS_TO_EVENTUALLY_IGNORE, ARCHS_TO_UNCONDITIONALLY_IGNORE, VIRTUALIZED_PPA_ARCH, CRED_FILE_PATH, COMMON_LAUNCHPAD_CACHE_DIR
 
29
 
 
30
 
 
31
def get_launchpad(use_staging=False, use_cred_file=os.path.expanduser(CRED_FILE_PATH)):
 
32
    '''Get THE Launchpad'''
 
33
    global launchpad
 
34
    if not launchpad:
 
35
        if use_staging:
 
36
            server = 'staging'
 
37
        else:
 
38
            server = 'production'
 
39
 
 
40
        # as launchpadlib isn't multiproc, fiddling the cache dir if any
 
41
        launchpadlib_dir = os.getenv("JOB_NAME")
 
42
        if launchpadlib_dir:
 
43
            launchpadlib_dir = os.path.join(COMMON_LAUNCHPAD_CACHE_DIR, launchpadlib_dir)
 
44
 
 
45
        if use_cred_file:
 
46
            launchpad = Launchpad.login_with('cupstream2distro', server, allow_access_levels=["WRITE_PRIVATE"],
 
47
                                             version='devel',  # devel because copyPackage is only available there
 
48
                                             credentials_file=use_cred_file,
 
49
                                             launchpadlib_dir=launchpadlib_dir)
 
50
        else:
 
51
            launchpad = Launchpad.login_with('cupstream2distro', server, allow_access_levels=["WRITE_PRIVATE"],
 
52
                                             version='devel',  # devel because copyPackage is only available there
 
53
                                             launchpadlib_dir=launchpadlib_dir)
 
54
 
 
55
    return launchpad
 
56
 
 
57
 
 
58
def get_ubuntu():
 
59
    '''Get the ubuntu distro'''
 
60
    lp = get_launchpad()
 
61
    return lp.distributions['ubuntu']
 
62
 
 
63
 
 
64
def get_ubuntu_archive():
 
65
    '''Get the ubuntu main archive'''
 
66
    return get_ubuntu().main_archive
 
67
 
 
68
 
 
69
def get_series(series_name):
 
70
    '''Return the launchpad object for the requested series'''
 
71
    return get_ubuntu().getSeries(name_or_version=series_name)
 
72
 
 
73
 
 
74
def get_bugs_titles(author_bugs):
 
75
    lp = get_launchpad()
 
76
    author_bugs_with_title = author_bugs.copy()
 
77
    for author in author_bugs:
 
78
        bug_title_sets = set()
 
79
        for bug in author_bugs[author]:
 
80
            try:
 
81
                bug_title_sets.add("{} (LP: #{})".format(lp.bugs[bug].title, bug))
 
82
            except KeyError:
 
83
                # still list non existing or if launchpad timeouts bugs
 
84
                bug_title_sets.add(u"Fix LP: #{}".format(bug))
 
85
        author_bugs_with_title[author] = bug_title_sets
 
86
 
 
87
    return author_bugs_with_title
 
88
 
 
89
 
 
90
def open_bugs_for_source(bugs_list, source_name, series_name):
 
91
    lp = get_launchpad()
 
92
    ubuntu = get_ubuntu()
 
93
 
 
94
    # don't nominate for current series
 
95
    if ubuntu.current_series.name == series_name:
 
96
        package = ubuntu.getSourcePackage(name=source_name)
 
97
    else:
 
98
        series = get_series(series_name)
 
99
        package = series.getSourcePackage(name=source_name)
 
100
 
 
101
    for bug_num in bugs_list:
 
102
        try:
 
103
            bug = lp.bugs[bug_num]
 
104
            bug.addTask(target=package)
 
105
            bug.lp_save()
 
106
        except (KeyError, lazr.restfulclient.errors.BadRequest, lazr.restfulclient.errors.ServerError):
 
107
            # ignore non existing or available bugs
 
108
            logging.info("Can't synchronize upstream/downstream bugs for bug #{}. Not blocking on that.".format(bug_num))
 
109
 
 
110
 
 
111
def get_available_all_and_ignored_archs(series, ppa=None):
 
112
    '''Return a set of available archs, the all arch and finally the archs we can eventually ignored if nothing is published in dest'''
 
113
    available_arch = set()
 
114
    if ppa and ppa.require_virtualized:
 
115
        available_arch = set(VIRTUALIZED_PPA_ARCH)
 
116
        arch_all_arch = VIRTUALIZED_PPA_ARCH[0]
 
117
    else:
 
118
        for arch in series.architectures:
 
119
            # HACK: filters armel as it's still seen as available on raring: https://launchpad.net/bugs/1077257
 
120
            if arch.architecture_tag == "armel":
 
121
                continue
 
122
            available_arch.add(arch.architecture_tag)
 
123
            if arch.is_nominated_arch_indep:
 
124
                arch_all_arch = arch.architecture_tag
 
125
 
 
126
    return (available_arch, arch_all_arch, ARCHS_TO_EVENTUALLY_IGNORE, ARCHS_TO_UNCONDITIONALLY_IGNORE)
 
127
 
 
128
 
 
129
def get_ppa(ppa_name):
 
130
    '''Return a launchpad ppa'''
 
131
    ppa_dispatch = ppa_name.split("/")
 
132
    return get_launchpad().people[ppa_dispatch[0]].getPPAByName(name=ppa_dispatch[1])
 
133
 
 
134
def is_series_current(series_name):
 
135
    '''Return if series_name is the edge development version'''
 
136
    return get_ubuntu().current_series.name == series_name
 
137
 
 
138
def get_resource_from_url(url):
 
139
    '''Return a lp resource from a launchpad url'''
 
140
    lp = get_launchpad()
 
141
    url = lp.resource_type_link.replace("/#service-root", "") + url.split("launchpad.net")[1]
 
142
    return lp.load(url)
 
143
 
 
144
def get_resource_from_token(url):
 
145
    '''Return a lp resource from a launchpad token'''
 
146
    lp = get_launchpad()
 
147
    return lp.load(url)
 
148
 
 
149
def is_dest_ubuntu_archive(series_link):
 
150
    '''return if series_link is the ubuntu archive'''
 
151
    return series_link == get_ubuntu_archive().self_link
 
152
 
 
153
def get_person(nickname):
 
154
    '''Return a launchpad person'''
 
155
    lp = get_launchpad()
 
156
    return lp.people[nickname]