Parent: [27f908] (diff)

Child: [a404b5] (diff)

Download this file

git_react.py    134 lines (122 with data), 4.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
import logging
from cStringIO import StringIO
from pylons import c, g
from pymongo import bson
from pyforge.lib.decorators import audit, react
from pyforge.lib.helpers import push_context, set_context, encode_keys
from pyforge.model import Project
from forgescm.lib import git
from pyforge import model as M
log = logging.getLogger(__name__)
## Auditors
@audit('scm.git.init')
def init(routing_key, data):
log.info('GIT init')
repo = c.app.repo
repo.type = 'git'
cmd = git.init()
cmd.clean_dir()
repo.clear_commits()
repo.parent = None
cmd.run()
log.info('Setup gitweb in %s', repo.repo_dir)
repo_name = c.project.shortname + c.app.config.options.mount_point
git.setup_gitweb(repo_name, repo.repo_dir)
git.setup_commit_hook(repo.repo_dir, c.app.config.script_name()[1:])
if cmd.sp.returncode:
g.publish('react', 'error', dict(
message=cmd.output))
repo.status = 'Error: %s' % cmd.output
else:
repo.status = 'Ready'
@audit('scm.git.clone')
def clone(routing_key, data):
repo = c.app.repo
log.info('Begin cloning %s', data['url'])
repo.type = 'git'
cmd = git.clone(data['url'], 'git_dest')
cmd.clean_dir()
repo.clear_commits()
cmd.run()
cmd.finish() # move cloned dir back to the mount point's cwd
log.info('Clone complete for %s', data['url'])
repo_name = c.project.shortname + c.app.config.options.mount_point
git.setup_gitweb(repo_name, repo.repo_dir)
git.setup_commit_hook(repo.repo_dir, c.app.config.script_name()[1:])
if cmd.sp.returncode:
errmsg = cmd.output
g.publish('react', 'error', dict(
message=errmsg))
repo.status = 'Error: %s' % errmsg
else:
g.publish('react', 'scm.cloned', dict(
url=data['url']))
@audit('scm.git.fork')
def fork(routing_key, data):
log.info('Begin forking %s => %s', data['forked_from'], data['forked_to'])
project = M.Project.query.get(_id=bson.ObjectId(data['forked_to']['project_id']))
data_context = data['forked_to'].copy()
data_context['project_shortname']=project.shortname
del data_context['project_id']
set_context(**encode_keys(data_context))
# Set repo metadata
repo = c.app.repo
repo.type = 'git'
repo.forked_from.update(data['forked_from'])
# Perform the clone
log.info('Cloning from %s', data['url'])
cmd = git.clone(data['url'], 'git_dest')
cmd.clean_dir()
repo.clear_commits()
cmd.run()
cmd.finish()
repo.status = 'Ready'
log.info('Clone complete for %s', data['url'])
repo_name = c.project.shortname + c.app.config.options.mount_point
git.setup_gitweb(repo_name, repo.repo_dir)
git.setup_commit_hook(repo.repo_dir, c.app.config.script_name()[1:])
if cmd.sp.returncode:
errmsg = cmd.output
g.publish('react', 'error', dict(
message=errmsg))
repo.status = 'Error: %s' % errmsg
return
else:
log.info("Sending scm.forked message")
g.publish('react', 'scm.forked', data)
@audit('scm.git.reclone')
def reclone(routing_key, data):
set_context(data['project_id'], data['mount_point'])
repo = c.app.repo
# Perform the clone
cmd = git.clone(repo.parent, '.')
cmd.clean_dir()
repo.clear_commits()
cmd.run()
if cmd.sp.returncode:
g.publish('react', 'error', dict(
message=cmd.sp.stdout.read()))
return
# Load the log
cmd = git.scm_log('-p')
cmd.run()
# Clear the old set of commits
repo.clear_commits()
parser = git.LogParser(repo._id)
parser.feed(StringIO(cmd.output))
# Update the repo status
repo.status = 'Ready'
## Reactors
@react('scm.git.refresh_commit')
def refresh_commit(routing_key, data):
set_context(data['project_id'], data['mount_point'])
repo = c.app.repo
hash = data['hash']
log.info('Refresh commit %s', hash)
# Load the log
if '..' in hash:
cmd = git.scm_log('-p', hash)
else:
cmd = git.scm_log('-p', '-1', hash)
parser = git.LogParser(repo._id)
cmd.run(output_consumer=parser.feed)