-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
162 lines (130 loc) · 4.89 KB
/
run.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
import argparse
import datetime
import json
from pathlib import Path
import os
from typing import List
from tqdm import tqdm
from src.gharchive import GHArchive
from src.good_messages import GoodMessages
from src.update_index import update_index, get_message_files
ROOT_DIR = Path(__file__).resolve().parent
def parse_args() -> dict:
parser = argparse.ArgumentParser()
parser.add_argument(
"days", type=str, nargs="*",
help="Days to scan, either YYYY[-MM[-DD]] or 'yesterday'"
)
parser.add_argument(
"--path", type=str, nargs="?", default="gharchive-dump",
help="The path where to store the event ndjson files (needs a lot of space!)"
)
parser.add_argument(
"-f", "--force", type=bool, nargs="?", default=False, const=True,
help="Force re-generation of the markdown files, even if existing",
)
parser.add_argument(
"-s", "--stash", type=bool, nargs="?", default=False, const=True,
help="Use the stash, Luke! If stash file is present, render the markdown from there",
)
parser.add_argument(
"-v", "--verbose", type=bool, nargs="?", default=False, const=True,
help="Display stuff during run"
)
args = vars(parser.parse_args())
days = set()
for day in args["days"]:
if day == "yesterday":
days.add(datetime.date.today() - datetime.timedelta(days=1))
else:
if len(day) == 10:
days.add(datetime.datetime.strptime(day, "%Y-%m-%d").date())
elif len(day) == 7:
date = datetime.datetime.strptime(day, "%Y-%m").date()
month = date.month
while date.month == month:
days.add(date)
date += datetime.timedelta(days=1)
elif len(day) == 4:
date = datetime.datetime.strptime(day, "%Y").date()
year = date.year
while date.year == year:
days.add(date)
date += datetime.timedelta(days=1)
else:
raise ValueError(f"Invalid date '{day}'")
args["days"] = sorted(days)
return args
def main(
days: List[datetime.date],
path: str,
verbose: bool,
force: bool,
stash: bool,
):
existing_files = get_message_files()
existing_dates = [
datetime.date(int(d[0]), int(d[1].lstrip("0")), int(d[2].lstrip("0")))
for d, fn in existing_files
]
archive = GHArchive(
raw_path=path,
verbose=verbose,
)
written_hashes = set()
for day in days:
if not force:
if day in existing_dates:
if verbose:
print(f"Skipping {day}, use -f to overwrite")
continue
proc = GoodMessages(verbose=verbose)
proc.written_hashes = written_hashes
key_interrupt = False
stash_file = (ROOT_DIR / "stash" / str(day)[:4] / f"{str(day)[:10]}.json")
if stash and stash_file.exists():
proc.load_stash(stash_file)
else:
try:
iterable = archive.iter_events(day, event_type="PushEvent")
if verbose:
iterable = tqdm(iterable, desc=f"parsing events {str(day)[:10]}")
for i, event in enumerate(iterable):
proc.process_event(event)
if verbose and i % 100_000 == 0:
print()
proc.dump_stats()
except KeyboardInterrupt:
break
# for testing/development continue here and export a partial day
key_interrupt = True
if not stash_file.parent.exists():
os.makedirs(stash_file.parent)
proc.store_stash(stash_file)
proc.store_words(stash_file.parent / f"{str(day)[:10]}-words.json")
proc.remove_duplicates()
if verbose:
print(f"{day}: {len(proc.commits)} commits")
md_file = (ROOT_DIR / "docs" / "good-messages" / str(day)[:4] / f"{str(day)[:10]}.md")
if not md_file.parent.exists():
os.makedirs(md_file.parent)
day_links = [
day - datetime.timedelta(days=1),
day + datetime.timedelta(days=1),
]
for i, d in enumerate(day_links):
if d.year != d.year:
day_links[i] = f"../{d.year}/{str(d)[:10]}.md"
else:
day_links[i] = f"{str(d)[:10]}.md"
md_file.write_text(
f"# [<]({day_links[0]}) {str(day)[:10]} [>]({day_links[1]})\n\n"
+ proc.render_stats_markdown() + "\n\n"
+ proc.render_markdown()
+ f"\n---\n\n# [<]({day_links[0]}) {str(day)[:10]} [>]({day_links[1]})\n\n"
)
if key_interrupt:
break
update_index(verbose=verbose)
if __name__ == "__main__":
main(**parse_args())