我是靠谱客的博主 伶俐酒窝,最近开发中收集的这篇文章主要介绍3分钟搭建OpenGrok多工程搜索(rc36)Tools:安装步骤:,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

OpenGrok秒搜代码,谁用谁Happy。支持多OpenGrok多工程,自动配置脚本,自动更新代码,自动OpenGrok数据库,也可以定制Opengrok的解析过滤。

 

本文仅仅在Ubuntu 14.04下面验证,请他环境可以参考,但是需要修改下python脚本。

Tools:

  1.         apache-tomcat-9.0.8(github下载地址)
  2.         java-8-openjdk-amd64(官网下载地址)
  3.         opengrok-1.1-rc36(github下载地址)
  4.         ctags(github下载地址)

     下载1,2,3的工具并解压:

android@ubuntu:~/OpenGrok2$ tree -L 1
.
├── apache-tomcat-9.0.8
├── apache-tomcat-9.0.8.tar.gz
├── java-8-openjdk-amd64
├── java-8-openjdk-amd64.zip
├── opengrok-1.1-rc36
└── opengrok-1.1-rc36.tar.gz
3 directories, 3 files

安装步骤:

1 安装Ctags

     需要安装最新的universal-ctags。

     请直接参考:

          https://www.cnblogs.com/zjutzz/p/9393397.html

2 执行python脚本:

if __name__ == '__main__':
_parser = argparse.ArgumentParser(description='Update or Config a Project for Source Search')
_parser.add_argument("-u", "--update", default=False, help="Update All exist Project in Config.ini")
_parser.add_argument("-p", "--project_name", help="Project Name")
_parser.add_argument("-s", "--sync_cmd", help="Sync cmd, like git clone xxx.git, repo sync xxx")
_args = _parser.parse_args()

-u True  全部更新配置过的Project ,可以用于添加到定时任务里面

-p 指定项目名称

 -s 设定项目同步命令 

android@ubuntu:~/OpenGrok2$ python UpdateProject.py -p MiniControl -s 'git clone https://gitee.com/lookfuyao/minicontrol.git'

如果有过历史记录,也可以直接不带任何参数。记录保存在Config.ini里面。

android@ubuntu:~/OpenGrok$ python UpdateProject.py
0: sc7731e-9
1: MiniControl
Please Select project:1
Cur Project is: MiniControl
Exe cmd: cd Projects/MiniControl; git clone https://gitee.com/lookfuyao/minicontrol.git
fatal: destination path 'minicontrol' already exists and is not an empty directory.
Exe cmd: cd Projects/MiniControl/minicontrol; git pull
Already up-to-date.
Exe cmd: ./opengrok-tools/bin/opengrok-indexer
-j=/home/android/OpenGrok/java-8-openjdk-amd64/bin/java
-a=/home/android/OpenGrok/opengrok-1.1-rc75/lib/opengrok.jar --
-c /usr/bin/ctags
-i d:*.git -i d:*.repo -i f:*.jar
-H -P
-s /home/android/OpenGrok/Projects/MiniControl
-d /home/android/OpenGrok/Datas/MiniControl
-W /home/android/OpenGrok/Datas/MiniControl/configuration.xml
Update MiniControl config /home/android/OpenGrok/Datas/MiniControl/configuration.xml success
Exe cmd: export JRE_HOME=./java-8-openjdk-amd64/jre;
./apache-tomcat-9.0.8/bin/shutdown.sh;
./apache-tomcat-9.0.8/bin/startup.sh &
Using CATALINA_BASE:
/home/android/OpenGrok/apache-tomcat-9.0.8
Using CATALINA_HOME:
/home/android/OpenGrok/apache-tomcat-9.0.8
Using CATALINA_TMPDIR: /home/android/OpenGrok/apache-tomcat-9.0.8/temp
Using JRE_HOME:
./java-8-openjdk-amd64/jre
Using CLASSPATH:
/home/android/OpenGrok/apache-tomcat-9.0.8/bin/bootstrap.jar:/home/android/OpenGrok/apache-tomcat-9.0.8/bin/tomcat-juli.jar
android@ubuntu:~/OpenGrok$ Tomcat started.

   可以自己修改下面的执行参数(包括过滤不需要的目录文件、指定ctags的目录等):


# open grok indexer
cmd = "%s 
-j=%s 
-a=%s -- 
-c %s 
-i %s 
-H -P 
-s %s 
-d %s 
-W %s" % (
os.path.join(work_path, OPEN_GROK_VERSION, "bin/indexer.py"),
os.path.join(work_path, JAVA_VERSION, "bin/java"),
# -j java path
os.path.join(work_path, OPEN_GROK_VERSION, "lib/opengrok.jar"),
# -a opengrok jar path
"/usr/bin/ctags",
# -c ctags path
"d:*.git -i d:*.repo -i f:*.jar",
# -i filter files and dirs
project_path,
# -s Project source path
os.path.join(work_path, "Datas", project_name),
# -d opengrok generate data
os.path.join(work_path, "Datas", project_name, "configuration.xml")
# -W cfg for apache
)

脚本内容如下,

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
try:
import configparser as configparser
except Exception:
import ConfigParser as configparser
import logging
import os
import shutil
import time
from xml.dom.minidom import parse
import xml.dom.minidom
import subprocess
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
CONFIG_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "Config.ini")
OPEN_GROK_VERSION = 'opengrok-1.1-rc36'
APACHE_TOMCAT_VERSION = 'apache-tomcat-9.0.8'
JAVA_VERSION = 'java-8-openjdk-amd64'
def get_all_projects():
cf = configparser.ConfigParser()
cf.read(CONFIG_FILE)
secs = cf.sections()
return secs
def get_project_sync_cmd(project):
cf = configparser.ConfigParser()
cf.read(CONFIG_FILE)
if cf.has_section(project):
cf.has_option(project, 'sync_cmd')
return cf.get(project, 'sync_cmd')
return None
def set_project_sync_cmd(project, sync_cmd):
if sync_cmd and (sync_cmd.startswith("git clone") or sync_cmd.startswith("repo init")):
cf = configparser.ConfigParser()
cf.read(CONFIG_FILE)
if not cf.has_section(project):
cf.add_section(project)
cf.set(project, 'sync_cmd', sync_cmd)
cf.write(open(CONFIG_FILE, 'w'))
def restart_apache_tomcat(work_path):
cmd = "export JRE_HOME=%s; %s; %s" % (
os.path.join(work_path, JAVA_VERSION, "jre"),
os.path.join(work_path, APACHE_TOMCAT_VERSION, "bin/shutdown.sh"),
os.path.join(work_path, APACHE_TOMCAT_VERSION, "bin/startup.sh")
)
exe_cmd(cmd)
def exe_cmd(cmd):
logging.debug("Exe cmd: %s" % cmd)
# result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
# for info in result.communicate():
#
logging.debug(info)
os.system(cmd)
def index_project(work_path, project_name, sync_cmd):
if work_path is None or project_name is None or sync_cmd is None:
logging.debug("Error Project or cmd %s %s" % (project_name, sync_cmd))
else:
# init code
project_path = os.path.join(work_path, "Projects", project_name)
if not os.path.exists(project_path):
os.makedirs(project_path)
if sync_cmd.startswith("git clone"):
sync = "git pull"
else:
sync = "repo sync"
cmd = "cd %s; %s " % (project_path, sync_cmd)
exe_cmd(cmd)
# sync code
if sync.startswith("git pull"):
cmd = os.listdir(project_path)[0]
cmd = "cd %s; git pull" % (os.path.join(project_path, cmd))
elif sync.startswith("repo sync"):
cmd = "cd %s; repo sync" % project_path
exe_cmd(cmd)
# open grok indexer
cmd = "%s 
-j=%s 
-a=%s -- 
-c %s 
-i %s 
-H -P 
-s %s 
-d %s 
-W %s" % (
os.path.join(work_path, OPEN_GROK_VERSION, "bin/indexer.py"),
os.path.join(work_path, JAVA_VERSION, "bin/java"),
# -j java path
os.path.join(work_path, OPEN_GROK_VERSION, "lib/opengrok.jar"),
# -a opengrok jar path
"/usr/bin/ctags",
# -c ctags path
"d:*.git -i d:*.repo -i f:*.jar",
# -i filter files and dirs
project_path,
# -s Project source path
os.path.join(work_path, "Datas", project_name),
# -d opengrok generate data
os.path.join(work_path, "Datas", project_name, "configuration.xml")
# -W cfg for apache
)
exe_cmd(cmd)
# create project in apache tomcat
apache_app_path = os.path.join(work_path, APACHE_TOMCAT_VERSION, "webapps", project_name + ".war")
if not os.path.exists(apache_app_path):
war_path = OPEN_GROK_VERSION + "/lib/source.war"
shutil.copyfile(war_path, apache_app_path)
restart_apache_tomcat(work_path)
time.sleep(3)
# set the project configuration.xml to apache tomcat
apache_app_config = os.path.join(
os.path.join(work_path, APACHE_TOMCAT_VERSION, "webapps", project_name),
"WEB-INF", "web.xml")
wait_time = 0
while not os.path.exists(apache_app_config):
if wait_time > 10:
logging.debug(
"Cannot find apache tomcat app=%s config_path=%s error!" % (project_name, apache_app_config))
return
wait_time = wait_time + 1
time.sleep(3)
tree = xml.dom.minidom.parse(apache_app_config)
nodes = tree.documentElement.getElementsByTagName('context-param')
for node in nodes:
cfg_node = node.getElementsByTagName('param-value')
if cfg_node:
value = os.path.join(work_path, "Datas", project_name, "configuration.xml")
cfg_node[0].childNodes[0].data = value
logging.debug("Update %s config %s success" % (project_name, value))
with open(apache_app_config, 'w') as fh:
tree.writexml(fh, indent='
', encoding='utf-8')
# writexml(fh)
break
restart_apache_tomcat(work_path)
def start_lock():
lock = os.path.join(os.path.dirname(os.path.realpath(__file__)), "lock.lock")
logging.debug("Lock file %s" % lock)
if os.path.exists(lock):
return False
exe_cmd("touch %s" % lock)
return True
def end_lock(code):
lock = os.path.join(os.path.dirname(os.path.realpath(__file__)), "lock.lock")
exe_cmd("rm %s" % lock)
exit(code)
if __name__ == '__main__':
_parser = argparse.ArgumentParser(description='Update or Config a Project for Source Search')
_parser.add_argument("-u", "--update", help="Update All exist Project in Config.ini")
_parser.add_argument("-p", "--project_name", help="Project Name")
_parser.add_argument("-s", "--sync_cmd", help="Sync cmd, like git clone xxx.git, repo sync xxx")
_args = _parser.parse_args()
_work_path = os.path.dirname(os.path.realpath(__file__))
_now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
_log_file_name = os.path.join(_work_path, "logs", 'log' + _now + ".log")
if not os.path.exists(os.path.dirname(_log_file_name)):
os.mkdir(os.path.dirname(_log_file_name))
logging.basicConfig(level=logging.DEBUG,
format=LOG_FORMAT,
datefmt=DATE_FORMAT,
filename=_log_file_name
)
logging.debug("This is a debug log.")
logging.info("This is a info log.")
logging.warning("This is a warning log.")
logging.error("This is a error log.")
logging.critical("This is a critical log.")
if not start_lock():
print("Cannot create lock file")
logging.debug("Cannot create lock file")
exit(1)
if _args.update == "True" or _args.update == "1":
_projects = get_all_projects()
if len(_projects) > 0:
for _project_name in _projects:
logging.debug("Start Update Project %s" % _project_name)
index_project(_work_path, _project_name, get_project_sync_cmd(_project_name))
logging.debug("End Update Project %s" % _project_name)
end_lock(0)
else:
logging.debug("No Exist Project Exit")
end_lock(1)
else:
_project_name = _args.project_name
if not _project_name:
_projects = get_all_projects()
if len(_projects) <= 0:
logging.debug("Pls set project with options -p")
end_lock(1)
else:
_index = 0
for _project in _projects:
print("%s: %s" % (_index, _project))
_index = _index + 1
_select = int(input("Please Select project:"))
if 0 <= _select < _index:
_project_name = _projects[_select]
else:
logging.debug("Project index error")
end_lock(2)
logging.debug("Cur Project is: %s" % _project_name)
_sync_cmd = _args.sync_cmd
set_project_sync_cmd(_project_name, _sync_cmd)
_project_path = os.path.join('Projects', _project_name)
_sync_cmd = get_project_sync_cmd(_project_name)
if not _sync_cmd:
_sync_cmd = input("Please Set %s Sync cmd (only support git clone xxx.git or repo sync xxx):")
if _sync_cmd:
if _sync_cmd.startswith("git clone") or _sync_cmd.startswith("repo sync"):
set_project_sync_cmd(_project_name, _sync_cmd)
else:
logging.debug("Sync cmd is Error!")
end_lock(3)
else:
logging.debug("Sync cmd is empty!")
end_lock(4)
index_project(_work_path, _project_name, _sync_cmd)
end_lock(0)

浏览器打开下面地址即可搜索代码(xxx替换为自己的项目名称):

http://localhost:8080/xxx

局域网的其他人也可以访问,把localhost换成自己的ip既可。

 

Ubuntu下面添加定时任务(cron):

比如下面的,就是每天的13:43分同步所有Projects,记得修改下自己的OpenGrok目录路径。

android@ubuntu:~$ crontab -e
----------------------------------------------
# m h
dom mon dow
command
43 13 * * * cd /home/android/OpenGrok; ./UpdateProject.py -u=True

最后

以上就是伶俐酒窝为你收集整理的3分钟搭建OpenGrok多工程搜索(rc36)Tools:安装步骤:的全部内容,希望文章能够帮你解决3分钟搭建OpenGrok多工程搜索(rc36)Tools:安装步骤:所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部