py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-01-06 18:56:36 +01:00
|
|
|
"""
|
|
|
|
Fetch contributors data from Github using their API, convert it to structured
|
2017-10-31 20:08:32 +01:00
|
|
|
JSON data for the /team page contributors section.
|
2017-01-06 18:56:36 +01:00
|
|
|
"""
|
2020-04-12 21:13:15 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
from scripts.lib.setup_path import setup_path
|
2017-02-05 21:24:28 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
setup_path()
|
2017-01-06 18:56:36 +01:00
|
|
|
|
|
|
|
import argparse
|
2020-06-11 00:54:34 +02:00
|
|
|
import logging
|
2017-01-06 18:56:36 +01:00
|
|
|
from datetime import date
|
2020-04-08 19:20:46 +02:00
|
|
|
from random import randrange
|
2020-06-11 00:54:34 +02:00
|
|
|
from time import sleep
|
2020-07-23 13:11:12 +02:00
|
|
|
from typing import Dict, List, Union
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from typing_extensions import TypedDict
|
2017-01-06 18:56:36 +01:00
|
|
|
|
2020-04-07 19:27:07 +02:00
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
|
2020-04-08 19:20:46 +02:00
|
|
|
import django
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-04-08 19:20:46 +02:00
|
|
|
django.setup()
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import json
|
2020-04-07 19:27:07 +02:00
|
|
|
|
2017-01-06 18:56:36 +01:00
|
|
|
import requests
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
2017-01-06 18:56:36 +01:00
|
|
|
|
2020-04-06 16:20:41 +02:00
|
|
|
duplicate_commits_file = os.path.join(os.path.dirname(__file__), 'duplicate_commits.json')
|
2017-01-06 18:56:36 +01:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2020-04-08 19:20:46 +02:00
|
|
|
parser.add_argument('--max-retries', type=int, default=10,
|
2017-01-06 18:56:36 +01:00
|
|
|
help='Number of times to retry fetching data from Github')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-05-02 06:24:43 +02:00
|
|
|
class ContributorsJSON(TypedDict):
|
|
|
|
date: str
|
2020-07-23 13:11:12 +02:00
|
|
|
contrib: List[Dict[str, Union[int, str]]]
|
|
|
|
|
|
|
|
class Contributor(TypedDict):
|
|
|
|
avatar_url: str
|
|
|
|
contributions: int
|
|
|
|
login: str
|
2017-11-16 14:05:26 +01:00
|
|
|
|
2020-04-08 19:20:46 +02:00
|
|
|
logger = logging.getLogger('zulip.fetch_contributors_json')
|
|
|
|
|
2020-07-23 13:11:12 +02:00
|
|
|
def fetch_contributors(repo_link: str, max_retries: int) -> List[Contributor]:
|
|
|
|
contributors: List[Contributor] = []
|
|
|
|
retry_attempts = 0
|
|
|
|
page_index = 1
|
|
|
|
|
|
|
|
while True:
|
|
|
|
response: requests.Response = requests.get(f"{repo_link}?page={page_index}", verify=os.environ.get('CUSTOM_CA_CERTIFICATES'))
|
|
|
|
if response.status_code == 200:
|
|
|
|
data = response.json()
|
|
|
|
if len(data) == 0:
|
|
|
|
return contributors
|
|
|
|
contributors.extend(data)
|
|
|
|
retry_attempts = 0
|
|
|
|
page_index += 1
|
|
|
|
else:
|
|
|
|
retry_attempts += 1
|
|
|
|
if retry_attempts > args.max_retries:
|
|
|
|
logger.warning("Failed retries fetching contributors data from Github.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
sleep_time = randrange(0, min(64, 2**retry_attempts))
|
|
|
|
sleep(sleep_time)
|
2017-01-06 18:56:36 +01:00
|
|
|
|
2017-11-16 14:05:26 +01:00
|
|
|
def write_to_disk(json_data: ContributorsJSON, out_file: str) -> None:
|
2017-01-06 18:56:36 +01:00
|
|
|
with open(out_file, 'w') as f:
|
2020-06-12 01:35:37 +02:00
|
|
|
json.dump(json_data, f, indent=2, sort_keys=True)
|
|
|
|
f.write("\n")
|
2017-01-06 18:56:36 +01:00
|
|
|
|
2020-04-07 19:27:07 +02:00
|
|
|
def update_contributor_data_file() -> None:
|
2017-01-06 18:56:36 +01:00
|
|
|
"""
|
2017-11-20 21:49:03 +01:00
|
|
|
Get contributors data from Github and insert them into a temporary
|
|
|
|
dictionary. Retry fetching each repository if responded with non HTTP 200
|
|
|
|
status.
|
2017-01-06 18:56:36 +01:00
|
|
|
"""
|
2018-08-25 19:16:33 +02:00
|
|
|
|
|
|
|
# This dictionary should hold all repositories that should be included in
|
|
|
|
# the total count, including those that should *not* have tabs on the team
|
|
|
|
# page (e.g. if they are deprecated).
|
2017-11-16 14:05:26 +01:00
|
|
|
repositories = {
|
2020-07-23 13:11:12 +02:00
|
|
|
'server': 'https://api.github.com/repos/zulip/zulip/contributors',
|
|
|
|
'desktop': 'https://api.github.com/repos/zulip/zulip-desktop/contributors',
|
|
|
|
'mobile': 'https://api.github.com/repos/zulip/zulip-mobile/contributors',
|
|
|
|
'python-zulip-api': 'https://api.github.com/repos/zulip/python-zulip-api/contributors',
|
|
|
|
'zulip-js': 'https://api.github.com/repos/zulip/zulip-js/contributors',
|
|
|
|
'zulipbot': 'https://api.github.com/repos/zulip/zulipbot/contributors',
|
|
|
|
'terminal': 'https://api.github.com/repos/zulip/zulip-terminal/contributors',
|
|
|
|
'zulip-ios-legacy': 'https://api.github.com/repos/zulip/zulip-ios-legacy/contributors',
|
|
|
|
'zulip-android': 'https://api.github.com/repos/zulip/zulip-android/contributors',
|
2017-11-16 14:05:26 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 01:09:50 +02:00
|
|
|
data: ContributorsJSON = dict(date=str(date.today()), contrib=[])
|
|
|
|
contribs_list: Dict[str, Dict[str, Union[str, int]]] = {}
|
2017-11-16 14:05:26 +01:00
|
|
|
|
2020-07-23 13:11:12 +02:00
|
|
|
for name, link in repositories.items():
|
|
|
|
contributors = fetch_contributors(link, args.max_retries)
|
|
|
|
for contributor in contributors:
|
|
|
|
username = contributor['login']
|
|
|
|
if username in contribs_list:
|
|
|
|
contribs_list[username][name] = contributor['contributions']
|
2020-04-08 19:20:46 +02:00
|
|
|
else:
|
2020-07-23 13:11:12 +02:00
|
|
|
contribs_list[username] = {
|
|
|
|
'avatar': contributor['avatar_url'],
|
|
|
|
'name': username,
|
|
|
|
name: contributor['contributions']
|
|
|
|
}
|
2017-11-16 14:05:26 +01:00
|
|
|
|
2020-04-21 21:07:39 +02:00
|
|
|
# remove duplicate contributions count
|
|
|
|
# find commits at the time of split and subtract from zulip-server
|
|
|
|
with open(duplicate_commits_file) as f:
|
|
|
|
duplicate_commits = json.load(f)
|
|
|
|
for committer in duplicate_commits:
|
|
|
|
if committer in contribs_list and contribs_list[committer].get('server'):
|
|
|
|
total_commits = contribs_list[committer]['server']
|
|
|
|
assert isinstance(total_commits, int)
|
|
|
|
duplicate_commits_count = duplicate_commits[committer]
|
|
|
|
original_commits = total_commits - duplicate_commits_count
|
|
|
|
contribs_list[committer]['server'] = original_commits
|
|
|
|
|
2020-07-23 13:11:12 +02:00
|
|
|
data['contrib'] = list(contribs_list.values())
|
2020-04-07 19:27:07 +02:00
|
|
|
write_to_disk(data, settings.CONTRIBUTOR_DATA_FILE_PATH)
|
2017-11-16 14:05:26 +01:00
|
|
|
|
2020-04-07 19:27:07 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
update_contributor_data_file()
|