Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/server.py @ 53018:65f6f1fe43ef
rhg-files: use the correct long-form flag for selecting a revision
This went unnoticed forever, I guess most users pass in `-r`.
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Wed, 26 Feb 2025 09:25:53 -0500 |
parents | 72d461cbd1f3 |
children |
rev | line source |
---|---|
30515
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 # |
46819
d4ba4d51f85f
contributor: change mentions of mpm to olivia
Rapha?l Gom?s <rgomes@octobus.net>
parents:
43085
diff
changeset
|
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com> |
30515
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 |
51901
f4733654f144
typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents:
49292
diff
changeset
|
8 from __future__ import annotations |
30515
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 ( |
30522
ff7df4bb75de
chgserver: make it a core module and drop extension flags
Yuya Nishihara <yuya@tcha.org>
parents:
30519
diff
changeset
|
15 chgserver, |
32005
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31554
diff
changeset
|
16 cmdutil, |
30516
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30515
diff
changeset
|
17 commandserver, |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
18 error, |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
19 hgweb, |
32563
3f0936b2cea9
server: use pycompat to get argv
Augie Fackler <raf@durin42.com>
parents:
32331
diff
changeset
|
20 pycompat, |
30515
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 |
46907
ffd3e823a7e5
urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46819
diff
changeset
|
24 from .utils import ( |
ffd3e823a7e5
urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46819
diff
changeset
|
25 procutil, |
ffd3e823a7e5
urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46819
diff
changeset
|
26 urlutil, |
ffd3e823a7e5
urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46819
diff
changeset
|
27 ) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
28 |
37122
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
29 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
30 def runservice( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
31 opts, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
32 parentfn=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
33 initfn=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
34 runfn=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
35 logfile=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
36 runargs=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
37 appendpid=False, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
38 ): |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
39 '''Run a command as a service.''' |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
40 |
37217
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37214
diff
changeset
|
41 postexecargs = {} |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37214
diff
changeset
|
42 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
43 if opts[b'daemon_postexec']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
44 for inst in opts[b'daemon_postexec']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
45 if inst.startswith(b'unlink:'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
46 postexecargs[b'unlink'] = inst[7:] |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
47 elif inst.startswith(b'chdir:'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
48 postexecargs[b'chdir'] = inst[6:] |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
49 elif inst != b'none': |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
50 raise error.Abort( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
51 _(b'invalid value for --daemon-postexec: %s') % inst |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
52 ) |
37217
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37214
diff
changeset
|
53 |
37214
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
54 # 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:
37123
diff
changeset
|
55 # 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:
37123
diff
changeset
|
56 # 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:
37123
diff
changeset
|
57 # 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:
37123
diff
changeset
|
58 # it is redirected prior to runfn(). |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
59 if pycompat.iswindows and opts[b'daemon_postexec']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
60 if b'unlink' in postexecargs and os.path.exists( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
61 postexecargs[b'unlink'] |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
62 ): |
37217
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37214
diff
changeset
|
63 procutil.stdout.flush() |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37214
diff
changeset
|
64 procutil.stderr.flush() |
37214
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
65 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
66 fd = os.open( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
67 postexecargs[b'unlink'], os.O_WRONLY | os.O_APPEND | os.O_BINARY |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
68 ) |
37217
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37214
diff
changeset
|
69 try: |
37218
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37217
diff
changeset
|
70 os.dup2(fd, procutil.stdout.fileno()) |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37217
diff
changeset
|
71 os.dup2(fd, procutil.stderr.fileno()) |
37217
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37214
diff
changeset
|
72 finally: |
73a60281a861
server: refactor 'daemon_postexec' instructions into a dictionary
Matt Harbison <matt_harbison@yahoo.com>
parents:
37214
diff
changeset
|
73 os.close(fd) |
37214
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
74 |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
75 def writepid(pid): |
52605
72d461cbd1f3
server: don't leak a file descriptor when I/O fails writing a pid file
Matt Harbison <matt_harbison@yahoo.com>
parents:
52410
diff
changeset
|
76 file = opts[b'pid_file'] |
72d461cbd1f3
server: don't leak a file descriptor when I/O fails writing a pid file
Matt Harbison <matt_harbison@yahoo.com>
parents:
52410
diff
changeset
|
77 if file: |
72d461cbd1f3
server: don't leak a file descriptor when I/O fails writing a pid file
Matt Harbison <matt_harbison@yahoo.com>
parents:
52410
diff
changeset
|
78 pid_line = b'%d\n' % pid |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
79 if appendpid: |
52605
72d461cbd1f3
server: don't leak a file descriptor when I/O fails writing a pid file
Matt Harbison <matt_harbison@yahoo.com>
parents:
52410
diff
changeset
|
80 util.appendfile(file, pid_line) |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
81 else: |
52605
72d461cbd1f3
server: don't leak a file descriptor when I/O fails writing a pid file
Matt Harbison <matt_harbison@yahoo.com>
parents:
52410
diff
changeset
|
82 util.writefile(file, pid_line) |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
83 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
84 if opts[b'daemon'] and not opts[b'daemon_postexec']: |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
85 # Signal child process startup with file removal |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
86 lockfd, lockpath = pycompat.mkstemp(prefix=b'hg-service-') |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
87 os.close(lockfd) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
88 try: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
89 if not runargs: |
37123
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37122
diff
changeset
|
90 runargs = procutil.hgcmd() + pycompat.sysargv[1:] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
91 runargs.append(b'--daemon-postexec=unlink:%s' % lockpath) |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
92 # 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
|
93 # changed directory. |
49292
d44e3c45f0e4
py3: replace `pycompat.xrange` by `range`
Manuel Jacob <me@manueljacob.de>
parents:
48966
diff
changeset
|
94 for i in range(1, len(runargs)): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
95 if runargs[i].startswith(b'--cwd='): |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
96 del runargs[i] |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
97 break |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
98 elif runargs[i].startswith(b'--cwd'): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
99 del runargs[i : i + 2] |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
100 break |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
101 |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
102 def condfn(): |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
103 return not os.path.exists(lockpath) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
104 |
37123
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37122
diff
changeset
|
105 pid = procutil.rundetached(runargs, condfn) |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
106 if pid < 0: |
37214
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
107 # 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:
37123
diff
changeset
|
108 # report it. |
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
109 if pycompat.iswindows and os.path.exists(lockpath): |
52410
e1f0125bb8c6
server: stop using the `pycompat.open()` shim
Matt Harbison <matt_harbison@yahoo.com>
parents:
51901
diff
changeset
|
110 with open(lockpath, 'rb') as log: |
37214
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
111 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:
37123
diff
changeset
|
112 procutil.stderr.write(line) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
113 raise error.Abort(_(b'child process failed to start')) |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
114 writepid(pid) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
115 finally: |
31554 | 116 util.tryunlink(lockpath) |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
117 if parentfn: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
118 return parentfn(pid) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
119 else: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
120 return |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
121 |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
122 if initfn: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
123 initfn() |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
124 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
125 if not opts[b'daemon']: |
37123
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37122
diff
changeset
|
126 writepid(procutil.getpid()) |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
127 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
128 if opts[b'daemon_postexec']: |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
129 try: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
130 os.setsid() |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
131 except AttributeError: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
132 pass |
37214
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
133 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
134 if b'chdir' in postexecargs: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
135 os.chdir(postexecargs[b'chdir']) |
37123
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37122
diff
changeset
|
136 procutil.hidewindow() |
37122
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
137 procutil.stdout.flush() |
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34924
diff
changeset
|
138 procutil.stderr.flush() |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
139 |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
140 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
|
141 logfilefd = nullfd |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
142 if logfile: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
143 logfilefd = os.open( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
144 logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND, 0o666 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
145 ) |
37218
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37217
diff
changeset
|
146 os.dup2(nullfd, procutil.stdin.fileno()) |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37217
diff
changeset
|
147 os.dup2(logfilefd, procutil.stdout.fileno()) |
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37217
diff
changeset
|
148 os.dup2(logfilefd, procutil.stderr.fileno()) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
149 stdio = ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
150 procutil.stdin.fileno(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
151 procutil.stdout.fileno(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
152 procutil.stderr.fileno(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
153 ) |
37218
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37217
diff
changeset
|
154 if nullfd not in stdio: |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
155 os.close(nullfd) |
37218
d2bd29dffc6c
server: minor code cleanup
Matt Harbison <matt_harbison@yahoo.com>
parents:
37217
diff
changeset
|
156 if logfile and logfilefd not in stdio: |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
157 os.close(logfilefd) |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
158 |
37214
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
159 # 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:
37123
diff
changeset
|
160 # complain about a sharing violation. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
161 if b'unlink' in postexecargs: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
162 os.unlink(postexecargs[b'unlink']) |
37214
f09a2eab11cf
server: add an error feedback mechanism for when the daemon fails to launch
Matt Harbison <matt_harbison@yahoo.com>
parents:
37123
diff
changeset
|
163 |
30515
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
164 if runfn: |
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
165 return runfn() |
30516
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30515
diff
changeset
|
166 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
167 |
30516
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30515
diff
changeset
|
168 _cmdservicemap = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
169 b'chgunix': chgserver.chgunixservice, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
170 b'pipe': commandserver.pipeservice, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
171 b'unix': commandserver.unixforkingservice, |
30516
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30515
diff
changeset
|
172 } |
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30515
diff
changeset
|
173 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
174 |
30519
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30518
diff
changeset
|
175 def _createcmdservice(ui, repo, opts): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
176 mode = opts[b'cmdserver'] |
30516
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30515
diff
changeset
|
177 try: |
40841
368ecbf734af
commandserver: enable logging when server process started
Yuya Nishihara <yuya@tcha.org>
parents:
38823
diff
changeset
|
178 servicefn = _cmdservicemap[mode] |
30516
dd539e2d89aa
server: move service table and factory from commandserver
Yuya Nishihara <yuya@tcha.org>
parents:
30515
diff
changeset
|
179 except KeyError: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
180 raise error.Abort(_(b'unknown mode %s') % mode) |
40842
82210d88d814
commandserver: install logger to record server events through canonical API
Yuya Nishihara <yuya@tcha.org>
parents:
40841
diff
changeset
|
181 commandserver.setuplogging(ui, repo) |
40841
368ecbf734af
commandserver: enable logging when server process started
Yuya Nishihara <yuya@tcha.org>
parents:
38823
diff
changeset
|
182 return servicefn(ui, repo, opts) |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
183 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
184 |
30519
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30518
diff
changeset
|
185 def _createhgwebservice(ui, repo, opts): |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
186 # this way we can check if something was given in the command-line |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
187 if opts.get(b'port'): |
46907
ffd3e823a7e5
urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46819
diff
changeset
|
188 opts[b'port'] = urlutil.getport(opts.get(b'port')) |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
189 |
32331
bd872f64a8ba
cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents:
32005
diff
changeset
|
190 alluis = {ui} |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
191 if repo: |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
192 baseui = repo.baseui |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
193 alluis.update([repo.baseui, repo.ui]) |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
194 else: |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
195 baseui = ui |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
196 webconf = opts.get(b'web_conf') or opts.get(b'webdir_conf') |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
197 if webconf: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
198 if opts.get(b'subrepos'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
199 raise error.Abort(_(b'--web-conf cannot be used with --subrepos')) |
32005
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31554
diff
changeset
|
200 |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
201 # 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:
30516
diff
changeset
|
202 # hgwebdir to reload webconf cleanly |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
203 servui = ui.copy() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
204 servui.readconfig(webconf, sections=[b'web']) |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
205 alluis.add(servui) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
206 elif opts.get(b'subrepos'): |
32005
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31554
diff
changeset
|
207 servui = ui |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31554
diff
changeset
|
208 |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31554
diff
changeset
|
209 # 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:
31554
diff
changeset
|
210 # message as long as webconf is None. |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31554
diff
changeset
|
211 if repo: |
2406dbba49bd
serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents:
31554
diff
changeset
|
212 webconf = dict() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
213 cmdutil.addwebdirpath(repo, b"", webconf) |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
214 else: |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
215 servui = ui |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
216 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
217 optlist = ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
218 b"name templates style address port prefix ipv6" |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
219 b" accesslog errorlog certificate encoding" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
220 ) |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
221 for o in optlist.split(): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
222 val = opts.get(o, b'') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
223 if val in (None, b''): # should check against default options instead |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
224 continue |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
225 for u in alluis: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
226 u.setconfig(b"web", o, val, b'serve') |
30518
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
227 |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
228 app = hgweb.createapp(baseui, repo, webconf) |
add7bcad1d9c
server: move service factory from hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30516
diff
changeset
|
229 return hgweb.httpservice(servui, app, opts) |
30519
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30518
diff
changeset
|
230 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40842
diff
changeset
|
231 |
30519
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30518
diff
changeset
|
232 def createservice(ui, repo, opts): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
233 if opts[b"cmdserver"]: |
30519
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30518
diff
changeset
|
234 return _createcmdservice(ui, repo, opts) |
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30518
diff
changeset
|
235 else: |
a0878bc87379
server: add public function to select either cmdserver or hgweb
Yuya Nishihara <yuya@tcha.org>
parents:
30518
diff
changeset
|
236 return _createhgwebservice(ui, repo, opts) |