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 |
|---|---|
|
|
|
the user config directory, then |
|
|
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¶vcspull._internal.scopes.ConfigScope¶
Where a configuration file lives, and therefore how much it is trusted.
alias of
Literal[‘system’, ‘user’, ‘project’, ‘external’]
Recognized config suffixes, weakest first within a single directory.
Machine-wide configuration directory.
Basename of a project configuration, before its suffix.
-
vcspull._internal.scopes._real(path)¶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
$HOMEstops 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
-
class vcspull._internal.scopes.ConfigSource¶class vcspull._internal.scopes.ConfigSource¶
Bases:
NamedTupleOne 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)¶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 toos.environ.
- Return type:
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)¶vcspull._internal.scopes.ceiling_paths(environ=os.environ, *, home)¶
Return the directories the upward walk stops at.
VCSPULL_CEILING_PATHSreplaces 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 toos.environ.home (
pathlib.Path) – User home directory, the default ceiling.
- Return type:
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)¶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, fromceiling_paths().
- Return type:
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)¶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") ()
-
vcspull._internal.scopes._project_files_in(directory)¶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']
-
vcspull._internal.scopes.resolve_sources(*, cwd, home=None, environ=os.environ, include_project=True)¶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 topathlib.Path.home().environ (
Mapping[str,str]) – Environment to read from. Defaults toos.environ.include_project (
bool) – SetFalse(or exportVCSPULL_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.yamland~/.vcspull.jsonexist.
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_PROJECTdrops 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)¶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--fileargument, or the file a write command is about to touch. A path that is neither user, system, nor inside the working directory isexternal.- Parameters:
config_path (
pathlib.Path) – Configuration file to classify.cwd (
pathlib.Path) – Working directory.home (
pathlib.Path) – User home directory.environ (
Mapping[str,str]) – Environment to read from. Defaults toos.environ.
- 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)¶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
-
vcspull._internal.scopes.escaping_destinations(destinations, *, config_dir, cwd)¶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
./vendoror 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:
destinations (
Iterable[pathlib.Path]) – Checkout destinations, asvcspull.config.extract_repos()computes them.config_dir (
pathlib.Path) – Directory holding the configuration file.cwd (
pathlib.Path) – Working directory, used to resolve relative destinations.
- 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)¶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 fromread_trusted().
- Return type:
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)¶vcspull._internal.scopes.trust_state_file(*, home=None, environ=os.environ)¶
Return the file recording trusted project directories.
- Parameters:
home (
pathlib.Path,optional) – User home directory. Defaults topathlib.Path.home().environ (
Mapping[str,str]) – Environment to read from. Defaults toos.environ.
- Return type:
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)¶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')})
-
vcspull._internal.scopes.write_trusted(state_file, directories)¶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']
-
vcspull._internal.scopes.trust_directory(state_file, directory)¶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
-
vcspull._internal.scopes.untrust_directory(state_file, directory)¶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()