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 | 8c7373d | 2020-01-07 15:28:55 +0100 | [diff] [blame^] | 11 | DEFAULT_GERRIT = "gerrit.onap.org" |
| 12 | API_PREFIX = "/r" |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 13 | API_PROJECTS = "/projects/" |
| 14 | |
| 15 | MAGIC_PREFIX = ")]}'" |
| 16 | |
Pawel Wieczorek | 5af7684 | 2020-01-07 15:28:53 +0100 | [diff] [blame] | 17 | GITWEB_ANCHOR = "#l{line}" |
| 18 | GIT_ANCHOR = "#n{line}" |
| 19 | |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 20 | |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 21 | def get_projects_list(gerrit): |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 22 | """Request list of all available projects from ONAP Gerrit.""" |
Pawel Wieczorek | 8c7373d | 2020-01-07 15:28:55 +0100 | [diff] [blame^] | 23 | resp = urllib.request.urlopen("https://{}{}{}".format(gerrit, API_PREFIX, API_PROJECTS)) |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 24 | resp_body = resp.read() |
| 25 | |
| 26 | no_magic = resp_body[len(MAGIC_PREFIX):] |
| 27 | decoded = no_magic.decode("utf-8") |
| 28 | projects = json.loads(decoded) |
| 29 | |
| 30 | return projects.keys() |
| 31 | |
| 32 | |
Pawel Wieczorek | 8c7373d | 2020-01-07 15:28:55 +0100 | [diff] [blame^] | 33 | def create_repos_list(projects, gerrit, ssh, git): |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 34 | """Create a map of all projects to their repositories' URLs.""" |
Pawel Wieczorek | 8c7373d | 2020-01-07 15:28:55 +0100 | [diff] [blame^] | 35 | gerrit_url = "https://{}{}".format(gerrit, API_PREFIX) |
| 36 | gerrit_project_url = "{}/{{}}.git".format(gerrit_url) |
| 37 | gitweb_code_url = "{}/gitweb?p={{}}.git;hb=HEAD;a=blob;f={{path}}{{anchor}}".format(gerrit_url) |
Pawel Wieczorek | 5af7684 | 2020-01-07 15:28:53 +0100 | [diff] [blame] | 38 | |
Pawel Wieczorek | e7c5ed5 | 2020-01-07 15:28:37 +0100 | [diff] [blame] | 39 | repos_list = {} |
| 40 | for project in projects: |
Pawel Wieczorek | 8c7373d | 2020-01-07 15:28:55 +0100 | [diff] [blame^] | 41 | project_url = gerrit_project_url.format(project) |
| 42 | code_url = gitweb_code_url.format(project) |
| 43 | anchor = GITWEB_ANCHOR |
| 44 | |
| 45 | if ssh and len(ssh) == 2: |
| 46 | user, port = ssh[0], ssh[1] |
| 47 | project_url = "ssh://{}@{}:{}/{}.git".format(user, gerrit, port, project) |
| 48 | if git: |
| 49 | code_url = "https://{}/{}/tree/{{path}}{{anchor}}".format(git, project) |
Pawel Wieczorek | 5af7684 | 2020-01-07 15:28:53 +0100 | [diff] [blame] | 50 | anchor = GIT_ANCHOR |
| 51 | |
Pawel Wieczorek | e7c5ed5 | 2020-01-07 15:28:37 +0100 | [diff] [blame] | 52 | repos_list[project] = { |
Pawel Wieczorek | 5af7684 | 2020-01-07 15:28:53 +0100 | [diff] [blame] | 53 | "url": project_url, |
Pawel Wieczorek | e7c5ed5 | 2020-01-07 15:28:37 +0100 | [diff] [blame] | 54 | "url-pattern": { |
Pawel Wieczorek | 5af7684 | 2020-01-07 15:28:53 +0100 | [diff] [blame] | 55 | "base-url": code_url, |
| 56 | "anchor": anchor |
Pawel Wieczorek | e7c5ed5 | 2020-01-07 15:28:37 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | return repos_list |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 61 | |
| 62 | |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 63 | def parse_arguments(): |
| 64 | """Return parsed command-line arguments.""" |
| 65 | parser = argparse.ArgumentParser(description=__doc__) |
| 66 | parser.add_argument('--gerrit', help='Gerrit address', default=DEFAULT_GERRIT) |
Pawel Wieczorek | 8c7373d | 2020-01-07 15:28:55 +0100 | [diff] [blame^] | 67 | parser.add_argument('--ssh', help='SSH information: user, port', nargs=2) |
| 68 | parser.add_argument('--git', help='external git address') |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 69 | |
| 70 | return parser.parse_args() |
| 71 | |
| 72 | |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 73 | def main(): |
| 74 | """Main entry point for the script.""" |
Pawel Wieczorek | e2aa848 | 2020-01-07 15:28:51 +0100 | [diff] [blame] | 75 | arguments = parse_arguments() |
| 76 | |
Pawel Wieczorek | 5af7684 | 2020-01-07 15:28:53 +0100 | [diff] [blame] | 77 | projects = get_projects_list(arguments.gerrit) |
Pawel Wieczorek | 8c7373d | 2020-01-07 15:28:55 +0100 | [diff] [blame^] | 78 | repos = create_repos_list(projects, arguments.gerrit, arguments.ssh, arguments.git) |
Pawel Wieczorek | 9ee1258 | 2020-01-07 15:26:52 +0100 | [diff] [blame] | 79 | config = { |
| 80 | "max-concurrent-indexers": 2, |
| 81 | "dbpath": "data", |
| 82 | "health-check-uri": "/healthz", |
| 83 | "repos": repos |
| 84 | } |
| 85 | print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': '))) |
| 86 | |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | sys.exit(main()) |