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 | |
| 6 | import json |
| 7 | import urllib.request |
| 8 | import sys |
| 9 | |
| 10 | ONAP_GERRIT = "https://gerrit.onap.org/r" |
Pawel Wieczorek | 5aed5e7 | 2020-01-07 15:27:54 +0100 | [diff] [blame] | 11 | ONAP_CGIT = "https://git.onap.org" |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 12 | API_PROJECTS = "/projects/" |
| 13 | |
| 14 | MAGIC_PREFIX = ")]}'" |
| 15 | |
| 16 | |
| 17 | def get_projects_list(): |
| 18 | """Request list of all available projects from ONAP Gerrit.""" |
| 19 | resp = urllib.request.urlopen(ONAP_GERRIT + API_PROJECTS) |
| 20 | resp_body = resp.read() |
| 21 | |
| 22 | no_magic = resp_body[len(MAGIC_PREFIX):] |
| 23 | decoded = no_magic.decode("utf-8") |
| 24 | projects = json.loads(decoded) |
| 25 | |
| 26 | return projects.keys() |
| 27 | |
| 28 | |
| 29 | def create_repos_list(projects): |
| 30 | """Create a map of all projects to their repositories' URLs.""" |
Pawel Wieczorek | e7c5ed5 | 2020-01-07 15:28:37 +0100 | [diff] [blame^] | 31 | repos_list = {} |
| 32 | for project in projects: |
| 33 | repos_list[project] = { |
| 34 | "url": "{}/{}".format(ONAP_CGIT, project), |
| 35 | "url-pattern": { |
| 36 | "base-url": "{url}/tree/{path}{anchor}", |
| 37 | "anchor": "#n{line}" |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return repos_list |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def main(): |
| 45 | """Main entry point for the script.""" |
| 46 | repos = create_repos_list(get_projects_list()) |
| 47 | config = { |
| 48 | "max-concurrent-indexers": 2, |
| 49 | "dbpath": "data", |
| 50 | "health-check-uri": "/healthz", |
| 51 | "repos": repos |
| 52 | } |
| 53 | print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': '))) |
| 54 | |
| 55 | |
| 56 | if __name__ == '__main__': |
| 57 | sys.exit(main()) |