Parent: [2168fe] (diff)

Child: [09085e] (diff)

Download this file

main.py    205 lines (172 with data), 6.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#-*- python -*-
import os
import shutil
import logging
from cStringIO import StringIO
from pprint import pformat
# Non-stdlib imports
import pkg_resources
import uvc.main, uvc.hg, uvc.util
from tg import expose, validate, redirect
from pylons import g, c, request
from formencode import validators
from pymongo.bson import ObjectId
from ming import schema
# Pyforge-specific imports
from pyforge.app import Application, ConfigOption, SitemapEntry
from pyforge.lib.helpers import push_config
from pyforge.lib.search import search
from pyforge.lib.decorators import audit, react
from pyforge.lib.security import require, has_artifact_access
from pyforge.model import ProjectRole
# Local imports
from . import model
from . import version
from .wsgi import WSGIHook
from .lib import hg
log = logging.getLogger(__name__)
class ForgeSCMApp(Application):
__version__ = version.__version__
permissions = ['configure', 'read' ]
config_options = Application.config_options + [
ConfigOption('type', schema.OneOf('git', 'hg', 'svn'), 'hg'),
]
wsgi=WSGIHook()
def __init__(self, project, config):
Application.__init__(self, project, config)
self.root = RootController()
@property
def repo(self):
return model.Repository.m.get(app_config_id=self.config._id)
@audit('scm.hg.init')
def scm_hg_init(self, routing_key, data):
repo = self.repo
cmd = hg.init()
cmd.clean_dir()
repo.clear_commits()
cmd.run()
if cmd.sp.returncode:
g.publish('react', 'error', dict(
message=cmd.output))
else:
log.info('Setting repo status for %s', repo)
repo.status = 'Ready'
repo.m.save()
@audit('scm.hg.clone')
def scm_hg_clone(self, routing_key, data):
repo = self.repo
# Perform the clone
cmd = hg.clone(data['url'], '.')
cmd.clean_dir()
cmd.run()
if cmd.sp.returncode:
g.publish('react', 'error', dict(
message=cmd.sp.stdout.read()))
return
# Load the log
cmd = hg.log('-g', '-p')
cmd.run()
# Clear the old set of commits
repo.clear_commits()
parser = hg.LogParser(repo._id)
parser.feed(StringIO(cmd.output))
# Update the repo status
repo.status = 'Ready'
repo.m.save()
@property
def sitemap(self):
menu_id = 'Repository (%s)' % self.config.options.mount_point
with push_config(c, app=self):
return [
SitemapEntry(menu_id, '.')[self.sidebar_menu()] ]
def sidebar_menu(self):
return [
SitemapEntry('Home', '.'),
SitemapEntry('Search', 'search'),
]
@property
def templates(self):
return pkg_resources.resource_filename('forgescm', 'templates')
@property
def repo_dir(self):
return pkg_resources.resource_filename(
'forgescm',
os.path.join('data', self.project._id, self.config.options.mount_point))
def install(self, project):
'Set up any default permissions and roles here'
self.uninstall(project)
# Give the installing user all the permissions
pr = c.user.project_role()
for perm in self.permissions:
self.config.acl[perm] = [ pr._id ]
self.config.acl['read'].append(
ProjectRole.m.get(name='*anonymous')._id)
self.config.m.save()
# Create a repository
repo = model.Repository.make(dict(
description='This is the repository object',
status='Pending'))
repo.m.insert()
rk = 'scm.%s.init' % self.config.options.type
g.publish('audit', rk, {})
def uninstall(self, project):
"Remove all the plugin's artifacts from the database"
model.Repository.m.remove(dict(app_config_id=self.config._id))
class RootController(object):
def __init__(self):
self.repo = CommitsController()
@expose('forgescm.templates.index')
def index(self):
return dict(repo=c.app.repo)
@expose('forgescm.templates.search')
@validate(dict(q=validators.UnicodeString(if_empty=None),
history=validators.StringBool(if_empty=False)))
def search(self, q=None, history=None):
'local plugin search'
results = []
count=0
if not q:
q = ''
else:
search_query = '''%s
AND is_history_b:%s
AND mount_point_s:%s''' % (
q, history, c.app.config.options.mount_point)
results = search(search_query)
if results: count=results.hits
return dict(q=q, history=history, results=results or [], count=count)
@expose()
def reinit(self):
repo = c.app.repo
repo.status = 'Pending'
repo.m.save()
g.publish('audit', 'scm.%s.init' % c.app.config.options.type, {})
redirect('.')
@expose()
def clone_from(self, url=None):
repo = c.app.repo
repo.status = 'Pending'
repo.m.save()
g.publish('audit', 'scm.%s.clone' % c.app.config.options.type, dict(
url=url))
redirect('.')
class CommitsController(object):
def _lookup(self, id, *remainder):
if ':' in id: id = id.split(':')[-1]
if '%3A' in id: id = id.split('%3A')[-1]
print id
return CommitController(id), remainder
class CommitController(object):
def __init__(self, id):
self.commit = model.Commit.m.get(hash=id)
@expose('forgescm.templates.commit_index')
def index(self):
return dict(value=self.commit)
def _lookup(self, id, *remainder):
return PatchController(id), remainder
class PatchController(object):
def __init__(self, id):
self.patch = model.Patch.m.get(_id=ObjectId.url_decode(id))
@expose('forgescm.templates.patch_index')
def index(self):
return dict(value=self.patch)