1. Alexander Solovyov
  2. artemis

Source

artemis / artemis.py

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# Author: Dmitriy Morozov <hg@foxcub.org>, 2007 -- 2009

"""A very simple and lightweight issue tracker for Mercurial."""

import os, random, mailbox, glob, socket, ConfigParser
import mimetypes
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from mercurial import util
from mercurial.i18n import _

# keep it lowercase
state = {'new': 'new', 'fixed': ['fixed', 'resolved']}
state['default'] = state['new']
issues_dir = ".issues"
filter_prefix = ".filter"
date_format = '%a, %d %b %Y %H:%M:%S'
maildir_dirs = ['new','cur','tmp']


def ilist(ui, repo, **opts):
    """List issues associated with the project"""

    # Process options
    show_all = opts['all']
    properties = []
    date_match = opts.get('date') and util.matchdate(opts['date'])

    # Find issues
    issues_path = os.path.join(repo.root, issues_dir)
    if not os.path.exists(issues_path): return

    issues = glob.glob(os.path.join(issues_path, '*'))

    _create_all_missing_dirs(issues_path, issues)

    # Process filter
    if opts['filter']:
        filters = glob.glob(os.path.join(issues_path, filter_prefix + '*'))
        config = ConfigParser.SafeConfigParser()
        config.read(filters)
        if not config.has_section(opts['filter']):
            ui.write('No filter %s defined\n' % opts['filter'])
        else:
            properties += config.items(opts['filter'])

    cmd_properties = _get_properties(opts['property'])
    list_properties = [p[0] for p in cmd_properties if len(p) == 1]
    list_properties_dict = {}
    properties += filter(lambda p: len(p) > 1, cmd_properties)

    for issue in issues:
        mbox = mailbox.Maildir(issue, factory=mailbox.MaildirMessage)
        root = _find_root_key(mbox)
        property_match = True
        for property, value in properties:
            if value:
                property_match = property_match and (mbox[root][property] == value)
            else:
                property_match = property_match and (property not in mbox[root])

        if (not show_all and
            (not properties or not property_match) and
            (properties or mbox[root]['State'].lower() in state['fixed'])):
            continue

        if (opts.get('date') and
            not date_match(util.parsedate(mbox[root]['date'])[0])):
            continue

        if not list_properties:
            ui.write("%s (%3d) [%s]: %s\n" % (
                    issue[len(issues_path)+1:], # +1 for trailing /
                    len(mbox) - 1,              # number of replies (-1 for self)
                    _status_msg(mbox[root]),
                    mbox[root]['Subject']))
        else:
            for lp in list_properties:
                if lp in mbox[root]:
                    list_properties_dict.setdefault(lp, set()).add(mbox[root][lp])

    if list_properties:
        for p, v in list_properties_dict.items():
            ui.write("%s:\n" % p)
            for value in sorted(v):
                ui.write("  %s\n" % value)


def iadd(ui, repo, id = None, comment = 0, **opts):
    """Adds a new issue, or comment to an existing issue ID or its comment COMMENT"""

    comment = int(comment)

    # First, make sure issues have a directory
    issues_path = os.path.join(repo.root, issues_dir)
    if not os.path.exists(issues_path): os.mkdir(issues_path)

    if id:
        issue_fn, issue_id = _find_issue(ui, repo, id)
        if not issue_fn:
            ui.warn('No such issue\n')
            return
        _create_missing_dirs(issues_path, issue_id)

    user = ui.username()

    default_issue_text  =         "From: %s\nDate: %s\n" % (user, util.datestr(format = date_format))
    if not id:
        default_issue_text +=     "State: %s\n" % state['default']
    default_issue_text +=         "Subject: brief description\n\n"
    default_issue_text +=         "Detailed description."

    # Get properties, and figure out if we need an explicit comment
    properties = _get_properties(opts['property'])
    no_comment = id and properties and opts['no_property_comment']
    message = opts['message']

    # Create the text
    if message:
        if not id:
            state_str = 'State: %s\n' % state['default']
        else:
            state_str = ''
        issue = "From: %s\nDate: %s\nSubject: %s\n%s" % \
                (user, util.datestr(format=date_format), message, state_str)
    elif not no_comment:
        issue = ui.edit(default_issue_text, user)

        if issue.strip() == '':
            ui.warn('Empty issue, ignoring\n')
            return
        if issue.strip() == default_issue_text:
            ui.warn('Unchanged issue text, ignoring\n')
            return
    else:
        # Write down a comment about updated properties
        properties_subject = ', '.join(['%s=%s' % (property, value) for (property, value) in properties])

        issue =     "From: %s\nDate: %s\nSubject: changed properties (%s)\n" % \
                     (user, util.datestr(format = date_format), properties_subject)

    # Create the message
    msg = mailbox.MaildirMessage(issue)
    if opts['attach']:
        outer = _attach_files(msg, opts['attach'])
    else:
        outer = msg

    # Pick random filename
    if not id:
        issue_fn = issues_path
        while os.path.exists(issue_fn):
            issue_id = _random_id()
            issue_fn = os.path.join(issues_path, issue_id)
    # else: issue_fn already set

    # Add message to the mailbox
    mbox = mailbox.Maildir(issue_fn, factory=mailbox.MaildirMessage)
    keys = _order_keys_date(mbox)
    mbox.lock()
    if id and comment >= len(mbox):
        ui.warn('No such comment number in mailbox, commenting on the issue itself\n')

    if not id:
        outer.add_header('Message-Id', "<%s-0-artemis@%s>" % (issue_id, socket.gethostname()))
    else:
        root = keys[0]
        outer.add_header('Message-Id', "<%s-%s-artemis@%s>" % (issue_id, _random_id(), socket.gethostname()))
        outer.add_header('References', mbox[(comment < len(mbox) and keys[comment]) or root]['Message-Id'])
        outer.add_header('In-Reply-To', mbox[(comment < len(mbox) and keys[comment]) or root]['Message-Id'])
    repo.add([issue_fn[(len(repo.root)+1):] + '/new/'  + mbox.add(outer)])   # +1 for the trailing /

    # Fix properties in the root message
    if properties:
        root = _find_root_key(mbox)
        msg = mbox[root]
        for property, value in properties:
            if property in msg:
                msg.replace_header(property, value)
            else:
                msg.add_header(property, value)
        mbox[root] = msg

    mbox.close()

    # If adding issue, add the new mailbox to the repository
    if not id:
        ui.status('Added new issue %s\n' % issue_id)
    else:
        _show_mbox(ui, mbox, 0)

def ishow(ui, repo, id, comment = 0, **opts):
    """Shows issue ID, or possibly its comment COMMENT"""

    comment = int(comment)
    issue, id = _find_issue(ui, repo, id)
    if not issue:
        return ui.warn('No such issue\n')

    _create_missing_dirs(os.path.join(repo.root, issues_dir), issue)

    if opts.get('mutt'):
        return util.system('mutt -R -f %s' % issue)

    mbox = mailbox.Maildir(issue, factory=mailbox.MaildirMessage)

    if opts['all']:
        ui.write('='*70 + '\n')
        i = 0
        keys = _order_keys_date(mbox)
        for k in keys:
            _write_message(ui, mbox[k], i, skip = opts['skip'])
            ui.write('-'*70 + '\n')
            i += 1
        return

    _show_mbox(ui, mbox, comment, skip = opts['skip'])

    if opts['extract']:
        attachment_numbers = map(int, opts['extract'])
        keys = _order_keys_date(mbox)
        msg = mbox[keys[comment]]
        counter = 1
        for part in msg.walk():
            ctype = part.get_content_type()
            maintype, subtype = ctype.split('/', 1)
            if maintype == 'multipart' or ctype == 'text/plain': continue
            if counter in attachment_numbers:
                filename = part.get_filename()
                if not filename:
                    ext = mimetypes.guess_extension(part.get_content_type()) or ''
                    filename = 'attachment-%03d%s' % (counter, ext)
                fp = open(filename, 'wb')
                fp.write(part.get_payload(decode = True))
                fp.close()
            counter += 1


def _find_issue(ui, repo, id):
    issues_path = os.path.join(repo.root, issues_dir)
    if not os.path.exists(issues_path): return False

    issues = glob.glob(os.path.join(issues_path, id + '*'))

    if len(issues) == 0:
        return False, 0
    elif len(issues) > 1:
        ui.status("Multiple choices:\n")
        for i in issues: ui.status('  ', i[len(issues_path)+1:], '\n')
        return False, 0

    return issues[0], issues[0][len(issues_path)+1:]

def _get_properties(property_list):
    return [p.split('=') for p in property_list]

def _write_message(ui, message, index = 0, skip = None):
    if index: ui.write("Comment: %d\n" % index)
    if ui.verbose:
        _show_text(ui, message.as_string().strip(), skip)
    else:
        for header in ['From', 'Date', 'Subject', 'State']:
            if header in message:
                ui.write('%s: %s\n' % (header, message[header]))
        counter = 1
        for part in message.walk():
            ctype = part.get_content_type()
            maintype, subtype = ctype.split('/', 1)
            if maintype == 'multipart':
                continue
            if ctype == 'text/plain':
                ui.write('\n')
                _show_text(ui, part.get_payload().strip(), skip)
            else:
                filename = part.get_filename()
                ui.write('\n%d: Attachment [%s, %s]: %s\n' % (
                        counter, ctype, _humanreadable(len(part.get_payload())),
                        filename))
                counter += 1

def _show_text(ui, text, skip = None):
    for line in text.splitlines():
        if not skip or not line.startswith(skip):
            ui.write(line + '\n')
    ui.write('\n')

def _show_mbox(ui, mbox, comment, **opts):
    # Output the issue (or comment)
    if comment >= len(mbox):
        comment = 0
        ui.warn('Comment out of range, showing the issue itself\n')
    keys = _order_keys_date(mbox)
    root = keys[0]
    msg = mbox[keys[comment]]
    ui.write('='*70 + '\n')
    if comment:
        ui.write('Subject: %s\n' % mbox[root]['Subject'])
        ui.write('State: %s\n' % mbox[root]['State'])
        ui.write('-'*70 + '\n')
    _write_message(ui, msg, comment, skip = ('skip' in opts) and opts['skip'])
    ui.write('-'*70 + '\n')

    # Read the mailbox into the messages and children dictionaries
    messages = {}
    children = {}
    i = 0
    for k in keys:
        m = mbox[k]
        messages[m['Message-Id']] = (i,m)
        children.setdefault(m['In-Reply-To'], []).append(m['Message-Id'])
        i += 1
    children[None] = []                # Safeguard against infinte loop on empty Message-Id

    # Iterate over children
    id = msg['Message-Id']
    id_stack = (id in children and map(lambda x: (x, 1), reversed(children[id]))) or []
    if not id_stack: return
    ui.write('Comments:\n')
    while id_stack:
        id,offset = id_stack.pop()
        id_stack += (id in children and map(lambda x: (x, offset+1), reversed(children[id]))) or []
        index, msg = messages[id]
        ui.write('  '*offset + '%d: [%s] %s\n' % (index, util.shortuser(msg['From']), msg['Subject']))
    ui.write('-'*70 + '\n')

def _find_root_key(maildir):
    for k,m in maildir.iteritems():
        if 'in-reply-to' not in m:
            return k

def _order_keys_date(mbox):
    keys = mbox.keys()
    root = _find_root_key(mbox)
    keys.sort(lambda k1,k2: -(k1 == root) or cmp(util.parsedate(mbox[k1]['date']), util.parsedate(mbox[k2]['date'])))
    return keys

def _random_id():
    return "%x" % random.randint(2**63, 2**64-1)

def _create_missing_dirs(issues_path, issue):
    for d in maildir_dirs:
        path = os.path.join(issues_path,issue,d)
        if not os.path.exists(path): os.mkdir(path)

def _create_all_missing_dirs(issues_path, issues):
    for i in issues:
        _create_missing_dirs(issues_path, i)

def _humanreadable(size):
    if size > 1024*1024:
        return '%5.1fM' % (float(size) / (1024*1024))
    elif size > 1024:
        return '%5.1fK' % (float(size) / 1024)
    else:
        return '%dB' % size

def _attach_files(msg, filenames):
    outer = MIMEMultipart()
    for k in msg.keys(): outer[k] = msg[k]
    outer.attach(MIMEText(msg.get_payload()))

    for filename in filenames:
        ctype, encoding = mimetypes.guess_type(filename)
        if ctype is None or encoding is not None:
            # No guess could be made, or the file is encoded (compressed), so
            # use a generic bag-of-bits type.
            ctype = 'application/octet-stream'
        maintype, subtype = ctype.split('/', 1)
        if maintype == 'text':
            fp = open(filename)
            # Note: we should handle calculating the charset
            attachment = MIMEText(fp.read(), _subtype=subtype)
            fp.close()
        elif maintype == 'image':
            fp = open(filename, 'rb')
            attachment = MIMEImage(fp.read(), _subtype=subtype)
            fp.close()
        elif maintype == 'audio':
            fp = open(filename, 'rb')
            attachment = MIMEAudio(fp.read(), _subtype=subtype)
            fp.close()
        else:
            fp = open(filename, 'rb')
            attachment = MIMEBase(maintype, subtype)
            attachment.set_payload(fp.read())
            fp.close()
            # Encode the payload using Base64
            encoders.encode_base64(attachment)
        # Set the filename parameter
        attachment.add_header('Content-Disposition', 'attachment', filename=filename)
        outer.attach(attachment)
    return outer

def _status_msg(msg):
    if msg['State'] == 'resolved':
        return 'resolved=' + msg['resolution']
    else:
        return msg['State']

cmdtable = {
    'ilist':    (ilist,
                 [('a', 'all', False,
                   'list all issues (by default only those with state new)'),
                  ('p', 'property', [],
                   'list issues with specific field values (e.g., -p state=fixed); lists all possible values of a property if no = sign'),
                  ('d', 'date', '', 'restrict to issues matching the date (e.g., -d ">12/28/2007)"'),
                  ('f', 'filter', '', 'restrict to pre-defined filter (in %s/%s*)' % (issues_dir, filter_prefix))],
                 _('hg ilist [OPTIONS]')),
    'iadd':       (iadd,
                 [('a', 'attach', [],
                   'attach file(s) (e.g., -a filename1 -a filename2)'),
                  ('p', 'property', [],
                   'update properties (e.g., -p state=fixed)'),
                  ('n', 'no-property-comment', None,
                   'do not add a comment about changed properties'),
                  ('m', 'message', '',
                   'use <text> as an issue subject')],
                 _('hg iadd [OPTIONS] [ID] [COMMENT]')),
    'ishow':      (ishow,
                 [('a', 'all', None, 'list all comments'),
                  ('s', 'skip', '>', 'skip lines starting with a substring'),
                  ('x', 'extract', [], 'extract attachments (provide attachment number as argument)'),
                  ('', 'mutt', False, 'use mutt to show issue')],
                 _('hg ishow [OPTIONS] ID [COMMENT]')),
}

# vim: expandtab