-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup_utils.py
150 lines (133 loc) · 4.41 KB
/
startup_utils.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
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/env python
import os
import logging
_win_path = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup'
_win_file = r'noipUpdater.bat'
_linux_path = '/etc/init.d'
_linux_file = 'noipUpdater'
def get_path():
"""
Get the path to the stat-up script
:return: path to start-up script
"""
if os.name == 'nt':
path = r'{}\{}'.format(_win_path, _win_file)
elif os.name == 'posix':
path = r'{}/{}'.format(_linux_path, _linux_file)
else:
path = None
return path
def get_file_name():
"""
Get the stat-up script name
:return: name
"""
if os.name == 'nt':
name = _win_file
elif os.name == 'posix':
name = _linux_file
else:
name = None
return name
def remove_startup():
"""
Delete start-up script
"""
os_name = os.name
path = get_path()
if os.path.exists(path):
success = True
logging.debug('Removing script from start-up folder - {}'.format(path))
if os_name == 'nt':
try:
os.remove(path)
except Exception as ex:
logging.exception(ex)
success = False
elif os_name == 'posix':
if _is_user_admin():
import subprocess
import traceback
try:
subprocess.call(['update-rc.d', '-f', _linux_file, 'remove'])
except Exception as ex:
logging.exception(ex)
success = False
else:
logging.error('User not root')
success = False
if success:
logging.info('Startup script removed.')
else:
logging.error('Please remove script from startup manually')
def _get_pythonw_path():
"""
Get the python executable path
:return: path to python executable file
"""
import sys
python_exe = sys.executable
return python_exe.replace('.exe', 'w.exe')
def _get_current_path():
"""
Get the current path location
:return: current path location
"""
return os.path.dirname(__file__)
def add_startup(_username, _password, _hostname):
"""
Adds new start-up script
:param _username: Login name for NO-IP
:param _password: Login password for NO-IP
:param _hostname: Host name to update
"""
os_name = os.name
admin_error = script = None
run_script = '"{}" -s {} {} {}'.format(os.path.join(_get_current_path(), 'updater.py'),
_username, _password, _hostname)
if os_name == 'nt':
script = '@echo off\nstart "" {} {}'.format(_get_pythonw_path(), run_script)
admin_error = 'Please move the "{}" file to the startup folder manually'.format(_win_file)
elif os_name == 'posix':
script = '#!/bin/sh\npython {}'.format(run_script)
admin_error = 'Please move the "{}" file to "/etc/init.d/" folder and configure it to run on startup manually'.\
format(_linux_file)
if not _is_user_admin():
logging.warning('Current user is not a root. Creating script in current directory')
logging.warning(admin_error)
path = os.path.join(_get_current_path(), get_file_name())
else:
path = get_path()
logging.debug('Script file location: {}'.format(path))
try:
script_file = open(path, 'w')
script_file.write(script)
script_file.close()
except Exception as ex:
logging.exception(ex.message)
if os_name == 'posix':
import subprocess
subprocess.call(['chmod', 'ugo+x', path])
if _is_user_admin():
subprocess.call(['update-rc.d', _linux_file, 'defaults'])
def _is_user_admin():
"""
Check the current user if is the system admin
:return: True if the current user is admin
"""
import traceback
if os.name == 'nt':
import ctypes
# WARNING: requires Windows XP SP2 or higher!
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except Exception as ex:
logging.exception(ex.message)
logging.exception(traceback.print_exc())
logging.debug('Admin check failed, assuming not an admin.')
return False
elif os.name == 'posix':
# Check for root on Posix
return os.getuid() == 0
else:
raise RuntimeError("Unsupported operating system for this module: %s" % (os.name,))