~brendan-donegan/phablet-tools/citrain-diff

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
#!/usr/bin/python
# This program is free software: you can redistribute it and/or modify it
# under the terms of the the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the applicable version of 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/>.
#
# Copyright (C) 2014 Canonical, Ltd.

import os
import shutil
import sys
import tempfile

from launchpadlib.launchpad import Launchpad
from argparse import ArgumentParser
from subprocess import check_call, PIPE

CITRAIN = 'ci-train-ppa-service'


def get_silo_source_packages(lp, distro, silo):
    distribution = lp.distributions[distro]
    citrain = lp.people[CITRAIN]
    silo_ppa = citrain.getPPAByName(
        distribution=distribution,
        name=silo.split('/')[1]
    )
    silo_sources = {}
    for source in silo_ppa.getPublishedSources(status='Published'):
        silo_sources[source.source_package_name] = source
    return silo_sources


def get_archive_source_packages(lp, distro, source_packages):
    distribution = lp.distributions[distro]
    archive_sources = {}
    for source in source_packages:
        archive_source = distribution.main_archive.getPublishedSources(
            source_name=source,
            exact_match=True,
            distro_series=distribution.current_series,
            status='Published'
        )[0]
        archive_sources[source] = archive_source
    return archive_sources


def get_dsc_files(silo_sources, archive_sources):
    dsc_files = {}
    for source in silo_sources:
        silo_source_dsc = [dsc for dsc in
                           silo_sources[source].sourceFileUrls()
                           if dsc.endswith('.dsc')][0]
        archive_source_dsc = [dsc for dsc in
                              archive_sources[source].sourceFileUrls()
                              if dsc.endswith('.dsc')][0]
        dsc_files[source] = (silo_source_dsc, archive_source_dsc)
    return dsc_files


def diff_package(dsc_files):
    # Download both dsc files to it
    temp_dir = tempfile.mkdtemp()
    os.chdir(temp_dir)
    check_call(['dget', '-u', dsc_files[0]], stdout=PIPE, stderr=PIPE)
    check_call(['dget', '-u', dsc_files[1]], stdout=PIPE, stderr=PIPE)
    silo_dsc_file = dsc_files[0].split('/')[-1]
    archive_dsc_file = dsc_files[1].split('/')[-1]
    # Debdiff them
    check_call('debdiff --auto-ver-sort %s %s; true' % (archive_dsc_file,
                                                        silo_dsc_file),
               shell=True)
    shutil.rmtree(temp_dir)


def main():
    parser = ArgumentParser("Get the proper diff of a silo")
    parser.add_argument("silo",
                        help="The name of the silo, e.g. "
                             "ubuntu-rtm/landing-001")
    args = parser.parse_args()

    lp = Launchpad.login_anonymously(sys.argv[0], 'production')

    distro = args.silo.split('/')[0]
    silo_src_pkgs = get_silo_source_packages(lp, distro, args.silo)
    archive_src_pkgs = get_archive_source_packages(lp, distro, silo_src_pkgs)
    dsc_files = get_dsc_files(silo_src_pkgs, archive_src_pkgs)
    if dsc_files:
        for source_package in dsc_files:
            diff_package(dsc_files[source_package])
    else:
        print("%s contains no source packages at the moment." % args.silo)

if __name__ == "__main__":
    sys.exit(main())