2 # -*- coding: utf-8 -*-
4 """Create configuration for code search."""
11 DEFAULT_GERRIT = "gerrit.onap.org"
13 API_PROJECTS = "/projects/"
17 CODE_LOCATION = "{path}{anchor}"
18 GITWEB_ANCHOR = "#l{line}"
19 GIT_ANCHOR = "#n{line}"
23 def get_projects_list(gerrit):
24 """Request list of all available projects from ONAP Gerrit."""
25 resp = urllib.request.urlopen("https://{}{}{}".format(gerrit, API_PREFIX, API_PROJECTS))
26 resp_body = resp.read()
28 no_magic = resp_body[len(MAGIC_PREFIX):]
29 decoded = no_magic.decode("utf-8")
30 projects = json.loads(decoded)
32 return projects.keys()
35 def create_repos_list(projects, gerrit, ssh, git, poll):
36 """Create a map of all projects to their repositories' URLs."""
37 gerrit_url = "https://{}{}".format(gerrit, API_PREFIX)
38 git_url = "git://{}".format(git)
39 gerrit_project_url_base = "{}/{{}}.git".format(gerrit_url)
40 gitweb_code_url_base = "{}/gitweb?p={{}}.git;hb=HEAD;a=blob;f=".format(gerrit_url)
41 git_project_url_base = "{}/{{}}.git".format(git_url)
44 for project in projects:
45 project_url = gerrit_project_url_base.format(project)
46 code_url = gitweb_code_url_base.format(project) + CODE_LOCATION
47 anchor = GITWEB_ANCHOR
49 if ssh and len(ssh) == 2:
50 user, port = ssh[0], ssh[1]
51 project_url = "ssh://{}@{}:{}/{}.git".format(user, gerrit, port, project)
53 code_url = "https://{}/{}/tree/".format(git, project) + CODE_LOCATION
54 project_url = git_project_url_base.format(project)
57 repos_list[project] = {
62 "ms-between-poll": poll * 1000
69 def parse_arguments():
70 """Return parsed command-line arguments."""
71 parser = argparse.ArgumentParser(description=__doc__)
72 group = parser.add_mutually_exclusive_group()
73 parser.add_argument('--gerrit', help='Gerrit address', default=DEFAULT_GERRIT)
74 group.add_argument('--ssh', help='SSH information for Gerrit access: user, port', nargs=2)
75 group.add_argument('--git', help='External git address. Does not support --ssh')
76 parser.add_argument('--poll-interval', help='Repositories polling interval in seconds', type=int, default=DEFAULT_POLL)
78 return parser.parse_args()
82 """Main entry point for the script."""
83 arguments = parse_arguments()
85 projects = get_projects_list(arguments.gerrit)
86 repos = create_repos_list(projects, arguments.gerrit, arguments.ssh, arguments.git, arguments.poll_interval)
88 "max-concurrent-indexers": 2,
90 "health-check-uri": "/healthz",
93 print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')))
96 if __name__ == '__main__':