File: remove.py

package info (click to toggle)
ditrack 0.8-1.1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 516 kB
  • ctags: 645
  • sloc: python: 3,762; makefile: 43
file content (113 lines) | stat: -rw-r--r-- 4,110 bytes parent folder | download | duplicates (3)
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
#
# remove.py - DITrack 'remove' command
#
# Copyright (c) 2007 The DITrack Project, www.ditrack.org.
#
# $Id: remove.py 1909 2007-08-16 23:46:45Z vss $
# $HeadURL: https://svn.xiolabs.com/ditrack/src/tags/0.8/DITrack/Command/remove.py $
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

import string
import sys

# DITrack modules
import DITrack.Command.generic

class Handler(DITrack.Command.generic.Handler):
    canonical_name = "remove"
    description = """Remove local comments.
usage: %s ID...""" % canonical_name

    def run(self, opts, globals):

	self.check_options(opts)

	db = DITrack.Util.common.open_db(globals, opts, "w")

        if len(opts.fixed) < 2:
            raise DITrack.Command.generic.SyntaxError

        issue_comments = {}

        for id in opts.fixed[1:]:

            parsed_id = DITrack.Common.Identifier(id)

            if not parsed_id.has_comment_part():
                DITrack.Util.common.err(
                    "Comment identifier expected: '%s'" % id
                )

            if parsed_id.comment.id.isdigit():
                DITrack.Util.common.err(
                    "Non-local comment identifier: '%s'" % id
                )

            issue_id, comment_id = parsed_id.issue.id, parsed_id.comment.id

            if issue_id not in issue_comments:
                issue_comments[issue_id] = {}

            issue_comments[issue_id][comment_id] = True

        # Sort the issue numbers that were mentioned in arguments
        issues = issue_comments.keys()
        issues.sort()

        for issue_id in issues:

            # This is (was) essentially a set of mentioned comment names.
            comments_to_remove = issue_comments[issue_id].keys()
            comments_to_remove.sort()
            comments_to_remove.reverse()

            issue = db[issue_id]

            # Fetch IDs only (hence x[0])
            existing_comments = [ x[0] for x in issue.comments(firm=False) ]
            existing_comments.sort
            existing_comments.reverse()

            for victim in comments_to_remove:

                if (not existing_comments) or (victim > existing_comments[0]):
                    DITrack.Util.common.err(
                        "No such comment: '%s.%s'" % (issue_id, victim)
                    )

                if victim < existing_comments[0]:
                    DITrack.Util.common.err(
                        "Can't remove '%s.%s': '%s.%s' is in the way"
                        % (issue_id, victim, issue_id, existing_comments[0])
                    )

                existing_comments.pop(0)

            # All correct. Remember the list.
            issue_comments[issue_id] = comments_to_remove

        # Now the removal loop itself.
        for issue_id in issues:
            for comment_id in issue_comments[issue_id]:
                db.remove_comment(issue_id, comment_id)