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
|
|
|
"""
|
|
|
|
|
2017-02-05 21:24:28 +01:00
|
|
|
# check for the venv
|
|
|
|
from lib import sanity_check
|
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
2018-05-14 00:53:45 +02:00
|
|
|
from typing import Any, Dict, List, Optional, Union, cast
|
2017-11-16 14:05:26 +01:00
|
|
|
from mypy_extensions import TypedDict
|
2017-01-06 18:56:36 +01:00
|
|
|
|
|
|
|
import os
|
2018-07-18 23:50:16 +02:00
|
|
|
import shutil
|
2017-01-06 18:56:36 +01:00
|
|
|
import sys
|
|
|
|
import argparse
|
2017-11-16 14:05:26 +01:00
|
|
|
from time import sleep
|
2017-01-06 18:56:36 +01:00
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
import requests
|
2017-10-12 07:54:25 +02:00
|
|
|
import json
|
2017-01-06 18:56:36 +01:00
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
|
|
|
|
from django.conf import settings
|
|
|
|
from zerver.lib.utils import split_by
|
|
|
|
|
2018-04-19 20:17:24 +02:00
|
|
|
FIXTURE_FILE = os.path.join(os.path.dirname(__file__), '../zerver/tests/fixtures/authors.json')
|
|
|
|
duplicate_commits_file = os.path.join(os.path.dirname(__file__),
|
|
|
|
'../zerver/tests/fixtures/duplicate_commits.json')
|
2017-01-06 18:56:36 +01:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--max-retries', type=int, default=3,
|
|
|
|
help='Number of times to retry fetching data from Github')
|
|
|
|
# In Travis CI and development environment, we use test fixture to avoid
|
|
|
|
# fetching from Github constantly.
|
|
|
|
parser.add_argument('--use-fixture', action='store_true', default=False,
|
|
|
|
help='Use fixture data instead of fetching from Github')
|
2017-01-24 07:19:25 +01:00
|
|
|
parser.add_argument('--not-required', action='store_true', default=False,
|
|
|
|
help='Consider failures to reach GitHub nonfatal')
|
2017-01-06 18:56:36 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-11-20 21:49:03 +01:00
|
|
|
|
2017-11-16 14:05:26 +01:00
|
|
|
ContributorsJSON = TypedDict('ContributorsJSON', {
|
|
|
|
'date': str,
|
2017-11-20 21:49:03 +01:00
|
|
|
'contrib': List[Dict[str, Union[str, int]]],
|
2017-11-16 14:05:26 +01:00
|
|
|
})
|
|
|
|
|
2017-01-06 18:56:36 +01:00
|
|
|
|
2017-11-16 14:05:26 +01:00
|
|
|
def fetch_contributors(repo_link: str) -> Optional[List[Dict[str, Dict[str, Any]]]]:
|
2018-08-02 16:29:47 +02:00
|
|
|
r = requests.get(repo_link, verify=os.environ.get('CUSTOM_CA_CERTIFICATES')) # type: requests.Response
|
2017-11-16 14:05:26 +01:00
|
|
|
return r.json() if r.status_code == 200 else None
|
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:
|
|
|
|
try:
|
2018-06-02 01:36:58 +02:00
|
|
|
f.write("{}\n".format(json.dumps(json_data, indent=2, sort_keys=True)))
|
2017-01-06 18:56:36 +01:00
|
|
|
except IOError as e:
|
|
|
|
print(e)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-11-16 14:05:26 +01:00
|
|
|
|
|
|
|
def run_production() -> 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
|
|
|
"""
|
2017-11-16 14:05:26 +01:00
|
|
|
repositories = {
|
|
|
|
'server': 'https://api.github.com/repos/zulip/zulip/stats/contributors',
|
|
|
|
'desktop': 'https://api.github.com/repos/zulip/zulip-electron/stats/contributors',
|
|
|
|
'mobile': 'https://api.github.com/repos/zulip/zulip-mobile/stats/contributors',
|
|
|
|
'python-zulip-api': 'https://api.github.com/repos/zulip/python-zulip-api/stats/contributors',
|
2018-01-15 15:54:22 +01:00
|
|
|
'zulip-js': 'https://api.github.com/repos/zulip/zulip-js/stats/contributors',
|
2017-11-16 14:05:26 +01:00
|
|
|
'zulipbot': 'https://api.github.com/repos/zulip/zulipbot/stats/contributors',
|
2018-03-23 00:23:38 +01:00
|
|
|
'terminal': 'https://api.github.com/repos/zulip/zulip-terminal/stats/contributors',
|
2017-11-16 14:05:26 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:49:03 +01:00
|
|
|
data = dict(date=str(date.today()), contrib=[]) # type: ContributorsJSON
|
|
|
|
contribs_list = {} # type: Dict[str, Dict[str, Union[str, int]]]
|
2017-11-16 14:05:26 +01:00
|
|
|
|
2017-11-20 21:49:03 +01:00
|
|
|
for _ in range(args.max_retries):
|
|
|
|
repos_done = []
|
|
|
|
for name, link in repositories.items():
|
|
|
|
contribs = fetch_contributors(link)
|
2017-11-16 14:05:26 +01:00
|
|
|
if contribs:
|
2017-11-20 21:49:03 +01:00
|
|
|
repos_done.append(name)
|
2017-11-16 14:05:26 +01:00
|
|
|
for contrib in contribs:
|
2018-03-23 17:31:23 +01:00
|
|
|
assert contrib is not None # TODO: To improve/clarify
|
|
|
|
|
|
|
|
author = contrib.get('author')
|
|
|
|
if author is None:
|
2018-02-08 21:16:14 +01:00
|
|
|
# This happens for users who've deleted their GitHub account.
|
|
|
|
continue
|
2018-03-23 17:31:23 +01:00
|
|
|
|
|
|
|
username = author.get('login')
|
|
|
|
assert username is not None # TODO: To improve/clarify
|
|
|
|
|
|
|
|
avatar = author.get('avatar_url')
|
|
|
|
assert avatar is not None # TODO: To improve/clarify
|
|
|
|
total = contrib.get('total')
|
|
|
|
assert total is not None # TODO: To improve/clarify
|
|
|
|
|
2017-11-16 14:05:26 +01:00
|
|
|
contrib_data = {
|
2018-03-23 17:31:23 +01:00
|
|
|
'avatar': avatar,
|
|
|
|
name: total,
|
2017-11-16 14:05:26 +01:00
|
|
|
}
|
2017-11-20 21:49:03 +01:00
|
|
|
if username in contribs_list:
|
|
|
|
contribs_list[username].update(contrib_data)
|
2017-11-16 14:05:26 +01:00
|
|
|
else:
|
2017-11-20 21:49:03 +01:00
|
|
|
contribs_list[username] = contrib_data
|
2018-03-03 13:21:55 +01:00
|
|
|
|
|
|
|
# remove duplicate contributions count
|
|
|
|
# find commits at the time of split and substract from zulip-server
|
|
|
|
with open(duplicate_commits_file, 'r') as f:
|
|
|
|
duplicate_commits = json.loads(f.read())
|
|
|
|
for committer in duplicate_commits:
|
2018-03-22 22:57:22 +01:00
|
|
|
if committer in contribs_list and contribs_list[committer].get('server'):
|
2018-03-03 13:21:55 +01:00
|
|
|
total_commits = cast(int, contribs_list[committer]['server'])
|
|
|
|
duplicate_commits_count = duplicate_commits[committer]
|
|
|
|
original_commits = total_commits - duplicate_commits_count
|
|
|
|
contribs_list[committer]['server'] = original_commits
|
|
|
|
|
2017-11-20 21:49:03 +01:00
|
|
|
for repo in repos_done:
|
|
|
|
del repositories[repo]
|
2017-11-16 14:05:26 +01:00
|
|
|
|
|
|
|
if not repositories:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Wait before retrying failed requests for Github to aggregate data.
|
|
|
|
sleep(2)
|
|
|
|
else:
|
|
|
|
print("ERROR: Failed fetching contributors data from Github.")
|
|
|
|
if not args.not_required:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-11-20 21:49:03 +01:00
|
|
|
for contributor_name, contributor_data in contribs_list.items():
|
|
|
|
contributor_data['name'] = contributor_name
|
|
|
|
data['contrib'].append(contributor_data)
|
2017-11-16 14:05:26 +01:00
|
|
|
|
|
|
|
write_to_disk(data, settings.CONTRIBUTORS_DATA)
|
|
|
|
|
|
|
|
|
|
|
|
def copy_fixture() -> None:
|
2017-01-06 18:56:36 +01:00
|
|
|
"""
|
2018-04-19 20:17:24 +02:00
|
|
|
Copy test fixture file from zerver/tests/fixtures. This is used to avoid
|
2017-01-06 18:56:36 +01:00
|
|
|
constantly fetching data from Github during testing.
|
|
|
|
"""
|
2018-07-18 23:50:16 +02:00
|
|
|
shutil.copyfile(FIXTURE_FILE, settings.CONTRIBUTORS_DATA)
|
2017-01-06 18:56:36 +01:00
|
|
|
|
2017-11-16 14:05:26 +01:00
|
|
|
|
2017-01-06 18:56:36 +01:00
|
|
|
if args.use_fixture:
|
|
|
|
copy_fixture()
|
|
|
|
else:
|
|
|
|
run_production()
|