Skip to content

Commit

Permalink
Move Git storage to the main thread
Browse files Browse the repository at this point in the history
Closes #29
  • Loading branch information
jacebrowning committed Apr 8, 2016
1 parent 04a7440 commit 3c93bf8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ watch: depends .clean-test
.PHONY: run
run: depends-dev .env
git init data
cd data && touch README && git commit -am "Add initial files"
ifdef DEBUG
$(HONCHO) run $(PYTHON) manage.py run
else
Expand Down
46 changes: 26 additions & 20 deletions api/routes/_common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import pprint
import contextlib
import logging
from multiprocessing import Process

from sh import git as _git, ErrorReturnCode # pylint: disable=no-name-in-module
from sh import git, ErrorReturnCode # pylint: disable=no-name-in-module
import requests
from flask import current_app, request

Expand All @@ -19,30 +19,36 @@
log = logging.getLogger(__name__)


def sync(obj):
"""Store updated metrics in version control."""
def sync(model):
"""Store all changes in version control."""
remote = current_app.config['ENV'] == 'prod'

def run(_sync=False): # pragma: no cover (separate process)
git = _git.bake(git_dir=os.path.join(DATA, ".git"), work_tree=DATA)
message = str(model) # YORM models can't be used in a different directory

with location(DATA):
git.add(all=True)
git.stash()
if remote:
git.pull()
try:
git.commit(message=str(obj))
git.stash('apply')
git.add(all=True)
git.commit(message=message)
except ErrorReturnCode:
commit = False
log.warning("No changes to save")
else:
commit = True

if _sync:
if commit:
git.push(force=True)
git.pull()

_sync = current_app.config['ENV'] == 'prod'
process = Process(target=run, args=[_sync])
process.start()

return obj
log.info("Saved model: %s", message)
if remote:
git.push()


@contextlib.contextmanager
def location(dirpath):
"""Change to a directory, temporarily."""
cwd = os.getcwd()
os.chdir(dirpath)
yield
os.chdir(cwd)


def track(obj):
Expand Down
Empty file added data/.gitkeep
Empty file.

0 comments on commit 3c93bf8

Please sign in to comment.