You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by tv...@apache.org on 2013/08/03 00:01:34 UTC
[1/2] git commit: [#6480] Add trac ticket importer plugin
Updated Branches:
refs/heads/tv/6480 [created] c09406972
[#6480] Add trac ticket importer plugin
Signed-off-by: Tim Van Steenburgh <tv...@gmail.com>
Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/5b4ccd4a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/5b4ccd4a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/5b4ccd4a
Branch: refs/heads/tv/6480
Commit: 5b4ccd4a8654ac1851bf949a6f7d04b44b250278
Parents: 93d12e6
Author: Tim Van Steenburgh <tv...@gmail.com>
Authored: Fri Aug 2 17:00:29 2013 +0000
Committer: Tim Van Steenburgh <tv...@gmail.com>
Committed: Fri Aug 2 17:00:29 2013 +0000
----------------------------------------------------------------------
Allura/allura/scripts/trac_export.py | 280 +++++++++++++++++++
ForgeImporters/forgeimporters/trac/__init__.py | 17 ++
.../trac/templates/tickets/index.html | 42 +++
ForgeImporters/forgeimporters/trac/tickets.py | 107 +++++++
.../forgetracker/scripts/import_tracker.py | 18 +-
scripts/trac_export.py | 257 +----------------
6 files changed, 458 insertions(+), 263 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b4ccd4a/Allura/allura/scripts/trac_export.py
----------------------------------------------------------------------
diff --git a/Allura/allura/scripts/trac_export.py b/Allura/allura/scripts/trac_export.py
new file mode 100644
index 0000000..aeb14ea
--- /dev/null
+++ b/Allura/allura/scripts/trac_export.py
@@ -0,0 +1,280 @@
+#!/usr/bin/env python
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+import sys
+import csv
+import urlparse
+import urllib2
+import json
+import time
+import re
+from optparse import OptionParser
+from itertools import islice
+from datetime import datetime
+
+import feedparser
+from html2text import html2text
+from BeautifulSoup import BeautifulSoup, NavigableString
+import dateutil.parser
+import pytz
+
+
+def parse_options():
+ optparser = OptionParser(usage=''' %prog <Trac URL>
+
+Export ticket data from a Trac instance''')
+ optparser.add_option('-o', '--out-file', dest='out_filename', help='Write to file (default stdout)')
+ optparser.add_option('--no-attachments', dest='do_attachments', action='store_false', default=True, help='Export attachment info')
+ optparser.add_option('--only-tickets', dest='only_tickets', action='store_true', help='Export only ticket list')
+ optparser.add_option('--start', dest='start_id', type='int', default=1, help='Start with given ticket numer (or next accessible)')
+ optparser.add_option('--limit', dest='limit', type='int', default=None, help='Limit number of tickets')
+ optparser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose operation')
+ options, args = optparser.parse_args()
+ if len(args) != 1:
+ optparser.error("Wrong number of arguments.")
+ return options, args
+
+
+class TracExport(object):
+
+ PAGE_SIZE = 100
+ TICKET_URL = 'ticket/%d'
+ QUERY_MAX_ID_URL = 'query?col=id&order=id&desc=1&max=2'
+ QUERY_BY_PAGE_URL = 'query?col=id&col=time&col=changetime&order=id&max=' + str(PAGE_SIZE)+ '&page=%d'
+ ATTACHMENT_LIST_URL = 'attachment/ticket/%d/'
+ ATTACHMENT_URL = 'raw-attachment/ticket/%d/%s'
+
+ FIELD_MAP = {
+ 'reporter': 'submitter',
+ 'owner': 'assigned_to',
+ }
+
+ def __init__(self, base_url, start_id=1):
+ """start_id - start with at least that ticket number (actual returned
+ ticket may have higher id if we don't have access to exact
+ one).
+ """
+ self.base_url = base_url.rstrip('/') + '/'
+ # Contains additional info for a ticket which cannot
+ # be get with single-ticket export (create/mod times is
+ # and example).
+ self.ticket_map = {}
+ self.start_id = start_id
+ self.page = (start_id - 1) / self.PAGE_SIZE + 1
+ self.ticket_queue = self.next_ticket_ids()
+
+ def remap_fields(self, dict):
+ "Remap fields to adhere to standard taxonomy."
+ out = {}
+ for k, v in dict.iteritems():
+ out[self.FIELD_MAP.get(k, k)] = v
+
+ out['id'] = int(out['id'])
+ if 'private' in out:
+ out['private'] = bool(int(out['private']))
+ return out
+
+ def full_url(self, suburl, type=None):
+ url = urlparse.urljoin(self.base_url, suburl)
+ if type is None:
+ return url
+ glue = '&' if '?' in suburl else '?'
+ return url + glue + 'format=' + type
+
+ @staticmethod
+ def log_url(url):
+ if options.verbose:
+ print >>sys.stderr, url
+
+ @classmethod
+ def trac2z_date(cls, s):
+ d = dateutil.parser.parse(s)
+ d = d.astimezone(pytz.UTC)
+ return d.strftime("%Y-%m-%dT%H:%M:%SZ")
+
+ @staticmethod
+ def match_pattern(regexp, string):
+ m = re.match(regexp, string)
+ assert m
+ return m.group(1)
+
+ def csvopen(self, url):
+ self.log_url(url)
+ f = urllib2.urlopen(url)
+ # Trac doesn't throw 403 error, just shows normal 200 HTML page
+ # telling that access denied. So, we'll emulate 403 ourselves.
+ # TODO: currently, any non-csv result treated as 403.
+ if not f.info()['Content-Type'].startswith('text/csv'):
+ raise urllib2.HTTPError(url, 403, 'Forbidden - emulated', f.info(), f)
+ return f
+
+ def parse_ticket_body(self, id):
+ # Use CSV export to get ticket fields
+ url = self.full_url(self.TICKET_URL % id, 'csv')
+ f = self.csvopen(url)
+ reader = csv.DictReader(f)
+ ticket_fields = reader.next()
+ ticket_fields['class'] = 'ARTIFACT'
+ return self.remap_fields(ticket_fields)
+
+ def parse_ticket_comments(self, id):
+ # Use RSS export to get ticket comments
+ url = self.full_url(self.TICKET_URL % id, 'rss')
+ self.log_url(url)
+ d = feedparser.parse(url)
+ res = []
+ for comment in d['entries']:
+ c = {}
+ c['submitter'] = comment.author
+ c['date'] = comment.updated_parsed
+ c['comment'] = html2text(comment.summary)
+ c['class'] = 'COMMENT'
+ res.append(c)
+ return res
+
+ def parse_ticket_attachments(self, id):
+ SIZE_PATTERN = r'(\d+) bytes'
+ TIMESTAMP_PATTERN = r'(.+) in Timeline'
+ # Scrape HTML to get ticket attachments
+ url = self.full_url(self.ATTACHMENT_LIST_URL % id)
+ self.log_url(url)
+ f = urllib2.urlopen(url)
+ soup = BeautifulSoup(f)
+ attach = soup.find('div', id='attachments')
+ list = []
+ while attach:
+ attach = attach.findNext('dt')
+ if not attach:
+ break
+ d = {}
+ d['filename'] = attach.a['href'].rsplit('/', 1)[1]
+ d['url'] = self.full_url(self.ATTACHMENT_URL % (id, d['filename']))
+ size_s = attach.span['title']
+ d['size'] = int(self.match_pattern(SIZE_PATTERN, size_s))
+ timestamp_s = attach.find('a', {'class': 'timeline'})['title']
+ d['date'] = self.trac2z_date(self.match_pattern(TIMESTAMP_PATTERN, timestamp_s))
+ d['by'] = attach.find(text=re.compile('added by')).nextSibling.renderContents()
+ d['description'] = ''
+ # Skip whitespace
+ while attach.nextSibling and type(attach.nextSibling) is NavigableString:
+ attach = attach.nextSibling
+ # if there's a description, there will be a <dd> element, other immediately next <dt>
+ if attach.nextSibling and attach.nextSibling.name == 'dd':
+ desc_el = attach.nextSibling
+ if desc_el:
+ # TODO: Convert to Allura link syntax as needed
+ d['description'] = ''.join(desc_el.findAll(text=True)).strip()
+ list.append(d)
+ return list
+
+ def get_max_ticket_id(self):
+ url = self.full_url(self.QUERY_MAX_ID_URL, 'csv')
+ f = self.csvopen(url)
+ reader = csv.DictReader(f)
+ fields = reader.next()
+ print fields
+ return int(fields['id'])
+
+ def get_ticket(self, id, extra={}):
+ '''Get ticket with given id
+ extra: extra fields to add to ticket (parsed elsewhere)
+ '''
+ t = self.parse_ticket_body(id)
+ t['comments'] = self.parse_ticket_comments(id)
+ if options.do_attachments:
+ atts = self.parse_ticket_attachments(id)
+ if atts:
+ t['attachments'] = atts
+ t.update(extra)
+ return t
+
+ def next_ticket_ids(self):
+ 'Go thru ticket list and collect available ticket ids.'
+ # We could just do CSV export, which by default dumps entire list
+ # Alas, for many busy servers with long ticket list, it will just
+ # time out. So, let's paginate it instead.
+ res = []
+
+ url = self.full_url(self.QUERY_BY_PAGE_URL % self.page, 'csv')
+ try:
+ f = self.csvopen(url)
+ except urllib2.HTTPError, e:
+ if 'emulated' in e.msg:
+ body = e.fp.read()
+ if 'beyond the number of pages in the query' in body or 'Log in with a SourceForge account' in body:
+ raise StopIteration
+ raise
+ reader = csv.reader(f)
+ cols = reader.next()
+ for r in reader:
+ if r and r[0].isdigit():
+ id = int(r[0])
+ extra = {'date': self.trac2z_date(r[1]), 'date_updated': self.trac2z_date(r[2])}
+ res.append((id, extra))
+ self.page += 1
+
+ return res
+
+ def __iter__(self):
+ return self
+
+ def next(self):
+ while True:
+ # queue empty, try to fetch more
+ if len(self.ticket_queue) == 0:
+ self.ticket_queue = self.next_ticket_ids()
+ # there aren't any more, we're really done
+ if len(self.ticket_queue) == 0:
+ raise StopIteration
+ id, extra = self.ticket_queue.pop(0)
+ if id >= self.start_id:
+ break
+ return self.get_ticket(id, extra)
+
+
+class DateJSONEncoder(json.JSONEncoder):
+ def default(self, obj):
+ if isinstance(obj, time.struct_time):
+ return time.strftime('%Y-%m-%dT%H:%M:%SZ', obj)
+ return json.JSONEncoder.default(self, obj)
+
+
+def main():
+ options, args = parse_options()
+ ex = TracExport(args[0], start_id=options.start_id)
+ # Implement iterator sequence limiting using islice()
+ doc = [t for t in islice(ex, options.limit)]
+
+ if not options.only_tickets:
+ doc = {
+ 'class': 'PROJECT',
+ 'trackers': {'default': {'artifacts': doc}}
+ }
+
+ out_file = sys.stdout
+ if options.out_filename:
+ out_file = open(options.out_filename, 'w')
+ out_file.write(json.dumps(doc, cls=DateJSONEncoder, indent=2, sort_keys=True))
+ # It's bad habit not to terminate lines
+ out_file.write('\n')
+
+
+if __name__ == '__main__':
+ main()
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b4ccd4a/ForgeImporters/forgeimporters/trac/__init__.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/__init__.py b/ForgeImporters/forgeimporters/trac/__init__.py
new file mode 100644
index 0000000..77505f1
--- /dev/null
+++ b/ForgeImporters/forgeimporters/trac/__init__.py
@@ -0,0 +1,17 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b4ccd4a/ForgeImporters/forgeimporters/trac/templates/tickets/index.html
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/templates/tickets/index.html b/ForgeImporters/forgeimporters/trac/templates/tickets/index.html
new file mode 100644
index 0000000..eaf9aac
--- /dev/null
+++ b/ForgeImporters/forgeimporters/trac/templates/tickets/index.html
@@ -0,0 +1,42 @@
+{#-
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-#}
+{% extends g.theme.master %}
+
+{% block title %}
+{{c.project.name}} / Import Trac Tickets
+{% endblock %}
+
+{% block header %}
+Import tickets from Trac
+{% endblock %}
+
+{% block content %}
+<form action="create" method="post" class="pad">
+ <label for="trac_url">URL of the Trac instance</label>
+ <input name="trac_url" />
+
+ <label for="mount_label">Label</label>
+ <input name="mount_label" value="Source" />
+
+ <label for="mount_point">Mount Point</label>
+ <input name="mount_point" value="source" />
+
+ <input type="submit" />
+</form>
+{% endblock %}
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b4ccd4a/ForgeImporters/forgeimporters/trac/tickets.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/tickets.py b/ForgeImporters/forgeimporters/trac/tickets.py
new file mode 100644
index 0000000..cc31741
--- /dev/null
+++ b/ForgeImporters/forgeimporters/trac/tickets.py
@@ -0,0 +1,107 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from datetime import (
+ datetime,
+ timedelta,
+ )
+import json
+
+import formencode as fe
+from formencode import validators as fev
+
+from pylons import tmpl_context as c
+from pylons import app_globals as g
+from tg import (
+ config,
+ expose,
+ redirect,
+ validate,
+ )
+from tg.decorators import (
+ with_trailing_slash,
+ without_trailing_slash,
+ )
+
+from allura.controllers import BaseController
+from allura.lib.decorators import require_post
+from allura.lib.import_api import AlluraImportApiClient
+from allura.model import ApiTicket
+from allura.scripts.trac_export import (
+ TracExport,
+ DateJSONEncoder,
+ )
+
+from forgeimporters.base import ToolImporter
+from forgetracker.tracker_main import ForgeTrackerApp
+from forgetracker.script.import_tracker import import_tracker
+
+
+class TracTicketImportSchema(fe.Schema):
+ trac_url = fev.URL(not_empty=True)
+ mount_point = fev.UnicodeString()
+ mount_label = fev.UnicodeString()
+
+
+class TracTicketImportController(BaseController):
+ @with_trailing_slash
+ @expose('jinja:forgeimporters.trac:templates/tickets/index.html')
+ def index(self, **kw):
+ return {}
+
+ @without_trailing_slash
+ @expose()
+ @require_post()
+ @validate(TracTicketImportSchema(), error_handler=index)
+ def create(self, trac_url, mount_point, mount_label, **kw):
+ app = TracTicketImporter.import_tool(c.project,
+ mount_point=mount_point,
+ mount_label=mount_label,
+ trac_url=trac_url,
+ user=c.user)
+ redirect(app.url())
+
+
+class TracTicketImporter(ToolImporter):
+ target_app = ForgeTrackerApp
+ source = 'Trac'
+ controller = TracTicketImportController
+ tool_label = 'Trac Ticket Importer'
+ tool_description = 'Import your tickets from Trac'
+
+ def import_tool(self, project=None, mount_point=None, mount_label=None,
+ trac_url=None, user=None):
+ """ Import Trac tickets into a new Allura Tracker tool.
+
+ """
+ mount_point = mount_point or 'tickets'
+ app = project.install_app(
+ 'Tickets',
+ mount_point=mount_point,
+ mount_label=mount_label or 'Tickets',
+ )
+ export = TracExport(trac_url)
+ export_string = json.dumps(export, cls=DateJSONEncoder)
+ api_ticket = ApiTicket(user_id=user._id,
+ capabilities={"import": ["Projects", project.shortname]},
+ expires=datetime.utcnow() + timedelta(minutes=60))
+ cli = AlluraImportApiClient(config['base_url'], api_ticket.api_key,
+ api_ticket.secret_key, False)
+ import_tracker(cli, project.shortname, mount_point, {},
+ export_string, validate=False)
+ g.post_event('project_updated')
+ return app
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b4ccd4a/ForgeTracker/forgetracker/scripts/import_tracker.py
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/scripts/import_tracker.py b/ForgeTracker/forgetracker/scripts/import_tracker.py
index 506e771..32b4d1c 100644
--- a/ForgeTracker/forgetracker/scripts/import_tracker.py
+++ b/ForgeTracker/forgetracker/scripts/import_tracker.py
@@ -25,7 +25,8 @@ from allura.lib.import_api import AlluraImportApiClient
log = logging.getLogger(__name__)
-def import_tracker(cli, project, tool, import_options, options, doc_txt, validate=True, verbose=False):
+def import_tracker(cli, project, tool, import_options, doc_txt,
+ validate=True, verbose=False, cont=False):
url = '/rest/p/' + project + '/' + tool
if validate:
url += '/validate_import'
@@ -33,8 +34,8 @@ def import_tracker(cli, project, tool, import_options, options, doc_txt, validat
url += '/perform_import'
existing_map = {}
- if options.cont:
- existing_tickets = cli.call('/rest/p/' + options.project + '/' + options.tracker + '/')['tickets']
+ if cont:
+ existing_tickets = cli.call('/rest/p/' + project + '/' + tool + '/')['tickets']
for t in existing_tickets:
existing_map[t['ticket_num']] = t['summary']
@@ -46,12 +47,12 @@ def import_tracker(cli, project, tool, import_options, options, doc_txt, validat
else:
tickets_in = doc
- if options.verbose:
+ if verbose:
print "Processing %d tickets" % len(tickets_in)
for cnt, ticket_in in enumerate(tickets_in):
if ticket_in['id'] in existing_map:
- if options.verbose:
+ if verbose:
print 'Ticket id %d already exists, skipping' % ticket_in['id']
continue
doc_import={}
@@ -60,7 +61,7 @@ def import_tracker(cli, project, tool, import_options, options, doc_txt, validat
doc_import['trackers']['default']['artifacts'] = [ticket_in]
res = cli.call(url, doc=json.dumps(doc_import), options=json.dumps(import_options))
assert res['status'] and not res['errors']
- if options.validate:
+ if validate:
if res['warnings']:
print "Ticket id %s warnings: %s" % (ticket_in['id'], res['warnings'])
else:
@@ -93,9 +94,10 @@ class ImportTracker(ScriptTask):
import_options['user_map'] = user_map
cli = AlluraImportApiClient(options.base_url, options.api_key, options.secret_key, options.verbose)
doc_txt = open(options.file_data).read()
- import_tracker(cli, options.project, options.tracker, import_options, options, doc_txt,
+ import_tracker(cli, options.project, options.tracker, import_options, doc_txt,
validate=options.validate,
- verbose=options.verbose)
+ verbose=options.verbose,
+ cont=options.cont)
@classmethod
def parser(cls):
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b4ccd4a/scripts/trac_export.py
----------------------------------------------------------------------
diff --git a/scripts/trac_export.py b/scripts/trac_export.py
index 002a1e8..ac90b17 100755
--- a/scripts/trac_export.py
+++ b/scripts/trac_export.py
@@ -17,259 +17,6 @@
# specific language governing permissions and limitations
# under the License.
-
-import sys
-import csv
-import urlparse
-import urllib2
-import json
-import time
-import re
-from optparse import OptionParser
-from itertools import islice
-from datetime import datetime
-
-import feedparser
-from html2text import html2text
-from BeautifulSoup import BeautifulSoup, NavigableString
-import dateutil.parser
-import pytz
-
-
-def parse_options():
- optparser = OptionParser(usage=''' %prog <Trac URL>
-
-Export ticket data from a Trac instance''')
- optparser.add_option('-o', '--out-file', dest='out_filename', help='Write to file (default stdout)')
- optparser.add_option('--no-attachments', dest='do_attachments', action='store_false', default=True, help='Export attachment info')
- optparser.add_option('--only-tickets', dest='only_tickets', action='store_true', help='Export only ticket list')
- optparser.add_option('--start', dest='start_id', type='int', default=1, help='Start with given ticket numer (or next accessible)')
- optparser.add_option('--limit', dest='limit', type='int', default=None, help='Limit number of tickets')
- optparser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose operation')
- options, args = optparser.parse_args()
- if len(args) != 1:
- optparser.error("Wrong number of arguments.")
- return options, args
-
-
-class TracExport(object):
-
- PAGE_SIZE = 100
- TICKET_URL = 'ticket/%d'
- QUERY_MAX_ID_URL = 'query?col=id&order=id&desc=1&max=2'
- QUERY_BY_PAGE_URL = 'query?col=id&col=time&col=changetime&order=id&max=' + str(PAGE_SIZE)+ '&page=%d'
- ATTACHMENT_LIST_URL = 'attachment/ticket/%d/'
- ATTACHMENT_URL = 'raw-attachment/ticket/%d/%s'
-
- FIELD_MAP = {
- 'reporter': 'submitter',
- 'owner': 'assigned_to',
- }
-
- def __init__(self, base_url, start_id=1):
- """start_id - start with at least that ticket number (actual returned
- ticket may have higher id if we don't have access to exact
- one).
- """
- self.base_url = base_url.rstrip('/') + '/'
- # Contains additional info for a ticket which cannot
- # be get with single-ticket export (create/mod times is
- # and example).
- self.ticket_map = {}
- self.start_id = start_id
- self.page = (start_id - 1) / self.PAGE_SIZE + 1
- self.ticket_queue = self.next_ticket_ids()
-
- def remap_fields(self, dict):
- "Remap fields to adhere to standard taxonomy."
- out = {}
- for k, v in dict.iteritems():
- out[self.FIELD_MAP.get(k, k)] = v
-
- out['id'] = int(out['id'])
- if 'private' in out:
- out['private'] = bool(int(out['private']))
- return out
-
- def full_url(self, suburl, type=None):
- url = urlparse.urljoin(self.base_url, suburl)
- if type is None:
- return url
- glue = '&' if '?' in suburl else '?'
- return url + glue + 'format=' + type
-
- @staticmethod
- def log_url(url):
- if options.verbose:
- print >>sys.stderr, url
-
- @classmethod
- def trac2z_date(cls, s):
- d = dateutil.parser.parse(s)
- d = d.astimezone(pytz.UTC)
- return d.strftime("%Y-%m-%dT%H:%M:%SZ")
-
- @staticmethod
- def match_pattern(regexp, string):
- m = re.match(regexp, string)
- assert m
- return m.group(1)
-
- def csvopen(self, url):
- self.log_url(url)
- f = urllib2.urlopen(url)
- # Trac doesn't throw 403 error, just shows normal 200 HTML page
- # telling that access denied. So, we'll emulate 403 ourselves.
- # TODO: currently, any non-csv result treated as 403.
- if not f.info()['Content-Type'].startswith('text/csv'):
- raise urllib2.HTTPError(url, 403, 'Forbidden - emulated', f.info(), f)
- return f
-
- def parse_ticket_body(self, id):
- # Use CSV export to get ticket fields
- url = self.full_url(self.TICKET_URL % id, 'csv')
- f = self.csvopen(url)
- reader = csv.DictReader(f)
- ticket_fields = reader.next()
- ticket_fields['class'] = 'ARTIFACT'
- return self.remap_fields(ticket_fields)
-
- def parse_ticket_comments(self, id):
- # Use RSS export to get ticket comments
- url = self.full_url(self.TICKET_URL % id, 'rss')
- self.log_url(url)
- d = feedparser.parse(url)
- res = []
- for comment in d['entries']:
- c = {}
- c['submitter'] = comment.author
- c['date'] = comment.updated_parsed
- c['comment'] = html2text(comment.summary)
- c['class'] = 'COMMENT'
- res.append(c)
- return res
-
- def parse_ticket_attachments(self, id):
- SIZE_PATTERN = r'(\d+) bytes'
- TIMESTAMP_PATTERN = r'(.+) in Timeline'
- # Scrape HTML to get ticket attachments
- url = self.full_url(self.ATTACHMENT_LIST_URL % id)
- self.log_url(url)
- f = urllib2.urlopen(url)
- soup = BeautifulSoup(f)
- attach = soup.find('div', id='attachments')
- list = []
- while attach:
- attach = attach.findNext('dt')
- if not attach:
- break
- d = {}
- d['filename'] = attach.a['href'].rsplit('/', 1)[1]
- d['url'] = self.full_url(self.ATTACHMENT_URL % (id, d['filename']))
- size_s = attach.span['title']
- d['size'] = int(self.match_pattern(SIZE_PATTERN, size_s))
- timestamp_s = attach.find('a', {'class': 'timeline'})['title']
- d['date'] = self.trac2z_date(self.match_pattern(TIMESTAMP_PATTERN, timestamp_s))
- d['by'] = attach.find(text=re.compile('added by')).nextSibling.renderContents()
- d['description'] = ''
- # Skip whitespace
- while attach.nextSibling and type(attach.nextSibling) is NavigableString:
- attach = attach.nextSibling
- # if there's a description, there will be a <dd> element, other immediately next <dt>
- if attach.nextSibling and attach.nextSibling.name == 'dd':
- desc_el = attach.nextSibling
- if desc_el:
- # TODO: Convert to Allura link syntax as needed
- d['description'] = ''.join(desc_el.findAll(text=True)).strip()
- list.append(d)
- return list
-
- def get_max_ticket_id(self):
- url = self.full_url(self.QUERY_MAX_ID_URL, 'csv')
- f = self.csvopen(url)
- reader = csv.DictReader(f)
- fields = reader.next()
- print fields
- return int(fields['id'])
-
- def get_ticket(self, id, extra={}):
- '''Get ticket with given id
- extra: extra fields to add to ticket (parsed elsewhere)
- '''
- t = self.parse_ticket_body(id)
- t['comments'] = self.parse_ticket_comments(id)
- if options.do_attachments:
- atts = self.parse_ticket_attachments(id)
- if atts:
- t['attachments'] = atts
- t.update(extra)
- return t
-
- def next_ticket_ids(self):
- 'Go thru ticket list and collect available ticket ids.'
- # We could just do CSV export, which by default dumps entire list
- # Alas, for many busy servers with long ticket list, it will just
- # time out. So, let's paginate it instead.
- res = []
-
- url = self.full_url(self.QUERY_BY_PAGE_URL % self.page, 'csv')
- try:
- f = self.csvopen(url)
- except urllib2.HTTPError, e:
- if 'emulated' in e.msg:
- body = e.fp.read()
- if 'beyond the number of pages in the query' in body or 'Log in with a SourceForge account' in body:
- raise StopIteration
- raise
- reader = csv.reader(f)
- cols = reader.next()
- for r in reader:
- if r and r[0].isdigit():
- id = int(r[0])
- extra = {'date': self.trac2z_date(r[1]), 'date_updated': self.trac2z_date(r[2])}
- res.append((id, extra))
- self.page += 1
-
- return res
-
- def __iter__(self):
- return self
-
- def next(self):
- while True:
- # queue empty, try to fetch more
- if len(self.ticket_queue) == 0:
- self.ticket_queue = self.next_ticket_ids()
- # there aren't any more, we're really done
- if len(self.ticket_queue) == 0:
- raise StopIteration
- id, extra = self.ticket_queue.pop(0)
- if id >= self.start_id:
- break
- return self.get_ticket(id, extra)
-
-
-class DateJSONEncoder(json.JSONEncoder):
- def default(self, obj):
- if isinstance(obj, time.struct_time):
- return time.strftime('%Y-%m-%dT%H:%M:%SZ', obj)
- return json.JSONEncoder.default(self, obj)
-
if __name__ == '__main__':
- options, args = parse_options()
- ex = TracExport(args[0], start_id=options.start_id)
- # Implement iterator sequence limiting using islice()
- doc = [t for t in islice(ex, options.limit)]
-
- if not options.only_tickets:
- doc = {
- 'class': 'PROJECT',
- 'trackers': {'default': {'artifacts': doc}}
- }
-
- out_file = sys.stdout
- if options.out_filename:
- out_file = open(options.out_filename, 'w')
- out_file.write(json.dumps(doc, cls=DateJSONEncoder, indent=2, sort_keys=True))
- # It's bad habit not to terminate lines
- out_file.write('\n')
+ from allura.scripts.trac_export import main
+ main()
[2/2] git commit: [#6480] Add trac project and wiki importers;
refactor bases
Posted by tv...@apache.org.
[#6480] Add trac project and wiki importers; refactor bases
Signed-off-by: Tim Van Steenburgh <tv...@gmail.com>
Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/c0940697
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/c0940697
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/c0940697
Branch: refs/heads/tv/6480
Commit: c094069721c10506bbbec789fd41f7524afbef0a
Parents: 5b4ccd4
Author: Tim Van Steenburgh <tv...@gmail.com>
Authored: Fri Aug 2 22:01:08 2013 +0000
Committer: Tim Van Steenburgh <tv...@gmail.com>
Committed: Fri Aug 2 22:01:08 2013 +0000
----------------------------------------------------------------------
ForgeImporters/forgeimporters/base.py | 96 +++++++++++++++-
ForgeImporters/forgeimporters/google/project.py | 66 +++--------
ForgeImporters/forgeimporters/google/tasks.py | 6 -
.../google/templates/project.html | 62 -----------
.../forgeimporters/templates/project_base.html | 89 ++++++++++++++-
ForgeImporters/forgeimporters/trac/project.py | 63 +++++++++++
.../forgeimporters/trac/templates/project.html | 32 ++++++
.../trac/templates/wiki/index.html | 42 +++++++
ForgeImporters/forgeimporters/trac/tickets.py | 2 +-
ForgeImporters/forgeimporters/trac/wiki.py | 110 +++++++++++++++++++
ForgeImporters/setup.py | 3 +
11 files changed, 445 insertions(+), 126 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/base.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/base.py b/ForgeImporters/forgeimporters/base.py
index 034a580..749ff68 100644
--- a/ForgeImporters/forgeimporters/base.py
+++ b/ForgeImporters/forgeimporters/base.py
@@ -15,16 +15,46 @@
# specific language governing permissions and limitations
# under the License.
+import logging
+
from pkg_resources import iter_entry_points
-from tg import expose
+from tg import expose, validate, flash, redirect, config
+from tg.decorators import with_trailing_slash
+from pylons import tmpl_context as c
+from formencode import validators as fev, schema
+
+from allura.lib.decorators import require_post
+from allura.lib.decorators import task
+from allura.lib.security import require_access
+from allura.lib.widgets.forms import NeighborhoodProjectShortNameValidator
+from allura.lib import exceptions
+
from paste.deploy.converters import aslist
-from formencode import validators as fev
from ming.utils import LazyProperty
from allura.controllers import BaseController
+log = logging.getLogger(__name__)
+
+
+class ProjectImportForm(schema.Schema):
+ def __init__(self, source):
+ super(ProjectImportForm, self).__init__()
+ self.add_field('tools', ToolsValidator(source))
+
+ neighborhood = fev.PlainText(not_empty=True)
+ project_name = fev.UnicodeString(not_empty=True, max=40)
+ project_shortname = NeighborhoodProjectShortNameValidator()
+
+
+@task
+def import_tool(importer_name, mount_point=None, mount_label=None, **kw):
+ importer = ToolImporter.by_name(importer_name)
+ importer.import_tool(c.project, mount_point, mount_label, **kw)
+
+
class ProjectImporter(BaseController):
"""
Base class for project importers.
@@ -33,6 +63,14 @@ class ProjectImporter(BaseController):
:meth:`process()` views described below.
"""
source = None
+ process_validator = None
+ index_template = None
+
+ def __init__(self, neighborhood, *a, **kw):
+ self.neighborhood = neighborhood
+
+ def _check_security(self):
+ require_access(self.neighborhood, 'register')
@LazyProperty
def tool_importers(self):
@@ -47,6 +85,8 @@ class ProjectImporter(BaseController):
tools[ep.name] = epv()
return tools
+ @with_trailing_slash
+ @expose()
def index(self, **kw):
"""
Override and expose this view to present the project import form.
@@ -58,9 +98,12 @@ class ProjectImporter(BaseController):
This will list the available tool importers. Other project fields
(e.g., project_name) should go in the project_fields block.
"""
- raise NotImplemented
+ return {'importer': self, 'tg_template': self.index_template}
- def process(self, tools=None, **kw):
+ @require_post()
+ @expose()
+ @validate(process_validator, error_handler=index)
+ def process(self, **kw):
"""
Override and expose this to handle a project import.
@@ -68,7 +111,50 @@ class ProjectImporter(BaseController):
tools installed and redirect to the new project, presumably with a
message indicating that some data will not be available immediately.
"""
- raise NotImplemented
+ try:
+ c.project = self.neighborhood.register_project(kw['project_shortname'],
+ project_name=kw['project_name'])
+ except exceptions.ProjectOverlimitError:
+ flash("You have exceeded the maximum number of projects you are allowed to create", 'error')
+ redirect('.')
+ except exceptions.ProjectRatelimitError:
+ flash("Project creation rate limit exceeded. Please try again later.", 'error')
+ redirect('.')
+ except Exception:
+ log.error('error registering project: %s', kw['project_shortname'], exc_info=True)
+ flash('Internal Error. Please try again later.', 'error')
+ redirect('.')
+
+ self.after_project_create(c.project, **kw)
+ for importer_name in kw['tools']:
+ import_tool.post(importer_name, **kw)
+
+ flash('Welcome to the %s Project System! '
+ 'Your project data will be imported and should show up here shortly.' % config['site_name'])
+ redirect(c.project.script_name + 'admin/overview')
+
+ @expose('json:')
+ @validate(process_validator)
+ def check_names(self, **kw):
+ """
+ Ajax form validation.
+
+ """
+ return c.form_errors
+
+ def after_project_create(self, project, **kw):
+ """
+ Called after project is created.
+
+ Useful for doing extra processing on the project before individual
+ tool imports happen.
+
+ :param project: The newly created project.
+ :param \*\*kw: The keyword arguments that were posted to the controller
+ method that created the project.
+
+ """
+ pass
class ToolImporter(object):
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/google/project.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/project.py b/ForgeImporters/forgeimporters/google/project.py
index d579606..dda41ea 100644
--- a/ForgeImporters/forgeimporters/google/project.py
+++ b/ForgeImporters/forgeimporters/google/project.py
@@ -17,16 +17,12 @@
import logging
-from tg import expose, validate, flash, redirect, config
+from formencode import validators as fev
+
+from tg import expose, validate
from tg.decorators import with_trailing_slash
-from pylons import tmpl_context as c
-from formencode import validators as fev, schema
from allura.lib.decorators import require_post
-from allura.lib.widgets.forms import NeighborhoodProjectShortNameValidator
-from allura.lib.security import require_access
-from allura.lib import helpers as h
-from allura.lib import exceptions
from .. import base
from . import tasks
@@ -35,15 +31,12 @@ from . import tasks
log = logging.getLogger(__name__)
-class GoogleCodeProjectForm(schema.Schema):
- neighborhood = fev.PlainText(not_empty=True)
+class GoogleCodeProjectForm(base.ProjectImportForm):
project_name = fev.Regex(r'^[a-z0-9][a-z0-9-]{,61}$',
not_empty=True,
messages={
'invalid': 'Please use only letters, numbers, and dashes.',
})
- project_shortname = NeighborhoodProjectShortNameValidator()
- tools = base.ToolsValidator('Google Code')
class GoogleCodeProjectImporter(base.ProjectImporter):
@@ -55,50 +48,25 @@ class GoogleCodeProjectImporter(base.ProjectImporter):
import.
"""
source = 'Google Code'
+ process_validator = GoogleCodeProjectForm(source)
+ index_template = 'jinja:forgeimporters.google:templates/project.html'
- def __init__(self, neighborhood, *a, **kw):
- super(GoogleCodeProjectImporter, self).__init__(*a, **kw)
- self.neighborhood = neighborhood
-
- def _check_security(self):
- require_access(self.neighborhood, 'register')
+ def after_project_create(self, project, **kw):
+ project.set_tool_data('google-code', project_name=project.name)
+ tasks.import_project_info.post()
@with_trailing_slash
- @expose('jinja:forgeimporters.google:templates/project.html')
+ @expose(index_template)
def index(self, **kw):
- return {'importer': self}
+ return super(self.__class__, self).index(**kw)
@require_post()
@expose()
- @validate(GoogleCodeProjectForm(), error_handler=index)
- def process(self, project_name=None, project_shortname=None, tools=None, **kw):
- project_name = h.really_unicode(project_name).encode('utf-8')
- project_shortname = h.really_unicode(project_shortname).encode('utf-8').lower()
-
- try:
- c.project = self.neighborhood.register_project(project_shortname,
- project_name=project_name)
- except exceptions.ProjectOverlimitError:
- flash("You have exceeded the maximum number of projects you are allowed to create", 'error')
- redirect('.')
- except exceptions.ProjectRatelimitError:
- flash("Project creation rate limit exceeded. Please try again later.", 'error')
- redirect('.')
- except Exception as e:
- log.error('error registering project: %s', project_shortname, exc_info=True)
- flash('Internal Error. Please try again later.', 'error')
- redirect('.')
-
- c.project.set_tool_data('google-code', project_name=project_name)
- tasks.import_project_info.post()
- for importer_name in tools:
- tasks.import_tool.post(importer_name)
-
- flash('Welcome to the %s Project System! '
- 'Your project data will be imported and should show up here shortly.' % config['site_name'])
- redirect(c.project.script_name + 'admin/overview')
+ @validate(process_validator, error_handler=index)
+ def process(self, **kw):
+ return super(self.__class__, self).process(**kw)
@expose('json:')
- @validate(GoogleCodeProjectForm())
- def check_names(self, project_name=None, project_shortname=None, tools=None, **kw):
- return c.form_errors
+ @validate(process_validator)
+ def check_names(self, **kw):
+ return super(self.__class__, self).check_names(**kw)
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/google/tasks.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/tasks.py b/ForgeImporters/forgeimporters/google/tasks.py
index 65dd126..433daa8 100644
--- a/ForgeImporters/forgeimporters/google/tasks.py
+++ b/ForgeImporters/forgeimporters/google/tasks.py
@@ -23,7 +23,6 @@ from ming.orm import ThreadLocalORMSession
from allura.lib.decorators import task
from . import GoogleCodeProjectExtractor
-from ..base import ToolImporter
@task
@@ -34,8 +33,3 @@ def import_project_info():
extractor.get_license()
ThreadLocalORMSession.flush_all()
g.post_event('project_updated')
-
-@task
-def import_tool(importer_name, mount_point=None, mount_label=None):
- importer = ToolImporter.by_name(importer_name)
- importer.import_tool(c.project, mount_point, mount_label)
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/google/templates/project.html
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/google/templates/project.html b/ForgeImporters/forgeimporters/google/templates/project.html
index 2cda0d0..172dcf5 100644
--- a/ForgeImporters/forgeimporters/google/templates/project.html
+++ b/ForgeImporters/forgeimporters/google/templates/project.html
@@ -18,68 +18,6 @@
-#}
{% extends 'forgeimporters:templates/project_base.html' %}
-{% block extra_css %}
- {{ super() }}
- <style type="text/css">
- #project-import-form #project-fields input {
- width: 88%;
- }
-
- .hidden { display: none; }
- </style>
-{% endblock %}
-
-{% block extra_js %}
- {{ super() }}
- <script type="text/javascript">
- var timers = {};
- function delay(callback, ms) {
- clearTimeout(timers[callback]);
- timers[callback] = setTimeout(callback, ms);
- }
-
- var manual = false;
- function suggest_name() {
- var $project_shortname = $('#project_shortname');
- if (!manual) {
- $project_shortname.val($('#project_name').val());
- }
- $project_shortname.trigger('change');
- }
-
- function check_names() {
- var data = {
- 'neighborhood': $('#neighborhood').val(),
- 'project_name': $('#project_name').val(),
- 'project_shortname': $('#project_shortname').val()
- };
- $.getJSON('check_names', data, function(result) {
- $('#project_name_error').addClass('hidden');
- $('#project_shortname_error').addClass('hidden');
- for(var field in result) {
- $('#'+field+'_error').text(result[field]).removeClass('hidden');
- }
- });
- }
-
- function update_url() {
- $('#url-fragment').text($('#project_shortname').val());
- }
-
- $(function() {
- $('#project_name').focus().bind('change keyup', suggest_name);
-
- $('#project_shortname').bind('change keyup', function(event) {
- if (event.type == 'keyup') {
- manual = true;
- }
- update_url();
- delay(check_names, 500);
- });
- });
- </script>
-{% endblock %}
-
{% block project_fields %}
<div class="grid-6">
<label>Google Project Name</label>
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/templates/project_base.html
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/templates/project_base.html b/ForgeImporters/forgeimporters/templates/project_base.html
index fc654f2..d4db30e 100644
--- a/ForgeImporters/forgeimporters/templates/project_base.html
+++ b/ForgeImporters/forgeimporters/templates/project_base.html
@@ -23,13 +23,97 @@
{% block title %}{{importer.source}} Project Importer{% endblock %}
{% block header %}{{importer.source}} Project Importer{% endblock %}
-{% block content %}
+{% block extra_css %}
+ {{ super() }}
+ <style type="text/css">
+ #project-import-form #project-fields input {
+ width: 88%;
+ }
+
+ .hidden { display: none; }
+ </style>
+{% endblock %}
+
+{% block extra_js %}
+ {{ super() }}
+ <script type="text/javascript">
+ var timers = {};
+ function delay(callback, ms) {
+ clearTimeout(timers[callback]);
+ timers[callback] = setTimeout(callback, ms);
+ }
+
+ var manual = false;
+ function suggest_name() {
+ var $project_shortname = $('#project_shortname');
+ if (!manual) {
+ $project_shortname.val($('#project_name').val());
+ }
+ $project_shortname.trigger('change');
+ }
+
+ function check_names() {
+ var data = {
+ 'neighborhood': $('#neighborhood').val(),
+ 'project_name': $('#project_name').val(),
+ 'project_shortname': $('#project_shortname').val()
+ };
+ $.getJSON('check_names', data, function(result) {
+ $('#project_name_error').addClass('hidden');
+ $('#project_shortname_error').addClass('hidden');
+ for(var field in result) {
+ $('#'+field+'_error').text(result[field]).removeClass('hidden');
+ }
+ });
+ }
+
+ function update_url() {
+ $('#url-fragment').text($('#project_shortname').val());
+ }
+ $(function() {
+ $('#project_name').focus().bind('change keyup', suggest_name);
+
+ $('#project_shortname').bind('change keyup', function(event) {
+ if (event.type == 'keyup') {
+ manual = true;
+ }
+ update_url();
+ delay(check_names, 500);
+ });
+ });
+ </script>
+{% endblock %}
+
+{% block content %}
<form id="project-import-form" method="POST" action="process">
<input type="hidden" id="neighborhood" name="neighborhood" value="{{importer.neighborhood.name}}"/>
<fieldset id="project-fields">
- {% block project_fields %}{% endblock %}
+ {% block project_fields %}
+ <div class="grid-6" style="clear:left">
+ <label>Project Name</label>
+ </div>
+ <div class="grid-10">
+ <input id="project_name" name="project_name" value="{{c.form_values['project_name']}}"/>
+ <div id="project_name_error" class="error{% if not c.form_errors['project_name'] %} hidden{% endif %}">
+ {{c.form_errors['project_name']}}
+ </div>
+ </div>
+
+ <div class="grid-6" style="clear:left">
+ <label>URL Name</label>
+ </div>
+ <div class="grid-10">
+ <input id="project_shortname" name="project_shortname" value="{{c.form_values['project_shortname']}}"/>
+ <div id="project_shortname_error" class="error{% if not c.form_errors['project_shortname'] %} hidden{% endif %}">
+ {{c.form_errors['project_shortname']}}
+ </div>
+ <div id="project-url">
+ http://{{request.environ['HTTP_HOST']}}{{importer.neighborhood.url()}}<span id="url-fragment">{{c.form_values['project_shortname']}}</span>
+ </div>
+ </div>
+ {% endblock %}
</fieldset>
<fieldset id="tool-fields">
@@ -50,5 +134,4 @@
<input type="submit" value="Import"/>
</form>
-
{% endblock %}
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/trac/project.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/project.py b/ForgeImporters/forgeimporters/trac/project.py
new file mode 100644
index 0000000..66e8326
--- /dev/null
+++ b/ForgeImporters/forgeimporters/trac/project.py
@@ -0,0 +1,63 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import logging
+
+from formencode import validators as fev
+
+from tg import expose, validate
+from tg.decorators import with_trailing_slash
+
+from allura.lib.decorators import require_post
+
+from .. import base
+
+
+log = logging.getLogger(__name__)
+
+
+class TracProjectForm(base.ProjectImportForm):
+ trac_url = fev.URL(not_empty=True)
+
+
+class TracProjectImporter(base.ProjectImporter):
+ """
+ Project importer for Trac.
+
+ """
+ source = 'Trac'
+ process_validator = TracProjectForm(source)
+ index_template = 'jinja:forgeimporters.trac:templates/project.html'
+
+ def after_project_create(self, project, **kw):
+ project.set_tool_data('trac', url=kw['trac_url'])
+
+ @with_trailing_slash
+ @expose(index_template)
+ def index(self, **kw):
+ return super(self.__class__, self).index(**kw)
+
+ @require_post()
+ @expose()
+ @validate(process_validator, error_handler=index)
+ def process(self, **kw):
+ return super(self.__class__, self).process(**kw)
+
+ @expose('json:')
+ @validate(process_validator)
+ def check_names(self, **kw):
+ return super(self.__class__, self).check_names(**kw)
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/trac/templates/project.html
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/templates/project.html b/ForgeImporters/forgeimporters/trac/templates/project.html
new file mode 100644
index 0000000..abcfb48
--- /dev/null
+++ b/ForgeImporters/forgeimporters/trac/templates/project.html
@@ -0,0 +1,32 @@
+{#-
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-#}
+{% extends 'forgeimporters:templates/project_base.html' %}
+
+{% block project_fields %}
+ <div class="grid-6">
+ <label>Trac URL</label>
+ </div>
+ <div class="grid-10">
+ <input id="trac_url" name="trac_url" value="{{c.form_values['trac_url']}}"/>
+ <div id="trac_ur_errorl" class="error{% if not c.form_errors['trac_url'] %} hidden{% endif %}">
+ {{c.form_errors['trac_url']}}
+ </div>
+ </div>
+ {{ super() }}
+{% endblock %}
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/trac/templates/wiki/index.html
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/templates/wiki/index.html b/ForgeImporters/forgeimporters/trac/templates/wiki/index.html
new file mode 100644
index 0000000..6083b9c
--- /dev/null
+++ b/ForgeImporters/forgeimporters/trac/templates/wiki/index.html
@@ -0,0 +1,42 @@
+{#-
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-#}
+{% extends g.theme.master %}
+
+{% block title %}
+{{c.project.name}} / Import Trac Wiki
+{% endblock %}
+
+{% block header %}
+Import wiki from Trac
+{% endblock %}
+
+{% block content %}
+<form action="create" method="post" class="pad">
+ <label for="trac_url">URL of the Trac instance</label>
+ <input name="trac_url" />
+
+ <label for="mount_label">Label</label>
+ <input name="mount_label" value="Source" />
+
+ <label for="mount_point">Mount Point</label>
+ <input name="mount_point" value="source" />
+
+ <input type="submit" />
+</form>
+{% endblock %}
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/trac/tickets.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/tickets.py b/ForgeImporters/forgeimporters/trac/tickets.py
index cc31741..969dfd2 100644
--- a/ForgeImporters/forgeimporters/trac/tickets.py
+++ b/ForgeImporters/forgeimporters/trac/tickets.py
@@ -48,7 +48,7 @@ from allura.scripts.trac_export import (
from forgeimporters.base import ToolImporter
from forgetracker.tracker_main import ForgeTrackerApp
-from forgetracker.script.import_tracker import import_tracker
+from forgetracker.scripts.import_tracker import import_tracker
class TracTicketImportSchema(fe.Schema):
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/forgeimporters/trac/wiki.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/trac/wiki.py b/ForgeImporters/forgeimporters/trac/wiki.py
new file mode 100644
index 0000000..2417863
--- /dev/null
+++ b/ForgeImporters/forgeimporters/trac/wiki.py
@@ -0,0 +1,110 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import argparse
+from datetime import (
+ datetime,
+ timedelta,
+ )
+import tempfile
+
+import formencode as fe
+from formencode import validators as fev
+
+from pylons import tmpl_context as c
+from pylons import app_globals as g
+from tg import (
+ config,
+ expose,
+ redirect,
+ validate,
+ )
+from tg.decorators import (
+ with_trailing_slash,
+ without_trailing_slash,
+ )
+
+from allura.controllers import BaseController
+from allura.lib.decorators import require_post
+from allura.model import ApiTicket
+
+from forgeimporters.base import ToolImporter
+
+from forgewiki.scripts.wiki_from_trac.extractors import WikiExporter
+from forgewiki.scripts.wiki_from_trac.loaders import load_data
+from forgewiki.scripts.wiki_from_trac.wiki_from_trac import WikiFromTrac
+from forgewiki.wiki_main import ForgeWikiApp
+
+
+class TracWikiImportSchema(fe.Schema):
+ trac_url = fev.URL(not_empty=True)
+ mount_point = fev.UnicodeString()
+ mount_label = fev.UnicodeString()
+
+
+class TracWikiImportController(BaseController):
+ @with_trailing_slash
+ @expose('jinja:forgeimporters.trac:templates/wiki/index.html')
+ def index(self, **kw):
+ return {}
+
+ @without_trailing_slash
+ @expose()
+ @require_post()
+ @validate(TracWikiImportSchema(), error_handler=index)
+ def create(self, trac_url, mount_point, mount_label, **kw):
+ app = TracWikiImporter.import_tool(c.project,
+ mount_point=mount_point,
+ mount_label=mount_label,
+ trac_url=trac_url,
+ user=c.user)
+ redirect(app.url())
+
+
+class TracWikiImporter(ToolImporter):
+ target_app = ForgeWikiApp
+ source = 'Trac'
+ controller = TracWikiImportController
+ tool_label = 'Trac Wiki Importer'
+ tool_description = 'Import your wiki from Trac'
+
+ def import_tool(self, project=None, mount_point=None, mount_label=None,
+ trac_url=None, user=None):
+ """ Import Trac wiki into a new Allura Wiki tool.
+
+ """
+ mount_point = mount_point or 'wiki'
+ app = project.install_app(
+ 'Wiki',
+ mount_point=mount_point,
+ mount_label=mount_label or 'Wiki',
+ )
+ api_ticket = ApiTicket(user_id=user._id,
+ capabilities={"import": ["Projects", project.shortname]},
+ expires=datetime.utcnow() + timedelta(minutes=60))
+ options = argparse.Namespace()
+ options.api_key = api_ticket.api_key
+ options.secret_key = api_ticket.secret_key
+ options.project = project.shortname
+ options.wiki = mount_point
+ options.base_url = config['base_url']
+ with tempfile.NamedTemporaryFile() as f:
+ WikiExporter(trac_url, options).export(f)
+ f.flush()
+ load_data(f.name, WikiFromTrac.parser(), options)
+ g.post_event('project_updated')
+ return app
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c0940697/ForgeImporters/setup.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/setup.py b/ForgeImporters/setup.py
index 8af3c1a..d79a687 100644
--- a/ForgeImporters/setup.py
+++ b/ForgeImporters/setup.py
@@ -35,7 +35,10 @@ setup(name='ForgeImporters',
# -*- Entry points: -*-
[allura.project_importers]
google-code = forgeimporters.google.project:GoogleCodeProjectImporter
+ trac = forgeimporters.trac.project:TracProjectImporter
[allura.importers]
google-code-repo = forgeimporters.google.code:GoogleRepoImporter
+ trac-tickets = forgeimporters.trac.tickets:TracTicketImporter
+ trac-wiki = forgeimporters.trac.wiki:TracWikiImporter
""",)