~andrewsomething/ubuntu-dev-tools/665202

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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 Canonical Ltd.
#
# Modified by Iain Lane <iain@orangesquash.org.uk>, taking some code written by
# Daniel Hahler <ubuntu@thequod.de>
#
# python-launchpadlib support was added by Markus Korn <thekorn@gmx.de>.
#
# ##################################################################
#
# 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.
# 
# 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.
#
# See file /usr/share/common-licenses/GPL-3 for more details.
#
# ##################################################################

import os
import sys
import email

from ubuntutools.lp.libsupport import get_launchpad, translate_api_web, translate_web_api

def read_config():
    instructions_file = open("instructions")
    instructions = email.message_from_file(instructions_file)
    instructions_file.close()
    instr = dict()
    
    for field in "subject", "assignee", "subscribers", "tags", "text", \
                 "buglist-url", "status":
        instr[field] = instructions.get(field)
    
    return instr

def read_list():
    pack_list = set()

    listfile = open("list")
    for line in listfile.readlines():
        if line.strip()!="":
            pack_list.add(line.strip("\n"))

    listfile.close()
    return pack_list

def check_configfiles():
    result = True

    bin_path = os.path.dirname(os.path.abspath(__file__))
    if bin_path == "/usr/bin":
        example_dir = "/usr/share/doc/ubuntu-dev-tools/examples"
    else:
        example_dir = "%s/examples" % bin_path

    if not os.path.exists("instructions"):
        os.system("cp %s/massfile.instructions instructions" % example_dir)
        print >> sys.stderr, \
            "No 'instructions' file found. Copied template from %s." % \
            example_dir
        result = False

    if not os.path.exists("list"):
        os.system("cp %s/massfile.list list" % example_dir)
        print >> sys.stderr, \
            "No 'list' file found. Copied template from %s." % example_dir
        result = False

    return result


def file_bug(config):
    launchpad = get_launchpad("ubuntu-dev-tools")

    try:
        summary = config["subject"].replace("$pack", config["sourcepackage"])
        description = config["text"].replace("$pack", config["sourcepackage"])
        

        product_url = "%subuntu/+source/%s" %(launchpad._root_uri, config["sourcepackage"])
        tags = filter(None, map(lambda t: t.strip("\n").strip(), config["tags"].split(",")))
        bug = launchpad.bugs.createBug(description=description, title=summary,
            target=product_url, tags=tags)
        
        print "Successfully filed bug %i: %s" %(bug.id, translate_api_web(bug.self_link))
        
        subscribers = filter(None, map(lambda t: t.strip("\n").strip(), config["subscribers"].split(",")))
        for sub in subscribers:
            subscribe_url = "%s~%s" %(launchpad._root_uri, sub)
            bug.subscribe(person=subscribe_url)
            
        #newly created bugreports have one task
        task = bug.bug_tasks[0]
        
        if config["status"]:
            status = config["status"].capitalize()
        else:
            status = "Confirmed"
        task.status = status
        
        assignee = config["assignee"]
        if assignee:
            assignee_url = "%s~%s" %(launchpad._root_uri, assignee)
            task.assignee = assignee_url
        task.lp_save()
    except:
        "Bug for '%s' was not filed." % config["sourcepackage"]

def read_buglist(url):
    if not url:
        return set()
        
    if len(url.split("?", 1)) == 2:
        # search options not supported, because there is no mapping web ui options <-> API options
        print >> sys.stderr, "Options in url are not supported, url: %s" %url
        sys.exit(1)
        
    launchpad = get_launchpad("ubuntu-dev-tools")
    packages = set()

    api_url = translate_web_api(url, launchpad)
    # workaround LP #303414
    # if this is fixed it should simply be: buglist = launchpad.load(api_url)
    api_url = api_url.split("?", 1)[0]
    project = launchpad.load(api_url)
    buglist = project.searchTasks()
    
    for bug in buglist:
        packages.add(bug.bug_target_name)
        
    return packages

def main():
    if not check_configfiles():
        sys.exit(1)

    config = read_config()
    pack_list = read_list()
    buglist = read_buglist(config["buglist-url"])
	
    for pack in pack_list:
        if pack not in buglist:
            config["sourcepackage"] = pack
            file_bug(config)

	
if __name__ == '__main__':
    main()