forked from penguinho/appium-old
-
Notifications
You must be signed in to change notification settings - Fork 16
/
authorize.py
54 lines (48 loc) · 2 KB
/
authorize.py
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
# Copyright 2012 Appium Committers
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import re
import shutil
import tempfile
import difflib
# read the contents of /etc/authorization
with open('/etc/authorization','r') as file:
content = file.read()
match = re.search('<key>system.privilege.taskport</key>\s*\n\s*<dict>\n\s*<key>allow-root</key>\n\s*(<[^>]+>)',content)
if match is None:
raise Exception('Could not find the system.privilege.taskport key in /etc/authorization')
elif re.search('<false/>', match.group(0)) is None:
print '/etc/authorization has already been modified to support appium'
exit(0)
new_text = match.group(0).replace(match.group(1), '<true/>')
new_content = content.replace(match.group(0), new_text)
# backup /etc/authorization
backupFile = tempfile.mkstemp('.backup','authorization')[1]
shutil.copy('/etc/authorization', backupFile)
print 'backed up /etc/authorization to %s...' % backupFile
# present diff to the user
diff = difflib.context_diff(content.splitlines(), new_content.splitlines())
print '\n'.join(diff)
answer = raw_input('Write changes (y/n)')
if answer.lower()[0] == 'y':
# write back the modified permissions
with open('/etc/authorization','w') as file:
file.write(new_content)
print 'wrote new /etc/authorization'
else:
print 'No changes were made.'