debian.substvars module¶
Facilities for reading and writing Debian substvars files
The aim of this module is to provide programmatic access to Debian substvars files to query and manipulate them. The format for the changelog is defined in deb-substvars(5)
Overview¶
The most common use-case for substvars is for package helpers to add or update a substvars (e.g., to add a dependency). This would look something like:
>>> from debian.substvars import Substvars
>>> from tempfile import TemporaryDirectory
>>> import os
>>> # Using a tmp dir for the sake of doctests
>>> with TemporaryDirectory() as debian_dir:
... filename = os.path.join(debian_dir, "foo.substvars")
... with Substvars.load_from_path(filename, missing_ok=True) as svars:
... svars.add_dependency("misc:Depends", "bar (>= 1.0)")
By default, the module creates new substvars as “mandatory” substvars (that triggers a warning by dpkg-gecontrol if not used. However, it does also support the “optional” substvars introduced in dpkg 1.21.8. See Substvars.as_substvars for an example of how to use the “optional” substvars.
The Substvars
class is the key class within this module.
Substvars Classes¶
- class debian.substvars.Substvar(initial_value: str = '', assignment_operator: str = '=')¶
Bases:
object
- _assignment_operator¶
- _value: str | Set[str]¶
- add_dependency(dependency_clause: str) None ¶
- property assignment_operator: str¶
- resolve() str ¶
- class debian.substvars.Substvars¶
Bases:
_Substvars_Base
[Substvars]Substvars is a dict-like object containing known substvars for a given package.
>>> substvars = Substvars() >>> substvars['foo'] = 'bar, golf' >>> substvars['foo'] 'bar, golf' >>> substvars.add_dependency('foo', 'dpkg (>= 1.20.0)') >>> substvars['foo'] 'bar, dpkg (>= 1.20.0), golf' >>> 'foo' in substvars True >>> sorted(substvars) ['foo'] >>> del substvars['foo'] >>> substvars['foo'] Traceback (most recent call last): ... KeyError: 'foo' >>> substvars.get('foo') >>> # None >>> substvars['foo'] = "" >>> substvars['foo'] ''
The Substvars object also provide methods for serializing and deserializing the substvars into and from the format used by dpkg-gencontrol.
The Substvars object can be used as a context manager, which causes the substvars to be saved when the context manager exits successfully (i.e., no exceptions are raised).
- _substvars_path: AnyPath | None¶
- add_dependency(substvar: str, dependency_clause: str) None ¶
Add a dependency clause to a given substvar
>>> substvars = Substvars() >>> # add_dependency automatically creates variables >>> 'misc:Recommends' not in substvars True >>> substvars.add_dependency('misc:Recommends', "foo (>= 1.0)") >>> substvars['misc:Recommends'] 'foo (>= 1.0)' >>> # It can be appended to other variables >>> substvars['foo'] = 'bar, golf' >>> substvars.add_dependency('foo', 'dpkg (>= 1.20.0)') >>> substvars['foo'] 'bar, dpkg (>= 1.20.0), golf' >>> # Exact duplicates are ignored >>> substvars.add_dependency('foo', 'dpkg (>= 1.20.0)') >>> substvars['foo'] 'bar, dpkg (>= 1.20.0), golf'
- property as_substvar: MutableMapping[str, Substvar]¶
Provides a mapping to the Substvars object for more advanced operations
Treating a substvars file mostly as a “str -> str” mapping is sufficient for many cases. But when full control over the substvars (like fiddling with the assignment operator) is needed this attribute is useful.
>>> content = ''' ... # Some comment (which is allowed but no one uses them - also, they are not preserved) ... shlib:Depends=foo (>= 1.0), libbar2 (>= 2.1-3~) ... random:substvar?=With the new assignment operator from dpkg 1.21.8 ... ''' >>> substvars = Substvars() >>> substvars.read_substvars(content.splitlines()) >>> substvars.as_substvar["shlib:Depends"].assignment_operator '=' >>> substvars.as_substvar["random:substvar"].assignment_operator '?=' >>> # Mutation is also possible >>> substvars.as_substvar["shlib:Depends"].assignment_operator = '?=' >>> print(substvars.dump(), end="") shlib:Depends?=foo (>= 1.0), libbar2 (>= 2.1-3~) random:substvar?=With the new assignment operator from dpkg 1.21.8
- dump() str ¶
Debug aid that generates a string representation of the content
For persisting the contents, please consider save() or write_substvars.
- classmethod load_from_path(substvars_path: AnyPath, missing_ok: bool = False) Self ¶
Shorthand for initializing a Substvars from a file
The return substvars will have substvars_path set to the provided path enabling save() to work out of the box. This also makes it easy to combine this with the context manager interface to automatically save the file again.
>>> import os >>> from tempfile import TemporaryDirectory >>> with TemporaryDirectory() as tmpdir: ... filename = os.path.join(tmpdir, "foo.substvars") ... # Obviously, this does not exist ... print("Exists before: " + str(os.path.exists(filename))) ... with Substvars.load_from_path(filename, missing_ok=True) as svars: ... svars.add_dependency("misc:Depends", "bar (>= 1.0)") ... print("Exists after: " + str(os.path.exists(filename))) Exists before: False Exists after: True
- Parameters:
substvars_path – The path to load from
missing_ok – If True, then the path does not have to exist (i.e. FileNotFoundError causes an empty Substvars object to be returned). Combined with the context manager, this is useful for packaging helpers that want to append / update to the existing if it exists or create it if it does not exist.
- read_substvars(fileobj: Iterable[str]) None ¶
Read substvars from an open text file in the format supported by dpkg-gencontrol
On success, all existing variables will be discarded and only variables from the file will be present after this method completes. In case of any IO related errors, the object retains its state prior to the call of this method.
>>> content = ''' ... # Some comment (which is allowed but no one uses them - also, they are not preserved) ... shlib:Depends=foo (>= 1.0), libbar2 (>= 2.1-3~) ... random:substvar?=With the new assignment operator from dpkg 1.21.8 ... ''' >>> substvars = Substvars() >>> substvars.read_substvars(content.splitlines()) >>> substvars["shlib:Depends"] 'foo (>= 1.0), libbar2 (>= 2.1-3~)' >>> substvars["random:substvar"] 'With the new assignment operator from dpkg 1.21.8'
- Parameters:
fileobj – An open file (in text mode using the UTF-8 encoding) or an iterable of str that provides line by line content.
- save() None ¶
Save the substvars file
Replace the path denoted by the substvars_path attribute with the in-memory version of the substvars. Note that the substvars_path property must be not None for this method to work.
- property substvars_path: AnyPath | None¶
- write_substvars(fileobj: IO[str]) None ¶
Write a copy of the substvars to an open text file
- Parameters:
fileobj – The open file (should open in text mode using the UTF-8 encoding)
- class debian.substvars._Substvars_Base¶
Bases:
AbstractContextManager
[T
],MutableMapping
[str
,str
],ABC