author | Yuya Nishihara <yuya@tcha.org> |
Sat, 06 Oct 2018 20:07:11 +0900 | |
changeset 40287 | 7623199def92 |
parent 38783 | e7aa113b14f7 |
child 40823 | 368ecbf734af |
permissions | -rw-r--r-- |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
1 |
# server.py - utility and factory of server |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
2 |
# |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
3 |
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
4 |
# |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
7 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
8 |
from __future__ import absolute_import |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
9 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
10 |
import os |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
11 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
12 |
from .i18n import _ |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
13 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
14 |
from . import ( |
30513
ff7df4bb75de
chgserver: make it a core module and drop extension flags
Yuya Nishihara <yuya@tcha.org>
parents:
30510
diff
changeset
|
15 |
chgserver, |
32005
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
16 |
cmdutil, |
30507
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
17 |
commandserver, |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
18 |
error, |
30509
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
19 |
hgweb, |
32530
3f0936b2cea9
server: use pycompat to get argv
Augie Fackler <raf@durin42.com>
parents:
32291
diff
changeset
|
20 |
pycompat, |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
21 |
util, |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
22 |
) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
23 |
|
37119
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
24 |
from .utils import ( |
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
25 |
procutil, |
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
26 |
) |
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
27 |
|
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
28 |
def runservice(opts, parentfn=None, initfn=None, runfn=None, logfile=None, |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
29 |
runargs=None, appendpid=False): |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
30 |
'''Run a command as a service.''' |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
31 |
|
37215
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
32 |
postexecargs = {} |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
33 |
|
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
34 |
if opts['daemon_postexec']: |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
35 |
for inst in opts['daemon_postexec']: |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
36 |
if inst.startswith('unlink:'): |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
37 |
postexecargs['unlink'] = inst[7:] |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
38 |
elif inst.startswith('chdir:'): |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
39 |
postexecargs['chdir'] = inst[6:] |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
40 |
elif inst != 'none': |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
41 |
raise error.Abort(_('invalid value for --daemon-postexec: %s') |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
42 |
% inst) |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
43 |
|
37212
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
44 |
# When daemonized on Windows, redirect stdout/stderr to the lockfile (which |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
45 |
# gets cleaned up after the child is up and running), so that the parent can |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
46 |
# read and print the error if this child dies early. See 594dd384803c. On |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
47 |
# other platforms, the child can write to the parent's stdio directly, until |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
48 |
# it is redirected prior to runfn(). |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
49 |
if pycompat.iswindows and opts['daemon_postexec']: |
37215
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
50 |
if 'unlink' in postexecargs and os.path.exists(postexecargs['unlink']): |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
51 |
procutil.stdout.flush() |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
52 |
procutil.stderr.flush() |
37212
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
53 |
|
37215
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
54 |
fd = os.open(postexecargs['unlink'], |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
55 |
os.O_WRONLY | os.O_APPEND | os.O_BINARY) |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
56 |
try: |
37216
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
57 |
os.dup2(fd, procutil.stdout.fileno()) |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
58 |
os.dup2(fd, procutil.stderr.fileno()) |
37215
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
59 |
finally: |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
60 |
os.close(fd) |
37212
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
61 |
|
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
62 |
def writepid(pid): |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
63 |
if opts['pid_file']: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
64 |
if appendpid: |
32548
d770a08ee9d9
server: write out pid using bytes IO instead of str IO
Augie Fackler <raf@durin42.com>
parents:
32530
diff
changeset
|
65 |
mode = 'ab' |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
66 |
else: |
32548
d770a08ee9d9
server: write out pid using bytes IO instead of str IO
Augie Fackler <raf@durin42.com>
parents:
32530
diff
changeset
|
67 |
mode = 'wb' |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
68 |
fp = open(opts['pid_file'], mode) |
32617
e48cb1c7a902
py3: simply use b'%d\n' to format pid in server.py
Yuya Nishihara <yuya@tcha.org>
parents:
32548
diff
changeset
|
69 |
fp.write('%d\n' % pid) |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
70 |
fp.close() |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
71 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
72 |
if opts['daemon'] and not opts['daemon_postexec']: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
73 |
# Signal child process startup with file removal |
38164
aac4be30e250
py3: wrap tempfile.mkstemp() to use bytes path
Yuya Nishihara <yuya@tcha.org>
parents:
37216
diff
changeset
|
74 |
lockfd, lockpath = pycompat.mkstemp(prefix='hg-service-') |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
75 |
os.close(lockfd) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
76 |
try: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
77 |
if not runargs: |
37120
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37119
diff
changeset
|
78 |
runargs = procutil.hgcmd() + pycompat.sysargv[1:] |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
79 |
runargs.append('--daemon-postexec=unlink:%s' % lockpath) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
80 |
# Don't pass --cwd to the child process, because we've already |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
81 |
# changed directory. |
38783
e7aa113b14f7
global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
38164
diff
changeset
|
82 |
for i in pycompat.xrange(1, len(runargs)): |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
83 |
if runargs[i].startswith('--cwd='): |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
84 |
del runargs[i] |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
85 |
break |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
86 |
elif runargs[i].startswith('--cwd'): |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
87 |
del runargs[i:i + 2] |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
88 |
break |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
89 |
def condfn(): |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
90 |
return not os.path.exists(lockpath) |
37120
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37119
diff
changeset
|
91 |
pid = procutil.rundetached(runargs, condfn) |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
92 |
if pid < 0: |
37212
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
93 |
# If the daemonized process managed to write out an error msg, |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
94 |
# report it. |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
95 |
if pycompat.iswindows and os.path.exists(lockpath): |
37216
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
96 |
with open(lockpath, 'rb') as log: |
37212
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
97 |
for line in log: |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
98 |
procutil.stderr.write(line) |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
99 |
raise error.Abort(_('child process failed to start')) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
100 |
writepid(pid) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
101 |
finally: |
31548 | 102 |
util.tryunlink(lockpath) |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
103 |
if parentfn: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
104 |
return parentfn(pid) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
105 |
else: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
106 |
return |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
107 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
108 |
if initfn: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
109 |
initfn() |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
110 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
111 |
if not opts['daemon']: |
37120
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37119
diff
changeset
|
112 |
writepid(procutil.getpid()) |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
113 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
114 |
if opts['daemon_postexec']: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
115 |
try: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
116 |
os.setsid() |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
117 |
except AttributeError: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
118 |
pass |
37212
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
119 |
|
37215
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
120 |
if 'chdir' in postexecargs: |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
121 |
os.chdir(postexecargs['chdir']) |
37120
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37119
diff
changeset
|
122 |
procutil.hidewindow() |
37119
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
123 |
procutil.stdout.flush() |
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
124 |
procutil.stderr.flush() |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
125 |
|
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
126 |
nullfd = os.open(os.devnull, os.O_RDWR) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
127 |
logfilefd = nullfd |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
128 |
if logfile: |
34924
bfcd0d227972
server: drop executable bit from daemon log file
Yuya Nishihara <yuya@tcha.org>
parents:
32617
diff
changeset
|
129 |
logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND, |
bfcd0d227972
server: drop executable bit from daemon log file
Yuya Nishihara <yuya@tcha.org>
parents:
32617
diff
changeset
|
130 |
0o666) |
37216
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
131 |
os.dup2(nullfd, procutil.stdin.fileno()) |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
132 |
os.dup2(logfilefd, procutil.stdout.fileno()) |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
133 |
os.dup2(logfilefd, procutil.stderr.fileno()) |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
134 |
stdio = (procutil.stdin.fileno(), procutil.stdout.fileno(), |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
135 |
procutil.stderr.fileno()) |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
136 |
if nullfd not in stdio: |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
137 |
os.close(nullfd) |
37216
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37215
diff
changeset
|
138 |
if logfile and logfilefd not in stdio: |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
139 |
os.close(logfilefd) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
140 |
|
37212
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
141 |
# Only unlink after redirecting stdout/stderr, so Windows doesn't |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
142 |
# complain about a sharing violation. |
37215
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
143 |
if 'unlink' in postexecargs: |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37212
diff
changeset
|
144 |
os.unlink(postexecargs['unlink']) |
37212
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37120
diff
changeset
|
145 |
|
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
146 |
if runfn: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
147 |
return runfn() |
30507
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
148 |
|
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
149 |
_cmdservicemap = { |
30513
ff7df4bb75de
chgserver: make it a core module and drop extension flags
Yuya Nishihara <yuya@tcha.org>
parents:
30510
diff
changeset
|
150 |
'chgunix': chgserver.chgunixservice, |
30507
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
151 |
'pipe': commandserver.pipeservice, |
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
152 |
'unix': commandserver.unixforkingservice, |
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
153 |
} |
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
154 |
|
30510
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30509
diff
changeset
|
155 |
def _createcmdservice(ui, repo, opts): |
30507
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
156 |
mode = opts['cmdserver'] |
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
157 |
try: |
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
158 |
return _cmdservicemap[mode](ui, repo, opts) |
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
159 |
except KeyError: |
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30506
diff
changeset
|
160 |
raise error.Abort(_('unknown mode %s') % mode) |
30509
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
161 |
|
30510
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30509
diff
changeset
|
162 |
def _createhgwebservice(ui, repo, opts): |
30509
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
163 |
# this way we can check if something was given in the command-line |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
164 |
if opts.get('port'): |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
165 |
opts['port'] = util.getport(opts.get('port')) |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
166 |
|
32291
bd872f64a8ba
cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents:
32005
diff
changeset
|
167 |
alluis = {ui} |
30509
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
168 |
if repo: |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
169 |
baseui = repo.baseui |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
170 |
alluis.update([repo.baseui, repo.ui]) |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
171 |
else: |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
172 |
baseui = ui |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
173 |
webconf = opts.get('web_conf') or opts.get('webdir_conf') |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
174 |
if webconf: |
32005
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
175 |
if opts.get('subrepos'): |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
176 |
raise error.Abort(_('--web-conf cannot be used with --subrepos')) |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
177 |
|
30509
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
178 |
# load server settings (e.g. web.port) to "copied" ui, which allows |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
179 |
# hgwebdir to reload webconf cleanly |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
180 |
servui = ui.copy() |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
181 |
servui.readconfig(webconf, sections=['web']) |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
182 |
alluis.add(servui) |
32005
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
183 |
elif opts.get('subrepos'): |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
184 |
servui = ui |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
185 |
|
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
186 |
# If repo is None, hgweb.createapp() already raises a proper abort |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
187 |
# message as long as webconf is None. |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
188 |
if repo: |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
189 |
webconf = dict() |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31548
diff
changeset
|
190 |
cmdutil.addwebdirpath(repo, "", webconf) |
30509
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
191 |
else: |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
192 |
servui = ui |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
193 |
|
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
194 |
optlist = ("name templates style address port prefix ipv6" |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
195 |
" accesslog errorlog certificate encoding") |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
196 |
for o in optlist.split(): |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
197 |
val = opts.get(o, '') |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
198 |
if val in (None, ''): # should check against default options instead |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
199 |
continue |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
200 |
for u in alluis: |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
201 |
u.setconfig("web", o, val, 'serve') |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
202 |
|
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
203 |
app = hgweb.createapp(baseui, repo, webconf) |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30507
diff
changeset
|
204 |
return hgweb.httpservice(servui, app, opts) |
30510
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30509
diff
changeset
|
205 |
|
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30509
diff
changeset
|
206 |
def createservice(ui, repo, opts): |
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30509
diff
changeset
|
207 |
if opts["cmdserver"]: |
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30509
diff
changeset
|
208 |
return _createcmdservice(ui, repo, opts) |
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30509
diff
changeset
|
209 |
else: |
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30509
diff
changeset
|
210 |
return _createhgwebservice(ui, repo, opts) |