Configuration scopes internals

Warning

Be careful with these! Internal APIs are not covered by version policies. They can break or be removed between minor versions!

If you need an internal API stabilized please file an issue.

vcspull._internal.scopes resolves active configuration files across system, user, and ancestor project scopes and evaluates destination containment against the trusted project directory set — the resolver beneath vcspull.config.load_scoped_configs(), vcspull config, and vcspull trust. See Configuration scopes for user-facing semantics.

Configuration scope resolution and project trust.

vcspull reads configuration from four scopes. Weakest first, every scope is unioned into one stack:

Scope

Location

system

/etc/vcspull/*.{yaml,yml,json}

user

the user config directory, then ~/.vcspull.{yaml,json}

project

.vcspull.{yaml,yml,json} in each ancestor of the working directory, outermost first

Everything here is a resolver: it decides which files participate and whether a project file may act, never what a file means. Nothing in this module parses repository entries, and nothing prompts — a caller that needs consent gets the list of offending destinations from requires_trust() and decides how to ask.

vcspull._internal.scopes.ConfigScope
data
data
vcspull._internal.scopes.ConfigScope

Where a configuration file lives, and therefore how much it is trusted.

alias of Literal[‘system’, ‘user’, ‘project’, ‘external’]

vcspull._internal.scopes.CONFIG_SUFFIXES: Final = ('.yaml', '.yml', '.json')
data
data
vcspull._internal.scopes.CONFIG_SUFFIXES: Final = ('.yaml', '.yml', '.json')

Recognized config suffixes, weakest first within a single directory.

vcspull._internal.scopes.SYSTEM_CONFIG_DIR: Final = PosixPath('/etc/vcspull')
data
data
vcspull._internal.scopes.SYSTEM_CONFIG_DIR: Final = PosixPath('/etc/vcspull')

Machine-wide configuration directory.

vcspull._internal.scopes.PROJECT_CONFIG_STEM: Final = '.vcspull'
data
data
vcspull._internal.scopes.PROJECT_CONFIG_STEM: Final = '.vcspull'

Basename of a project configuration, before its suffix.

vcspull._internal.scopes._real(path)
function[source]
function[source]
vcspull._internal.scopes._real(path)

Return path with ~ expanded and every symlink followed.

Both sides of a containment or ceiling comparison go through this, so a symlinked $HOME stops the project walk and a symlinked workspace root cannot smuggle a destination back out of its directory.

Examples

>>> link = tmp_path / "link"
>>> link.symlink_to(tmp_path, target_is_directory=True)
>>> _real(link / "sub") == _real(tmp_path / "sub")
True
Parameters:

path (Path)

Return type:

Path

class vcspull._internal.scopes.ConfigSource
class vcspull._internal.scopes.ConfigSource

Bases: NamedTuple

One configuration file in the resolution stack.

Sources are ordered weakest to strongest, so a later source overrides an earlier one for any repository they both name.

vcspull._internal.scopes.env_flag(name, environ=os.environ)
function[source]
function[source]
vcspull._internal.scopes.env_flag(name, environ=os.environ)

Return whether an environment variable is set to a truthy word.

Parameters:
  • name (str) – Environment variable to read.

  • environ (Mapping[str, str]) – Environment to read from. Defaults to os.environ.

Return type:

bool

Examples

>>> env_flag("VCSPULL_YES", {"VCSPULL_YES": "1"})
True
>>> env_flag("VCSPULL_YES", {"VCSPULL_YES": "0"})
False
>>> env_flag("VCSPULL_YES", {})
False
vcspull._internal.scopes.ceiling_paths(environ=os.environ, *, home)
function[source]
function[source]
vcspull._internal.scopes.ceiling_paths(environ=os.environ, *, home)

Return the directories the upward walk stops at.

VCSPULL_CEILING_PATHS replaces the default stop set. Setting it to an empty value lets the walk run to the filesystem root. Every entry is resolved, so a ceiling reached through a symlink still stops the walk.

Parameters:
  • environ (Mapping[str, str]) – Environment to read from. Defaults to os.environ.

  • home (pathlib.Path) – User home directory, the default ceiling.

Return type:

frozenset[Path]

Examples

>>> ceiling_paths({}, home=tmp_path) == frozenset({_real(tmp_path)})
True
>>> sorted(
...     ceiling_paths(
...         {"VCSPULL_CEILING_PATHS": f"/srv{os.pathsep}/opt"},
...         home=tmp_path,
...     )
... )
[PosixPath('/opt'), PosixPath('/srv')]
>>> ceiling_paths({"VCSPULL_CEILING_PATHS": ""}, home=tmp_path)
frozenset()
vcspull._internal.scopes.project_dirs(cwd, *, ceilings)
function[source]
function[source]
vcspull._internal.scopes.project_dirs(cwd, *, ceilings)

Return the ancestors of cwd that may hold a project config.

Outermost first, so the returned order is weakest to strongest. A ceiling directory stops the walk and is itself excluded; the filesystem root ends it either way.

Parameters:
  • cwd (pathlib.Path) – Directory to walk up from.

  • ceilings (frozenset of pathlib.Path) – Resolved directories to stop at, from ceiling_paths().

Return type:

tuple[Path, ...]

Examples

>>> home = pathlib.Path("/home/u")
>>> project_dirs(pathlib.Path("/home/u/work/api"), ceilings=frozenset({home}))
(PosixPath('/home/u/work'), PosixPath('/home/u/work/api'))

Standing on the ceiling yields nothing:

>>> project_dirs(home, ceilings=frozenset({home}))
()

Outside the ceiling, the walk runs to the root:

>>> project_dirs(pathlib.Path("/srv/app"), ceilings=frozenset({home}))
(PosixPath('/'), PosixPath('/srv'), PosixPath('/srv/app'))
vcspull._internal.scopes._config_files_in(directory)
function[source]
function[source]
vcspull._internal.scopes._config_files_in(directory)

Return sorted config files directly inside directory.

A directory that cannot be listed, or one holding a directory named foo.yaml, contributes nothing rather than raising.

Examples

>>> scope_dir = tmp_path / "scope"
>>> (scope_dir / "not-a-file.yaml").mkdir(parents=True)
>>> _ = (scope_dir / "b.json").write_text("{}", encoding="utf-8")
>>> _ = (scope_dir / "a.yaml").write_text("", encoding="utf-8")
>>> [path.name for path in _config_files_in(scope_dir)]
['a.yaml', 'b.json']
>>> _config_files_in(tmp_path / "missing")
()
Parameters:

directory (Path)

Return type:

tuple[Path, ...]

vcspull._internal.scopes._project_files_in(directory)
function[source]
function[source]
vcspull._internal.scopes._project_files_in(directory)

Return the project configs directly inside directory.

Examples

>>> _ = (tmp_path / ".vcspull.yaml").write_text("", encoding="utf-8")
>>> _ = (tmp_path / ".vcspull.json").write_text("{}", encoding="utf-8")
>>> [path.name for path in _project_files_in(tmp_path)]
['.vcspull.yaml', '.vcspull.json']
Parameters:

directory (Path)

Return type:

tuple[Path, ...]

vcspull._internal.scopes.resolve_sources(*, cwd, home=None, environ=os.environ, include_project=True)
function[source]
function[source]
vcspull._internal.scopes.resolve_sources(*, cwd, home=None, environ=os.environ, include_project=True)

Return the configuration stack in effect, weakest source first.

Parameters:
  • cwd (pathlib.Path) – Working directory the project walk starts from.

  • home (pathlib.Path, optional) – User home directory, the default project-walk ceiling. Defaults to pathlib.Path.home().

  • environ (Mapping[str, str]) – Environment to read from. Defaults to os.environ.

  • include_project (bool) – Set False (or export VCSPULL_NO_PROJECT=1) to drop the project scope and resolve only system and user configuration.

Returns:

Existing files only, ordered weakest to strongest.

Return type:

tuple of ConfigSource

Raises:

vcspull.exc.MultipleConfigError – When both ~/.vcspull.yaml and ~/.vcspull.json exist.

Examples

A project config in the working directory joins the stack:

>>> _ = (tmp_path / ".vcspull.yaml").write_text("", encoding="utf-8")
>>> [source.scope for source in resolve_sources(cwd=tmp_path, home=tmp_path.parent)]
['project']

VCSPULL_NO_PROJECT drops it:

>>> resolve_sources(
...     cwd=tmp_path,
...     home=tmp_path.parent,
...     environ={"VCSPULL_NO_PROJECT": "1"},
... )
()
vcspull._internal.scopes.classify_scope(config_path, *, cwd, home, environ=os.environ)
function[source]
function[source]
vcspull._internal.scopes.classify_scope(config_path, *, cwd, home, environ=os.environ)

Return the scope a single configuration file belongs to.

Unlike resolve_sources(), this answers the question for a path the caller already holds — a --file argument, or the file a write command is about to touch. A path that is neither user, system, nor inside the working directory is external.

Parameters:
Return type:

ConfigScope

Examples

>>> project = tmp_path / "proj"
>>> project.mkdir()
>>> classify_scope(
...     project / ".vcspull.yaml", cwd=project, home=tmp_path / "home"
... )
'project'
>>> classify_scope(
...     tmp_path / "home" / ".vcspull.yaml",
...     cwd=project,
...     home=tmp_path / "home",
... )
'user'
>>> classify_scope(
...     tmp_path / "elsewhere.yaml", cwd=project, home=tmp_path / "home"
... )
'external'
vcspull._internal.scopes._is_within(path, directory)
function[source]
function[source]
vcspull._internal.scopes._is_within(path, directory)

Return whether path is directory or lives beneath it.

Examples

>>> _is_within(pathlib.Path("/w/vendor/x"), pathlib.Path("/w"))
True
>>> _is_within(pathlib.Path("/w"), pathlib.Path("/w"))
True
>>> _is_within(pathlib.Path("/workspace"), pathlib.Path("/w"))
False
Parameters:
Return type:

bool

vcspull._internal.scopes.escaping_destinations(destinations, *, config_dir, cwd)
function[source]
function[source]
vcspull._internal.scopes.escaping_destinations(destinations, *, config_dir, cwd)

Return the repository destinations that resolve outside config_dir.

A configuration is contained when every repository it declares would be checked out at or beneath the directory holding it. Containment is what keeps the trust gate quiet for the overwhelming majority of project configs, which target ./vendor or a sibling checkout.

The check is over destinations rather than the workspace-root keys, because a repository entry may override its destination with path: and never consult its workspace root at all. Symlinks are followed on both sides, so a destination that points out of the tree through a link is still reported.

Parameters:
Returns:

Offending destinations, resolved, in order and without duplicates. Empty when the configuration is contained.

Return type:

tuple of pathlib.Path

Examples

>>> project = tmp_path / "project"
>>> project.mkdir()
>>> escaping_destinations(
...     [project / "vendor" / "flask"], config_dir=project, cwd=project
... )
()
>>> escaping_destinations(
...     [pathlib.Path("~/.ssh/evil")], config_dir=project, cwd=project
... ) == (_real(pathlib.Path("~/.ssh/evil")),)
True
vcspull._internal.scopes.requires_trust(source, destinations, *, cwd, trusted)
function[source]
function[source]
vcspull._internal.scopes.requires_trust(source, destinations, *, cwd, trusted)

Return the destinations that need explicit consent before loading.

Empty means the source may load silently: system, user, and explicit configurations are automatic, an already-trusted project directory is remembered, and a contained project config never asks.

Parameters:
  • source (ConfigSource) – Source under consideration.

  • destinations (Iterable[pathlib.Path]) – Checkout destinations that file declares.

  • cwd (pathlib.Path) – Working directory, used to resolve relative destinations.

  • trusted (frozenset of pathlib.Path) – Resolved directories from read_trusted().

Return type:

tuple[Path, ...]

Examples

>>> project = tmp_path / "project"
>>> project.mkdir()
>>> source = ConfigSource("project", project / ".vcspull.yaml", project)
>>> escaping = [pathlib.Path("~/.ssh/evil")]
>>> requires_trust(
...     source, escaping, cwd=project, trusted=frozenset()
... ) == (_real(pathlib.Path("~/.ssh/evil")),)
True

A user-scope source is never gated, and neither is a trusted directory:

>>> user = ConfigSource("user", tmp_path / "u.yaml", tmp_path)
>>> requires_trust(user, escaping, cwd=project, trusted=frozenset())
()
>>> requires_trust(
...     source, escaping, cwd=project, trusted=frozenset({_real(project)})
... )
()
vcspull._internal.scopes.trust_state_file(*, home=None, environ=os.environ)
function[source]
function[source]
vcspull._internal.scopes.trust_state_file(*, home=None, environ=os.environ)

Return the file recording trusted project directories.

Parameters:
Return type:

Path

Examples

>>> trust_state_file(
...     home=pathlib.Path("/home/u"), environ={"XDG_STATE_HOME": "/state"}
... )
PosixPath('/state/vcspull/trusted')
>>> trust_state_file(home=pathlib.Path("/home/u"), environ={})
PosixPath('/home/u/.local/state/vcspull/trusted')
vcspull._internal.scopes.read_trusted(state_file)
function[source]
function[source]
vcspull._internal.scopes.read_trusted(state_file)

Return the trusted project directories recorded in state_file.

A missing or unreadable state file means nothing is trusted yet.

Examples

>>> read_trusted(tmp_path / "never-written")
frozenset()
>>> state = tmp_path / "trusted"
>>> write_trusted(state, [pathlib.Path("/w/api")])
>>> read_trusted(state)
frozenset({PosixPath('/w/api')})
Parameters:

state_file (Path)

Return type:

frozenset[Path]

vcspull._internal.scopes.write_trusted(state_file, directories)
function[source]
function[source]
vcspull._internal.scopes.write_trusted(state_file, directories)

Record directories as the complete set of trusted project directories.

Examples

>>> state = tmp_path / "state" / "trusted"
>>> write_trusted(state, [pathlib.Path("/w/b"), pathlib.Path("/w/a")])
>>> state.read_text(encoding="utf-8").splitlines()
['/w/a', '/w/b']
Parameters:
Return type:

None

vcspull._internal.scopes.trust_directory(state_file, directory)
function[source]
function[source]
vcspull._internal.scopes.trust_directory(state_file, directory)

Add directory to the trusted set.

The project directory is recorded rather than the file, so a second config in an already-trusted project does not ask again.

Examples

>>> state = tmp_path / "trusted"
>>> trust_directory(state, tmp_path / "api")
>>> _real(tmp_path / "api") in read_trusted(state)
True
Parameters:
Return type:

None

vcspull._internal.scopes.untrust_directory(state_file, directory)
function[source]
function[source]
vcspull._internal.scopes.untrust_directory(state_file, directory)

Remove directory from the trusted set.

Examples

>>> state = tmp_path / "trusted"
>>> trust_directory(state, tmp_path / "api")
>>> untrust_directory(state, tmp_path / "api")
>>> read_trusted(state)
frozenset()
Parameters:
Return type:

None