3分鐘搭建OpenGrok多工程搜索(rc36)

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章