Mercurial > public > mercurial-scm > hg
comparison mercurial/server.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | 82210d88d814 |
children | 687b865b95ad |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
19 hgweb, | 19 hgweb, |
20 pycompat, | 20 pycompat, |
21 util, | 21 util, |
22 ) | 22 ) |
23 | 23 |
24 from .utils import ( | 24 from .utils import procutil |
25 procutil, | 25 |
26 ) | 26 |
27 | 27 def runservice( |
28 def runservice(opts, parentfn=None, initfn=None, runfn=None, logfile=None, | 28 opts, |
29 runargs=None, appendpid=False): | 29 parentfn=None, |
30 initfn=None, | |
31 runfn=None, | |
32 logfile=None, | |
33 runargs=None, | |
34 appendpid=False, | |
35 ): | |
30 '''Run a command as a service.''' | 36 '''Run a command as a service.''' |
31 | 37 |
32 postexecargs = {} | 38 postexecargs = {} |
33 | 39 |
34 if opts['daemon_postexec']: | 40 if opts['daemon_postexec']: |
36 if inst.startswith('unlink:'): | 42 if inst.startswith('unlink:'): |
37 postexecargs['unlink'] = inst[7:] | 43 postexecargs['unlink'] = inst[7:] |
38 elif inst.startswith('chdir:'): | 44 elif inst.startswith('chdir:'): |
39 postexecargs['chdir'] = inst[6:] | 45 postexecargs['chdir'] = inst[6:] |
40 elif inst != 'none': | 46 elif inst != 'none': |
41 raise error.Abort(_('invalid value for --daemon-postexec: %s') | 47 raise error.Abort( |
42 % inst) | 48 _('invalid value for --daemon-postexec: %s') % inst |
49 ) | |
43 | 50 |
44 # When daemonized on Windows, redirect stdout/stderr to the lockfile (which | 51 # When daemonized on Windows, redirect stdout/stderr to the lockfile (which |
45 # gets cleaned up after the child is up and running), so that the parent can | 52 # gets cleaned up after the child is up and running), so that the parent can |
46 # read and print the error if this child dies early. See 594dd384803c. On | 53 # read and print the error if this child dies early. See 594dd384803c. On |
47 # other platforms, the child can write to the parent's stdio directly, until | 54 # other platforms, the child can write to the parent's stdio directly, until |
49 if pycompat.iswindows and opts['daemon_postexec']: | 56 if pycompat.iswindows and opts['daemon_postexec']: |
50 if 'unlink' in postexecargs and os.path.exists(postexecargs['unlink']): | 57 if 'unlink' in postexecargs and os.path.exists(postexecargs['unlink']): |
51 procutil.stdout.flush() | 58 procutil.stdout.flush() |
52 procutil.stderr.flush() | 59 procutil.stderr.flush() |
53 | 60 |
54 fd = os.open(postexecargs['unlink'], | 61 fd = os.open( |
55 os.O_WRONLY | os.O_APPEND | os.O_BINARY) | 62 postexecargs['unlink'], os.O_WRONLY | os.O_APPEND | os.O_BINARY |
63 ) | |
56 try: | 64 try: |
57 os.dup2(fd, procutil.stdout.fileno()) | 65 os.dup2(fd, procutil.stdout.fileno()) |
58 os.dup2(fd, procutil.stderr.fileno()) | 66 os.dup2(fd, procutil.stderr.fileno()) |
59 finally: | 67 finally: |
60 os.close(fd) | 68 os.close(fd) |
82 for i in pycompat.xrange(1, len(runargs)): | 90 for i in pycompat.xrange(1, len(runargs)): |
83 if runargs[i].startswith('--cwd='): | 91 if runargs[i].startswith('--cwd='): |
84 del runargs[i] | 92 del runargs[i] |
85 break | 93 break |
86 elif runargs[i].startswith('--cwd'): | 94 elif runargs[i].startswith('--cwd'): |
87 del runargs[i:i + 2] | 95 del runargs[i : i + 2] |
88 break | 96 break |
97 | |
89 def condfn(): | 98 def condfn(): |
90 return not os.path.exists(lockpath) | 99 return not os.path.exists(lockpath) |
100 | |
91 pid = procutil.rundetached(runargs, condfn) | 101 pid = procutil.rundetached(runargs, condfn) |
92 if pid < 0: | 102 if pid < 0: |
93 # If the daemonized process managed to write out an error msg, | 103 # If the daemonized process managed to write out an error msg, |
94 # report it. | 104 # report it. |
95 if pycompat.iswindows and os.path.exists(lockpath): | 105 if pycompat.iswindows and os.path.exists(lockpath): |
124 procutil.stderr.flush() | 134 procutil.stderr.flush() |
125 | 135 |
126 nullfd = os.open(os.devnull, os.O_RDWR) | 136 nullfd = os.open(os.devnull, os.O_RDWR) |
127 logfilefd = nullfd | 137 logfilefd = nullfd |
128 if logfile: | 138 if logfile: |
129 logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND, | 139 logfilefd = os.open( |
130 0o666) | 140 logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND, 0o666 |
141 ) | |
131 os.dup2(nullfd, procutil.stdin.fileno()) | 142 os.dup2(nullfd, procutil.stdin.fileno()) |
132 os.dup2(logfilefd, procutil.stdout.fileno()) | 143 os.dup2(logfilefd, procutil.stdout.fileno()) |
133 os.dup2(logfilefd, procutil.stderr.fileno()) | 144 os.dup2(logfilefd, procutil.stderr.fileno()) |
134 stdio = (procutil.stdin.fileno(), procutil.stdout.fileno(), | 145 stdio = ( |
135 procutil.stderr.fileno()) | 146 procutil.stdin.fileno(), |
147 procutil.stdout.fileno(), | |
148 procutil.stderr.fileno(), | |
149 ) | |
136 if nullfd not in stdio: | 150 if nullfd not in stdio: |
137 os.close(nullfd) | 151 os.close(nullfd) |
138 if logfile and logfilefd not in stdio: | 152 if logfile and logfilefd not in stdio: |
139 os.close(logfilefd) | 153 os.close(logfilefd) |
140 | 154 |
143 if 'unlink' in postexecargs: | 157 if 'unlink' in postexecargs: |
144 os.unlink(postexecargs['unlink']) | 158 os.unlink(postexecargs['unlink']) |
145 | 159 |
146 if runfn: | 160 if runfn: |
147 return runfn() | 161 return runfn() |
162 | |
148 | 163 |
149 _cmdservicemap = { | 164 _cmdservicemap = { |
150 'chgunix': chgserver.chgunixservice, | 165 'chgunix': chgserver.chgunixservice, |
151 'pipe': commandserver.pipeservice, | 166 'pipe': commandserver.pipeservice, |
152 'unix': commandserver.unixforkingservice, | 167 'unix': commandserver.unixforkingservice, |
153 } | 168 } |
169 | |
154 | 170 |
155 def _createcmdservice(ui, repo, opts): | 171 def _createcmdservice(ui, repo, opts): |
156 mode = opts['cmdserver'] | 172 mode = opts['cmdserver'] |
157 try: | 173 try: |
158 servicefn = _cmdservicemap[mode] | 174 servicefn = _cmdservicemap[mode] |
159 except KeyError: | 175 except KeyError: |
160 raise error.Abort(_('unknown mode %s') % mode) | 176 raise error.Abort(_('unknown mode %s') % mode) |
161 commandserver.setuplogging(ui, repo) | 177 commandserver.setuplogging(ui, repo) |
162 return servicefn(ui, repo, opts) | 178 return servicefn(ui, repo, opts) |
179 | |
163 | 180 |
164 def _createhgwebservice(ui, repo, opts): | 181 def _createhgwebservice(ui, repo, opts): |
165 # this way we can check if something was given in the command-line | 182 # this way we can check if something was given in the command-line |
166 if opts.get('port'): | 183 if opts.get('port'): |
167 opts['port'] = util.getport(opts.get('port')) | 184 opts['port'] = util.getport(opts.get('port')) |
191 webconf = dict() | 208 webconf = dict() |
192 cmdutil.addwebdirpath(repo, "", webconf) | 209 cmdutil.addwebdirpath(repo, "", webconf) |
193 else: | 210 else: |
194 servui = ui | 211 servui = ui |
195 | 212 |
196 optlist = ("name templates style address port prefix ipv6" | 213 optlist = ( |
197 " accesslog errorlog certificate encoding") | 214 "name templates style address port prefix ipv6" |
215 " accesslog errorlog certificate encoding" | |
216 ) | |
198 for o in optlist.split(): | 217 for o in optlist.split(): |
199 val = opts.get(o, '') | 218 val = opts.get(o, '') |
200 if val in (None, ''): # should check against default options instead | 219 if val in (None, ''): # should check against default options instead |
201 continue | 220 continue |
202 for u in alluis: | 221 for u in alluis: |
203 u.setconfig("web", o, val, 'serve') | 222 u.setconfig("web", o, val, 'serve') |
204 | 223 |
205 app = hgweb.createapp(baseui, repo, webconf) | 224 app = hgweb.createapp(baseui, repo, webconf) |
206 return hgweb.httpservice(servui, app, opts) | 225 return hgweb.httpservice(servui, app, opts) |
226 | |
207 | 227 |
208 def createservice(ui, repo, opts): | 228 def createservice(ui, repo, opts): |
209 if opts["cmdserver"]: | 229 if opts["cmdserver"]: |
210 return _createcmdservice(ui, repo, opts) | 230 return _createcmdservice(ui, repo, opts) |
211 else: | 231 else: |