Code:
#!/usr/local/bin/python
import os
import sys
import subprocess as sp
import traceback
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
fromaddr = 'svn@test.com'
toaddrs = 'test@test.com'
REPO_NAME = '/home/dev/repo' # REPOSITORY name !!!!!!
def email(subj, msg):
try:
body = '\r\n'.join(("From: %s" % fromaddr, "To: %s" % toaddrs, "Subject: %s" % subj, "", msg))
server = smtplib.SMTP('smtp.google.com')
server.login('mike@test.com', 'test')
server.sendmail(fromaddr, toaddrs, body)
server.quit()
except Exception, ex:
print ex
pass
PATH = sys.argv[1]
REV = sys.argv[2]
# SVN Setup
SVN = "/usr/local/bin/svn"
SVNLOOK = "/usr/local/bin/svnlook"
WATCH = "/"
#Temporary checkout folder
TMP_ROOT = '/home/dev/domains/test.com/public_html/area51'
class SPError(Exception):
def __init__(self, command, returncode, stdout, stderr):
self.command = command
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
def __str__(self):
return repr(self)
def __repr__(self):
mesg = """
COMMAND: %s
RETURNCODE: %s
STDOUT:
%s
STDERR:
%s
"""
mesg = '\n'.join([t.lstrip() for t in mesg.strip().split('\n')])
print mesg % (self.command, self.returncode, self.stdout,
self.stderr)
return mesg % (self.command, self.returncode, self.stdout,
self.stderr)
def spcall(command, shell=False, input=None):
pipe = sp.Popen(command, shell=shell, stdin=sp.PIPE, stdout=sp.PIPE,
stderr=sp.PIPE)
(stdout, stderr) = pipe.communicate(input=input)
if pipe.returncode != 0:
if isinstance(command, list):
command = ' '.join(command)
raise SPError(command, pipe.returncode, stdout, stderr)
return (stdout, stderr)
def files_changed():
(stdout, stderr) = spcall([SVNLOOK, "dirs-changed", PATH, "-r", REV])
lines = []
for line in stdout.split('\n'):
if line.startswith(WATCH):
lines.append(line)
return lines
def main():
try:
files = files_changed()
if len(files) == 0:
print "No change."
sys.exit(0)
for line in files:
os.chdir(TMP_ROOT)
spcall([SVN, "update" , "%s%s" % (REPO_NAME, line)])
except SPError, inst:
email("Post-Commit Subprocess Failure", repr(inst))
except:
mesg = traceback.format_exc()
email("Post-Commit Unknown Failure", mesg)
if __name__ == '__main__':
main()
#!/usr/local/bin/python
import os
import sys
import subprocess as sp
import traceback
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
fromaddr = 'svn@test.com'
toaddrs = 'test@test.com'
REPO_NAME = '/home/dev/repo' # REPOSITORY name !!!!!!
def email(subj, msg):
try:
body = '\r\n'.join(("From: %s" % fromaddr, "To: %s" % toaddrs, "Subject: %s" % subj, "", msg))
server = smtplib.SMTP('smtp.google.com')
server.login('mike@test.com', 'test')
server.sendmail(fromaddr, toaddrs, body)
server.quit()
except Exception, ex:
print ex
pass
PATH = sys.argv[1]
REV = sys.argv[2]
# SVN Setup
SVN = "/usr/local/bin/svn"
SVNLOOK = "/usr/local/bin/svnlook"
WATCH = "/"
#Temporary checkout folder
TMP_ROOT = '/home/dev/domains/test.com/public_html/area51'
class SPError(Exception):
def __init__(self, command, returncode, stdout, stderr):
self.command = command
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
def __str__(self):
return repr(self)
def __repr__(self):
mesg = """
COMMAND: %s
RETURNCODE: %s
STDOUT:
%s
STDERR:
%s
"""
mesg = '\n'.join([t.lstrip() for t in mesg.strip().split('\n')])
print mesg % (self.command, self.returncode, self.stdout,
self.stderr)
return mesg % (self.command, self.returncode, self.stdout,
self.stderr)
def spcall(command, shell=False, input=None):
pipe = sp.Popen(command, shell=shell, stdin=sp.PIPE, stdout=sp.PIPE,
stderr=sp.PIPE)
(stdout, stderr) = pipe.communicate(input=input)
if pipe.returncode != 0:
if isinstance(command, list):
command = ' '.join(command)
raise SPError(command, pipe.returncode, stdout, stderr)
return (stdout, stderr)
def files_changed():
(stdout, stderr) = spcall([SVNLOOK, "dirs-changed", PATH, "-r", REV])
lines = []
for line in stdout.split('\n'):
if line.startswith(WATCH):
lines.append(line)
return lines
def main():
try:
files = files_changed()
if len(files) == 0:
print "No change."
sys.exit(0)
for line in files:
os.chdir(TMP_ROOT)
spcall([SVN, "update" , "%s%s" % (REPO_NAME, line)])
except SPError, inst:
email("Post-Commit Subprocess Failure", repr(inst))
except:
mesg = traceback.format_exc()
email("Post-Commit Unknown Failure", mesg)
if __name__ == '__main__':
main()
Hvala,
Music is essential for my life...