No Description

install 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. from __future__ import division, with_statement, print_function
  3. import distutils.spawn
  4. from contextlib import closing
  5. import json
  6. import os
  7. import sys
  8. import tarfile
  9. from urllib.request import urlopen
  10. from threading import Thread
  11. def makedirs(dirname):
  12. if not os.path.exists(dirname):
  13. os.makedirs(dirname)
  14. def get_platform():
  15. name = sys.platform
  16. if name.startswith('darwin'):
  17. name = 'darwin64'
  18. elif name.startswith('linux'):
  19. import platform
  20. machine = platform.machine()
  21. if machine == 'armv7l':
  22. name = 'linux_armv7l'
  23. elif machine == 'aarch64':
  24. name = 'linux_aarch64'
  25. elif machine == 'x86_64':
  26. name = 'linux64'
  27. else:
  28. name = 'linux32'
  29. return name
  30. class Install(Thread):
  31. base_url = 'http://downloads.openmedialibrary.com/'
  32. status = {}
  33. failed = False
  34. def __init__(self, target, base_url=None):
  35. self.target = target
  36. if base_url:
  37. self.base_url = base_url
  38. Thread.__init__(self)
  39. self.daemon = True
  40. self.start()
  41. def run(self):
  42. if sys.platform.startswith('linux'):
  43. import platform
  44. distro = platform.linux_distribution()
  45. if distro[0] == 'Ubuntu' and distro[1] < '15.10':
  46. try:
  47. from OpenSSL.SSL import TLSv1_2_METHOD
  48. except:
  49. print("To run Open Media Library on versions of Ubuntu before 15.10, you need to manually update pyOpenSSL.")
  50. print("To install the current version of pyOpenSSL, run:")
  51. print("\tsudo apt-get install python3-pip python3-dev libffi-dev libssl-dev libevent-2.0-5")
  52. print("\tsudo pip3 install -U pyOpenSSL")
  53. self.failed = True
  54. return
  55. apt_packages = ''
  56. dnf_packages = ''
  57. try:
  58. from PIL import Image
  59. import lxml
  60. except:
  61. apt_packages += ' python3 python3-pil python3-lxml'
  62. dnf_packages += ' python3-pillow python3-lxml'
  63. try:
  64. import gi
  65. gi.require_version('Gtk', '3.0')
  66. from gi.repository import Gtk, GLib
  67. except:
  68. apt_packages += ' gir1.2-gtk-3.0'
  69. #dnf_packages += ' webkitgtk4'
  70. try:
  71. import OpenSSL
  72. from OpenSSL.SSL import TLSv1_2_METHOD
  73. except:
  74. apt_packages += ' python3-openssl'
  75. dnf_packages += ' python3-pyOpenSSL'
  76. try:
  77. import Crypto
  78. except:
  79. apt_packages += ' python3-crypto'
  80. dnf_packages += ' python3-crypto'
  81. if not has_bin('pdftocairo'):
  82. apt_packages += ' poppler-utils'
  83. dnf_packages += ' poppler-utils'
  84. apt_packages = apt_packages.strip()
  85. dnf_packages = dnf_packages.strip()
  86. if has_bin('apt-get'):
  87. if apt_packages:
  88. print('You need additional packages: %s' % apt_packages)
  89. print('run: sudo apt-get install ' + apt_packages)
  90. self.failed = True
  91. return
  92. elif has_bin('dnf'):
  93. if dnf_packages:
  94. print('Installing additional packages: %s' % dnf_packages)
  95. print('run: sudo dnf install ' + dnf_packages)
  96. self.failed = True
  97. return
  98. elif apt_packages:
  99. print('You need to install Pillow, lxml and pyOpenSSL\ni.e. sudo pip3 install pillow lxml pyOpenSSL')
  100. if 'poppler' in apt_packages:
  101. print('You need to install pdftocairo (part of poppler-utils).')
  102. print("After installing these packages, run install again.")
  103. self.failed = True
  104. return
  105. target = self.target
  106. target = os.path.normpath(os.path.abspath(target))
  107. makedirs(target)
  108. os.chdir(target)
  109. release = self.get_release()
  110. self.status['release'] = release
  111. print("Installing Open Media Library:")
  112. platform = get_platform()
  113. for module in sorted(release['modules']):
  114. if release['modules'][module].get('platform', platform) == platform:
  115. self.status['installing'] = module
  116. self.status['progress'] = 0
  117. self.status['size'] = 0
  118. package_tar = release['modules'][module]['name']
  119. url = self.url(package_tar)
  120. package_tar = os.path.join(target, package_tar)
  121. self.download(url, package_tar)
  122. tar = tarfile.open(package_tar)
  123. tar.extractall()
  124. tar.close()
  125. os.unlink(package_tar)
  126. os.symlink('openmedialibrary/ctl', 'ctl')
  127. self.status['progress'] = 0
  128. self.status['installing'] = 'setup'
  129. makedirs('updates')
  130. os.system('./ctl setup')
  131. release_path = 'data/release.json'
  132. makedirs('data')
  133. with open(release_path, 'w') as fd:
  134. json.dump(release, fd, indent=2)
  135. if sys.platform.startswith('linux'):
  136. os.system('./ctl install_launcher')
  137. self.status['progress'] = 1
  138. self.status['done'] = True
  139. def download(self, url, filename):
  140. makedirs(os.path.dirname(filename))
  141. print('downloading',os.path.basename(filename))
  142. with open(filename, 'wb') as f:
  143. with closing(urlopen(url)) as u:
  144. size = int(u.headers.get('content-length', 0))
  145. self.status['size'] = size
  146. available = 0
  147. data = u.read(4096)
  148. while data:
  149. if size:
  150. available += len(data)
  151. self.status['progress'] = available/size
  152. f.write(data)
  153. data = u.read(4096)
  154. def get_release(self):
  155. with closing(urlopen(self.url('release.json'))) as u:
  156. data = json.loads(u.read().decode('utf-8'))
  157. return data
  158. def url(self, url):
  159. return self.base_url + url
  160. def has_bin(cmd):
  161. path = distutils.spawn.find_executable(cmd)
  162. return path != None
  163. if __name__ == '__main__':
  164. if len(sys.argv) == 1:
  165. if sys.platform == 'darwin':
  166. target = os.path.expanduser('~/Library/Application Support/Open Media Libary')
  167. elif sys.platform.startswith('linux'):
  168. data_home = os.path.expanduser(os.environ.get('XDG_DATA_HOME', '~/.local/share'))
  169. target = os.path.join(data_home, 'openmedialibrary')
  170. else:
  171. target = os.path.join(os.curdir, 'openmedialibrary')
  172. elif len(sys.argv) != 2:
  173. print('usage: %s [target]' % sys.argv[0])
  174. sys.exit(1)
  175. else:
  176. target = sys.argv[1]
  177. if os.path.exists(target):
  178. print('%s exists, remove it before re-installing Open Media Library' % target)
  179. sys.exit(1)
  180. install = Install(target)
  181. install.join()
  182. if not install.failed:
  183. print("You can launch Open Media Library from the menu or run: %s open"%os.path.join(target, 'ctl'))
  184. #subprocess.Popen([os.path.join(target, 'ctl'), 'open'])