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}"
22 def get_projects_list(gerrit):
23 """Request list of all available projects from ONAP Gerrit."""
24 resp = urllib.request.urlopen("https://{}{}{}".format(gerrit, API_PREFIX, API_PROJECTS))
25 resp_body = resp.read()
27 no_magic = resp_body[len(MAGIC_PREFIX):]
28 decoded = no_magic.decode("utf-8")
29 projects = json.loads(decoded)
31 return projects.keys()
34 def create_repos_list(projects, gerrit, ssh, git):
35 """Create a map of all projects to their repositories' URLs."""
36 gerrit_url = "https://{}{}".format(gerrit, API_PREFIX)
37 gerrit_project_url_base = "{}/{{}}.git".format(gerrit_url)
38 gitweb_code_url_base = "{}/gitweb?p={{}}.git;hb=HEAD;a=blob;f=".format(gerrit_url)
41 for project in projects:
42 project_url = gerrit_project_url_base.format(project)
43 code_url = gitweb_code_url_base.format(project) + CODE_LOCATION
44 anchor = GITWEB_ANCHOR
46 if ssh and len(ssh) == 2:
47 user, port = ssh[0], ssh[1]
48 project_url = "ssh://{}@{}:{}/{}.git".format(user, gerrit, port, project)
50 code_url = "https://{}/{}/tree/".format(git, project) + CODE_LOCATION
53 repos_list[project] = {
64 def parse_arguments():
65 """Return parsed command-line arguments."""
66 parser = argparse.ArgumentParser(description=__doc__)
67 parser.add_argument('--gerrit', help='Gerrit address', default=DEFAULT_GERRIT)
68 parser.add_argument('--ssh', help='SSH information: user, port', nargs=2)
69 parser.add_argument('--git', help='external git address')
71 return parser.parse_args()
75 """Main entry point for the script."""
76 arguments = parse_arguments()
78 projects = get_projects_list(arguments.gerrit)
79 repos = create_repos_list(projects, arguments.gerrit, arguments.ssh, arguments.git)
81 "max-concurrent-indexers": 2,
83 "health-check-uri": "/healthz",
86 print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')))
89 if __name__ == '__main__':