-
Notifications
You must be signed in to change notification settings - Fork 3
/
gddownloader.py
173 lines (155 loc) · 6.98 KB
/
gddownloader.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import sys
import traceback
from logger.downloadlogger import Logger
from datamanager.dbmanager import DBManager
from downloader.gitdownloader import GitDownloader
from datamanager.mongomanager import MongoDBManager
from downloader.githubdownloader import GithubDownloader
from helpers import get_number_of, print_usage, read_file_in_lines
from properties import GitHubAuthToken, dataFolderPath, gitExecutablePath, verbose, \
download_issues, download_issue_comments, download_issue_events, \
download_commits, download_commit_comments, download_source_code, \
download_issues_full, download_commits_full, download_contributors, \
update_existing_repos, use_database
# Initialize all required objects
db = MongoDBManager() if use_database == 'mongo' else DBManager()
lg = Logger(verbose)
ghd = GithubDownloader(GitHubAuthToken)
gd = GitDownloader(gitExecutablePath, lg, GitHubAuthToken)
def download_repo(repo_address):
"""
Downloads all the data of a repository given its GitHub URL.
:param repo_address: the URL of the repository of which the data are downloaded.
"""
repo_api_address = "https://api.github.com/repos/" + '/'.join(repo_address.split('/')[-2:])
repo_name = '_'.join(repo_address.split('/')[-2:])
lg.log_action("Downloading project " + repo_name)
if db.project_exists(repo_name):
if update_existing_repos:
lg.log_action("Project already exists! Updating...")
else:
lg.log_action("Project already exists! Skipping...")
return
db.initialize_write_to_disk(repo_name)
project = db.read_project_from_disk(repo_name)
try:
project_info = ghd.download_object(repo_api_address)
project.add_info(project_info)
db.write_project_info_to_disk(repo_name, project["info"])
lg.start_action("Retrieving project statistics...", 6)
project_stats = {}
project_stats["issues"] = get_number_of(ghd, repo_api_address, "issues", "state=all")
lg.step_action()
project_stats["issue_comments"] = get_number_of(ghd, repo_api_address, "issues/comments")
lg.step_action()
project_stats["issue_events"] = get_number_of(ghd, repo_api_address, "issues/events")
lg.step_action()
project_stats["commits"] = get_number_of(ghd, repo_api_address, "commits")
lg.step_action()
project_stats["commit_comments"] = get_number_of(ghd, repo_api_address, "comments")
lg.step_action()
project_stats["contributors"] = get_number_of(ghd, repo_api_address, "contributors")
lg.step_action()
project.add_stats(project_stats)
lg.end_action()
db.write_project_stats_to_disk(repo_name, project["info"], project["stats"])
if download_issues:
lg.start_action("Retrieving issues...", project_stats["issues"])
repo_issues_address = repo_api_address + "/issues"
for issue in ghd.download_paginated_object(repo_issues_address, ["state=all"]):
if not project.issue_exists(issue):
if download_issues_full:
if issue["state"] == "closed":
issue = ghd.download_object(repo_issues_address + "/" + str(issue["number"]))
else:
issue["closed_by"] = None
project.add_issue(issue)
db.write_project_issue_to_disk(repo_name, issue)
else:
if download_issues_full and not project.full_issue_exists(issue):
if issue["state"] == "closed":
issue = ghd.download_object(repo_issues_address + "/" + str(issue["number"]))
else:
issue["closed_by"] = None
project.add_issue(issue)
db.write_project_issue_to_disk(repo_name, issue)
lg.step_action()
lg.end_action()
if download_issue_comments:
lg.start_action("Retrieving issue comments...", project_stats["issue_comments"])
repo_issue_comments_address = repo_issues_address + "/comments"
for issue_comment in ghd.download_paginated_object(repo_issue_comments_address):
if not project.issue_comment_exists(issue_comment):
project.add_issue_comment(issue_comment)
db.write_project_issue_comment_to_disk(repo_name, issue_comment)
lg.step_action()
lg.end_action()
if download_issue_events:
lg.start_action("Retrieving issue events...", project_stats["issue_events"])
repo_issue_events_address = repo_issues_address + "/events"
for issue_event in ghd.download_paginated_object(repo_issue_events_address):
if not project.issue_event_exists(issue_event):
project.add_issue_event(issue_event)
db.write_project_issue_event_to_disk(repo_name, issue_event)
lg.step_action()
lg.end_action()
if download_commits:
lg.start_action("Retrieving commits...", project_stats["commits"])
repo_commits_address = repo_api_address + "/commits"
for commit in ghd.download_paginated_object(repo_commits_address):
if not project.commit_exists(commit):
if download_commits_full:
commit = ghd.download_object(repo_commits_address + "/" + str(commit["sha"]))
project.add_commit(commit)
db.write_project_commit_to_disk(repo_name, commit)
else:
if download_commits_full and not project.full_commit_exists(commit):
commit = ghd.download_object(repo_commits_address + "/" + str(commit["sha"]))
project.add_commit(commit)
db.write_project_commit_to_disk(repo_name, commit)
lg.step_action()
lg.end_action()
if download_commit_comments:
lg.start_action("Retrieving commit comments...", project_stats["commit_comments"])
repo_commit_comments_address = repo_api_address + "/comments"
for commit_comment in ghd.download_paginated_object(repo_commit_comments_address):
if not project.commit_comment_exists(commit_comment):
project.add_commit_comment(commit_comment)
db.write_project_commit_comment_to_disk(repo_name, commit_comment)
lg.step_action()
lg.end_action()
if download_contributors:
lg.start_action("Retrieving contributors...", project_stats["contributors"])
repo_contributors_address = repo_api_address + "/contributors"
for contributor in ghd.download_paginated_object(repo_contributors_address):
if not project.contributor_exists(contributor):
project.add_contributor(contributor)
db.write_project_contributor_to_disk(repo_name, contributor)
lg.step_action()
lg.end_action()
if download_source_code:
lg.start_action("Retrieving source code...")
git_repo_path = os.path.join(dataFolderPath, repo_name, "sourcecode")
if not gd.git_repo_exists(git_repo_path):
gd.git_clone(repo_address, git_repo_path)
else:
gd.git_pull(git_repo_path)
lg.end_action()
except Exception:
# Catch any exception and print it before exiting
sys.exit(traceback.format_exc())
finally:
# This line of code is always executed even if an exception occurs
db.finalize_write_to_disk(repo_name, project)
if __name__ == "__main__":
if ((not sys.argv) or len(sys.argv) <= 1):
print_usage()
elif(sys.argv[1].startswith("https://github.com")):
download_repo(sys.argv[1])
elif(os.path.exists(sys.argv[1])):
repos = read_file_in_lines(sys.argv[1])
for repo in repos:
download_repo(repo)
else:
print_usage()