-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-win-service.py
executable file
·105 lines (92 loc) · 3.63 KB
/
setup-win-service.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
# *****************************************************************************
# Marche - A server control daemon
# Copyright (c) 2015-2023 by the authors, see LICENSE
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Christian Felder <c.felder@fz-juelich.de>
#
# *****************************************************************************
import argparse
import sys
import winreg
from os import path
from subprocess import PIPE, Popen
import marche
def parse_args(argv):
parser = argparse.ArgumentParser(description="Marche MS Windows Service "
"Setup")
parser.add_argument("environments", type=str, nargs='*',
help="Environment settings, "
"e.g. TANGO_HOST=localhost:10000")
return parser.parse_args(argv)
def main(argv):
environment = list(parse_args(argv[1:]).environments)
search_directories = [
path.join(path.realpath(sys.prefix), "Scripts"),
path.join(path.dirname(path.dirname(path.realpath(marche.__file__))),
"bin"),
]
servicename = "marched"
marcheroot = None
for pdir in search_directories:
marched = path.join(pdir, "marched")
if path.isfile(marched):
marcheroot = path.dirname(pdir)
break
else:
marched = None
if marcheroot:
print("AppDirectory:", marcheroot)
print(" Application:", marched)
else:
print("marched not found in:", ', '.join(search_directories),
file=sys.stderr)
sys.exit(-1)
service_subkey = "SYSTEM\\CurrentControlSet\\services\\" + servicename
service = None
try:
service = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, service_subkey, 0,
winreg.KEY_WRITE)
except WindowsError:
print("Creating service:", servicename)
cmd = [
"sc.exe", "create", servicename, "start=", "auto",
"binPath=", "srvany.exe",
]
print(' '.join(cmd))
print()
stdout, stderr = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate()
print(stdout)
print(stderr)
finally:
if service:
service.Close()
print("Updating registry")
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, service_subkey, 0,
winreg.KEY_WRITE) as hdl:
if environment:
winreg.SetValueEx(hdl, "Environment", 0, winreg.REG_MULTI_SZ,
environment)
winreg.SetValueEx(hdl, "Type", 0, winreg.REG_DWORD,
0x110) # interactive
with winreg.CreateKey(hdl, "Parameters") as par:
winreg.SetValueEx(par, "AppDirectory", 0, winreg.REG_SZ,
marcheroot)
winreg.SetValueEx(par, "Application", 0, winreg.REG_SZ,
sys.executable + ' ' + marched)
if __name__ == "__main__":
main(sys.argv)