Skip to content

Instantly share code, notes, and snippets.

@khill
Created January 17, 2012 19:29
Show Gist options
  • Select an option

  • Save khill/1628341 to your computer and use it in GitHub Desktop.

Select an option

Save khill/1628341 to your computer and use it in GitHub Desktop.
Simple library for building and searching an index from a subversion repository
import pysvn
import datetime
import os
from whoosh.index import create_in, open_dir
from whoosh.fields import *
from whoosh.qparser import QueryParser
schema = Schema(author=TEXT(stored=True),
message=TEXT(stored=True),
revision=KEYWORD(stored=True),
timestamp=DATETIME(stored=True))
def open_index(fname):
''' opens an index at the path using the specified file name '''
if not os.path.exists(fname):
os.makedirs(fname)
index = create_in(fname, schema)
return index
def get_svn_login(realm, username, may_save):
''' prompts user for svn login if needed '''
username = raw_input('Username: ')
password = raw_input('Password: ')
return (True, username, password, True)
def index_svn_commits(svn_url, index_path):
''' extracts commit messages from subversion at the given URL and indexes the commit messages into the given index path'''
c = pysvn.Client()
c.callback_get_login = get_svn_login
msgs = c.log(svn_url)
index = open_index(index_path)
writer = index.writer()
for msg in msgs:
writer.add_document(author = unicode(msg.author),
message = unicode(msg.message),
revision = unicode(msg.revision.number),
timestamp = datetime.datetime.fromtimestamp(msg.date))
writer.commit(optimize=True)
def dump_terms(index_path, field):
''' dumps the list of terms in the given field '''
index = open_dir(index_path)
search = index.searcher()
terms = list(searcher.lexicon(field))
return terms
def search_commits(index_path, query):
''' searches the index using the given query object and returns matching commits '''
index = open_dir(index_path)
searcher = index.searcher()
results = searcher.search(query)
return results
def get_query(params):
''' takes a dictionary and constructs a whoosh query object for searching '''
qp = QueryParser('message', schema)
q_phrases = []
for field,value in params.items():
q_phrases.append('%s:%s' % (field, unicode(value)))
return qp.parse(' AND '.join(q_phrases))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment