示例#1
0
def _handleconfigchange(ui, cw):
    # Obtain the old and new content so we can show a diff.
    newbuf = pycompat.bytesio()
    cw.write(newbuf)
    newbuf.seek(0)
    newlines = [pycompat.sysstr(l.rstrip()) for l in newbuf.readlines()]
    oldlines = []
    if os.path.exists(cw.path):
        with open(cw.path, 'rb') as fh:
            oldlines = [pycompat.sysstr(l.rstrip()) for l in fh.readlines()]

    diff = list(difflib.unified_diff(oldlines, newlines,
                                     'hgrc.old', 'hgrc.new',
                                     lineterm=''))

    if len(diff):
        ui.write(b'Your config file needs updating.\n')
        if not ui.promptchoice(b'Would you like to see a diff of the changes first (Yn)? $$ &Yes $$ &No'):
            for line in diff:
                ui.write(b'%s\n' % pycompat.bytestr(line))
            ui.write(b'\n')

        if not ui.promptchoice(b'Write changes to hgrc file (Yn)? $$ &Yes $$ &No'):
            with open(cw.path, 'wb') as fh:
                fh.write(newbuf.getvalue())
        else:
            ui.write(b'config changes not written; we would have written the following:\n')
            ui.write(newbuf.getvalue())
            return 1
示例#2
0
def capturestderr():
    """Replace utils.procutil.stderr with a pycompat.bytesio instance

    The instance is made available as the return value of __enter__.

    This contextmanager is reentrant.

    """
    orig = utils.procutil.stderr
    utils.procutil.stderr = pycompat.bytesio()
    try:
        yield utils.procutil.stderr
    finally:
        utils.procutil.stderr = orig
    def open(self, url, data=None, timeout=None):
        path = self.ui.config(b'urlintercept', b'path')
        if not path:
            raise error.Abort(b'no urlintercept path defined!')

        with open(path, 'rb') as fh:
            expected = fh.readline().rstrip()
            response = fh.read()

        # TRACKING py3 - we require a Request object
        if isinstance(url, urllibcompat.urlreq.request):
            url = pycompat.bytestr(url.get_full_url())

        if url != expected:
            raise error.Abort(b'Incorrect URL. Got %s; expected %s' %
                              (url, expected))

        self.ui.write(b'intercepting url\n')
        return pycompat.bytesio(response)
    def open(self, req, data=None, timeout=None):
        """
        Args:
            req (Request)

        Returns:
            pycompat.bytesio (Python version dependent) object with
            extra `getcode` attribute.

        Raises:
            HTTPError, URLError
        """
        path = self.ui.config(b'advancedurlintercept', b'path')
        if not path:
            raise error.Abort(b'no urlintercept path defined!')

        with open(path, 'rb') as fh:
            data = json.load(fh)

        response = data[req.get_full_url()]

        if response["code"] is None:
            # No response, i.e. raise URLError
            raise urllibcompat.urlerr.urlerror(b"fake error")

        if response["code"] != 200:
            raise urllibcompat.urlerr.httperror(
                url,
                response["code"],
                b"", None, None)

        return_obj = pycompat.bytesio(
            pycompat.bytestr(json.dumps(response["data"]))
        )

        # Urllib "openers" expect a file-like object with some
        # extra helper parameters. Add them here.
        return_obj.getcode = lambda: 200

        return return_obj