comparison mercurial/cmdutil.py @ 4378:e89f9afc462b

Refactor commands.serve to allow other commands to run as services. This introduces a new function, cmdutil.service.
author Bryan O'Sullivan <bos@serpentine.com>
date Fri, 27 Apr 2007 21:30:55 -0700
parents 1ccd3b9a7b1f
children 736e49292809
comparison
equal deleted inserted replaced
4377:e33ad7cea15f 4378:e89f9afc462b
197 repo.ui.status(_('recording removal of %s as rename to %s ' 197 repo.ui.status(_('recording removal of %s as rename to %s '
198 '(%d%% similar)\n') % 198 '(%d%% similar)\n') %
199 (oldrel, newrel, score * 100)) 199 (oldrel, newrel, score * 100))
200 if not dry_run: 200 if not dry_run:
201 repo.copy(old, new, wlock=wlock) 201 repo.copy(old, new, wlock=wlock)
202
203 def service(opts, parentfn=None, initfn=None, runfn=None):
204 '''Run a command as a service.'''
205
206 if opts['daemon'] and not opts['daemon_pipefds']:
207 rfd, wfd = os.pipe()
208 args = sys.argv[:]
209 args.append('--daemon-pipefds=%d,%d' % (rfd, wfd))
210 pid = os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
211 args[0], args)
212 os.close(wfd)
213 os.read(rfd, 1)
214 if parentfn:
215 return parentfn(pid)
216 else:
217 os._exit(0)
218
219 if initfn:
220 initfn()
221
222 if opts['pid_file']:
223 fp = open(opts['pid_file'], 'w')
224 fp.write(str(os.getpid()) + '\n')
225 fp.close()
226
227 if opts['daemon_pipefds']:
228 rfd, wfd = [int(x) for x in opts['daemon_pipefds'].split(',')]
229 os.close(rfd)
230 try:
231 os.setsid()
232 except AttributeError:
233 pass
234 os.write(wfd, 'y')
235 os.close(wfd)
236 sys.stdout.flush()
237 sys.stderr.flush()
238 fd = os.open(util.nulldev, os.O_RDWR)
239 if fd != 0: os.dup2(fd, 0)
240 if fd != 1: os.dup2(fd, 1)
241 if fd != 2: os.dup2(fd, 2)
242 if fd not in (0, 1, 2): os.close(fd)
243
244 if runfn:
245 return runfn()
202 246
203 class changeset_printer(object): 247 class changeset_printer(object):
204 '''show changeset information when templating not requested.''' 248 '''show changeset information when templating not requested.'''
205 249
206 def __init__(self, ui, repo, patch, buffered): 250 def __init__(self, ui, repo, patch, buffered):