Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """Create configuration for code search.""" |
| 5 | |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 6 | import argparse |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 7 | import json |
| 8 | import urllib.request |
| 9 | import sys |
| 10 | |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 11 | DEFAULT_GERRIT = "https://gerrit.onap.org/r" |
Pawel Wieczorek | b4436f2 | 2020-01-07 15:28:51 +0100 | [diff] [blame^] | 12 | DEFAULT_GIT = "https://git.onap.org" |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 13 | API_PROJECTS = "/projects/" |
| 14 | |
| 15 | MAGIC_PREFIX = ")]}'" |
| 16 | |
| 17 | |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 18 | def get_projects_list(gerrit): |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 19 | """Request list of all available projects from ONAP Gerrit.""" |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 20 | resp = urllib.request.urlopen(gerrit + API_PROJECTS) |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 21 | 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 Wieczorek | b4436f2 | 2020-01-07 15:28:51 +0100 | [diff] [blame^] | 30 | def create_repos_list(projects, git): |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 31 | """Create a map of all projects to their repositories' URLs.""" |
Pawel Wieczorek | e7c5ed5 | 2020-01-07 15:28:37 +0100 | [diff] [blame] | 32 | repos_list = {} |
| 33 | for project in projects: |
| 34 | repos_list[project] = { |
Pawel Wieczorek | b4436f2 | 2020-01-07 15:28:51 +0100 | [diff] [blame^] | 35 | "url": "{}/{}".format(git, project), |
Pawel Wieczorek | e7c5ed5 | 2020-01-07 15:28:37 +0100 | [diff] [blame] | 36 | "url-pattern": { |
| 37 | "base-url": "{url}/tree/{path}{anchor}", |
| 38 | "anchor": "#n{line}" |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return repos_list |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 43 | |
| 44 | |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 45 | def 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 Wieczorek | b4436f2 | 2020-01-07 15:28:51 +0100 | [diff] [blame^] | 49 | parser.add_argument('--git', help='git address', default=DEFAULT_GIT) |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 50 | |
| 51 | return parser.parse_args() |
| 52 | |
| 53 | |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 54 | def main(): |
| 55 | """Main entry point for the script.""" |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 56 | arguments = parse_arguments() |
| 57 | |
Pawel Wieczorek | b4436f2 | 2020-01-07 15:28:51 +0100 | [diff] [blame^] | 58 | repos = create_repos_list(get_projects_list(arguments.gerrit), arguments.git) |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 59 | 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 | |
| 68 | if __name__ == '__main__': |
| 69 | sys.exit(main()) |