-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdecorator.py
executable file
·76 lines (53 loc) · 1.82 KB
/
decorator.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
#! /bin/env python
#
# github decorator
# \m/ 2013, skazhy
import os
import fileinput
from datetime import date, timedelta
from subprocess import Popen
REPO = "decorator"
repodir = os.path.join(os.path.dirname(__file__), REPO)
densities = {".": 1, "-": 10, "+": 20, "#": 30}
class HumbleList(list):
def __getitem__(self, i):
try:
return super(HumbleList, self).__getitem__(i)
except IndexError:
return " "
def transform_matrix(matrix):
rows = max(len(r) for r in matrix)
return [[matrix[c][r] for c in range(7)] for r in range(rows)]
def exe(*cmds, **kwargs):
cwd = kwargs.get("cwd", repodir)
for cmd in cmds:
pid = Popen(["/bin/sh", "-c", cmd], cwd=cwd).pid
while(os.waitpid(pid, 0)[1] != 0):
continue
return
def create_repo():
exe("git init --quiet %s" % REPO, cwd=None)
def commit(day, minutes):
timestamp = day.strftime("%Y.%m.%d " + "16:%02i:00 +0200" % minutes)
filename = "coolfile"
exe("echo '%s' > %s" % (timestamp, filename),
"git add %s" % filename,
"git commit -m '%s' --date='%s' " % (timestamp, timestamp))
def closest_sunday(day):
# GitHub weeks start on Sundays. *americans*
return day - timedelta(days=day.weekday() + 1 % 7)
def decorate(matrix):
create_repo()
d = closest_sunday(date.today() - timedelta(weeks=len(matrix)-1))
for row in matrix:
for col in row:
minutes = 0
for _ in range(densities.get(col, 0)):
commit(d, minutes)
minutes += 1
d += timedelta(days=1)
if __name__ == "__main__":
stdin_matrix = [HumbleList(line[:-1]) for line in fileinput.input()]
for _ in range(len(stdin_matrix), 7):
stdin_matrix.append(HumbleList([None]))
decorate(transform_matrix(stdin_matrix))