Parent: [fc442f] (diff)

Child: [0e4a3d] (diff)

Download this file

svn.py    122 lines (99 with data), 3.6 kB

  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
import os
import errno
import stat
import logging
import subprocess
import cPickle as pickle
from datetime import datetime
import pysvn
import pymongo
from pylons import c
from ming.orm.mapped_class import MappedClass
from ming.orm.property import FieldProperty
from ming.utils import LazyProperty
from pyforge.model import Repository, Commit, ArtifactReference, User
from pyforge.lib import helpers as h
log = logging.getLogger(__name__)
class SVNRepository(Repository):
MAGIC_FILENAME='.SOURCEFORGE-REPOSITORY'
class __mongometa__:
name='svn-repository'
def index(self):
result = Repository.index(self)
result.update(
type_s='SVNRepository')
return result
@LazyProperty
def _impl(self):
return pysvn.Client()
@LazyProperty
def local_url(self):
return 'file://%s/%s' % (self.fs_path, self.name)
def init(self):
if not self.fs_path.endswith('/'): self.fs_path += '/'
try:
os.makedirs(self.fs_path)
except OSError, e: # pragma no cover
if e.errno != errno.EEXIST: raise
# We may eventually require --template=...
log.info('svnadmin create %s%s', self.fs_path, self.name)
result = subprocess.call(['svnadmin', 'create', self.name],
cwd=self.fs_path)
magic_file = os.path.join(self.fs_path, self.name, self.MAGIC_FILENAME)
with open(magic_file, 'w') as f:
f.write('svn')
os.chmod(magic_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)
self.status = 'ready'
def log(self, *args, **kwargs):
try:
return [SVNCommit.from_svn(entry, self)
for entry in self._impl.log(self.local_url, *args, **kwargs) ]
except: # pragma no cover
log.exception('Error performing SVN log:')
return []
def revision(self, num):
return self.log(
revision_start=pysvn.Revision(
pysvn.opt_revision_kind.number, num),
limit=1)[0]
def diff(self, r0, r1):
r0 = pysvn.Revision(pysvn.opt_revision_kind.number, r0)
r1 = pysvn.Revision(pysvn.opt_revision_kind.number, r1)
return self._impl.diff(
'/tmp', self.local_url, r0,
self.local_url, r1)
class SVNCommit(Commit):
type_s='SvnCommit'
def __init__(self, id, repo):
self._id = id
self._repo = repo
@classmethod
def from_svn(cls, entry, repo):
result = cls(id=entry.revision.number, repo=repo)
result.__dict__['_impl'] = entry
result.author_username=entry.author
result.author=User.query.get(username=entry.author)
result.datetime=datetime.utcfromtimestamp(entry.date)
return result
def __getattr__(self, name):
return getattr(self._impl, name)
def dump_ref(self):
'''Return a pickle-serializable reference to an artifact'''
try:
d = ArtifactReference(dict(
project_id=c.project._id,
mount_point=c.app.config.options.mount_point,
artifact_type=pymongo.bson.Binary(pickle.dumps(self.__class__)),
artifact_id=self._id))
return d
except AttributeError: # pragma no cover
return None
def url(self):
return self._repo.url() + str(self._id)
def primary(self, *args):
return self
def shorthand_id(self):
return '[r%s]' % self._id
def diff(self):
return self._repo.diff(self._id-1, self._id)
MappedClass.compile_all()