blob: 109219d2f444d7cd557abdd12a311665b425c2b5 [file] [log] [blame]
Pawel Wieczorek9ee12582020-01-07 15:26:52 +01001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4"""Create configuration for code search."""
5
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +01006import argparse
Pawel Wieczorek9ee12582020-01-07 15:26:52 +01007import json
8import urllib.request
9import sys
10
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010011DEFAULT_GERRIT = "https://gerrit.onap.org/r"
Pawel Wieczorekb4436f22020-01-07 15:28:51 +010012DEFAULT_GIT = "https://git.onap.org"
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010013API_PROJECTS = "/projects/"
14
15MAGIC_PREFIX = ")]}'"
16
17
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010018def get_projects_list(gerrit):
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010019 """Request list of all available projects from ONAP Gerrit."""
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010020 resp = urllib.request.urlopen(gerrit + API_PROJECTS)
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010021 resp_body = resp.read()
22
23 no_magic = resp_body[len(MAGIC_PREFIX):]
24 decoded = no_magic.decode("utf-8")
25 projects = json.loads(decoded)
26
27 return projects.keys()
28
29
Pawel Wieczorekb4436f22020-01-07 15:28:51 +010030def create_repos_list(projects, git):
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010031 """Create a map of all projects to their repositories' URLs."""
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010032 repos_list = {}
33 for project in projects:
34 repos_list[project] = {
Pawel Wieczorekb4436f22020-01-07 15:28:51 +010035 "url": "{}/{}".format(git, project),
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010036 "url-pattern": {
37 "base-url": "{url}/tree/{path}{anchor}",
38 "anchor": "#n{line}"
39 }
40 }
41
42 return repos_list
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010043
44
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010045def parse_arguments():
46 """Return parsed command-line arguments."""
47 parser = argparse.ArgumentParser(description=__doc__)
48 parser.add_argument('--gerrit', help='Gerrit address', default=DEFAULT_GERRIT)
Pawel Wieczorekb4436f22020-01-07 15:28:51 +010049 parser.add_argument('--git', help='git address', default=DEFAULT_GIT)
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010050
51 return parser.parse_args()
52
53
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010054def main():
55 """Main entry point for the script."""
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010056 arguments = parse_arguments()
57
Pawel Wieczorekb4436f22020-01-07 15:28:51 +010058 repos = create_repos_list(get_projects_list(arguments.gerrit), arguments.git)
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010059 config = {
60 "max-concurrent-indexers": 2,
61 "dbpath": "data",
62 "health-check-uri": "/healthz",
63 "repos": repos
64 }
65 print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')))
66
67
68if __name__ == '__main__':
69 sys.exit(main())