Skip to content

Commit

Permalink
Simplify sync by cloning in a temporary directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mscherer committed Sep 28, 2022
1 parent 01f044b commit 7fbf740
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
4 changes: 1 addition & 3 deletions console
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ if __name__ == "__main__":

print('# ----------- Syncing the meta with the database ----------- #')

if not os.path.exists(backend.META) or not os.path.isdir(backend.META):
backend.clone()
backend.clone()

backend.pull()

print(sync(SESSION, meta.Election.all()))
exit()
Expand Down
5 changes: 1 addition & 4 deletions elekto/controllers/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,5 @@
@csrf.exempt
def webhook_sync():
backend = meta.Meta(APP.config['META'])
if not os.path.exists(backend.META) or not os.path.isdir(backend.META):
backend.clone()
else:
backend.pull()
backend.clone()
return sync(SESSION, meta.Election.all())
12 changes: 6 additions & 6 deletions elekto/models/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import os
import random
import subprocess
import tempfile
import flask as F

from datetime import datetime
Expand All @@ -32,20 +34,18 @@ class Meta:
"""

def __init__(self, config):
self.META = os.path.abspath(config['PATH'])
# keep this, as it ties the directory to the object lifecycle
self.TMPDIR = tempfile.TemporaryDirectory("elekto")
self.META = self.TMPDIR.name
self.ELECDIR = config['ELECDIR']
self.REMOTE = config['REMOTE']
self.BRANCH = config['BRANCH']
self.SECRET = config['SECRET']
self.git = '/usr/bin/git'
self.pref = "/usr/bin/git --git-dir={}/.git --work-tree={}\
".format(self.META, self.META)

def clone(self):
os.system('{} clone -b {} -- {} {}'.format(self.git, self.BRANCH, self.REMOTE, self.META))
subprocess.check_call([self.git, 'clone', '-b', self.BRANCH, '--', self.REMOTE, self.META])

def pull(self):
os.system('{} pull --ff-only origin {}'.format(self.pref, self.BRANCH))


class Election(Meta):
Expand Down

0 comments on commit 7fbf740

Please sign in to comment.