Debian Codegraph @

setup/linux-installer.py

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