Commit Graph

35 Commits

Author SHA1 Message Date
Pierre-Yves David
30913031d4 error: get Abort from 'error' instead of 'util'
The home of 'Abort' is 'error' not 'util' however, a lot of code seems to be
confused about that and gives all the credit to 'util' instead of the
hardworking 'error'. In a spirit of equity, we break the cycle of injustice and
give back to 'error' the respect it deserves. And screw that 'util' poser.

For great justice.
2015-10-08 12:55:45 -07:00
Matt Mackall
da38b28f4f commandserver: mark developer-only logging option 2015-06-25 17:44:15 -05:00
Gregory Szorc
5380dea2a7 global: mass rewrite to use modern exception syntax
Python 2.6 introduced the "except type as instance" syntax, replacing
the "except type, instance" syntax that came before. Python 3 dropped
support for the latter syntax. Since we no longer support Python 2.4 or
2.5, we have no need to continue supporting the "except type, instance".

This patch mass rewrites the exception syntax to be Python 2.6+ and
Python 3 compatible.

This patch was produced by running `2to3 -f except -w -n .`.
2015-06-23 22:20:08 -07:00
Yuya Nishihara
1c7ce2bd17 cmdserver: protect pipe server streams against corruption caused by direct io
Because pipe-mode server uses stdio as IPC channel, other modules should not
touch stdio directly and use ui instead.  However, this strategy is brittle
because several Python functions read and write stdio implicitly.

    print 'hello'  # should use ui.write()
    # => ch = 'h', size = 1701604463 'ello', data = '\n'

This patch adds protection for such mistakes.  Both stdio files and low-level
file descriptors are redirected to /dev/null while command server uses them.
2014-11-15 13:50:43 +09:00
Yuya Nishihara
2808848bc1 cmdserver: postpone creation of pipe server until run()
This makes it easy to swap file descriptors while running command server.
2014-11-15 13:04:41 +09:00
Yuya Nishihara
0c88d9a994 cmdserver: use given streams as pipe channels like other commands
Because commandserver itself is an hg subcommand, it shouldn't use stdio
directly in principle.
2014-11-15 12:43:35 +09:00
Yuya Nishihara
a9b00f4522 cmdserver: include pid of server handling requests in hello message
Because unix-mode server forks child process per connection, client does not
know the pid of the server that will handle requests.  The pid is necessary
to interrupt hung process:

 1. client connects to socket server
 2. server accepts the connection, forks, and tells pid
 3. client requests "runcommand pull"
    .. hung ..
 4. client sends SIGINT to the (forked) server
 5. server returns from I/O wait

Note that getsockopt(SO_PEERCRED) of Linux cannot be used because the server
fork()s after accept().
2014-10-18 12:24:50 +09:00
Yuya Nishihara
0baf7a676d cmdserver: add service that listens on unix domain socket and forks process
Typical use case of 'unix' mode is a background hg daemon.

    $ hg serve --cmdserver unix --cwd / -a /tmp/hg-`id -u`.sock

Unlike 'pipe' mode in which parent process keeps stdio channel, 'unix' server
can be detached.  So clients can freely connect and disconnect from server,
saving Python start-up time.

It might be better to write "--cmdserver socket -a unix:/sockpath" instead
of "--cmdserver unix -a /sockpath" in case hgweb gets the ability to listen
on unix domain socket.
2014-10-04 16:46:50 +09:00
Yuya Nishihara
754a73fea4 cmdserver: make server streams switchable
In 'unix' mode, server instance will be created per connection, and fin/fout
are set to socket files.
2014-09-27 15:10:14 +09:00
Yuya Nishihara
11d619a47e cmdserver: switch service objects by mode
server class will be changed to accept fin/fout pair instead of mode string
so that it can interact with socket files.
2014-09-27 15:04:46 +09:00
Yuya Nishihara
84c7e5f2c1 cmdserver: wrap 'pipe' mode server by service object
This is the stub for new mode that will listen for connections on unix domain
socket.

Though --daemon option is not banned in 'pipe' mode, it is useless because
the detached 'pipe' mode server exits immediately due to null stdin. Should
it abort if --daemon is specified with --cmdserver pipe or --stdio?
2014-09-27 14:52:09 +09:00
Yuya Nishihara
27aa215891 cmdserver: drop useless in_ attribute from channeledoutput
The previous patch makes sure that in_ == out, so it's no longer needed
to keep in_ for __getattr__.  Also, it seems strange for channeledoutput
to have in_ stream which is actually a writable file object.
2014-09-27 12:37:53 +09:00
Yuya Nishihara
99d8be652a cmdserver: get file attributes of 'e'-channel from stdout, not from stderr
It seems wrong to get attributes from object different than the underlying
file.  In the following example, it doesn't make sense to flush stderr after
writing to stdout:

    self.ferr.write(str(a))
    if not getattr(self.ferr, 'closed', False):
        self.ferr.flush()
2014-09-27 12:27:03 +09:00
Yuya Nishihara
2e0a7a3cee cmdserver: correct doc of channeledoutput
in_ is only used as the source of file attributes.
2014-09-27 12:15:01 +09:00
Yuya Nishihara
96a8ea56df cmdserver: forcibly use L channel to read password input (issue3161)
Command server is designed to use the channel protocol even if the server
process is accessible to tty, whereas vanilla hg should be able to read
password from tty in that case.  So it isn't enough to swap sys.stdin:

    # works only if the server process is detached from the console
    sys.stdin = self.fin
    getpass.getpass('')
    sys.stdin = oldin

or test isatty:

    # vanilla hg can't talk to tty if stdin is redirected
    if self._isatty(self.fin):
        return getpass.getpass('')
    else:
        ...

Since ui.nontty flag is undocumented and command-server channels don't provide
isatty(), this change won't affect the other uses of ui._isatty().

issue3161 also suggests to provide some context of messages.  I think it can
be implemented by using the generic templating function.
2014-04-26 18:13:06 +09:00
Yuya Nishihara
b6eb1297a9 cmdserver: allow to start server without repository
Typical use case is to clone repository through command server.  Clone may
require user interaction, so command-server protocol is beneficial over
raw stdio channels.
2014-03-03 23:21:24 +09:00
Yuya Nishihara
da4723cea3 cmdserver: mask return code of runcommand in the same way as dispatch.run
"hg help" does not state that the code for abort is 255, but it's confusing
to have different code between hg command and command server.

Tests of python-hglib 1.2 passed with this change.
2014-03-03 15:50:51 +09:00
Yuya Nishihara
cb7c7a6489 localrepo: add hook point to invalidate everything on each command-server run
MQ extension will wrap this function to invalidate its state.

repo.invalidate cannot be wrapped for this purpose because qpush obtains
repo.lock in the middle of the operation, triggering repo.invalidate.  Also,
it seems wrong to obtain lock earlier because mq data is non-store parts.
2014-03-03 19:41:23 +09:00
Simon Heimberg
cbac9e9e39 localrepo: prevent to copy repo local config, copy baseui instead
Copying a repos local configuration to another repo is a bad idea because the
2nd repo gets the configuration of the first. Prevent this by really calling
repo.baseui.copy when repo.ui.copy is called.
This requires some changes in commandserver which needs to clone repo.ui for
rejecting temporary changes.

This patch has its roots back in the topic "repo isolation" around 68ae3063a47d
and was suggested by mpm.
2013-11-11 22:59:26 +01:00
Mads Kiilerich
d98fa22802 commandserver: report capabilities sorted 2012-12-12 02:38:14 +01:00
Augie Fackler
787a15630a commandserver: clean up use of two-argument raise
This makes any attempt to port to Python 3 harder, and the new syntax
is supported in 2.4 already.
2013-01-01 12:50:04 -06:00
Mads Kiilerich
2372d51b68 fix wording and not-completely-trivial spelling errors and bad docstrings 2012-08-15 22:39:18 +02:00
Brodie Rao
92158e04de cleanup: "raise SomeException()" -> "raise SomeException" 2012-05-12 16:00:58 +02:00
Brodie Rao
d6a6abf2b0 cleanup: eradicate long lines 2012-05-12 15:54:54 +02:00
Idan Kamara
351bf40844 cmdserver: invalidate the dirstate when running commands (issue3271)
The dirstate is invalidated separately outside of invalidate() which is
already being called (other callers of invalidate() seems to suggest the
separation is there for a reason).
2012-02-15 23:44:10 +02:00
Idan Kamara
ce5a7b8382 cmdserver: repo.invalidate() on every runcommand
This will trigger the filecache and recreate every cached property that was
changed by something other than this cmdserver instance (e.g. by running
'hg commit' at the cmdline).
2011-07-25 22:19:28 +03:00
Idan Kamara
bab89cfab6 cmdserver: take repo.baseui as our ui
The ui passed to server() is really repo.ui, that is it contains its local
configuration as well.

When running commands that use a different repo than the servers cached repo,
we don't want to use that ui as the baseui for the new repo.
2011-07-14 11:46:15 +03:00
Idan Kamara
550bca7579 cmdserver: restore old working dir after dispatch when we have --cwd 2011-07-11 17:49:45 +03:00
Idan Kamara
b66cd482a9 cmdserver: assign repo.baseui before running commands
There are places in the code that use localrepository.baseui (see hg.remoteui),
we need the ui descriptors (and possibly other things) to be set
correctly on it, so output written to the remoteui descriptors ends up at the
right place.

Before this change, tests such as 'test-bookmarks-pushpull.t' didn't work.
2011-06-24 19:44:17 +03:00
Idan Kamara
29b1c451dc cmdserver: copy repo.ui before running commands 2011-06-24 19:43:59 +03:00
Idan Kamara
f916e19611 cmdserver: fix read-loop string concatenation 2011-06-24 16:36:24 +03:00
Idan Kamara
046eba79ef cmdserver: write the hello message as one chunk on the 'o' channel
This is a guaranteed by the protocol: clients know they need to read one chunk
off of the 'o' channel and treat that as the hello message.

They should ignore fields they don't recognize so they stay compatible with
new versions of the server in case we decide to add something.
2011-06-22 17:13:04 +03:00
Idan Kamara
29341e1294 cmdserver, runcommand: properly handle the client sending no arguments
No real reason for a client to do this, but still possible.

Previously if the client sent no arguments, a list with an empty string ['']
would be used as the arguments to dispatch, which would cause hg to complain
about an ambiguous command.

Instead, we simply check for no arguments and use an empty list instead (which
is equivalent to invoking hg with no args on the command line).
2011-06-21 15:38:10 +03:00
Idan Kamara
1d2537f10a cmdserver: don't raise EOFError when trying to read 0 bytes from the client 2011-06-21 15:13:39 +03:00
Idan Kamara
40027596a8 serve: add --cmdserver option to communicate with hg over a pipe 2011-06-03 17:27:41 +03:00