Cfc#:dZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddlmZmZmZmZmZmZmZmZmZ ddlmZeeeefZej:d Zej>d Z Gd d Z!ejDd k\ser GddejFee eefeZ$nGddejJee eZ$Gdde$dZ&y#e$rYwxYw)a 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 :class:`Substvars` class is the key class within this module. Substvars Classes ----------------- N)ABC) OrderedDict)MutableMapping) TracebackType) DictSetOptionalUnionIteratorIOIterable TYPE_CHECKINGType)PathLikeTzB^(?P\w[-:\dA-Za-z]*)(?P[?]?=)(?P.*)$cfeZdZddgZ d dZedZejdZdZdZ dZ y ) Substvar_assignment_operator_valuec ||_||_yN)rassignment_operator)self initial_valuers 2/usr/lib/python3/dist-packages/debian/substvars.py__init__zSubstvar.__init__As$ #6 c|jSr)rrs rrzSubstvar.assignment_operatorRs(((rc6|dvrtd|z||_y)N>?==z-Operator must be one of: "=", or "?=" - got: ) ValueErrorr)r new_operators rrzSubstvar.assignment_operatorWs% { *L|[\ \$0!rc |jdk(r |h|_yt|jtr<|jjdDchc]}|j c}|_|jj |ycc}w)N,)r isinstancestrsplitstripadd)rdependency_clausevs radd_dependencyzSubstvar.add_dependency^sg ;;" ,-DK  dkk3 '.2kk.?.?.DE1779EDK )*FsB ct|jtr$djt |jS|jS)Nz, )r(rsetjoinsortedrs rresolvezSubstvar.resolvehs2 dkk3 '99VDKK01 1{{rc|t|tsy|j|jk7ry|j|jk(SNF)r(rrr4rothers r__eq__zSubstvar.__eq__nsA = 5( ;  # #u'@'@ @||~00rN)r&r") __name__ __module__ __qualname__ __slots__rpropertyrsetterr/r4r9rrrr=sT'2I%'%(7"))1 1 + 1rr) c eZdZy)_Substvars_BaseN)r:r;r<r@rrrDrDxs rrDceZdZdZdZy)rDc|Srr@rs r __enter__z_Substvars_Base.__enter__sKrcyrr@)rexc_typeexc_valexc_tbs r__exit__z_Substvars_Base.__exit__srN)r:r;r<rGrLr@rrrDrDs   rceZdZdZddgZdZeddZedZ e jdZ edZ e jd Z d Z fd Z d Zd efdZdZdZdZdZedZdZdZdZdZdZxZS) SubstvarsaSubstvars 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). _vars_dict_substvars_pathc0t|_d|_yr)rrOrPrs rrzSubstvars.__init__s%-#rc|} t|dd5}|j|ddd||_|S#1swYxYw#t$r*}|jtjk7s|sYd}~Cd}~wwxYw)aShorthand 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 :param substvars_path: The path to load from :param 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. rutf-8encodingN)openread_substvarsOSErrorerrnoENOENTsubstvars_path)clsr\ missing_ok substvarsfdes rload_from_pathzSubstvars.load_from_pathsz6E  ncG< -((, - $2    - - ww%,,&j/9 s*A:AAA A9 A44A9c|jSrrOrs r_varszSubstvars._varssrc||_yrrd)r vars_dicts rrezSubstvars._varss $rc|jSrrPrs rr\zSubstvars.substvars_paths###rc||_yrri)rnew_paths rr\zSubstvars.substvars_paths  (rc |j|}|j|y#t$rt}||j|<Y6wxYw)aAdd 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' N)reKeyErrorrr/)rsubstvarr-variables rr/zSubstvars.add_dependencysL, ,zz(+H  12 ,zH#+DJJx  ,s#"AAcJ||jt| |||Sr)savesuperrL)rrIrJrK __class__s rrLzSubstvars.__exit__s(   IIKw'6::rc,t|jSr)iterrers r__iter__zSubstvars.__iter__sDJJrreturnc,t|jSr)lenrOrs r__len__zSubstvars.__len__s4??##rc||jvSrre)ritems r __contains__zSubstvars.__contains__stzz!!rc<|j|jSr)rer4rkeys r __getitem__zSubstvars.__getitem__szz#&&((rc|j|=yrr|rs r __delitem__zSubstvars.__delitem__ s JJsOrc4t||j|<yr)rre)rrvalues r __setitem__zSubstvars.__setitem__$s"5/ 3rc|jS)aProvides 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 r|rs r as_substvarzSubstvars.as_substvar(s6zzrcZ|t|tsy|j|jk(Sr6)r(rNrer7s rr9zSubstvars.__eq__Es& = 5) <zzU[[((rcbdjd|jjDS)zDebug aid that generates a string representation of the content For persisting the contents, please consider `save()` or `write_substvars`. r&c3xK|]2\}}dj||j|j4ywz{}{}{} Nformatrr4.0kr.s r z!Substvars.dump..Qs71a"((A,A,A199;O8:)r2reitemsrs rdumpzSubstvars.dumpKs/ ww#'::#3#3#5 rc|j tdt|jdd5}|j|cdddS#1swYyxYw)zSave 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. NzcThe substvar does not have a substvars_path: Please set substvars_path first or use write_substvarswrTrU)rP TypeErrorrWwrite_substvars)rr`s rrqzSubstvars.saveUs^    'OP P$&&g > ,"''+ , , ,s A  Acd|jd|jjDy)zWrite a copy of the substvars to an open text file :param fileobj: The open file (should open in text mode using the UTF-8 encoding) c3xK|]2\}}dj||j|j4ywrrrs rrz,Substvars.write_substvars..js7#q!&,,Q0E0Eqyy{SrN) writelinesrer)rfileobjs rrzSubstvars.write_substvarsds, '+zz'7'7'9 rct}|D]i}|jdk(s|ddk(rtj|j d}|sF|j \}}}t ||||<k||_y)a 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' :param fileobj: An open file (in text mode using the UTF-8 encoding) or an iterable of str that provides line by line content. r&r#z )rN)rr+_SUBSTVAR_PATTERNmatchrstripgroupsrre)rrrglinemvarnamerrs rrXzSubstvars.read_substvarsns0 M  ZDzz|r!T!W^!'' F(;rer?r\r/rLrvintrzr~rrrrr9rrqrrX __classcell__)rss@rrNrNs>01I$ ""H  \\$$ $$((3:; $$")*8)  ,!rrN)'r contextlibrZresystypingabcr collectionsrcollections.abcrtypesrrrr r r r r rrosrr)bytesAnyPath ImportErrorTypeVarrcompilerr version_infoAbstractContextManagerrDGenericrNr@rrrs!H #*ZZZ Hc5()GFNN3BJJI 7171tv *;;A>sTWx@XZ] &..+^SG ,Gk  sCC$#C$