This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
foss-barcode.py
executable file
·169 lines (143 loc) · 5.59 KB
/
foss-barcode.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/python
# foss-barcode - command to start/stop the FOSS Barcode web interface
# Copyright 2010 Linux Foundation
# Jeff Licquia <licquia@linuxfoundation.org>
import sys
import os
import pwd
import time
import signal
import optparse
import shutil
from django.core.management import execute_manager
command_line_usage = "%prog [options] start | stop"
command_line_options = [
optparse.make_option("--force-root", action="store_true",
dest="force_root", default=False,
help="allow running as root"),
optparse.make_option("--server-only", action="store_true",
dest="server_only", default=False,
help="don't open a browser"),
optparse.make_option("--interface", action="store",
dest="interface", default=None,
help="listen on network interface (port or ip:port)"),
]
def get_base_path():
this_module_path = os.path.dirname(os.path.abspath(__file__))
if os.path.basename(this_module_path) == "bin":
this_module_path = os.path.dirname(this_module_path)
return os.path.join(this_module_path, "fossbarcode")
def set_import_path():
sys.path.append(get_base_path())
sys.path.append(os.path.join(get_base_path(), ".."))
set_import_path()
import settings
def check_current_user():
if os.getuid() == 0:
try:
compliance_user = pwd.getpwnam("compliance")
except KeyError:
sys.stderr.write("Could not find user 'compliance'.\n")
sys.exit(1)
os.setuid(compliance_user.pw_uid)
# Setting up userdir mode.
def setup_userdir():
if not os.path.exists(settings.USERDIR_ROOT):
os.mkdir(settings.USERDIR_ROOT)
manager_args = [settings.USERDIR_ROOT, "syncdb", "--noinput"]
execute_manager(settings, manager_args)
if not os.path.exists(os.path.join(settings.USERDIR_ROOT, "media")):
os.mkdir(os.path.join(settings.USERDIR_ROOT, "media"))
# we need to link back to the real media subdirs (except user_data)
for dirlink in ( 'css', 'docs', 'images', 'js' ):
if not os.path.exists(os.path.join(settings.USERDIR_ROOT, "media", dirlink)):
os.symlink(os.path.join(settings.PROJECT_ROOT, "fossbarcode", "media", dirlink),
os.path.join(settings.USERDIR_ROOT, "media", dirlink))
if not os.path.exists(os.path.join(settings.USERDIR_ROOT, "media", "user_data")):
os.mkdir(os.path.join(settings.USERDIR_ROOT, "media", "user_data"))
def start_server(run_browser, interface=None):
pid_path = os.path.join(settings.STATE_ROOT, "server.pid")
if os.path.exists(pid_path):
server_pid = int(open(pid_path).read())
pid_found = False
try:
os.kill(server_pid, 0)
pid_found = True
except OSError:
pid_found = False
if pid_found:
sys.stderr.write("The server is already running.\n")
sys.exit(1)
else:
os.unlink(pid_path)
if settings.USERDIR_ROOT:
setup_userdir()
childpid = os.fork()
if childpid == 0:
os.setsid()
log_fn = os.path.join(settings.STATE_ROOT, "server.log")
try:
log_fd = os.open(log_fn, os.O_WRONLY | os.O_APPEND | os.O_CREAT)
except OSError:
log_fd = -1
if log_fd < 0:
sys.stderr.write("Could not open log file; logging to stdout.\n")
else:
os.dup2(log_fd, 1)
os.dup2(log_fd, 2)
os.close(0)
manager_args = ["fossbarcode", "runserver", "--noreload"]
if interface:
manager_args.append(interface)
execute_manager(settings, manager_args)
else:
time.sleep(1)
pid_file = open(pid_path, "w")
pid_file.write(str(childpid))
pid_file.close()
if run_browser:
if interface:
if interface.find(":") != -1:
(ipaddr, port) = interface.split(":")
if ipaddr == "0.0.0.0":
interface = "127.0.0.1:" + port
app_url = "http://%s/barcode" % interface
else:
app_url = "http://127.0.0.1:8000/barcode"
sys.stdout.write("Waiting for the server to start...\n")
time.sleep(10)
sys.stdout.write("Starting a web browser.\n")
os.execlp("xdg-open", "xdg-open", app_url)
else:
sys.exit(0)
def stop_server():
pid_path = os.path.join(settings.STATE_ROOT, "server.pid")
if os.path.exists(pid_path):
server_pid = int(open(pid_path).read())
sys.stdout.write("Killing process %d...\n" % server_pid)
try:
try:
os.kill(server_pid, signal.SIGTERM)
finally:
os.unlink(pid_path)
except OSError, e:
sys.stderr.write("Could not kill process: %s\n" % str(e))
sys.exit(1)
else:
sys.stderr.write("No server process found to stop.\n")
sys.exit(1)
def main():
cmdline_parser = optparse.OptionParser(usage=command_line_usage,
option_list=command_line_options)
(options, args) = cmdline_parser.parse_args()
if len(args) != 1 or args[0] not in ["start", "stop"]:
cmdline_parser.error("incorrect arguments")
# Switch users if needed.
if args[0] == "start":
if not options.force_root:
check_current_user()
start_server(not options.server_only, options.interface)
else:
stop_server()
if __name__ == "__main__":
main()