#! /usr/bin/env python import subprocess import json import sys import re PHAB_HTTP_URI = "https://secure.phabricator.com/api/" PHAB_SSH_HOST = "dweller@secure.phabricator.com" PHAB_SSH_PORT = 22 class ExecutionException(Exception): def __init__(self, command, exit_code): self.command = command self.exit_code = exit_code def execute(*cmd, **kwargs): ''' Execute command. returns stdout if execution exited with 0 raise ExecutionException otherwise. ''' if cmd and isinstance(cmd[0], (list, tuple)): cmd = cmd[0] # print(str(cmd)) stdin = kwargs.get('stdin') p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE) stdout, _ = p.communicate(stdin) c = p.wait() if c != 0: raise ExecutionException(' '.join(cmd), c) return stdout.strip() def error(message): print print ' ' + str(message) print sys.exit(2) try: output = execute('arc', 'get-config', 'phabricator.uri', 'default') match = re.search('Current Value: "(.*)"', output) if match is None: error('arc is not configured with a `phabricator.uri`') api = match.group(1) if api != PHAB_HTTP_URI: error('arc configuration mismatch: ' 'arc is configured to use `%s`, but expected `%s`' % ( api, PHAB_HTTP_URI)) except OSError: error('Could not execute `arc`. Check your path. Maybe start a new shell') try: output = execute('arc', 'call-conduit', 'user.whoami', stdin='{}') except: error('arcanist conduit token is not configured!') output = json.loads(output) if output['errorMessage'] is not None: error(output['errorMessage']) api_user = output['response'] try: output = execute( 'ssh', '-p ' + str(PHAB_SSH_PORT), PHAB_SSH_HOST, 'conduit user.whoami', stdin='{}') except ExecutionException: error('Failed to connect using SSH. Check your keys') output = json.loads(output) if output['error_info'] is not None: error(output['error_info']) ssh_user = output['result'] api_user_name = api_user['userName'] ssh_user_name = ssh_user['userName'] if api_user != ssh_user: if api_user_name == ssh_user_name: error('Identity mismatch: ' 'API and SSH access provide the same username, but the account' 'details are different.') else: error('Identity mismatch: ' 'API token belongs to user `%s`, ' 'but SSH key matches user `%s`' % ( api_user_name, ssh_user_name)) print 'Successfuly configured to use arc as user `%s`' % api_user_name