Skip to content

Commit

Permalink
some cleaning up
Browse files Browse the repository at this point in the history
moa new project now creates a git repo
  • Loading branch information
Mark Fiers committed Sep 24, 2012
1 parent 33daaa4 commit 8d48ea3
Showing 1 changed file with 67 additions and 63 deletions.
130 changes: 67 additions & 63 deletions lib/python/moa/plugin/system/moaGit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,29 @@
-----------------------------------------------------------
"""

import glob
import os
import subprocess
import sys
import time

import git
import moa.args
import moa.logger as l
from moa.sysConf import sysConf


#import smmap
#import git


# def hook_defineCommands():
# sysConf['commands']['gitlog'] = {
# 'desc' : 'display a nicely formatted git log',
# 'call': gitlog
# }
# sysConf['commands']['gittag'] = {
# 'desc' : 'Tag the current version',
# 'call': tag
# }
# sysConf['commands']['gitadd'] = {
# 'desc' : 'Add the current job to the git repository',
# 'call': gitadd
# }


# def _getRepo(job):
# """
# Return the git repository object
# """
# wd = job.wd
# try:
# repo = git.Repo(wd)
# return repo
# except git.InvalidGitRepositoryError:
# return None
def _getRepo(job):
"""
Return the git repository object
"""

wd = job.wd
try:
repo = git.Repo(wd)
return repo
except git.InvalidGitRepositoryError:
return None

def _checkInRepo(job):
"""
Expand All @@ -65,6 +46,7 @@ def _checkInRepo(job):
l.debug("NOT in a git repository (%s)" % job.wd)
return False


def _realCommit(repo, files, wd, message):
"""
Do the actual commit
Expand All @@ -90,71 +72,68 @@ def _realCommit(repo, files, wd, message):
except git.exc.GitCommandError:
pass

def _checkGitIgnore(gitignoreFile):
"""
See if there is moa dir specific git ignore file - if not create one

def _checkGitIgnore(wd):
"""Approach is simple - we're ignoring everything unless
specifically added. Both for the moa job in&output files as the
.moa files. (unless specified differntly in the config
"""

gitignoreFile = os.path.join(wd, '.gitignore')
if not os.path.exists(gitignoreFile):
if 'ignore' in sysConf.plugins.system.moaGit:
to_ignore = sysConf.plugins.system.moaGit.ignore
else:
to_ignore = ['*']
with open(gitignoreFile, 'w') as F:
F.write("log\n")
F.write("log.d/\n")
F.write("log.latest\n")
F.write("last_run_id\n")
F.write("status\n")
F.write("*.fof\n")
F.write("*~\n")
for ignore in to_ignore:
F.write("%s\n" % ignore)


def _commitDir(wd, message):
"""
Simpler utility to add files to a repository without
initiating a moa job object
"""
repo = sysConf.git.repo
if not repo: return

if not repo:
return

moadir = os.path.join(wd, '.moa')

if not os.path.exists(moadir):
return

gitignoreFile = os.path.join(wd, '.moa', '.gitignore')
_checkGitIgnore(gitignoreFile)

files = set([gitignoreFile])
for gl in ['.moa/template',
'.moa/template.d/*',
'.moa/config',
'.moa/history',
'.moa/local_bash_history',
'moa.*', '*.md',
'Readme', 'README', 'Readme.*',
'Blog', 'BLOG', 'Readme.*',
'Changelog', 'CHANGELOG', 'Changelog.*' ]:
_checkGitIgnore(wd)

files = set()
for gl in sysConf.plugins.system.moaGit.commit:
for f in glob.glob(os.path.join(wd, gl)):
files.update(set(glob.glob(os.path.join(wd, gl))))

remove = [x for x in files if x[-1] == '~']
files.difference_update(remove)

_realCommit(repo, list(files), wd, message)
#repo.git.commit(os.path.join(wd, '.moa', 'template.d'))


def _commit(job, message):
"""
Commit the current job to the repository
"""
repo = sysConf.git.repo
if not repo: return
if not repo:
return

gitignoreFile = os.path.join(job.wd, '.moa', '.gitignore')
_checkGitIgnore(gitignoreFile)
_checkGitIgnore(job.wd)

files = [gitignoreFile]
files = []
files.extend(job.getFiles())
_realCommit(repo, files, job.wd, message)

#repo.git.commit(os.path.join(job.wd, '.moa', 'template.d'))


@moa.args.command
def gitadd(job, args):
"""
Expand All @@ -163,6 +142,7 @@ def gitadd(job, args):
l.debug("adding to git %s" % job.wd)
_commit(job, "add/refresh of %s" % job.wd)


def tag(job):
repo = _getRepo(job)
if not repo:
Expand All @@ -174,6 +154,7 @@ def tag(job):
l.info('tagging with "%s"' % tagname)
repo.create_tag(tagname, message=message)


def hook_prepare_3():
"""
Register a function for submitting a job
Expand All @@ -187,15 +168,38 @@ def hook_prepare_3():
if not sysConf.git.repo and sysConf.plugin_settings.moaGit.warn:
moa.ui.warn("Cannot find a git repository!")


def hook_finish():
"""
Handle all git changes post execution - actually defer to plugin specific calls
Handle all git changes post execution - actually defer to
plugin specific calls
"""
if sysConf.git.repo == None:
if sysConf.git.repo is None:
return
sysConf.pluginHandler.run('git_finish_%s' % sysConf.originalCommand)

def gitlog(job):

def hook_postNew():
"""Handle all git changes post New - checking if this is a new
'project'. If so, and there is no git repo yet - create one
"""
if sysConf.git.repo:
return

# was a 'project' created? If not return
if sysConf.job.template.name != 'project':
return

l.warning("Creating a new git repository")
sysConf.git.repo = git.Repo.init(sysConf.job.wd)
_commit(sysConf.job, "New Moa Project")


@moa.args.command
def gitlog(job, args):
"""
Print a log to screen
"""
Expand Down

0 comments on commit 8d48ea3

Please sign in to comment.