~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/massage-bug-import-xml

  • Committer: Curtis Hovey
  • Date: 2011-08-21 14:21:06 UTC
  • mto: This revision was merged to the branch mainline in revision 13745.
  • Revision ID: curtis.hovey@canonical.com-20110821142106-x93hajd6iguma8gx
Update test that was enforcing bad grammar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
 
58
58
    - Fixing up the bug nickname, adding the existing nickname as a tag,
59
59
 
60
 
    - Resolving duplicates to a bug that is not itself a duplicate
61
 
      (i.e. remove chains of duplicates),
62
 
 
63
60
    - Fixing up the description, including truncating it if it's too long,
64
61
 
65
62
    - Fixing up the first comment, including truncating it if it's too long,
67
64
    - Normalizing whitespace.
68
65
 
69
66
    """
70
 
    # Resolve duplicates as far as they'll go.
71
 
    duplicates = dict(
72
 
        (node.getparent().get("id"), node.text)
73
 
        for node in root.findall('{%s}bug/{%s}duplicateof' % (NS, NS))
74
 
        if node.text is not None and node.text.isdigit())
75
 
 
76
 
    def resolve(bug_id):
77
 
        dupe_of = duplicates.get(bug_id)
78
 
        return (bug_id if dupe_of is None else resolve(dupe_of))
79
 
 
80
 
    duplicates = dict(
81
 
        (bug_id, resolve(bug_id)) for bug_id in duplicates)
82
 
 
83
67
    # Scan the tree, fixing up issues.
84
68
    for bug in root.findall('{%s}bug' % NS):
85
69
        # Get or create the tags element.
99
83
        if nickname.text is None or fix_nickname:
100
84
            nickname.text = u"%s-%s" % (project_name, bug.get('id'))
101
85
 
102
 
        # Resolve duplicateof, if it exists.
103
 
        if bug.get("id") in duplicates:
104
 
            bug.find("{%s}duplicateof" % NS).text = duplicates[bug.get("id")]
105
 
 
106
86
        # Get the first comment and its text. We'll need these later.
107
87
        first_comment = bug.find('{%s}comment' % NS)
108
88
        first_comment_text = first_comment.find('{%s}text' % NS)
193
173
    usage = "Usage: %prog [options]"
194
174
    description = """
195
175
        This acts as a filter: pipe bug import XML into stdin and capture
196
 
        stdout. By default it removes duplicate chains and ensures that bug
197
 
        descriptions and the first comment are correct. If either the
198
 
        description or the first comment exceeds 50,000 characters it is
 
176
        stdout. By default it will ensure that bug descriptions and the first
 
177
        comment are correct. If either exceeds 50,000 characters it is
199
178
        truncated and an attachment is created to hold the original.
200
179
        """
201
180
    parser = OptionParser(
219
198
        fix_nickname=False,
220
199
        tag_nickname=False)
221
200
 
222
 
    options, filenames = parser.parse_args(arguments)
 
201
    options, args = parser.parse_args(arguments)
 
202
    if len(args) != 0:
 
203
        parser.error("Positional arguments are not recognized.")
223
204
    if options.project_name is None:
224
205
        parser.error("A project name must be specified.")
225
206
 
226
 
    if len(filenames) == 0:
227
 
        filenames = ["-"]
228
 
 
229
 
    for filename in filenames:
230
 
        tree = etree.parse(sys.stdin if filename == "-" else filename)
231
 
        massage(
232
 
            root=tree.getroot(),
233
 
            project_name=options.project_name,
234
 
            fix_nickname=options.fix_nickname,
235
 
            tag_nickname=options.tag_nickname)
236
 
        tree.write(
237
 
            (sys.stdout if filename == "-" else filename), encoding='utf-8',
238
 
            pretty_print=True, xml_declaration=True)
 
207
    tree = etree.parse(sys.stdin)
 
208
    massage(
 
209
        root=tree.getroot(),
 
210
        project_name=options.project_name,
 
211
        fix_nickname=options.fix_nickname,
 
212
        tag_nickname=options.tag_nickname)
 
213
    tree.write(
 
214
        sys.stdout, encoding='utf-8',
 
215
        pretty_print=True, xml_declaration=True)
239
216
 
240
217
    return 0
241
218