comparison rhodecode/lib/indexers/__init__.py @ 785:277427ac29a9 beta

complete rewrite of paster commands, used fancy celery-pylons baseCommand to implement better support for paster commands fixed make-index, and paster celeryd. Moved config's add_cache to utils
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 28 Nov 2010 05:31:05 +0100
parents 7486da5f0628
children 1fed3c9161bb 9f6560667743
comparison
equal deleted inserted replaced
784:30d3161c6683 785:277427ac29a9
1 import os 1 import os
2 import sys 2 import sys
3 import traceback
3 from os.path import dirname as dn, join as jn 4 from os.path import dirname as dn, join as jn
4 5
5 #to get the rhodecode import 6 #to get the rhodecode import
6 sys.path.append(dn(dn(dn(os.path.realpath(__file__))))) 7 sys.path.append(dn(dn(dn(os.path.realpath(__file__)))))
7 8
9 from rhodecode.model import init_model
10 from rhodecode.model.scm import ScmModel
8 from rhodecode.config.environment import load_environment 11 from rhodecode.config.environment import load_environment
9 from rhodecode.model.scm import ScmModel 12 from rhodecode.lib.utils import BasePasterCommand, Command, add_cache
13
10 from shutil import rmtree 14 from shutil import rmtree
11 from webhelpers.html.builder import escape 15 from webhelpers.html.builder import escape
12 from vcs.utils.lazy import LazyProperty 16 from vcs.utils.lazy import LazyProperty
17
18 from sqlalchemy import engine_from_config
13 19
14 from whoosh.analysis import RegexTokenizer, LowercaseFilter, StopFilter 20 from whoosh.analysis import RegexTokenizer, LowercaseFilter, StopFilter
15 from whoosh.fields import TEXT, ID, STORED, Schema, FieldType 21 from whoosh.fields import TEXT, ID, STORED, Schema, FieldType
16 from whoosh.index import create_in, open_dir 22 from whoosh.index import create_in, open_dir
17 from whoosh.formats import Characters 23 from whoosh.formats import Characters
18 from whoosh.highlight import highlight, SimpleFragmenter, HtmlFormatter 24 from whoosh.highlight import highlight, SimpleFragmenter, HtmlFormatter
19 25
20 import traceback
21 26
22 #EXTENSIONS WE WANT TO INDEX CONTENT OFF 27 #EXTENSIONS WE WANT TO INDEX CONTENT OFF
23 INDEX_EXTENSIONS = ['action', 'adp', 'ashx', 'asmx', 'aspx', 'asx', 'axd', 'c', 28 INDEX_EXTENSIONS = ['action', 'adp', 'ashx', 'asmx', 'aspx', 'asx', 'axd', 'c',
24 'cfg', 'cfm', 'cpp', 'cs', 'css', 'diff', 'do', 'el', 'erl', 29 'cfg', 'cfm', 'cpp', 'cs', 'css', 'diff', 'do', 'el', 'erl',
25 'h', 'htm', 'html', 'ini', 'java', 'js', 'jsp', 'jspx', 'lisp', 30 'h', 'htm', 'html', 'ini', 'java', 'js', 'jsp', 'jspx', 'lisp',
43 48
44 IDX_NAME = 'HG_INDEX' 49 IDX_NAME = 'HG_INDEX'
45 FORMATTER = HtmlFormatter('span', between='\n<span class="break">...</span>\n') 50 FORMATTER = HtmlFormatter('span', between='\n<span class="break">...</span>\n')
46 FRAGMENTER = SimpleFragmenter(200) 51 FRAGMENTER = SimpleFragmenter(200)
47 52
48 from paste.script import command
49 import ConfigParser
50 53
51 class MakeIndex(command.Command): 54 class MakeIndex(BasePasterCommand):
52 55
53 max_args = 1 56 max_args = 1
54 min_args = 1 57 min_args = 1
55 58
56 usage = "CONFIG_FILE" 59 usage = "CONFIG_FILE"
57 summary = "Creates index for full text search given configuration file" 60 summary = "Creates index for full text search given configuration file"
58 group_name = "RhodeCode" 61 group_name = "RhodeCode"
59 takes_config_file = -1 62 takes_config_file = -1
60 parser = command.Command.standard_parser(verbose=True) 63 parser = Command.standard_parser(verbose=True)
61 parser.add_option('--repo-location', 64
62 action='store',
63 dest='repo_location',
64 help="Specifies repositories location to index REQUIRED",
65 )
66 parser.add_option('-f',
67 action='store_true',
68 dest='full_index',
69 help="Specifies that index should be made full i.e"
70 " destroy old and build from scratch",
71 default=False)
72 def command(self): 65 def command(self):
73 config_name = self.args[0]
74 p = config_name.split('/')
75 root = '.' if len(p) == 1 else '/'.join(p[:-1])
76 config = ConfigParser.ConfigParser({'here':root})
77 config.read(config_name)
78 66
79 index_location = dict(config.items('app:main'))['index_dir'] 67 from pylons import config
68 add_cache(config)
69 engine = engine_from_config(config, 'sqlalchemy.db1.')
70 init_model(engine)
71
72 index_location = config['index_dir']
80 repo_location = self.options.repo_location 73 repo_location = self.options.repo_location
81 74
82 #====================================================================== 75 #======================================================================
83 # WHOOSH DAEMON 76 # WHOOSH DAEMON
84 #====================================================================== 77 #======================================================================
91 .run(full_index=self.options.full_index) 84 .run(full_index=self.options.full_index)
92 l.release() 85 l.release()
93 except LockHeld: 86 except LockHeld:
94 sys.exit(1) 87 sys.exit(1)
95 88
89 def update_parser(self):
90 self.parser.add_option('--repo-location',
91 action='store',
92 dest='repo_location',
93 help="Specifies repositories location to index REQUIRED",
94 )
95 self.parser.add_option('-f',
96 action='store_true',
97 dest='full_index',
98 help="Specifies that index should be made full i.e"
99 " destroy old and build from scratch",
100 default=False)
96 101
97 class ResultWrapper(object): 102 class ResultWrapper(object):
98 def __init__(self, search_type, searcher, matcher, highlight_items): 103 def __init__(self, search_type, searcher, matcher, highlight_items):
99 self.search_type = search_type 104 self.search_type = search_type
100 self.searcher = searcher 105 self.searcher = searcher