sapling/phabricator/arcconfig.py
Durham Goode e34660b057 commands: update to use registrar instead of cmdutil
Summary: Upstream has deprecated cmdutil.commands() in favor of registrar.commands()

Test Plan: Ran the tests

Reviewers: #mercurial, quark

Reviewed By: quark

Subscribers: mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D5106486

Signature: t1:5106486:1495485074:0e20f00622cc651e8c9dda837f84dd84cc51099e
2017-05-22 13:38:37 -07:00

64 lines
1.8 KiB
Python

# Copyright 2016 Facebook Inc.
#
# Locate and load arcanist configuration for a project
import errno
import json
import os
from mercurial import (
error,
registrar,
)
cmdtable = {}
command = registrar.command(cmdtable)
class ArcConfigError(Exception):
pass
def _load_file(filename):
try:
with open(filename, 'r') as f:
return json.loads(f.read())
except IOError as ex:
if ex.errno == errno.ENOENT:
return None
raise
def load_for_path(path):
# location where `arc install-certificate` writes .arcrc
if os.name == 'nt':
envvar = 'APPDATA'
else:
envvar = 'HOME'
homedir = os.getenv(envvar)
if not homedir:
raise ArcConfigError('$%s environment variable not found' % envvar)
# Use their own file as a basis
userconfig = _load_file(os.path.join(homedir, '.arcrc')) or {}
# Walk up the path and augment with an .arcconfig if we find it,
# terminating the search at that point.
path = os.path.abspath(path)
while len(path) > 1:
config = _load_file(os.path.join(path, '.arcconfig'))
if config is not None:
userconfig.update(config)
# Return the located path too, as we need this for figuring
# out where we are relative to the fbsource root.
userconfig['_arcconfig_path'] = path
return userconfig
path = os.path.dirname(path)
raise ArcConfigError('no .arcconfig found')
@command('debugarcconfig')
def debugarcconfig(ui, repo, *args, **opts):
""" exists purely for testing and diagnostic purposes """
try:
config = load_for_path(repo.root)
ui.write(json.dumps(config, sort_keys=True), '\n')
except ArcConfigError as ex:
raise error.Abort(str(ex))