-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathget_community_skills.py
83 lines (71 loc) · 2.29 KB
/
get_community_skills.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# vim: set fileencoding=utf-8:
"""
This script gets the data from the Github API to get the names and
languages of all the repositories of the 'Creative Commons' organization
and generate the required skills.json
"""
# Standard library
import json
import logging
import os
import sys
import traceback
# Third-party
from github import Github
logger = logging.getLogger("sync_community_skills")
GITHUB_ORGANIZATION = "creativecommons"
GITHUB_TOKEN = os.environ["ADMIN_GITHUB_TOKEN"]
class ScriptError(Exception):
def __init__(self, message, code=None):
self.code = code if code else 1
message = "({}) {}".format(self.code, message)
super(ScriptError, self).__init__(message)
def generate_databag():
"""
This method pulls the names and languages from the 'PyGithub'
and loads them into the databag after a little formatting and
then this databag will be used to generate the required skills.json
The databag schema is down below:
databag schema
{
"name": "",
"languages": []
}
"""
print("Pulling from OS@CC...")
github_client = Github(GITHUB_TOKEN)
cc = github_client.get_organization(GITHUB_ORGANIZATION)
repos = list(cc.get_repos())
if not repos:
raise ScriptError(
"Unable to setup the Github Client to get the requested"
" Github organization and the repos of that organization"
)
repos.sort(key=lambda repo: repo.name)
data = []
for repo in repos:
data.append({"name": repo.name, "languages": repo.get_languages()})
return data
def generate_skills():
"""
Writing the result array into skills.json file
"""
print(json.dumps(generate_databag(), indent=2, sort_keys=True))
if __name__ == "__main__":
try:
generate_skills()
except SystemExit as e:
sys.exit(e.code)
except KeyboardInterrupt:
logger.log(logging.INFO, "Halted via KeyboardInterrupt.")
sys.exit(130)
except ScriptError:
error_type, error_value, error_traceback = sys.exc_info()
logger.log(logging.CRITICAL, f"{error_value}")
sys.exit(error_value.code)
except Exception:
logger.log(
logging.ERROR, f"Unhandled exception: {traceback.format_exc()}"
)
sys.exit(1)