Debian Codegraph @

bypy/macos/sign.py

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 # License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
4
5 import os
6 import plistlib
7 from glob import glob
8
9 from bypy.macos_sign import codesign, create_entitlements_file, make_certificate_useable, notarize_app, verify_signature
10 from bypy.utils import current_dir
11
12 entitlements = {
13 # MAP_JIT is used by libpcre which is bundled with Qt
14 'com.apple.security.cs.allow-jit': True,
15
16 # v8 and therefore WebEngine need this as they don't use MAP_JIT
17 'com.apple.security.cs.allow-unsigned-executable-memory': True,
18
19 # calibre itself does not use DYLD env vars, but don't know about its
20 # dependencies.
21 'com.apple.security.cs.allow-dyld-environment-variables': True,
22
23 # Allow loading of unsigned plugins or frameworks
24 # 'com.apple.security.cs.disable-library-validation': True,
25 }
26
27
28 def files_in(folder):
29 for record in os.walk(folder):
30 for f in record[-1]:
31 yield os.path.join(record[0], f)
32
33
34 def expand_dirs(items, exclude=lambda x: x.endswith('.so')):
35 items = set(items)
36 dirs = set(x for x in items if os.path.isdir(x))
37 items.difference_update(dirs)
38 for x in dirs:
39 items.update({y for y in files_in(x) if not exclude(y)})
40 return items
41
42
43 def get_executable(info_path):
44 with open(info_path, 'rb') as f:
45 return plistlib.load(f)['CFBundleExecutable']
46
47
48 def find_sub_apps(contents_dir='.'):
49 for app in glob(os.path.join(contents_dir, '*.app')):
50 cdir = os.path.join(app, 'Contents')
51 for sapp in find_sub_apps(cdir):
52 yield sapp
53 yield app
54
55
56 def sign_MacOS(contents_dir='.'):
57 # Sign everything in MacOS except the main executable
58 # which will be signed automatically by codesign when
59 # signing the app bundles
60 with current_dir(os.path.join(contents_dir, 'MacOS')):
61 exe = get_executable('../Info.plist')
62 items = {x for x in os.listdir('.') if x != exe and not os.path.islink(x)}
63 if items:
64 codesign(items)
65
66
67 def do_sign_app(appdir):
68 appdir = os.path.abspath(appdir)
69 with current_dir(os.path.join(appdir, 'Contents')):
70 sign_MacOS()
71 # Sign the sub application bundles
72 sub_apps = list(find_sub_apps())
73 sub_apps.append('Frameworks/QtWebEngineCore.framework/Versions/Current/Helpers/QtWebEngineProcess.app')
74 for sa in sub_apps:
75 sign_MacOS(os.path.join(sa, 'Contents'))
76 codesign(sub_apps)
77
78 # Sign all .so files
79 so_files = {x for x in files_in('.') if x.endswith('.so')}
80 codesign(so_files)
81
82 # Sign everything in PlugIns
83 with current_dir('PlugIns'):
84 items = set(os.listdir('.'))
85 codesign(expand_dirs(items))
86
87 # Sign everything else in Frameworks
88 with current_dir('Frameworks'):
89 fw = set(glob('*.framework'))
90 codesign(fw)
91 items = set(os.listdir('.')) - fw
92 codesign(expand_dirs(items))
93
94 # Now sign the main app
95 codesign(appdir)
96 verify_signature(appdir)
97 return 0
98
99
100 def sign_app(appdir, notarize):
101 create_entitlements_file(entitlements)
102 with make_certificate_useable():
103 do_sign_app(appdir)
104 if notarize:
105 notarize_app(appdir, 'calibre')