openmedialibrary/oml/commands.py

186 lines
5.8 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-05-16 08:06:11 +00:00
from __future__ import division
2014-05-04 17:26:43 +00:00
2014-05-17 14:31:05 +00:00
import subprocess
2014-05-20 00:08:28 +00:00
from os.path import join, exists, dirname
2014-05-17 14:31:05 +00:00
import os
2014-05-20 00:08:28 +00:00
from flask.ext.script import Command, Option
import settings
root_dir = dirname(settings.base_dir)
def run(*cmd):
p = subprocess.Popen(cmd)
p.wait()
return p.returncode
def get(*cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, error = p.communicate()
return stdout
2014-05-04 17:26:43 +00:00
2014-05-17 14:31:05 +00:00
def r(*cmd):
print ' '.join(cmd)
return subprocess.call(cmd)
2014-05-20 00:08:28 +00:00
def version(module):
os.chdir(join(root_dir, module))
version = get('git', 'log', '-1', '--format=%cd', '--date=iso').split(' ')[0].replace('-', '')
version += '-' + get('git', 'rev-list', 'HEAD', '--count').strip()
version += '-' + get('git', 'describe', '--always').strip()
os.chdir(root_dir)
return version
class Version(Command):
"""
Print current version
"""
def run(self):
print version('openmedialibrary')
2014-05-17 21:40:34 +00:00
class Debug(Command):
"""
2014-05-20 00:08:28 +00:00
Start in debug mode
2014-05-17 21:40:34 +00:00
"""
def run(self):
pass
class Start(Command):
"""
Start Open Media Libary
"""
def run(self):
pass
class Stop(Command):
"""
Stop Open Media Libary
"""
def run(self):
pass
2014-05-04 17:26:43 +00:00
2014-05-17 22:32:08 +00:00
class Update(Command):
"""
Update to latest development version
"""
def run(self):
pass
2014-05-20 00:08:28 +00:00
class PostUpdate(Command):
"""
Called by update to fix stuff
"""
def get_options(self):
return [
Option('-o', '--old', dest='old'),
Option('-n', '--new', dest='new'),
]
def run(selfi, old, new):
if old <= '20140521-65-e14c686' and new > '20140521-65-e14c686':
if not os.path.exists(settings.db_path):
r('./ctl', 'setup')
2014-05-25 18:06:12 +00:00
if old <= '20140525-92-eac91e7' and new > '20140525-92-eac91e7':
import user.models
for u in user.models.User.query:
u.update_name()
u.save()
import item.models
for f in item.models.File.query:
changed = False
for key in ('mediastate', 'coverRatio', 'previewRatio'):
if key in f.info:
del f.info[key]
changed = True
if changed:
f.save()
2014-05-20 00:08:28 +00:00
2014-05-04 17:26:43 +00:00
class Setup(Command):
"""
2014-05-20 00:08:28 +00:00
Setup new node
2014-05-04 17:26:43 +00:00
"""
def run(self):
2014-05-17 14:31:05 +00:00
r('./ctl', 'db', 'upgrade')
2014-05-04 17:26:43 +00:00
import setup
setup.create_default_lists()
2014-05-16 08:06:11 +00:00
settings.db.session.connection().execute("PRAGMA journal_mode=WAL")
settings.db.session.commit()
2014-05-04 17:26:43 +00:00
class UpdateStatic(Command):
"""
2014-05-20 00:08:28 +00:00
Update static files
2014-05-04 17:26:43 +00:00
"""
def run(self):
oxjs = os.path.join(settings.static_path, 'oxjs')
if not os.path.exists(oxjs):
r('git', 'clone', 'https://git.0x2620.org/oxjs.git', oxjs)
2014-05-17 11:45:57 +00:00
elif os.path.exists(os.path.join(oxjs, '.git')):
os.system('cd "%s" && git pull' % oxjs)
2014-05-16 08:06:11 +00:00
r('python2', os.path.join(oxjs, 'tools', 'build', 'build.py'))
r('python2', os.path.join(settings.static_path, 'py', 'build.py'))
2014-05-04 17:26:43 +00:00
class Release(Command):
"""
2014-05-20 00:08:28 +00:00
Release new version
2014-05-04 17:26:43 +00:00
"""
def run(self):
print 'checking...'
import os
import json
import hashlib
import ed25519
os.chdir(root_dir)
with open(os.path.expanduser('~/.openmedialibrary_release.key')) as fd:
2014-05-04 17:26:43 +00:00
SIG_KEY=ed25519.SigningKey(fd.read())
SIG_ENCODING='base64'
def sign(release):
value = []
for module in sorted(release['modules']):
value += ['%s/%s' % (release['modules'][module]['version'], release['modules'][module]['sha1'])]
value = '\n'.join(value)
sig = SIG_KEY.sign(value, encoding=SIG_ENCODING)
release['signature'] = sig
def sha1sum(path):
h = hashlib.sha1()
with open(path) as fd:
for chunk in iter(lambda: fd.read(128*h.block_size), ''):
h.update(chunk)
return h.hexdigest()
MODULES = ['platform', 'openmedialibrary']
VERSIONS = {module:version(module) for module in MODULES}
EXCLUDE=[
'--exclude', '.git', '--exclude', '.bzr',
'--exclude', '.*.swp', '--exclude', '._*', '--exclude', '.DS_Store'
]
#run('./ctl', 'update_static')
for module in MODULES:
tar = join('updates', '%s-%s.tar.bz2' % (module, VERSIONS[module]))
if not exists(tar):
cmd = ['tar', 'cvjf', tar, '%s/' % module] + EXCLUDE
if module in ('openmedialibrary', ):
cmd += ['--exclude', '*.pyc']
if module == 'openmedialibrary':
cmd += ['--exclude', 'oxjs/examples', '--exclude', 'gunicorn.pid']
run(*cmd)
release = {}
release['modules'] = {module: {
'name': '%s-%s.tar.bz2' % (module, VERSIONS[module]),
'version': VERSIONS[module],
'sha1': sha1sum(join('updates', '%s-%s.tar.bz2' % (module, VERSIONS[module])))
} for module in MODULES}
sign(release)
with open('updates/release.json', 'w') as fd:
json.dump(release, fd, indent=2)
print 'signed latest release in updates/release.json'