openmedialibrary/oml/commands.py

337 lines
10 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
2014-09-02 22:32:44 +00:00
2014-05-04 17:26:43 +00:00
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
2015-03-07 18:58:24 +00:00
import shutil
2016-02-01 07:45:34 +00:00
import signal
import subprocess
import sys
2014-05-17 14:31:05 +00:00
2014-05-20 00:08:28 +00:00
import settings
from utils import run, get
2014-05-20 00:08:28 +00:00
root_dir = dirname(settings.base_dir)
HIDDEN = ['install_update', 'install_launcher', 'uninstall_launcher']
2014-05-17 14:31:05 +00:00
def r(*cmd):
return subprocess.call(cmd)
2014-05-20 00:08:28 +00:00
def version(module):
2014-08-05 09:47:16 +00:00
if os.path.exists(join(root_dir, module, '.git')):
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)
else:
2015-11-26 12:40:39 +00:00
if 'modules' in settings.release and module in settings.release['modules']:
2015-03-07 18:58:24 +00:00
version = settings.release['modules'][module]['version']
else:
version = -1
2014-05-20 00:08:28 +00:00
return version
def command_version(*args):
"""
Print current version
"""
2014-09-02 22:32:44 +00:00
print(version('openmedialibrary'))
def command_debug(*args):
"""
Start in debug mode
"""
pass
def command_start(*args):
"""
Start Open Media Libary
"""
pass
def command_stop(*args):
"""
Stop Open Media Libary
"""
2016-02-01 07:45:34 +00:00
if sys.platform == 'win32':
from utils import check_pid
if args:
pid = args[0]
else:
pid = os.path.join(settings.data_path, 'openmedialibrary.pid')
try:
with open(pid) as fd:
pid = int(fd.read())
except:
return
if check_pid(pid):
os.kill(pid, signal.SIGTERM)
def command_restart(*args):
"""
Restart Open Media Libary
"""
if sys.platform == 'win32':
2016-04-14 19:27:30 +00:00
from urllib.request import urlopen
from urllib.parse import urlencode
url = 'http://{address}:{port}/api/'.format(**settings.server)
data = urlencode({'action': 'quit', 'data': '{}'}).encode()
try:
urlopen(url, data, timeout=2)
except:
pass
2016-02-01 07:45:34 +00:00
from utils import check_pid, ctl
pidfile = os.path.join(settings.data_path, 'openmedialibrary.pid')
try:
with open(pidfile) as fd:
pid = int(fd.read())
except:
pid = None
if pid and check_pid(pid):
os.kill(pid, signal.SIGTERM)
ctl('server', pidfile)
def command_install_launcher(*args):
"""
Install launcher
"""
import integration
integration.install_launcher()
def command_uninstall_launcher(*args):
"""
Uninstall launcher
"""
import integration
integration.uninstall_launcher()
def command_install_update(*args):
"""
Install available updates
"""
pass
def command_update(*args):
"""
Update to latest development version
"""
import update
2016-01-22 12:27:02 +00:00
print('Downloading...')
if not update.download():
print('Download failed')
return
print('Installing...')
if not update.install():
print("Installation failed")
def command_postupdate(*args):
"""
Called after update with -o old -n new
"""
2015-03-07 16:17:52 +00:00
o, old, n, new = args
if o != '-o' or n != '-n':
print('usage: -o oldversion -n newversion')
sys.exit(1)
if old <= '20140521-65-e14c686' and new > '20140521-65-e14c686':
2019-01-30 16:52:16 +00:00
if not os.path.exists(settings.db_path) and sys.platform != 'win32':
2015-03-07 16:17:52 +00:00
r('./ctl', 'setup')
import setup
setup.upgrade_db(old, new)
def command_setup(*args):
"""
Setup new node
"""
import setup
setup.create_db()
def command_update_static(*args):
"""
Update static files
"""
2014-08-11 17:32:49 +00:00
import setup
import utils
2014-08-11 17:32:49 +00:00
setup.create_db()
2015-03-07 18:58:24 +00:00
old_oxjs = os.path.join(settings.static_path, 'oxjs')
2024-06-10 13:57:12 +00:00
oxjs = os.path.join(settings.top_dir, 'oxjs')
2015-03-07 18:58:24 +00:00
if os.path.exists(old_oxjs) and not os.path.exists(oxjs):
shutil.move(old_oxjs, oxjs)
if not os.path.exists(oxjs):
2019-01-18 10:48:41 +00:00
r('git', 'clone', '--depth', '1', 'https://code.0x2620.org/0x2620/oxjs.git', oxjs)
elif os.path.exists(os.path.join(oxjs, '.git')):
os.system('cd "%s" && git pull' % oxjs)
2016-01-13 17:46:24 +00:00
r('python3', os.path.join(oxjs, 'tools', 'build', 'build.py'), '-nogeo')
utils.update_static()
2024-06-10 13:57:12 +00:00
reader = os.path.join(settings.top_dir, 'reader')
2015-03-07 19:37:37 +00:00
if not os.path.exists(reader):
2019-01-18 10:48:41 +00:00
r('git', 'clone', '--depth', '1', 'https://code.0x2620.org/0x2620/openmedialibrary_reader.git', reader)
2015-03-07 19:37:37 +00:00
elif os.path.exists(os.path.join(reader, '.git')):
os.system('cd "%s" && git pull' % reader)
def command_release(*args):
"""
Release new version
"""
2014-09-02 22:32:44 +00:00
print('checking...')
import base64
import ed25519
import hashlib
import json
import OpenSSL.crypto
from OpenSSL.crypto import load_privatekey, FILETYPE_PEM
import os
2015-03-08 08:54:45 +00:00
release_name = args[0] if args else 'release'
os.chdir(root_dir)
2015-02-27 13:16:50 +00:00
with open(os.path.expanduser('~/.openmedialibrary_release.key'), 'rb') as fd:
SIG_KEY=ed25519.SigningKey(fd.read())
SIG_ENCODING='base64'
with open(os.path.expanduser('~/.openmedialibrary_tls_release.key'), 'rb') as fd:
tls_key = load_privatekey(FILETYPE_PEM, fd.read())
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).encode()
sig = SIG_KEY.sign(value, encoding=SIG_ENCODING).decode()
release['signature'] = sig
digest = 'sha1'
tls_sig = OpenSSL.crypto.sign(tls_key, value, digest)
release['signature_%s'%digest] = base64.b64encode(tls_sig).decode()
import update
if not update.verify(release):
print('verifiying signature failed!')
return False
return True
def sha1sum(path):
h = hashlib.sha1()
2015-02-27 13:16:50 +00:00
with open(path, 'rb') as fd:
for chunk in iter(lambda: fd.read(128*h.block_size), b''):
h.update(chunk)
return h.hexdigest()
2015-03-07 19:37:37 +00:00
MODULES = ['platform', 'openmedialibrary', 'oxjs', 'reader']
2016-02-06 10:37:08 +00:00
PLATFORM_MODULES = {
'platform_linux32': 'linux32',
'platform_linux64': 'linux64',
2016-07-03 11:52:09 +00:00
'platform_linux_armv7l': 'linux_armv7l',
'platform_linux_aarch64': 'linux_aarch64',
2016-02-06 10:37:08 +00:00
'platform_darwin64': 'darwin64',
2016-02-22 14:54:58 +00:00
'platform_win32': 'win32',
2016-02-06 10:37:08 +00:00
}
MODULES += list(PLATFORM_MODULES)
VERSIONS = {module:version(module) for module in MODULES}
2017-05-05 09:42:39 +00:00
EXCLUDE = [
'--exclude', '.git', '--exclude', '.bzr',
2014-09-05 16:36:03 +00:00
'--exclude', 'pip_cache',
2016-01-13 17:46:24 +00:00
'--exclude', '.*.swp', '--exclude', '._*', '--exclude', '.DS_Store',
'--exclude', 'Geo/png'
]
#run('./ctl', 'update_static')
for module in MODULES:
tar = join('updates', '%s-%s.tar.bz2' % (module, VERSIONS[module]))
if not exists(tar):
2017-05-05 09:42:39 +00:00
cmd = ['tar', 'cvjf', tar] + EXCLUDE
if module in ('openmedialibrary', ):
cmd += ['--exclude', '*.pyc']
if module == 'openmedialibrary':
2015-03-07 19:37:37 +00:00
cmd += ['--exclude', 'gunicorn.pid']
2015-03-08 08:54:45 +00:00
if module == 'oxjs':
2015-03-07 19:37:37 +00:00
cmd += ['--exclude', 'oxjs/examples']
2016-02-22 15:03:22 +00:00
if module == 'platform_win32':
cmd += ['--exclude', 'build', '--exclude', 'dist']
2017-05-05 09:42:39 +00:00
cmd += [module + '/']
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}
2016-02-06 10:37:08 +00:00
for module in PLATFORM_MODULES:
release['modules'][module]['platform'] = PLATFORM_MODULES[module]
if sign(release):
with open('updates/%s.json' % release_name, 'w') as fd:
json.dump(release, fd, indent=2, sort_keys=True)
print('signed latest release in updates/%s.json' % release_name)
def command_shell(*args):
'''
Runs a Python shell inside the application context.
'''
context = None
banner = 'Open Media Library'
import db
with db.session():
# Try BPython
try:
from bpython import embed
embed(banner=banner, locals_=context)
return
except ImportError:
2014-05-17 21:40:34 +00:00
pass
# Try IPython
try:
try:
# 0.10.x
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed(banner=banner)
ipshell(global_ns=dict(), local_ns=context)
except ImportError:
# 0.12+
2016-01-12 09:26:28 +00:00
from IPython.terminal.ipapp import TerminalIPythonApp
app = TerminalIPythonApp.instance()
app.extensions = ['autoreload']
app.exec_lines = ['%autoreload 2']
app.display_banner = False
print(banner)
app.initialize(argv=[])
app.start()
return
except ImportError:
2014-05-17 21:40:34 +00:00
pass
2014-05-04 17:26:43 +00:00
import code
# Use basic python shell
code.interact(banner, local=context)
2014-08-17 17:17:14 +00:00
2016-01-06 18:35:03 +00:00
def command_dump_json(*args):
"""
Dump items to json
"""
if not args:
print('usage: ./ctl json_dump dump.json')
sys.exit(1)
import db
2016-01-15 07:59:35 +00:00
import state
2016-01-06 18:35:03 +00:00
with db.session():
2016-01-15 07:59:35 +00:00
library = state.user().library
library.export_json(args[0])
2016-01-06 18:35:03 +00:00
2014-08-17 17:17:14 +00:00
def main():
actions = globals()
commands = [c[8:] for c in actions if c.startswith('command_')]
command = sys.argv[1] if len(sys.argv) > 1 else None
if command and command in commands:
2015-03-07 16:17:52 +00:00
globals()["command_%s"%command](*sys.argv[2:])
2014-08-17 17:17:14 +00:00
else:
2016-01-06 18:35:03 +00:00
print("usage: ./ctl [action]")
2014-08-17 17:17:14 +00:00
indent = max([len(command) for command in commands]) + 4
for command in sorted(commands):
if command in HIDDEN:
continue
2014-08-17 17:17:14 +00:00
space = ' ' * (indent - len(command))
info = actions["command_%s"%command].__doc__.split('\n')
info = [' %s%s' % (' ' * indent, i.strip()) for i in info]
info = '\n'.join(info).strip()
2014-09-02 22:32:44 +00:00
print((" %s%s%s" % (command, space, info)))
2014-08-17 17:17:14 +00:00
sys.exit(1)