Debian Codegraph @

setup/linux-installer.py

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