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