openmedialibrary/install

177 lines
6.5 KiB
Text
Raw Normal View History

2015-11-29 15:00:00 +00:00
#!/usr/bin/env python3
from __future__ import division, with_statement, print_function
from contextlib import closing
import json
import os
import sys
import tarfile
from urllib.request import urlopen
from threading import Thread
import subprocess
2016-01-18 05:23:59 +00:00
def makedirs(dirname):
2014-10-31 17:30:09 +00:00
if not os.path.exists(dirname):
os.makedirs(dirname)
class Install(Thread):
base_url = 'http://downloads.openmedialibrary.com/'
status = {}
2016-01-07 10:12:48 +00:00
failed = False
2016-01-18 06:34:20 +00:00
def __init__(self, target, base_url=None):
self.target = target
2016-01-18 06:34:20 +00:00
if base_url:
self.base_url = base_url
Thread.__init__(self)
self.daemon = True
self.start()
def run(self):
2015-11-02 23:28:09 +00:00
if sys.platform.startswith('linux'):
2016-01-07 11:42:00 +00:00
import platform
distro = platform.linux_distribution()
2016-01-16 11:49:46 +00:00
if distro[0] == 'Ubuntu' and distro[1] < '15.10':
2016-01-07 11:42:00 +00:00
try:
from OpenSSL.SSL import TLSv1_2_METHOD
except:
2016-01-16 14:43:54 +00:00
print("To run Open Media Library on versions of Ubuntu before 15.10, you need to manually update pyOpenSSL.")
print("To install the current version of pyOpenSSL, run:")
print("\tsudo apt-get install python3-pip python3-dev libffi-dev libssl-dev libevent-2.0-5")
print("\tsudo pip3 install -U pyOpenSSL")
2016-01-07 11:42:00 +00:00
self.failed = True
return
2014-08-22 16:38:44 +00:00
apt_packages = ''
dnf_packages = ''
try:
from PIL import Image
import lxml
except:
2015-11-04 12:43:10 +00:00
apt_packages += ' python3.4 python3-pil python3-lxml'
dnf_packages += ' python3-pillow python3-lxml'
2016-01-17 06:44:08 +00:00
try:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib
except:
apt_packages += ' gir1.2-gtk-3.0'
#dnf_packages += ' webkitgtk4'
2015-11-04 12:43:10 +00:00
try:
import OpenSSL
2016-01-16 11:49:46 +00:00
from OpenSSL.SSL import TLSv1_2_METHOD
2015-11-04 12:43:10 +00:00
except:
apt_packages += ' python3-openssl'
dnf_packages += ' python3-pyOpenSSL'
try:
import Crypto
except:
apt_packages += ' python3-crypto'
dnf_packages += ' python3-pyCrypto'
2015-11-04 12:43:10 +00:00
if not has_bin('pdftocairo'):
2014-08-22 16:38:44 +00:00
apt_packages += ' poppler-utils'
dnf_packages += ' poppler-utils'
2016-01-17 06:35:08 +00:00
apt_packages = apt_packages.strip()
dnf_packages = dnf_packages.strip()
2014-08-22 16:38:44 +00:00
if has_bin('apt-get') and apt_packages:
2016-01-07 10:24:09 +00:00
print('You need additional packages: %s' % apt_packages)
2016-01-17 06:35:08 +00:00
print('run: sudo apt-get install ' + apt_packages)
2016-01-07 10:24:09 +00:00
self.failed = True
return
elif has_bin('dnf') and dnf_packages:
print('Installing additional packages: %s' % dnf_packages)
2016-01-17 06:35:08 +00:00
print('run: sudo dnf install ' + dnf_packages)
2016-01-07 10:24:09 +00:00
self.failed = True
return
2016-01-07 10:16:42 +00:00
elif apt_packages:
2015-11-04 12:43:10 +00:00
print('You need to install Pillow, lxml and pyOpenSSL\ni.e. sudo pip3 install pillow lxml pyOpenSSL')
2014-08-22 16:38:44 +00:00
if 'poppler' in apt_packages:
2016-01-16 14:43:54 +00:00
print('You need to install pdftocairo (part of poppler-utils).')
print("After installing these packages, run install again.")
2016-01-07 10:12:48 +00:00
self.failed = True
return
2016-01-07 10:09:57 +00:00
target = self.target
2016-01-18 06:34:20 +00:00
target = os.path.normpath(os.path.abspath(target))
2016-01-18 05:23:59 +00:00
makedirs(target)
2016-01-07 10:09:57 +00:00
os.chdir(target)
release = self.get_release()
self.status['release'] = release
2016-01-18 06:34:20 +00:00
print("Installing Open Media Library:")
for module in sorted(release['modules']):
2016-01-07 10:09:57 +00:00
self.status['installing'] = module
self.status['progress'] = 0
self.status['size'] = 0
package_tar = release['modules'][module]['name']
url = self.url(package_tar)
2016-01-18 06:34:20 +00:00
package_tar = os.path.join(target, package_tar)
2016-01-07 10:09:57 +00:00
self.download(url, package_tar)
tar = tarfile.open(package_tar)
tar.extractall()
tar.close()
os.unlink(package_tar)
os.symlink('openmedialibrary/ctl', 'ctl')
self.status['progress'] = 0
self.status['installing'] = 'setup'
2016-01-18 05:23:59 +00:00
makedirs('updates')
2016-01-12 15:00:51 +00:00
os.system('./ctl setup')
2016-01-18 06:34:20 +00:00
release_path = 'data/release.json'
makedirs('data')
with open(release_path, 'w') as fd:
json.dump(release, fd, indent=2)
if sys.platform.startswith('linux'):
os.system('./ctl install_launcher')
2016-01-12 15:00:51 +00:00
self.status['progress'] = 1
self.status['done'] = True
def download(self, url, filename):
2016-01-18 05:23:59 +00:00
makedirs(os.path.dirname(filename))
2016-01-17 06:44:08 +00:00
print('downloading',os.path.basename(filename))
2014-10-31 17:11:03 +00:00
with open(filename, 'wb') as f:
with closing(urlopen(url)) as u:
size = int(u.headers.get('content-length', 0))
self.status['size'] = size
available = 0
data = u.read(4096)
while data:
if size:
available += len(data)
self.status['progress'] = available/size
f.write(data)
data = u.read(4096)
def get_release(self):
with closing(urlopen(self.url('release.json'))) as u:
data = json.loads(u.read().decode('utf-8'))
return data
def url(self, url):
return self.base_url + url
def has_bin(cmd):
return subprocess.call(['which', cmd], stdout=subprocess.PIPE) == 0
if __name__ == '__main__':
if len(sys.argv) == 1:
if sys.platform == 'darwin':
target = os.path.expanduser('~/Library/Application Support/Open Media Libary')
elif sys.platform.startswith('linux'):
target = os.path.expanduser('~/.local/share/openmedialibrary')
else:
target = os.path.join(os.curdir, 'openmedialibrary')
elif len(sys.argv) != 2:
print('usage: %s [target]' % sys.argv[0])
sys.exit(1)
else:
target = sys.argv[1]
if os.path.exists(target):
2014-08-22 16:38:44 +00:00
print('%s exists, remove it before re-installing Open Media Library' % target)
sys.exit(1)
2015-12-02 15:30:17 +00:00
install = Install(target)
install.join()
2016-01-07 10:12:48 +00:00
if not install.failed:
print("You can launch Open Media Library from the menu or run: %s open"%os.path.join(target, 'ctl'))
2016-01-13 06:06:06 +00:00
#subprocess.Popen([os.path.join(target, 'ctl'), 'open'])