-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelperTool_CodeSign_RunScript.py
More file actions
executable file
·150 lines (104 loc) · 4.42 KB
/
HelperTool_CodeSign_RunScript.py
File metadata and controls
executable file
·150 lines (104 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
''' Place this script at the root of the project directory
and enter this for a Run Script during build phase of the helper app
${PROJECT_DIR}"/HelperTool_CodeSign_RunScript.py
put the run script right after Target Dependencies
What this script does is write the SMPrivilegedExecutables dictionary
into the Main app's Info.plist and the SMAuthorizedClients array into
the Helper Tool's Info.plist.
You still need to add this
-sectcreate __TEXT __info_plist helper/helper-Info.plist
-sectcreate __TEXT __launchd_plist helper/helper-Launchd.plist
to the Other Linker Flags Settings for your helper tool.
and the helper tool must be signed by the same identity as the main app_name
'''
import os
import subprocess
import plistlib
'''
the name for the helper tool will be default to this format
com.your.app.helper
which is the main apps bundleID with a .helper extension.
if the executable you're installing is something other than that specify it here
'''
helper_name_override = '';
'''Set these to the appropriate path'''
helper_info = 'helper/helper-Info.plist'
helper_launchd = 'helper/helper-Launchd.plist'
tmp_filename = 'code_sign_tmp_file'
def getCodeSignIdent(build_dir):
identity = os.getenv('CODE_SIGN_IDENTITY')
checkVar(identity,'identity')
file = os.path.join(build_dir,tmp_filename)
open(file, 'w').close()
result = subprocess.check_output(['codesign','--force','--sign',identity,file])
result = subprocess.check_output(['codesign','-d','-r', '-',file])
code_sign_identity = result.split("=> ")[1]
code_sign_identity = code_sign_identity.rstrip(os.linesep)
os.remove(file)
return code_sign_identity
def editAppInfoPlist(cert_id,app_name):
app_info_plist = os.getenv('PRODUCT_SETTINGS_PATH')
checkVar(app_info_plist,'App Info.plist')
try:
p = plistlib.readPlist(app_info_plist)
bundle_id = p['CFBundleIdentifier'].split("$")[0]
if not helper_name_override:
helper_id = ''.join([bundle_id, app_name,'.helper'])
else:
helper_id = helper_name_override
csstring = cert_id.replace(tmp_filename,helper_id)
p['SMPrivilegedExecutables'] = {helper_id:csstring}
except:
print "There is No Info.plist for them main app, somethings very wrong"
exit(1)
plistlib.writePlist(p,app_info_plist)
return bundle_id, helper_id
def editHelperInfoPlist(cert_id,project_path,bundle_id,app_name):
helper_info_plist = os.path.join(project_path,helper_info)
checkVar(helper_info_plist,'Helper info.plist')
app_id = ''.join([bundle_id,app_name])
csstring = cert_id.replace(tmp_filename,app_id)
try:
p = plistlib.readPlist(helper_info_plist)
p['SMAuthorizedClients'] = [csstring]
except:
p = {
'CFBundleIdentifier':helper_id,
'CFBundleName':helper_id,
'CFBundleVersion':'0.1.0',
'CFBundleInfoDictionaryVersion':'6.0',
'SMAuthorizedClients':[cstring],
}
plistlib.writePlist(p,helper_info_plist)
def editHelperLaunchD(project_path,helper_id):
helper_launchd_plist = os.path.join(project_path,helper_launchd)
checkVar(helper_launchd_plist,'Helper launchd.plist')
try:
p = plistlib.readPlist(helper_launchd_plist)
p['Label'] = helper_id
p['MachServices'] = {helper_id:True}
except:
p = {'Label':helper_id,
'MachServices':{helper_id:True}}
plistlib.writePlist(p,helper_launchd_plist)
def checkVar(var,description):
if var == "" or var == None:
print ("the variable %s is blank" % description)
exit(1)
def main():
# Configure info from environment
build_dir = os.getenv('BUILT_PRODUCTS_DIR')
checkVar(build_dir,'BUILT_PRODUCTS_DIR')
project_path = os.getenv('PROJECT_DIR')
checkVar(project_path,"PROJECT_DIR")
app_name = os.getenv('PRODUCT_NAME')
checkVar(app_name,"PRODUCT_NAME(App Name)")
# Get the existing cert values
code_sign_identity = getCodeSignIdent(build_dir)
# write out to the helper tool
bundle_id,helper_id = editAppInfoPlist(code_sign_identity,app_name)
editHelperInfoPlist(code_sign_identity,project_path,bundle_id,app_name)
editHelperLaunchD(project_path,helper_id)
if __name__ == "__main__":
main()