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-07-27 03:05:45 +02:00
|
|
|
JSON data for the /about page authors 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__)
|
|
|
|
|
2017-01-06 18:56:36 +01:00
|
|
|
from typing import Any, Dict, List, Optional, Union, Text
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
from datetime import date
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
FIXTURE_FILE = os.path.join(os.path.dirname(__file__), '../zerver/fixtures/authors.json')
|
|
|
|
GITHUB_LINK = 'https://api.github.com/repos/zulip/zulip/stats/contributors'
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
def fetch_data(retries, link):
|
|
|
|
# type: (int, str) -> Optional[List[Dict[str, Any]]]
|
|
|
|
for _ in range(retries):
|
|
|
|
try:
|
|
|
|
r = requests.get(link) # type: requests.Response
|
|
|
|
if r.status_code == 200:
|
|
|
|
return r.json()
|
|
|
|
else:
|
|
|
|
print('Github API return non 200 status.')
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def write_to_disk(json_data, out_file):
|
|
|
|
# type: (Dict[str, Any], str) -> None
|
|
|
|
with open(out_file, 'w') as f:
|
|
|
|
try:
|
|
|
|
f.write("{}\n".format(json.dumps(json_data)))
|
|
|
|
except IOError as e:
|
|
|
|
print(e)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def run_production():
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Fetch data from Github and stored it in
|
|
|
|
`static/generated/github-contributors.json`
|
|
|
|
"""
|
|
|
|
json_data = fetch_data(args.max_retries, GITHUB_LINK) # type: Optional[List[Dict[str, Any]]]
|
|
|
|
if json_data:
|
|
|
|
# Successfully fetch data from Github
|
|
|
|
contribs = []
|
|
|
|
for user in json_data:
|
|
|
|
author = user.get('author')
|
2017-08-11 00:54:31 +02:00
|
|
|
if author is None:
|
2017-02-05 18:31:59 +01:00
|
|
|
print("Unable to access fields for %s" % (user,))
|
2017-08-11 00:54:31 +02:00
|
|
|
continue
|
|
|
|
result_user = dict(
|
|
|
|
avatar=author.get('avatar_url'),
|
|
|
|
name=author.get('login'),
|
|
|
|
commits=user.get('total')
|
|
|
|
)
|
|
|
|
contribs.append(result_user)
|
2017-01-06 18:56:36 +01:00
|
|
|
|
|
|
|
out_contrib_data = split_by(
|
|
|
|
sorted(contribs, key=lambda k: k.get('commits'), reverse=True),
|
2017-07-27 03:05:45 +02:00
|
|
|
6, None
|
2017-01-06 18:56:36 +01:00
|
|
|
) # type: List[List[Optional[Dict[str, Union[Text, int]]]]]
|
|
|
|
|
|
|
|
out_data = dict(
|
|
|
|
data=out_contrib_data,
|
|
|
|
date=str(date.today())
|
|
|
|
) # type: Dict[str, Any]
|
|
|
|
|
|
|
|
write_to_disk(out_data, settings.CONTRIBUTORS_DATA)
|
|
|
|
|
2017-01-24 07:19:25 +01:00
|
|
|
elif not args.not_required:
|
2017-01-06 18:56:36 +01:00
|
|
|
print('Fail to fetch data from Github.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def copy_fixture():
|
|
|
|
# type: () -> None
|
|
|
|
"""
|
|
|
|
Copy test fixture file from zerver/fixtures. This is used to avoid
|
|
|
|
constantly fetching data from Github during testing.
|
|
|
|
"""
|
|
|
|
subprocess.check_call(['cp', FIXTURE_FILE, settings.CONTRIBUTORS_DATA])
|
|
|
|
|
|
|
|
if args.use_fixture:
|
|
|
|
copy_fixture()
|
|
|
|
else:
|
|
|
|
run_production()
|