def callconduit(repo, name, params): """call Conduit API, params is a dict. return json.loads result, or None""" host, token = readurltoken(repo) url, authinfo = util.url(b'/'.join([host, b'api', name])).authinfo() repo.ui.debug(b'Conduit Call: %s %s\n' % (url, params)) params = params.copy() params[b'api.token'] = token data = urlencodenested(params) curlcmd = repo.ui.config(b'phabricator', b'curlcmd') if curlcmd: sin, sout = procutil.popen2(b'%s -d @- %s' % (curlcmd, procutil.shellquote(url))) sin.write(data) sin.close() body = sout.read() else: urlopener = urlmod.opener(repo.ui, authinfo) request = util.urlreq.request(url, data=data) body = urlopener.open(request).read() repo.ui.debug(b'Conduit Response: %s\n' % body) parsed = json.loads(body) if parsed.get(r'error_code'): msg = (_(b'Conduit Error (%s): %s') % (parsed[r'error_code'], parsed[r'error_info'])) raise error.Abort(msg) return parsed[r'result']
def callconduit(ui, name, params): """call Conduit API, params is a dict. return json.loads result, or None""" host, token = readurltoken(ui) url, authinfo = util.url(b'/'.join([host, b'api', name])).authinfo() ui.debug(b'Conduit Call: %s %s\n' % (url, pycompat.byterepr(params))) params = params.copy() params[b'api.token'] = token data = urlencodenested(params) curlcmd = ui.config(b'phabricator', b'curlcmd') if curlcmd: sin, sout = procutil.popen2(b'%s -d @- %s' % (curlcmd, procutil.shellquote(url))) sin.write(data) sin.close() body = sout.read() else: urlopener = urlmod.opener(ui, authinfo) request = util.urlreq.request(pycompat.strurl(url), data=data) with contextlib.closing(urlopener.open(request)) as rsp: body = rsp.read() ui.debug(b'Conduit Response: %s\n' % body) parsed = pycompat.rapply( lambda x: encoding.unitolocal(x) if isinstance(x, pycompat.unicode) else x, json.loads(body)) if parsed.get(b'error_code'): msg = (_(b'Conduit Error (%s): %s') % (parsed[b'error_code'], parsed[b'error_info'])) raise error.Abort(msg) return parsed[b'result']
def _connect(self): root = self.cvsroot conntype = None user, host = None, None cmd = [b'cvs', b'server'] self.ui.status(_(b"connecting to %s\n") % root) if root.startswith(b":pserver:"): root = root[9:] m = re.match( r'(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?(.*)', root ) if m: conntype = b"pserver" user, passw, serv, port, root = m.groups() if not user: user = b"anonymous" if not port: port = 2401 else: port = int(port) format0 = b":pserver:%s@%s:%s" % (user, serv, root) format1 = b":pserver:%s@%s:%d%s" % (user, serv, port, root) if not passw: passw = b"A" cvspass = os.path.expanduser(b"~/.cvspass") try: pf = open(cvspass, b'rb') for line in pf.read().splitlines(): part1, part2 = line.split(b' ', 1) # /1 :pserver:[email protected]:2401/cvsroot/foo # Ah<Z if part1 == b'/1': part1, part2 = part2.split(b' ', 1) format = format1 # :pserver:[email protected]:/cvsroot/foo Ah<Z else: format = format0 if part1 == format: passw = part2 break pf.close() except IOError as inst: if inst.errno != errno.ENOENT: if not getattr(inst, 'filename', None): inst.filename = cvspass raise sck = socket.socket() sck.connect((serv, port)) sck.send( b"\n".join( [ b"BEGIN AUTH REQUEST", root, user, passw, b"END AUTH REQUEST", b"", ] ) ) if sck.recv(128) != b"I LOVE YOU\n": raise error.Abort(_(b"CVS pserver authentication failed")) self.writep = self.readp = sck.makefile(b'r+') if not conntype and root.startswith(b":local:"): conntype = b"local" root = root[7:] if not conntype: # :ext:user@host/home/user/path/to/cvsroot if root.startswith(b":ext:"): root = root[5:] m = re.match(br'(?:([^@:/]+)@)?([^:/]+):?(.*)', root) # Do not take Windows path "c:\foo\bar" for a connection strings if os.path.isdir(root) or not m: conntype = b"local" else: conntype = b"rsh" user, host, root = m.group(1), m.group(2), m.group(3) if conntype != b"pserver": if conntype == b"rsh": rsh = encoding.environ.get(b"CVS_RSH") or b"ssh" if user: cmd = [rsh, b'-l', user, host] + cmd else: cmd = [rsh, host] + cmd # popen2 does not support argument lists under Windows cmd = [procutil.shellquote(arg) for arg in cmd] cmd = procutil.quotecommand(b' '.join(cmd)) self.writep, self.readp = procutil.popen2(cmd) self.realroot = root self.writep.write(b"Root %s\n" % root) self.writep.write( b"Valid-responses ok error Valid-requests Mode" b" M Mbinary E Checked-in Created Updated" b" Merged Removed\n" ) self.writep.write(b"valid-requests\n") self.writep.flush() r = self.readp.readline() if not r.startswith(b"Valid-requests"): raise error.Abort( _( b'unexpected response from CVS server ' b'(expected "Valid-requests", but got %r)' ) % r ) if b"UseUnchanged" in r: self.writep.write(b"UseUnchanged\n") self.writep.flush() self.readp.readline()