Debian Codegraph @

setup/linux-installer.sh

raw
1 #!/bin/sh
2 # linux-installer.sh
3 # Copyright (C) 2018 Kovid Goyal <kovid at kovidgoyal.net>
4
5 search_for_python() {
6 # We have to search for python as Ubuntu, in its infinite wisdom decided
7 # to release 18.04 with no python symlink, making it impossible to run polyglot
8 # python scripts.
9
10 # We cannot use command -v as it is not implemented in the posh shell shipped with
11 # Ubuntu/Debian. Similarly, there is no guarantee that which is installed.
12 # Shell scripting is a horrible joke, thank heavens for python.
13 local IFS=:
14 if [ $ZSH_VERSION ]; then
15 # zsh does not split by default
16 setopt sh_word_split
17 fi
18 local candidate_path
19 local candidate_python
20 local pythons=python3:python2
21 # disable pathname expansion (globbing)
22 set -f
23 for candidate_path in $PATH
24 do
25 if [ ! -z $candidate_path ]
26 then
27 for candidate_python in $pythons
28 do
29 if [ ! -z "$candidate_path" ]
30 then
31 if [ -x "$candidate_path/$candidate_python" ]
32 then
33 printf "$candidate_path/$candidate_python"
34 return
35 fi
36 fi
37 done
38 fi
39 done
40 set +f
41 printf "python"
42 }
43
44 PYTHON=$(search_for_python)
45 echo Using python executable: $PYTHON
46
47 $PYTHON -c "import sys; script_launch=lambda:sys.exit('Download of installer failed!'); exec(sys.stdin.read()); script_launch()" "$@" <<'CALIBRE_LINUX_INSTALLER_HEREDOC'
48 # {{{
49 # HEREDOC_START
50 #!/usr/bin/env python
51 # vim:fileencoding=utf-8
52 # License: GPLv3 Copyright: 2009, Kovid Goyal <kovid at kovidgoyal.net>
53 from __future__ import absolute_import, division, print_function, unicode_literals
54
55 import errno
56 import hashlib
57 import os
58 import platform
59 import re
60 import shutil
61 import signal
62 import socket
63 import ssl
64 import stat
65 import subprocess
66 import sys
67 import tempfile
68 from contextlib import closing
69
70 is64bit = platform.architecture()[0] == '64bit'
71 py3 = sys.version_info[0] > 2
72 enc = getattr(sys.stdout, 'encoding', 'utf-8') or 'utf-8'
73 if enc.lower() == 'ascii':
74 enc = 'utf-8'
75 dl_url = calibre_version = signature = None
76 has_ssl_verify = hasattr(ssl, 'create_default_context')
77 is_linux_arm = is_linux_arm64 = False
78 machine = (os.uname()[4] or '').lower()
79 arch = 'x86_64'
80 if machine.startswith('arm') or machine.startswith('aarch64'):
81 is_linux_arm = True
82 is_linux_arm64 = machine.startswith('arm64') or machine.startswith('aarch64')
83 arch = 'arm64'
84
85
86 if py3:
87 unicode = str
88 raw_input = input
89 import http.client as httplib
90 from urllib.parse import urlparse
91 from urllib.request import BaseHandler, Request, addinfourl, build_opener, getproxies, urlopen
92 def encode_for_subprocess(x):
93 return x
94 else:
95 from urllib import addinfourl, getproxies, urlopen
96
97 import httplib
98 from future_builtins import map
99 from urllib2 import BaseHandler, Request, build_opener
100 from urlparse import urlparse
101
102 def encode_for_subprocess(x):
103 if isinstance(x, unicode):
104 x = x.encode(enc)
105 return x
106
107
108 class TerminalController: # {{{
109 BOL = '' #: Move the cursor to the beginning of the line
110 UP = '' #: Move the cursor up one line
111 DOWN = '' #: Move the cursor down one line
112 LEFT = '' #: Move the cursor left one char
113 RIGHT = '' #: Move the cursor right one char
114
115 # Deletion:
116 CLEAR_SCREEN = '' #: Clear the screen and move to home position
117 CLEAR_EOL = '' #: Clear to the end of the line.
118 CLEAR_BOL = '' #: Clear to the beginning of the line.
119 CLEAR_EOS = '' #: Clear to the end of the screen
120
121 # Output modes:
122 BOLD = '' #: Turn on bold mode
123 BLINK = '' #: Turn on blink mode
124 DIM = '' #: Turn on half-bright mode
125 REVERSE = '' #: Turn on reverse-video mode
126 NORMAL = '' #: Turn off all modes
127
128 # Cursor display:
129 HIDE_CURSOR = '' #: Make the cursor invisible
130 SHOW_CURSOR = '' #: Make the cursor visible
131
132 # Terminal size:
133 COLS = None #: Width of the terminal (None for unknown)
134 LINES = None #: Height of the terminal (None for unknown)
135
136 # Foreground colors:
137 BLACK = BLUE = GREEN = CYAN = RED = MAGENTA = YELLOW = WHITE = ''
138
139 # Background colors:
140 BG_BLACK = BG_BLUE = BG_GREEN = BG_CYAN = ''
141 BG_RED = BG_MAGENTA = BG_YELLOW = BG_WHITE = ''
142
143 _STRING_CAPABILITIES = """
144 BOL=cr UP=cuu1 DOWN=cud1 LEFT=cub1 RIGHT=cuf1
145 CLEAR_SCREEN=clear CLEAR_EOL=el CLEAR_BOL=el1 CLEAR_EOS=ed BOLD=bold
146 BLINK=blink DIM=dim REVERSE=rev UNDERLINE=smul NORMAL=sgr0
147 HIDE_CURSOR=cinvis SHOW_CURSOR=cnorm""".split()
148 _COLORS = """BLACK BLUE GREEN CYAN RED MAGENTA YELLOW WHITE""".split()
149 _ANSICOLORS = "BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE".split()
150
151 def __init__(self, term_stream=sys.stdout):
152 # Curses isn't available on all platforms
153 try:
154 import curses
155 except:
156 return
157
158 # If the stream isn't a tty, then assume it has no capabilities.
159 if not hasattr(term_stream, 'isatty') or not term_stream.isatty():
160 return
161
162 # Check the terminal type. If we fail, then assume that the
163 # terminal has no capabilities.
164 try:
165 curses.setupterm()
166 except:
167 return
168
169 # Look up numeric capabilities.
170 self.COLS = curses.tigetnum('cols')
171 self.LINES = curses.tigetnum('lines')
172
173 # Look up string capabilities.
174 for capability in self._STRING_CAPABILITIES:
175 (attrib, cap_name) = capability.split('=')
176 setattr(self, attrib, self._escape_code(self._tigetstr(cap_name)))
177
178 # Colors
179 set_fg = self._tigetstr('setf')
180 if set_fg:
181 if not isinstance(set_fg, bytes):
182 set_fg = set_fg.encode('utf-8')
183 for i,color in zip(range(len(self._COLORS)), self._COLORS):
184 setattr(self, color,
185 self._escape_code(curses.tparm((set_fg), i)))
186 set_fg_ansi = self._tigetstr('setaf')
187 if set_fg_ansi:
188 if not isinstance(set_fg_ansi, bytes):
189 set_fg_ansi = set_fg_ansi.encode('utf-8')
190 for i,color in zip(range(len(self._ANSICOLORS)), self._ANSICOLORS):
191 setattr(self, color,
192 self._escape_code(curses.tparm((set_fg_ansi),
193 i)))
194 set_bg = self._tigetstr('setb')
195 if set_bg:
196 if not isinstance(set_bg, bytes):
197 set_bg = set_bg.encode('utf-8')
198 for i,color in zip(range(len(self._COLORS)), self._COLORS):
199 setattr(self, 'BG_'+color,
200 self._escape_code(curses.tparm((set_bg), i)))
201 set_bg_ansi = self._tigetstr('setab')
202 if set_bg_ansi:
203 if not isinstance(set_bg_ansi, bytes):
204 set_bg_ansi = set_bg_ansi.encode('utf-8')
205 for i,color in zip(range(len(self._ANSICOLORS)), self._ANSICOLORS):
206 setattr(self, 'BG_'+color,
207 self._escape_code(curses.tparm((set_bg_ansi),
208 i)))
209
210 def _escape_code(self, raw):
211 if not raw:
212 raw = ''
213 if not isinstance(raw, unicode):
214 raw = raw.decode('ascii')
215 return raw
216
217 def _tigetstr(self, cap_name):
218 # String capabilities can include "delays" of the form "$<2>".
219 # For any modern terminal, we should be able to just ignore
220 # these, so strip them out.
221 import curses
222 if isinstance(cap_name, bytes):
223 cap_name = cap_name.decode('utf-8')
224 cap = self._escape_code(curses.tigetstr(cap_name))
225 return re.sub(r'\$<\d+>[/*]?', '', cap)
226
227 def render(self, template):
228 return re.sub(r'\$\$|\${\w+}', self._render_sub, template)
229
230 def _render_sub(self, match):
231 s = match.group()
232 if s == '$$':
233 return s
234 else:
235 return getattr(self, s[2:-1])
236
237
238 class ProgressBar:
239 BAR = '%3d%% ${GREEN}[${BOLD}%s%s${NORMAL}${GREEN}]${NORMAL}\n'
240 HEADER = '${BOLD}${CYAN}%s${NORMAL}\n\n'
241
242 def __init__(self, term, header):
243 self.term = term
244 if not (self.term.CLEAR_EOL and self.term.UP and self.term.BOL):
245 raise ValueError("Terminal isn't capable enough -- you "
246 "should use a simpler progress display.")
247 self.width = self.term.COLS or 75
248 self.bar = term.render(self.BAR)
249 self.header = self.term.render(self.HEADER % header.center(self.width))
250 self.cleared = 1 # : true if we haven't drawn the bar yet.
251
252 def update(self, percent, message=''):
253 out = (sys.stdout.buffer if py3 else sys.stdout)
254 if self.cleared:
255 out.write(self.header.encode(enc))
256 self.cleared = 0
257 n = int((self.width-10)*percent)
258 msg = message.center(self.width)
259 msg = (self.term.BOL + self.term.UP + self.term.CLEAR_EOL + (
260 self.bar % (100*percent, '='*n, '-'*(self.width-10-n))) + self.term.CLEAR_EOL + msg).encode(enc)
261 out.write(msg)
262 out.flush()
263
264 def clear(self):
265 out = (sys.stdout.buffer if py3 else sys.stdout)
266 if not self.cleared:
267 out.write((self.term.BOL + self.term.CLEAR_EOL + self.term.UP + self.term.CLEAR_EOL + self.term.UP + self.term.CLEAR_EOL).encode(enc))
268 self.cleared = 1
269 out.flush()
270 # }}}
271
272
273 def prints(*args, **kwargs): # {{{
274 f = kwargs.get('file', sys.stdout.buffer if py3 else sys.stdout)
275 end = kwargs.get('end', b'\n')
276 enc = getattr(f, 'encoding', 'utf-8') or 'utf-8'
277
278 if isinstance(end, unicode):
279 end = end.encode(enc)
280 for x in args:
281 if isinstance(x, unicode):
282 x = x.encode(enc)
283 f.write(x)
284 f.write(b' ')
285 f.write(end)
286 if py3 and f is sys.stdout.buffer:
287 f.flush()
288 # }}}
289
290
291 class Reporter: # {{{
292
293 def __init__(self, fname):
294 try:
295 self.pb = ProgressBar(TerminalController(), 'Downloading '+fname)
296 except ValueError:
297 prints('Downloading', fname)
298 self.pb = None
299 self.last_percent = 0
300
301 def __call__(self, blocks, block_size, total_size):
302 percent = (blocks*block_size)/float(total_size)
303 if self.pb is None:
304 if percent - self.last_percent > 0.05:
305 self.last_percent = percent
306 prints('Downloaded {0:%}'.format(percent))
307 else:
308 try:
309 self.pb.update(percent)
310 except:
311 import traceback
312 traceback.print_exc()
313 # }}}
314
315
316 # Downloading {{{
317
318 def clean_cache(cache, fname):
319 for x in os.listdir(cache):
320 if fname not in x:
321 os.remove(os.path.join(cache, x))
322
323
324 def check_signature(dest, signature):
325 if not os.path.exists(dest):
326 return None
327 m = hashlib.sha512()
328 with open(dest, 'rb') as f:
329 raw = f.read()
330 m.update(raw)
331 if m.hexdigest().encode('ascii') == signature:
332 return raw
333
334
335 class RangeHandler(BaseHandler):
336
337 def http_error_206(self, req, fp, code, msg, hdrs):
338 # 206 Partial Content Response
339 r = addinfourl(fp, hdrs, req.get_full_url())
340 r.code = code
341 r.msg = msg
342 return r
343 https_error_206 = http_error_206
344
345
346 def do_download(dest):
347 prints('Will download and install', os.path.basename(dest))
348 reporter = Reporter(os.path.basename(dest))
349 offset = 0
350 if os.path.exists(dest):
351 offset = os.path.getsize(dest)
352
353 # Get content length and check if range is supported
354 rq = urlopen(dl_url)
355 headers = rq.info()
356 size = int(headers['content-length'])
357 accepts_ranges = headers.get('accept-ranges', None) == 'bytes'
358 mode = 'wb'
359 if accepts_ranges and offset > 0:
360 req = Request(rq.geturl())
361 req.add_header('Range', 'bytes=%s-'%offset)
362 mode = 'ab'
363 rq.close()
364 handler = RangeHandler()
365 opener = build_opener(handler)
366 rq = opener.open(req)
367 with open(dest, mode) as f:
368 while f.tell() < size:
369 raw = rq.read(8192)
370 if not raw:
371 break
372 f.write(raw)
373 reporter(f.tell(), 1, size)
374 rq.close()
375 if os.path.getsize(dest) < size:
376 print('Download failed, try again later')
377 raise SystemExit(1)
378 prints('Downloaded %s bytes'%os.path.getsize(dest))
379
380
381 def download_tarball():
382 fname = 'calibre-%s-%s.%s'%(calibre_version, arch, 'txz')
383 tdir = tempfile.gettempdir()
384 cache = os.path.join(tdir, 'calibre-installer-cache')
385 if not os.path.exists(cache):
386 os.makedirs(cache)
387 clean_cache(cache, fname)
388 dest = os.path.join(cache, fname)
389 raw = check_signature(dest, signature)
390 if raw is not None:
391 print('Using previously downloaded', fname)
392 return raw
393 cached_sigf = dest +'.signature'
394 cached_sig = None
395 if os.path.exists(cached_sigf):
396 with open(cached_sigf, 'rb') as sigf:
397 cached_sig = sigf.read()
398 if cached_sig != signature and os.path.exists(dest):
399 os.remove(dest)
400 try:
401 with open(cached_sigf, 'wb') as f:
402 f.write(signature)
403 except IOError as e:
404 if e.errno != errno.EACCES:
405 raise
406 print('The installer cache directory has incorrect permissions.'
407 ' Delete %s and try again.'%cache)
408 raise SystemExit(1)
409 do_download(dest)
410 prints('Checking downloaded file integrity...')
411 raw = check_signature(dest, signature)
412 if raw is None:
413 os.remove(dest)
414 print('The downloaded files\' signature does not match. '
415 'Try the download again later.')
416 raise SystemExit(1)
417 return raw
418 # }}}
419
420
421 # Get tarball signature securely {{{
422
423 def get_proxies(debug=True):
424 proxies = getproxies()
425 for key, proxy in list(proxies.items()):
426 if not proxy or '..' in proxy:
427 del proxies[key]
428 continue
429 if proxy.startswith(key+'://'):
430 proxy = proxy[len(key)+3:]
431 if key == 'https' and proxy.startswith('http://'):
432 proxy = proxy[7:]
433 if proxy.endswith('/'):
434 proxy = proxy[:-1]
435 if len(proxy) > 4:
436 proxies[key] = proxy
437 else:
438 prints('Removing invalid', key, 'proxy:', proxy)
439 del proxies[key]
440
441 if proxies and debug:
442 prints('Using proxies:', repr(proxies))
443 return proxies
444
445
446 class HTTPError(ValueError):
447
448 def __init__(self, url, code):
449 msg = '%s returned an unsupported http response code: %d (%s)' % (
450 url, code, httplib.responses.get(code, None))
451 ValueError.__init__(self, msg)
452 self.code = code
453 self.url = url
454
455
456 class CertificateError(ValueError):
457 pass
458
459
460 def _dnsname_match(dn, hostname, max_wildcards=1):
461 """Matching according to RFC 6125, section 6.4.3
462
463 http://tools.ietf.org/html/rfc6125#section-6.4.3
464 """
465 pats = []
466 if not dn:
467 return False
468
469 parts = dn.split(r'.')
470 leftmost, remainder = parts[0], parts[1:]
471
472 wildcards = leftmost.count('*')
473 if wildcards > max_wildcards:
474 # Issue #17980: avoid denials of service by refusing more
475 # than one wildcard per fragment. A survery of established
476 # policy among SSL implementations showed it to be a
477 # reasonable choice.
478 raise CertificateError(
479 "too many wildcards in certificate DNS name: " + repr(dn))
480
481 # speed up common case w/o wildcards
482 if not wildcards:
483 return dn.lower() == hostname.lower()
484
485 # RFC 6125, section 6.4.3, subitem 1.
486 # The client SHOULD NOT attempt to match a presented identifier in which
487 # the wildcard character comprises a label other than the left-most label.
488 if leftmost == '*':
489 # When '*' is a fragment by itself, it matches a non-empty dotless
490 # fragment.
491 pats.append('[^.]+')
492 elif leftmost.startswith('xn--') or hostname.startswith('xn--'):
493 # RFC 6125, section 6.4.3, subitem 3.
494 # The client SHOULD NOT attempt to match a presented identifier
495 # where the wildcard character is embedded within an A-label or
496 # U-label of an internationalized domain name.
497 pats.append(re.escape(leftmost))
498 else:
499 # Otherwise, '*' matches any dotless string, e.g. www*
500 pats.append(re.escape(leftmost).replace(r'\*', '[^.]*'))
501
502 # add the remaining fragments, ignore any wildcards
503 for frag in remainder:
504 pats.append(re.escape(frag))
505
506 pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
507 return pat.match(hostname)
508
509
510 def match_hostname(cert, hostname):
511 """Verify that *cert* (in decoded format as returned by
512 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
513 rules are followed, but IP addresses are not accepted for *hostname*.
514
515 CertificateError is raised on failure. On success, the function
516 returns nothing.
517 """
518 if not cert:
519 raise ValueError("empty or no certificate")
520 dnsnames = []
521 san = cert.get('subjectAltName', ())
522 for key, value in san:
523 if key == 'DNS':
524 if _dnsname_match(value, hostname):
525 return
526 dnsnames.append(value)
527 if not dnsnames:
528 # The subject is only checked when there is no dNSName entry
529 # in subjectAltName
530 for sub in cert.get('subject', ()):
531 for key, value in sub:
532 # XXX according to RFC 2818, the most specific Common Name
533 # must be used.
534 if key == 'commonName':
535 if _dnsname_match(value, hostname):
536 return
537 dnsnames.append(value)
538
539 if len(dnsnames) > 1:
540 raise CertificateError("hostname %r "
541 "doesn't match either of %s"
542 % (hostname, ', '.join(map(repr, dnsnames))))
543 elif len(dnsnames) == 1:
544 # python 2.7.2 does not read subject alt names thanks to this
545 # bug: http://bugs.python.org/issue13034
546 # And the utter lunacy that is the linux landscape could have
547 # any old version of python whatsoever with or without a hot fix for
548 # this bug. Not to mention that python 2.6 may or may not
549 # read alt names depending on its patchlevel. So we just bail on full
550 # verification if the python version is less than 2.7.3.
551 # Linux distros are one enormous, honking disaster.
552 if sys.version_info[:3] < (2, 7, 3) and dnsnames[0] == 'calibre-ebook.com':
553 return
554 raise CertificateError("hostname %r "
555 "doesn't match %r"
556 % (hostname, dnsnames[0]))
557 else:
558 raise CertificateError("no appropriate commonName or "
559 "subjectAltName fields were found")
560
561
562 if has_ssl_verify:
563 class HTTPSConnection(httplib.HTTPSConnection):
564
565 def __init__(self, ssl_version, *args, **kwargs):
566 kwargs['context'] = ssl.create_default_context(cafile=kwargs.pop('cert_file'))
567 if hasattr(ssl, 'VERIFY_X509_STRICT'):
568 kwargs['context'].verify_flags &= ~ssl.VERIFY_X509_STRICT
569 httplib.HTTPSConnection.__init__(self, *args, **kwargs)
570 else:
571 class HTTPSConnection(httplib.HTTPSConnection):
572
573 def __init__(self, ssl_version, *args, **kwargs):
574 httplib.HTTPSConnection.__init__(self, *args, **kwargs)
575 self.calibre_ssl_version = ssl_version
576
577 def connect(self):
578 """Connect to a host on a given (SSL) port, properly verifying the SSL
579 certificate, both that it is valid and that its declared hostnames
580 match the hostname we are connecting to."""
581
582 if hasattr(self, 'source_address'):
583 sock = socket.create_connection((self.host, self.port),
584 self.timeout, self.source_address)
585 else:
586 # python 2.6 has no source_address
587 sock = socket.create_connection((self.host, self.port), self.timeout)
588 if self._tunnel_host:
589 self.sock = sock
590 self._tunnel()
591 self.sock = ssl.wrap_socket(sock, cert_reqs=ssl.CERT_REQUIRED, ca_certs=self.cert_file, ssl_version=self.calibre_ssl_version)
592 getattr(ssl, 'match_hostname', match_hostname)(self.sock.getpeercert(), self.host)
593
594 CACERT = b'''\
595 -----BEGIN CERTIFICATE-----
596 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
627 nR0=
628 -----END CERTIFICATE-----
629 '''
630
631
632 def get_https_resource_securely(url, timeout=60, max_redirects=5, ssl_version=None):
633 '''
634 Download the resource pointed to by url using https securely (verify server
635 certificate). Ensures that redirects, if any, are also downloaded
636 securely. Needs a CA certificates bundle (in PEM format) to verify the
637 server's certificates.
638 '''
639 if ssl_version is None:
640 ssl_version = getattr(ssl, 'PROTOCOL_TLS_CLIENT', None) or getattr(ssl, 'PROTOCOL_TLSv1_2', ssl.PROTOCOL_TLSv1)
641 with tempfile.NamedTemporaryFile(prefix='calibre-ca-cert-') as f:
642 f.write(CACERT)
643 f.flush()
644 p = urlparse(url)
645 if p.scheme != 'https':
646 raise ValueError('URL %s scheme must be https, not %r' % (url, p.scheme))
647
648 hostname, port = p.hostname, p.port
649 proxies = get_proxies()
650 has_proxy = False
651 for q in ('https', 'http'):
652 if q in proxies:
653 try:
654 h, po = proxies[q].rpartition(':')[::2]
655 po = int(po)
656 if h:
657 hostname, port, has_proxy = h, po, True
658 break
659 except Exception:
660 # Invalid proxy, ignore
661 pass
662
663 c = HTTPSConnection(ssl_version, hostname, port, cert_file=f.name, timeout=timeout)
664 if has_proxy:
665 c.set_tunnel(p.hostname, p.port)
666
667 with closing(c):
668 c.connect() # This is needed for proxy connections
669 path = p.path or '/'
670 if p.query:
671 path += '?' + p.query
672 c.request('GET', path)
673 response = c.getresponse()
674 if response.status in (httplib.MOVED_PERMANENTLY, httplib.FOUND, httplib.SEE_OTHER):
675 if max_redirects <= 0:
676 raise ValueError('Too many redirects, giving up')
677 newurl = response.getheader('Location', None)
678 if newurl is None:
679 raise ValueError('%s returned a redirect response with no Location header' % url)
680 return get_https_resource_securely(
681 newurl, timeout=timeout, max_redirects=max_redirects-1, ssl_version=ssl_version)
682 if response.status != httplib.OK:
683 raise HTTPError(url, response.status)
684 return response.read()
685 # }}}
686
687
688 def extract_tarball(raw, destdir):
689 prints('Extracting application files...')
690 with open('/dev/null', 'w') as null:
691 p = subprocess.Popen(
692 list(map(encode_for_subprocess, ['tar', 'xJof', '-', '-C', destdir])),
693 stdout=null, stdin=subprocess.PIPE, close_fds=True, preexec_fn=lambda:
694 signal.signal(signal.SIGPIPE, signal.SIG_DFL))
695 p.stdin.write(raw)
696 p.stdin.close()
697 if p.wait() != 0:
698 prints('Extracting of application files failed with error code: %s' % p.returncode)
699 raise SystemExit(1)
700
701
702 def get_tarball_info(version):
703 global dl_url, signature, calibre_version
704 print('Downloading tarball signature securely...')
705 if version:
706 sigfname = 'calibre-' + version + '-' + arch + '.txz.sha512'
707 try:
708 signature = get_https_resource_securely('https://code.calibre-ebook.com/signatures/' + sigfname)
709 except HTTPError as err:
710 if err.code != 404:
711 raise
712 signature = get_https_resource_securely('https://code.calibre-ebook.com/signatures/old/' + sigfname)
713 calibre_version = version
714 dl_url = 'https://download.calibre-ebook.com/' + version + '/calibre-' + version + '-' + arch + '.txz'
715 else:
716 raw = get_https_resource_securely(
717 'https://code.calibre-ebook.com/tarball-info/' + arch)
718 signature, calibre_version = raw.rpartition(b'@')[::2]
719 dl_url = 'https://calibre-ebook.com/dist/linux-' + arch
720 if not signature or not calibre_version:
721 raise ValueError('Failed to get install file signature, invalid signature returned')
722 dl_url = os.environ.get('CALIBRE_INSTALLER_LOCAL_URL', dl_url)
723 if isinstance(calibre_version, bytes):
724 calibre_version = calibre_version.decode('utf-8')
725
726
727 def download_and_extract(destdir, version):
728 get_tarball_info(version)
729 raw = download_tarball()
730
731 if os.path.exists(destdir):
732 shutil.rmtree(destdir)
733 os.makedirs(destdir)
734
735 print('Extracting files to %s ...'%destdir)
736 extract_tarball(raw, destdir)
737
738
739 def run_installer(install_dir, isolated, bin_dir, share_dir, version):
740 destdir = os.path.abspath(os.path.expanduser(install_dir or '/opt'))
741 if destdir == '/usr/bin':
742 prints(destdir, 'is not a valid install location. Choose', end='')
743 prints('a location like /opt or /usr/local')
744 return 1
745 destdir = os.path.realpath(os.path.join(destdir, 'calibre'))
746 if os.path.exists(destdir):
747 if not os.path.isdir(destdir):
748 prints(destdir, 'exists and is not a directory. Choose a location like /opt or /usr/local')
749 return 1
750 print('Installing to', destdir)
751
752 download_and_extract(destdir, version)
753
754 if not isolated:
755 pi = [os.path.join(destdir, 'calibre_postinstall')]
756 if bin_dir is not None:
757 pi.extend(['--bindir', bin_dir])
758 if share_dir is not None:
759 pi.extend(['--sharedir', share_dir])
760 subprocess.call(pi)
761 prints('Run "calibre" to start calibre')
762 else:
763 prints('Run "%s/calibre" to start calibre' % destdir)
764 return 0
765
766
767 def check_umask():
768 # A bad umask can cause system breakage because of bugs in xdg-mime
769 # See https://www.mobileread.com/forums/showthread.php?t=277803
770 mask = os.umask(18) # 18 = 022
771 os.umask(mask)
772 forbid_user_read = mask & stat.S_IRUSR
773 forbid_user_exec = mask & stat.S_IXUSR
774 forbid_group_read = mask & stat.S_IRGRP
775 forbid_group_exec = mask & stat.S_IXGRP
776 forbid_other_read = mask & stat.S_IROTH
777 forbid_other_exec = mask & stat.S_IXOTH
778 if forbid_user_read or forbid_user_exec or forbid_group_read or forbid_group_exec or forbid_other_read or forbid_other_exec:
779 prints(
780 'WARNING: Your current umask disallows reading of files by some users,'
781 ' this can cause system breakage when running the installer because'
782 ' of bugs in common system utilities.'
783 )
784 sys.stdin = open('/dev/tty') # stdin is a pipe from wget
785 while True:
786 q = raw_input('Should the installer (f)ix the umask, (i)gnore it or (a)bort [f/i/a Default is abort]: ') or 'a'
787 if q in 'f i a'.split():
788 break
789 prints('Response', q, 'not understood')
790 if q == 'f':
791 mask = mask & ~stat.S_IRUSR & ~stat.S_IXUSR & ~stat.S_IRGRP & ~stat.S_IXGRP & ~stat.S_IROTH & ~stat.S_IXOTH
792 os.umask(mask)
793 prints('umask changed to: {:03o}'.format(mask))
794 elif q == 'i':
795 prints('Ignoring bad umask and proceeding anyway, you have been warned!')
796 else:
797 raise SystemExit('The system umask is unsuitable, aborting')
798
799
800 def check_for_libEGL():
801 import ctypes
802 try:
803 ctypes.CDLL('libEGL.so.1')
804 return
805 except Exception:
806 pass
807 raise SystemExit('You are missing the system library libEGL.so.1. Try installing packages such as libegl1 and libopengl0')
808
809
810 def check_for_libOpenGl():
811 import ctypes
812 try:
813 ctypes.CDLL('libOpenGL.so.0')
814 return
815 except Exception:
816 pass
817 raise SystemExit('You are missing the system library libOpenGL.so.0. Try installing packages such as libopengl0')
818
819
820 def check_for_libxcb_cursor():
821 import ctypes
822 try:
823 ctypes.CDLL('libxcb-cursor.so.0')
824 return
825 except Exception:
826 pass
827 raise SystemExit('You are missing the system library libxcb-cursor.so.0. Try installing packages such as libxcb-cursor0 or xcb-cursor')
828
829
830 def check_glibc_version(min_required=(2, 31), release_date='2020-02-01'):
831 # See https://sourceware.org/glibc/wiki/Glibc%20Timeline
832 import ctypes
833 libc = ctypes.CDLL(None)
834 try:
835 f = libc.gnu_get_libc_version
836 except AttributeError:
837 raise SystemExit('Your system is not based on GNU libc. The calibre binaries require GNU libc')
838 f.restype = ctypes.c_char_p
839 ver = f().decode('ascii')
840 q = tuple(map(int, ver.split('.')))
841 if q < min_required:
842 raise SystemExit(
843 ('Your system has GNU libc version {}. The calibre binaries require at least'
844 ' version: {} (released on {}). Update your system.'
845 ).format(ver, '.'.join(map(str, min_required)), release_date))
846
847
848 def check_for_recent_freetype():
849 import ctypes
850 f = None
851 try:
852 f = ctypes.CDLL('libfreetype.so.6')
853 except OSError:
854 raise SystemExit('Your system is missing the FreeType library libfreetype.so. Try installing the freetype package.')
855 try:
856 f.FT_Get_Color_Glyph_Paint
857 except AttributeError:
858 raise SystemExit('Your system has too old a version of the FreeType library.'
859 ' freetype >= 2.11 is needed for the FT_Get_Color_Glyph_Paint function which is required by Qt WebEngine')
860
861
862 def main(install_dir=None, isolated=False, bin_dir=None, share_dir=None, ignore_umask=False, version=None):
863 if not ignore_umask and not isolated:
864 check_umask()
865 if (is_linux_arm and not is_linux_arm64) or not is64bit:
866 raise SystemExit(
867 'You are running on a 32-bit system. The calibre binaries are only'
868 ' available for 64-bit systems. You will have to compile from'
869 ' source.')
870 glibc_versions = {
871 (6, 0, 0) : {'min_required': (2, 31), 'release_date': '2020-02-01'},
872 (7, 17, 0) : {'min_required': (2, 35), 'release_date': '2022-02-03'}
873 }
874 if is_linux_arm64:
875 glibc_versions.update({
876 (6, 8, 0) : {'min_required': (2, 34), 'release_date': '2021-08-02'}
877 })
878 q = tuple(map(int, version.split('.'))) if version else (sys.maxsize, 999, 999)
879 for key in sorted(glibc_versions, reverse=True):
880 if q >= key:
881 check_glibc_version(**glibc_versions[key])
882 break
883 if q[0] >= 6:
884 check_for_libEGL()
885 check_for_libOpenGl()
886 if q[0] >= 7:
887 check_for_libxcb_cursor()
888 if q >= (7, 16, 0):
889 check_for_recent_freetype()
890 run_installer(install_dir, isolated, bin_dir, share_dir, version)
891
892
893 try:
894 __file__
895 from_file = True
896 except NameError:
897 from_file = False
898
899
900 def update_installer_wrapper():
901 # To update: python3 -c "import runpy; runpy.run_path('setup/linux-installer.py', run_name='update_wrapper')"
902 with open(__file__, 'rb') as f:
903 src = f.read().decode('utf-8')
904 wrapper = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'linux-installer.sh')
905 with open(wrapper, 'r+b') as f:
906 raw = f.read().decode('utf-8')
907 nraw = re.sub(r'^# HEREDOC_START.+^# HEREDOC_END', lambda m: '# HEREDOC_START\n{}\n# HEREDOC_END'.format(src), raw, flags=re.MULTILINE | re.DOTALL)
908 if 'update_installer_wrapper()' not in nraw:
909 raise SystemExit('regex substitute of HEREDOC failed')
910 f.seek(0), f.truncate()
911 f.write(nraw.encode('utf-8'))
912
913
914 def script_launch():
915 def path(x):
916 return os.path.expanduser(x)
917
918 def to_bool(x):
919 return x.lower() in ('y', 'yes', '1', 'true')
920
921 type_map = {x: path for x in 'install_dir isolated bin_dir share_dir ignore_umask version'.split()}
922 type_map['isolated'] = type_map['ignore_umask'] = to_bool
923 kwargs = {}
924
925 for arg in sys.argv[1:]:
926 if arg:
927 m = re.match('([a-z_]+)=(.+)', arg)
928 if m is None:
929 raise SystemExit('Unrecognized command line argument: ' + arg)
930 k = m.group(1)
931 if k not in type_map:
932 raise SystemExit('Unrecognized command line argument: ' + arg)
933 kwargs[k] = type_map[k](m.group(2))
934 main(**kwargs)
935
936
937 if __name__ == '__main__' and from_file:
938 main()
939 elif __name__ == 'update_wrapper':
940 update_installer_wrapper()
941
942 # HEREDOC_END
943 # }}}
944 CALIBRE_LINUX_INSTALLER_HEREDOC