Debian Codegraph @

setup/linux-installer.sh

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/bin/sh`.
2 # linux-installer.sh
3 # Copyright (C) 2018 Kovid Goyal <kovid at kovidgoyal.net>
4
5 search_for_pythonscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. $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 [scip-shell system . . `[`. ! -z $candidate_path ]
26 then
27 for candidate_python in $pythons
28 do
29 if [scip-shell system . . `[`. ! -z "$candidate_path" ]
30 then
31 if [scip-shell system . . `[`. -x "$candidate_path/$candidate_python" ]
32 then
33 printfscip-shell system . . printf. "$candidate_path/$candidate_python"
34 return
35 fi
36 fi
37 done
38 fi
39 done
40 set +f
41 printfscip-shell system . . printf. "python"
42 }
43
44 PYTHON=$(search_for_pythonscip-shell shell shell-project 0.0.0 search_for_python().)
45 echoscip-shell system . . 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 try:
641 ssl_version = ssl.PROTOCOL_TLSv1_2
642 except AttributeError:
643 ssl_version = ssl.PROTOCOL_TLSv1 # old python
644 with tempfile.NamedTemporaryFile(prefix='calibre-ca-cert-') as f:
645 f.write(CACERT)
646 f.flush()
647 p = urlparse(url)
648 if p.scheme != 'https':
649 raise ValueError('URL %s scheme must be https, not %r' % (url, p.scheme))
650
651 hostname, port = p.hostname, p.port
652 proxies = get_proxies()
653 has_proxy = False
654 for q in ('https', 'http'):
655 if q in proxies:
656 try:
657 h, po = proxies[q].rpartition(':')[::2]
658 po = int(po)
659 if h:
660 hostname, port, has_proxy = h, po, True
661 break
662 except Exception:
663 # Invalid proxy, ignore
664 pass
665
666 c = HTTPSConnection(ssl_version, hostname, port, cert_file=f.name, timeout=timeout)
667 if has_proxy:
668 c.set_tunnel(p.hostname, p.port)
669
670 with closing(c):
671 c.connect() # This is needed for proxy connections
672 path = p.path or '/'
673 if p.query:
674 path += '?' + p.query
675 c.request('GET', path)
676 response = c.getresponse()
677 if response.status in (httplib.MOVED_PERMANENTLY, httplib.FOUND, httplib.SEE_OTHER):
678 if max_redirects <= 0:
679 raise ValueError('Too many redirects, giving up')
680 newurl = response.getheader('Location', None)
681 if newurl is None:
682 raise ValueError('%s returned a redirect response with no Location header' % url)
683 return get_https_resource_securely(
684 newurl, timeout=timeout, max_redirects=max_redirects-1, ssl_version=ssl_version)
685 if response.status != httplib.OK:
686 raise HTTPError(url, response.status)
687 return response.read()
688 # }}}
689
690
691 def extract_tarball(raw, destdir):
692 prints('Extracting application files...')
693 with open('/dev/null', 'w') as null:
694 p = subprocess.Popen(
695 list(map(encode_for_subprocess, ['tar', 'xJof', '-', '-C', destdir])),
696 stdout=null, stdin=subprocess.PIPE, close_fds=True, preexec_fn=lambda:
697 signal.signal(signal.SIGPIPE, signal.SIG_DFL))
698 p.stdin.write(raw)
699 p.stdin.close()
700 if p.wait() != 0:
701 prints('Extracting of application files failed with error code: %s' % p.returncode)
702 raise SystemExit(1)
703
704
705 def get_tarball_info(version):
706 global dl_url, signature, calibre_version
707 print('Downloading tarball signature securely...')
708 if version:
709 sigfname = 'calibre-' + version + '-' + arch + '.txz.sha512'
710 try:
711 signature = get_https_resource_securely('https://code.calibre-ebook.com/signatures/' + sigfname)
712 except HTTPError as err:
713 if err.code != 404:
714 raise
715 signature = get_https_resource_securely('https://code.calibre-ebook.com/signatures/old/' + sigfname)
716 calibre_version = version
717 dl_url = 'https://download.calibre-ebook.com/' + version + '/calibre-' + version + '-' + arch + '.txz'
718 else:
719 raw = get_https_resource_securely(
720 'https://code.calibre-ebook.com/tarball-info/' + arch)
721 signature, calibre_version = raw.rpartition(b'@')[::2]
722 dl_url = 'https://calibre-ebook.com/dist/linux-' + arch
723 if not signature or not calibre_version:
724 raise ValueError('Failed to get install file signature, invalid signature returned')
725 dl_url = os.environ.get('CALIBRE_INSTALLER_LOCAL_URL', dl_url)
726 if isinstance(calibre_version, bytes):
727 calibre_version = calibre_version.decode('utf-8')
728
729
730 def download_and_extract(destdir, version):
731 get_tarball_info(version)
732 raw = download_tarball()
733
734 if os.path.exists(destdir):
735 shutil.rmtree(destdir)
736 os.makedirs(destdir)
737
738 print('Extracting files to %s ...'%destdir)
739 extract_tarball(raw, destdir)
740
741
742 def run_installer(install_dir, isolated, bin_dir, share_dir, version):
743 destdir = os.path.abspath(os.path.expanduser(install_dir or '/opt'))
744 if destdir == '/usr/bin':
745 prints(destdir, 'is not a valid install location. Choose', end='')
746 prints('a location like /opt or /usr/local')
747 return 1
748 destdir = os.path.realpath(os.path.join(destdir, 'calibre'))
749 if os.path.exists(destdir):
750 if not os.path.isdir(destdir):
751 prints(destdir, 'exists and is not a directory. Choose a location like /opt or /usr/local')
752 return 1
753 print('Installing to', destdir)
754
755 download_and_extract(destdir, version)
756
757 if not isolated:
758 pi = [os.path.join(destdir, 'calibre_postinstall')]
759 if bin_dir is not None:
760 pi.extend(['--bindir', bin_dir])
761 if share_dir is not None:
762 pi.extend(['--sharedir', share_dir])
763 subprocess.call(pi)
764 prints('Run "calibre" to start calibre')
765 else:
766 prints('Run "%s/calibre" to start calibre' % destdir)
767 return 0
768
769
770 def check_umask():
771 # A bad umask can cause system breakage because of bugs in xdg-mime
772 # See https://www.mobileread.com/forums/showthread.php?t=277803
773 mask = os.umask(18) # 18 = 022
774 os.umask(mask)
775 forbid_user_read = mask & stat.S_IRUSR
776 forbid_user_exec = mask & stat.S_IXUSR
777 forbid_group_read = mask & stat.S_IRGRP
778 forbid_group_exec = mask & stat.S_IXGRP
779 forbid_other_read = mask & stat.S_IROTH
780 forbid_other_exec = mask & stat.S_IXOTH
781 if forbid_user_read or forbid_user_exec or forbid_group_read or forbid_group_exec or forbid_other_read or forbid_other_exec:
782 prints(
783 'WARNING: Your current umask disallows reading of files by some users,'
784 ' this can cause system breakage when running the installer because'
785 ' of bugs in common system utilities.'
786 )
787 sys.stdin = open('/dev/tty') # stdin is a pipe from wget
788 while True:
789 q = raw_input('Should the installer (f)ix the umask, (i)gnore it or (a)bort [f/i/a Default is abort]: ') or 'a'
790 if q in 'f i a'.split():
791 break
792 prints('Response', q, 'not understood')
793 if q == 'f':
794 mask = mask & ~stat.S_IRUSR & ~stat.S_IXUSR & ~stat.S_IRGRP & ~stat.S_IXGRP & ~stat.S_IROTH & ~stat.S_IXOTH
795 os.umask(mask)
796 prints('umask changed to: {:03o}'.format(mask))
797 elif q == 'i':
798 prints('Ignoring bad umask and proceeding anyway, you have been warned!')
799 else:
800 raise SystemExit('The system umask is unsuitable, aborting')
801
802
803 def check_for_libEGL():
804 import ctypes
805 try:
806 ctypes.CDLL('libEGL.so.1')
807 return
808 except Exception:
809 pass
810 raise SystemExit('You are missing the system library libEGL.so.1. Try installing packages such as libegl1 and libopengl0')
811
812
813 def check_for_libOpenGl():
814 import ctypes
815 try:
816 ctypes.CDLL('libOpenGL.so.0')
817 return
818 except Exception:
819 pass
820 raise SystemExit('You are missing the system library libOpenGL.so.0. Try installing packages such as libopengl0')
821
822
823 def check_for_libxcb_cursor():
824 import ctypes
825 try:
826 ctypes.CDLL('libxcb-cursor.so.0')
827 return
828 except Exception:
829 pass
830 raise SystemExit('You are missing the system library libxcb-cursor.so.0. Try installing packages such as libxcb-cursor0 or xcb-cursor')
831
832
833 def check_glibc_version(min_required=(2, 31), release_date='2020-02-01'):
834 # See https://sourceware.org/glibc/wiki/Glibc%20Timeline
835 import ctypes
836 libc = ctypes.CDLL(None)
837 try:
838 f = libc.gnu_get_libc_version
839 except AttributeError:
840 raise SystemExit('Your system is not based on GNU libc. The calibre binaries require GNU libc')
841 f.restype = ctypes.c_char_p
842 ver = f().decode('ascii')
843 q = tuple(map(int, ver.split('.')))
844 if q < min_required:
845 raise SystemExit(
846 ('Your system has GNU libc version {}. The calibre binaries require at least'
847 ' version: {} (released on {}). Update your system.'
848 ).format(ver, '.'.join(map(str, min_required)), release_date))
849
850
851 def check_for_recent_freetype():
852 import ctypes
853 f = None
854 try:
855 f = ctypes.CDLL('libfreetype.so.6')
856 except OSError:
857 raise SystemExit('Your system is missing the FreeType library libfreetype.so. Try installing the freetype package.')
858 try:
859 f.FT_Get_Color_Glyph_Paint
860 except AttributeError:
861 raise SystemExit('Your system has too old a version of the FreeType library.'
862 ' freetype >= 2.11 is needed for the FT_Get_Color_Glyph_Paint function which is required by Qt WebEngine')
863
864
865 def main(install_dir=None, isolated=False, bin_dir=None, share_dir=None, ignore_umask=False, version=None):
866 if not ignore_umask and not isolated:
867 check_umask()
868 if (is_linux_arm and not is_linux_arm64) or not is64bit:
869 raise SystemExit(
870 'You are running on a 32-bit system. The calibre binaries are only'
871 ' available for 64-bit systems. You will have to compile from'
872 ' source.')
873 glibc_versions = {
874 (6, 0, 0) : {'min_required': (2, 31), 'release_date': '2020-02-01'},
875 (7, 17, 0) : {'min_required': (2, 35), 'release_date': '2022-02-03'}
876 }
877 if is_linux_arm64:
878 glibc_versions.update({
879 (6, 8, 0) : {'min_required': (2, 34), 'release_date': '2021-08-02'}
880 })
881 q = tuple(map(int, version.split('.'))) if version else (sys.maxsize, 999, 999)
882 for key in sorted(glibc_versions, reverse=True):
883 if q >= key:
884 check_glibc_version(**glibc_versions[key])
885 break
886 if q[0] >= 6:
887 check_for_libEGL()
888 check_for_libOpenGl()
889 if q[0] >= 7:
890 check_for_libxcb_cursor()
891 if q >= (7, 16, 0):
892 check_for_recent_freetype()
893 run_installer(install_dir, isolated, bin_dir, share_dir, version)
894
895
896 try:
897 __file__
898 from_file = True
899 except NameError:
900 from_file = False
901
902
903 def update_installer_wrapper():
904 # To update: python3 -c "import runpy; runpy.run_path('setup/linux-installer.py', run_name='update_wrapper')"
905 with open(__file__, 'rb') as f:
906 src = f.read().decode('utf-8')
907 wrapper = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'linux-installer.sh')
908 with open(wrapper, 'r+b') as f:
909 raw = f.read().decode('utf-8')
910 nraw = re.sub(r'^# HEREDOC_START.+^# HEREDOC_END', lambda m: '# HEREDOC_START\n{}\n# HEREDOC_END'.format(src), raw, flags=re.MULTILINE | re.DOTALL)
911 if 'update_installer_wrapper()' not in nraw:
912 raise SystemExit('regex substitute of HEREDOC failed')
913 f.seek(0), f.truncate()
914 f.write(nraw.encode('utf-8'))
915
916
917 def script_launch():
918 def path(x):
919 return os.path.expanduser(x)
920
921 def to_bool(x):
922 return x.lower() in ('y', 'yes', '1', 'true')
923
924 type_map = {x: path for x in 'install_dir isolated bin_dir share_dir ignore_umask version'.split()}
925 type_map['isolated'] = type_map['ignore_umask'] = to_bool
926 kwargs = {}
927
928 for arg in sys.argv[1:]:
929 if arg:
930 m = re.match('([a-z_]+)=(.+)', arg)
931 if m is None:
932 raise SystemExit('Unrecognized command line argument: ' + arg)
933 k = m.group(1)
934 if k not in type_map:
935 raise SystemExit('Unrecognized command line argument: ' + arg)
936 kwargs[k] = type_map[k](m.group(2))
937 main(**kwargs)
938
939
940 if __name__ == '__main__' and from_file:
941 main()
942 elif __name__ == 'update_wrapper':
943 update_installer_wrapper()
944
945 # HEREDOC_END
946 # }}}
947 CALIBRE_LINUX_INSTALLER_HEREDOC