Created
March 20, 2014 23:06
-
-
Save corpix/9675970 to your computer and use it in GitHub Desktop.
Streamo!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import subprocess | |
import shutil | |
import hashlib | |
import time | |
from string import Template | |
STREAMS = [ | |
{ | |
'path': '/home/corpix/Code/streamo/streams/test', | |
'cmds': { | |
'create': 'mkdir $path; echo `date` > $path/date', # Will run in streams directory | |
'update': 'echo `date` > $path/date' # Will run inside concrete stream | |
} | |
} | |
] | |
INDEX_ROOT = '/home/corpix/Code/streamo/index' | |
ctr = 0 | |
def run(cmd, cwd=None): | |
print('Running', cmd, 'in', cwd) | |
if subprocess.call(cmd, shell=True, cwd=cwd) is not 0: | |
raise RuntimeError('Non zero exit code') | |
def sha1(s): | |
m = hashlib.sha1() | |
m.update(s) | |
return m.hexdigest() | |
def get_sipath(stream_path): | |
global ctr | |
ctr += 1 | |
ts = '%s-%s-%s' % (int(time.time()), os.getpid(), ctr) | |
path_hash = sha1(stream_path) | |
return os.path.join(INDEX_ROOT, '%s-%s' % (ts, path_hash)) | |
def get_sipaths(stream_path): | |
path_hash = sha1(stream_path) | |
lst = [ f for f in os.listdir(INDEX_ROOT) if f.endswith(path_hash) ] | |
lst.sort() | |
return lst | |
def create_stream(stream): | |
sipath = get_sipath(stream['path']) | |
createCmd = Template(stream['cmds']['create']).substitute(dict(path=sipath)) | |
try: | |
run(createCmd, os.path.dirname(INDEX_ROOT)) | |
except Exception as e: | |
return None, e | |
return sipath, None | |
def get_stream_revs(stream): | |
h = sha1(stream['path']) | |
lst = [ f for f in os.listdir(INDEX_ROOT) if f.endswith(h) ] | |
lst.sort() | |
return lst | |
def prune_stream(stream): | |
lst = get_stream_revs(stream) | |
if len(lst) > 1: | |
for d in lst[:-1]: | |
print('Removing', d) | |
shutil.rmtree(os.path.join(INDEX_ROOT, d)) | |
def create_link(target, lnk_path): | |
if os.path.islink(lnk_path): | |
print('Removing symlink', lnk_path, 'pointing to', os.readlink(lnk_path)) | |
os.unlink(lnk_path) | |
print('Creating symlink to', target, 'pointing to', lnk_path) | |
os.symlink(target, lnk_path) | |
def update_stream(stream): | |
sipath, err = create_stream(stream) | |
if err: | |
return None, err | |
if 'update' in stream['cmds']: | |
updateCmd = Template(stream['cmds']['update']).substitute(dict(path=sipath)) | |
run(updateCmd, sipath) | |
create_link(sipath, stream['path']) | |
return sipath, None | |
for stream in STREAMS: | |
prune_stream(stream) | |
if not os.path.exists(stream['path']): | |
sipath, err = create_stream(stream) | |
if err: | |
raise err | |
create_link(sipath, stream['path']) | |
else: | |
_, err = update_stream(stream) | |
if err: | |
raise err | |
prune_stream(stream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment