|
import os
import re
import logging
from mercurial import ui, hg, commands, error
from api.emptyui import emptyui
from proc import start_proc, monitor_proc
import daemon as _daemon
from ogconfig import IGNORE_PATTERNS
from bugzscout import report_exception, report_error
DEFAULT = 'default'
TIP_REV = 'tip'
NULL_REV = 'null'
SEARCH_AREA = 'Search'
def get_logger():
from daemon import LOG_NAME
return logging.getLogger(LOG_NAME)
class _subrepoui(ui.ui):
def __init__(self, *args, **kwargs):
super(_subrepoui, self).__init__(*args, **kwargs)
self.setconfig('extensions', 'kilnext.subzero', '')
self.setconfig('extensions', 'kilnext.nosymlink', '')
self.setconfig('extensions', 'kilnext.nobinary', '')
def readconfig(self, filename, *args, **kwargs):
if os.path.join('.hg', 'hgrc') in filename:
# We need the repo's hgrc file to get metadata.
super(_subrepoui, self).readconfig(filename, *args, **kwargs)
def _reposubpath(repo):
repo = os.path.basename(repo)
return os.path.join(repo[:2], repo[2:4], repo)
def _repopath(root, repo):
repo = os.path.basename(repo)
p = os.path.join(root, _reposubpath(repo))
if not os.path.exists(p):
p = os.path.join(root, repo)
return p
def enqueue(daemon, repo):
id = daemon.redis.incr(_daemon.INDEX_NEXT)
daemon.redis.zadd(_daemon.INDEX_QUEUE, repo, id)
def index(repo, conf):
logger = get_logger()
repos_dir = conf.read('', 'KilnRepositoryRoot')
l = w = None
try:
# ab/cd/abcd... directory structure
hgrepo = hg.repository(emptyui(), _repopath(repos_dir, repo))
except error.RepoError:
# old directory structure
hgrepo = hg.repository(emptyui(), os.path.join(repos_dir, repo))
logger.debug('Indexing %s' % repo)
try:
# Mercurial waits for all locks by default. We don't want to, we'll re-enqueue.
l = hgrepo.lock(wait=False)
w = hgrepo.wlock(wait=False)
except error.LockHeld:
# Lock is held. Unlock anything we locked and remove from the list.
if w: w.release()
if l: l.release()
logger.debug('%s was already locked.' % repo)
return False
try:
proc = _start_index(hgrepo, repo, conf)
return monitor_proc(proc, bugzscout_area=SEARCH_AREA)
finally:
if w: w.release()
if l: l.release()
def _start_index(hgrepo, repo, conf):
repos_dir = conf.read('', 'KilnRepositoryRoot')
java = conf.read('OpenGrok', 'Java')
opengrok = conf.read('OpenGrok', 'Jar')
ctags = conf.read('OpenGrok', 'CTags')
data_dir = conf.read('OpenGrok', 'DataDir')
config_file = os.path.join(data_dir, 'configuration.xml')
config_update = conf.read('OpenGrok', 'ConfigUpdate')
logging_properties = conf.read('OpenGrok', 'LoggingProperties', default='')
java_logging_properties = '-Djava.util.logging.config.file=%s' % logging_properties
index_class_name = 'org.opensolaris.opengrok.index.Indexer'
repo_subpath = _reposubpath(repo).replace('\\', '/')
ignores = sum([['-i', p] for p in IGNORE_PATTERNS], [])
return start_proc(java, java_logging_properties, '-cp', opengrok, index_class_name, '-q', '-e', '-P', '-c', ctags, '-d', data_dir, '-s', repos_dir, '-I', repo_subpath, '-O', 'on', *(ignores + ['/'+repo_subpath]), bugzscout_area='Search')
def update(repo, conf):
repos_dir = conf.read('', 'KilnRepositoryRoot')
repo = os.path.join(repos_dir, repo)
r = hg.repository(_subrepoui(), _repopath(repos_dir, repo))
l = r.lock()
try:
# We first check for a deleted repository.
# If it's deleted, we want to update to revision "null"
branch = None
if r.ui.config('meta', 'deleted', default='false').strip().lower() == 'true':
branch = NULL_REV
else:
# If it's not deleted, we check to be sure there is a 'default' branch.
# If not, we return the first branch we find.
r.ui.pushbuffer()
commands.branches(r.ui, r)
out = r.ui.popbuffer()
if not out:
# No branches, empty repo.
return True
branches = [s.split()[0] for s in out.strip().splitlines()]
if DEFAULT in branches:
branch = DEFAULT
else:
branch = TIP_REV
r.ui.pushbuffer()
if hg.clean(r, branch):
# True means failed
report_error('Failed to update repo.\nRepo: %s\nBranch: %s' % (repo, branch))
return False
r.ui.popbuffer()
return True
except Exception, e:
report_exception(e, 'Failed to update repo.\nRepo: %s\nBranch: %s' % (repo, branch))
return False
finally:
l.release()
def get_all_repos(conf):
re_uuid = re.compile(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$')
re_2dir = re.compile(r'^[0-9a-f]{2}$')
repos_dir = conf.read('', 'KilnRepositoryRoot')
repos = []
for dir1 in os.listdir(repos_dir):
if re_2dir.match(dir1):
for dir2 in os.listdir(os.path.join(repos_dir, dir1)):
if re_2dir.match(dir2):
for repo in os.listdir(os.path.join(repos_dir, dir1, dir2)):
if re_uuid.match(repo):
repos.append(repo)
elif re_uuid.match(dir1):
repos.append(dir1)
return repos
|
Loading...