#!/usr/bin/python
from datetime import datetime
from common import JiraClient
import getpass, subprocess
def formatted_attachment_list(attachments):
counter = 1
for a in attachments:
print "-" * 80
print "%-10s: %d" % ("ID No.", counter)
print "%-10s: %s (%s)" % ('Filename', a.filename, a.mimetype)
print "%-10s: %s" % ('Author', a.author)
print "%-10s: %s" % ('Created', a.created)
print "%-10s: %s bytes" % ('Size', a.filesize)
counter += 1
print "-" * 80
def parse_patch_ids(ids):
# This is a little naive but OK for now
return ids.replace(',', ' ').split()
def apply_patches(attachments, ids, level=0):
for index in ids:
f = attachments[int(index)-1].get_attached_file()
cmd = ['patch', '-p%d' % level]
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(f.read())
(stdout, _) = p.communicate()
if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, cmd)
return [line for line in stdout.split("\n")]
def main(issue, username, passwd, level=0):
client = JiraClient(username, passwd)
attachments = client.get_attachments(issue)
formatted_attachment_list(attachments)
print "Attachment ID(s) to apply? ",
response = raw_input()
output = apply_patches(attachments, parse_patch_ids(response), level)
print '\n'.join(output)
if __name__ == '__main__':
from sys import argv, stderr, exit
from getopt import getopt, GetoptError
try:
(options, arguments) = getopt(argv[1:], 'u:p:h',
['username', 'strip', 'help'])
except GetoptError, error:
print >>stderr, error
exit(1)
username = None
strip = 0
for opt,arg in options:
if opt in ('-u', '--username'):
username = arg
if opt in ('-p', '--strip'):
strip = int(arg)
if opt in ('-h', '--help'):
print "%s [-u username] [-p num] [-h] jira_issue" % argv[0]
exit(0)
if not username:
username = getpass.getuser()
passwd = getpass.getpass()
assert len(arguments)
main(arguments[0], username, passwd, strip)
# vi:ai sw=4 ts=4 tw=0 et