Skip to content

Instantly share code, notes, and snippets.

@z4r
Created January 17, 2012 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z4r/1626621 to your computer and use it in GitHub Desktop.
Save z4r/1626621 to your computer and use it in GitHub Desktop.
SVN Context Manager
from subprocess import call
SVN_CONF = '''[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz
'''
SVN_PASSWD = '''[users]
{user} = {password}
'''
SVN_AUTHZ = '''[groups]
admin = {user}
[/]
* = r
@admin = rw
'''
class SVNServer(object):
def __init__(self, root, name, user, password):
self.root = '{0}{1}'.format(root, name)
self.passwd = SVN_PASSWD.format(user = user, password = password)
self.passwd_path = '{0}/conf/passwd'.format(self.root)
self.authz = SVN_AUTHZ.format(user = user)
self.authz_path = '{0}/conf/authz'.format(self.root)
self.conf_path = '{0}/conf/svnserve.conf'.format(self.root)
self.log_file = '{0}.log'.format(self.root)
self.pid_file = '{0}.pid'.format(self.root)
def __enter__(self):
call(["svnadmin", "create", self.root])
with open(self.passwd_path, 'w') as f:
f.write(self.passwd)
with open(self.authz_path, 'w') as f:
f.write(self.authz)
with open(self.conf_path, 'w') as f:
f.write(SVN_CONF)
call([
"svnserve", "--daemon",
"--root", self.root,
"--log-file", self.log_file,
"--pid-file", self.pid_file
])
return self
def __exit__(self, *args):
try:
with open(self.pid_file, 'r') as f:
call(["kill", "-9", f.read().strip()])
finally:
call(["rm", self.pid_file])
call(["rm", self.log_file])
call(["rm", "-rf", self.root])
if __name__ == '__main__':
from time import sleep
def keyboard_loop():
while True:
try:
sleep(1)
except KeyboardInterrupt:
break
with SVNServer('./', 'myRepos', 'guest', 'guest') as server:
try:
# DO SOMETHING
keyboard_loop()
finally:
# CLEAN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment