Fix plugins with MSVC, thanks to Gulam Faruque.
[syx.git] / scripts / announcer.py
blob22fa790c2f29c63d9a06746697054eefbd051029
1 #!/usr/bin/env python
2 import os
3 import sys
4 import string
6 # Tune this
8 FROM_MAIL = "lethalman88@gmail.com"
9 TO_MAIL = "syx-commit@googlegroups.com"
10 SENDMAIL = "esmtp -t"
12 # Some other config
14 DEBUG = False
15 LASTCOMMIT = '.announcer.lastcommit'
17 def git (command, stripped=True):
18 if DEBUG:
19 print "* "+command
20 return map (lambda x: x[:-1], os.popen("git "+command).readlines ())
22 def git_exit (command):
23 if DEBUG:
24 print "* "+command
25 return os.system("git %s &>/dev/null" % command)
27 def sendmail (mail):
28 return os.popen(SENDMAIL, "w").write (mail)
30 def error (message):
31 sys.stderr.write ("ERROR: "+message+"\n")
32 sys.exit (1)
34 class Rev (object):
35 def __init__ (self, name, verify=True):
36 self.name = name
37 if verify:
38 self.verify ()
40 def verify (self):
41 assert not git_exit ("rev-list -n 1 %s --" % self.name)
43 class Branch (Rev):
44 def checkout (self):
45 assert not git_exit ("checkout "+self.name)
47 def __repr__ (self):
48 return "Branch: %s" % (self.name)
50 def __str__ (self):
51 return self.name
53 class Commit (Rev):
54 email_tmpl = string.Template ("""From: $fromail
55 To: $tomail
56 Content-type: text/plain
57 Subject: $branch $hash
59 Branch: $branch
60 $whatchanged
62 $diff""")
64 def __init__ (self, name, branch, verify=True):
65 Rev.__init__ (self, name, verify)
66 self.branch = branch
68 def email_report (self):
69 whatchanged = self.whatchanged ()
70 diff = self.diff (self.ancestor ())
71 return self.email_tmpl.substitute (fromail=FROM_MAIL, tomail=TO_MAIL,
72 branch=self.branch.name, hash=self.name,
73 whatchanged=whatchanged,
74 diff=diff)
76 def whatchanged (self):
77 out = git ("whatchanged -n 1 %s" % self)
78 return '\n'.join (out)
80 def diff (self, commit):
81 out = git ("diff %s %s" % (commit, self))
82 return '\n'.join (out)
84 def ancestor (self):
85 out = git ("rev-list -n 1 %s^" % self.name)
86 return Commit (out[0], self.branch, False)
88 def xnew_commits (self):
89 out = git ("rev-list %s..%s" % (self.name, self.branch.name))
90 for hash in out:
91 yield Commit (hash, self.branch)
93 def __repr__ (self):
94 return "%s %s" % (self.branch, self.name)
96 def __str__ (self):
97 return self.name
99 def fetch_last_commits ():
100 last_commits = []
101 f = file (LASTCOMMIT)
102 for line in f.xreadlines ():
103 s = filter (lambda x: x.strip(), line.split (' '))
104 branch = Branch (s[0].strip ())
105 last_commits.append (Commit (s[1].strip(), branch))
106 f.close ()
107 return last_commits
109 def write_last_commits (commits):
110 f = file (LASTCOMMIT, "w")
111 for commit in commits:
112 f.write (repr (commit) + "\n")
113 f.close ()
115 dryrun = False
116 nopull = False
117 if len(sys.argv) > 1:
118 if '-n' in sys.argv:
119 nopull = True
120 if '-d' in sys.argv:
121 dryrun = True
122 if '-h' in sys.argv:
123 error ("Use -n to not pull. Use -d to dry run")
125 if not nopull:
126 assert not git_exit ("pull")
128 last_commits = fetch_last_commits ()
129 new_last_commits = []
130 for last_commit in last_commits:
131 new_commits = list (reversed (list (last_commit.xnew_commits ())))
132 last_commit.branch.checkout ()
133 for commit in new_commits:
134 print "* Sendmail %r" % commit
135 if not dryrun:
136 sendmail (commit.email_report ())
137 if new_commits:
138 new_last_commits.append (new_commits[-1])
139 else:
140 new_last_commits.append (last_commit)
142 if not dryrun:
143 write_last_commits (new_last_commits)