)uyczdZddlZddlZddlZddlZddlZ ddlmZmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZedZedZn #e$rdZYnwxYwddlmZmZmZmZmZddlmZm Z  dd lm!Z!n #e$rYnwxYwd Z"e#e"gZ$ej%e&Z'Gd d e(Z)Gd de)Z*Gdde)e+Z,dZ-Gdde.Z/dZ0Gdde.Z1Gdde.Z2dZ3dZ4dZ5dZ6Gddej7ddZ8d Z9Gd!d"e:Z;Gd#d$e;%Z<Gd&d'e<Z=Gd(d)e<Z>Gd*d+e<Z?dS),a9Utilities for parsing and creating machine-readable debian/copyright files. The specification for the format (also known as DEP5) is available here: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Start from the Copyright docstring for usage information. Copyright Classes ----------------- N)AnyCallable FrozenSetIOIterableIteratorListOptionalPatternTextTupleUnion TYPE_CHECKINGcast)FilesParagraphLicenseParagraph)HeaderrrF)parse_deb822_fileDeb822ParagraphElementDeb822FileElement'Deb822NoDuplicateFieldsParagraphElementSyntaxOrParseError)RestrictedFieldRestrictedFieldError)Deb822ValueTypezBhttps://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ceZdZdZdS)Errorz)Base class for exceptions in this module.N__name__ __module__ __qualname____doc__2/usr/lib/python3/dist-packages/debian/copyright.pyrrSs3333r$rceZdZdZdS)NotMachineReadableErrorzFRaised when the input is not a machine-readable debian/copyright file.Nrr#r$r%r'r'WsPPPPr$r'ceZdZdZdS)MachineReadableFormatErrorzRaised when the input is not valid. This is both a `copyright.Error` and a `ValueError` to ease handling of errors coming from this module. Nrr#r$r%r)r)[sr$r)c\|rt|t|dSN)r)loggerwarning)msgstricts r% _complainr0cs0 .(--- NN3r$ceZdZdZdfd ZedZejdZdZd Z d Z d Z d Z d Z dZddZxZS) CopyrightaRepresents a debian/copyright file. A Copyright object contains a Header paragraph and a list of additional Files or License paragraphs. It provides methods to iterate over those paragraphs, in addition to adding new ones. It also provides a mechanism for finding the Files paragraph (if any) that matches a particular filename. Typical usage:: with io.open('debian/copyright', 'rt', encoding='utf-8') as f: c = copyright.Copyright(f) header = c.header # Header exposes standard fields, e.g. print('Upstream name: ', header.upstream_name) lic = header.license if lic: print('Overall license: ', lic.synopsis) # You can also retrieve and set custom fields. header['My-Special-Field'] = 'Very special' # Find the license for a given file. paragraph = c.find_files_paragraph('debian/rules') if paragraph: print('License for debian/rules: ', paragraph.license) # Dump the result, including changes, to another file. with io.open('debian/copyright.new', 'wt', encoding='utf-8') as f: c.dump(f=f) It is possible to build up a Copyright from scratch, by modifying the header and using add_files_paragraph and add_license_paragraph. See the associated method docstrings. Nutf-8Tc$tt|g|_|d} t ||| |_n.#t $r!}tt|d}~wwxYw|jD]}|t|}d|vr-t|||}|j |Ed|vr+t||}|j |ttd||std||_dStj|_t|_|j |jj|j |jdS)a Create a new copyright file in the current format. :param sequence: Sequence of lines, e.g. a list of strings or a file-like object. If not specified, a blank Copyright object is initialized. :param encoding: Encoding to use, in case input is raw byte strings. It is recommended to use unicode objects everywhere instead, e.g. by opening files in text mode. :param strict: Raise if format errors are detected in the data. Raises: :class:`NotMachineReadableError` if 'sequence' does not contain a machine-readable debian/copyright file. MachineReadableFormatError if 'sequence' is not a valid file. N)sequenceencoding#accept_files_with_duplicated_fieldsFiles)r/Licensez=Non-header paragraph has neither "Files" nor "License" fieldszno paragraphs in input)superr2__init___Copyright__paragraphsr_Copyright__filerr'strrrappendrr0_Copyright__headerrnew_empty_file_underlying_paragraph) selfr5r6r/headereppfpl __class__s r%r;zCopyright.__init__s" i'')))  F 6/!)H@FJHHH & 6 6 6-c!ff555 6[ : :>#AYYFF\\'6&AAAB%,,R0000!^^)!V44B%,,R0000128:::: H-.FGGG"DMMM,:<z+Copyright.all_paragraphs..s".L.LQq.L.L.L.L.L.Lr$) itertoolschainrDr<rKs r%all_paragraphszCopyright.all_paragraphss, }.L.L$:K.L.L.LMMMr$c*|S)zXIterate over all paragraphs see all_paragraphs() for more information )rWrKs r%__iter__zCopyright.__iter__s""$$$r$c$d|jDS)z>Returns an iterator over the contained FilesParagraph objects.c3DK|]}t|t|VdSr+)rMrrRs r%rTz1Copyright.all_files_paragraphs..s1NNa 1n0M0MNNNNNNNr$r<rKs r%all_files_paragraphszCopyright.all_files_paragraphssON4,NNNNr$cfd}|D]}||r|}|S)zReturns the FilesParagraph for the given filename. In accordance with the spec, this method returns the last FilesParagraph that matches the filename. If no paragraphs matched, returns None. N)r]matches)rCfilenameresultrFs r%find_files_paragraphzCopyright.find_files_paragraphsC**,,  Ayy""  r$c6t|tstdd}t|jD]\}}t|tr|}|j|dz||j|dz|jdS)zAdds a FilesParagraph to this object. The paragraph is inserted directly after the last FilesParagraph (which might be before a standalone LicenseParagraph). z+paragraph must be a FilesParagraph instanceN)rMrrN enumerater<insertr=rB)rC paragraphlast_iirFs r%add_files_paragraphzCopyright.add_files_paragraphs)^44 KIJJ Jd/00  DAq!^,,    !Y777 6A:y'FGGGGGr$c$d|jDS)z=Returns an iterator over standalone LicenseParagraph objects.c3DK|]}t|t|VdSr+)rMrrRs r%rTz3Copyright.all_license_paragraphs.. s2PPa 1>N0O0OPPPPPPPr$r\rKs r%all_license_paragraphsz Copyright.all_license_paragraphssQP4,PPPPr$ct|tstd|j||j|jdS)zoAdds a LicenceParagraph to this object. The paragraph is inserted after any other paragraphs. z-paragraph must be a LicenseParagraph instanceN)rMrrNr<r?r=rB)rCris r%add_license_paragraphzCopyright.add_license_paragraph s] )%566 MKLL L   +++ 9:;;;;;r$cj|j}|||dS|S)aaDumps the contents of the copyright file. If f is None, returns a unicode object. Otherwise, writes the contents to f, which must be a file-like object that is opened in text mode (i.e. that accepts unicode objects directly). It is thus up to the caller to arrange for the file to do any appropriate encoding. N)r=dumpwrite)rCfss r%rszCopyright.dumps5 K     = GGAJJJ4r$)Nr3Tr+)rr r!r"r;propertyrDsetterrWrYr]rbrlrorqrs __classcell__rIs@r%r2r2js ""H141414141414fX  ]] N N N%%%OOO   HHH"QQQ < < <r$r2c,d|vrtd|S)zNReturns s if it is a single line; otherwise raises MachineReadableFormatError. zmust be single line)r)rvs r% _single_liner~'s! qyy()>??? Hr$c>eZdZdZedZedZdS) _LineBasedz@Namespace for conversion methods for line-based lists as tuples.ctdd|pdDDS)z?Returns the lines in 's', with whitespace stripped, as a tuple.c3K|]}||V dSr+r#rSvs r%rTz&_LineBased.from_str..7s:1Qr$c3>K|]}|VdSr+)strip)rSlines r%rTz&_LineBased.from_str..8s*NNtdjjllNNNNNNr$)tupler splitlinesr}s r%from_strz_LineBased.from_str3sZNNR/@/@/K/K/M/MNNN r$ct|}|sdSd}t|dkr||dSdg}|D]#}|d||z$d|S)zReturns the sequence as a string with each element on its own line. If 'seq' has one element, the result will be on a single line. Otherwise, the first line will be blank. Ncv|}|stdd|vrtd|S)Nvalues must not be emptyr|z values must not contain newlines)rr)r}s r%process_and_validatez/_LineBased.to_str..process_and_validateGsJ A M01KLLLqyy06888Hr$rerr r|)listlenr?join)seqlrtmprvs r%to_strz_LineBased.to_str;s II 4    q66Q;;''!-- -d 6 6A JJs11!444 5 5 5 5yy~~r$N)rr r!r" staticmethodrrr#r$r%rr/sPJJ\\r$rc^eZdZdZejdZedZe dZ dS)_SpaceSeparatedzENamespace for conversion methods for space-separated lists as tuples.z\sc\td|pdDS)z>Returns the values in s as a tuple (empty if only whitespace).c3K|]}||V dSr+r#rs r%rTz+_SpaceSeparated.from_str..ds'771Q7Q777777r$r)rsplitr}s r%rz_SpaceSeparated.from_str`s/77b 1 1777777r$c&t|}|sdSg}|D]e}|j|rtd|}|std||fd|S)zAReturns the sequence as a space-separated string (None if empty).Nz"values must not contain whitespacerr)r _has_spacesearchr)rr?r)clsrrrrvs r%rz_SpaceSeparated.to_strfs II 4  A~$$Q'' :08::: A M01KLLL JJqMMMMxx}}r$N) rr r!r"recompilerrr classmethodrr#r$r%rrZsaOOE""J88\8 [r$rcL|dSt|S)zFormats multiline text for insertion in a Deb822ParagraphElement field. Each line except for the first one is prefixed with a single space. Lines that are blank or only whitespace are replaced with ' .' N)format_multiline_linesrr}s r%format_multiliner{s$ yt !!,,.. 1 11r$cg}t|D];\}}|dkr|sd}d|z}|| ? ??r$c"eZdZdZfdZxZS)_ClassInitMetaaMetaclass for classes that can be initialized at creation time. Implement the method:: @classmethod def _class_init(cls, new_attrs): pass on a class, and apply this metaclass to it. The _class_init method will be called right after the class is created. The 'new_attrs' param is a dict containing the attributes added in the definition of the class. ctt||||||dSr+)r:rr; _class_init)rnamebasesattrsrIs r%r;z_ClassInitMeta.__init__s? nc""++D%??? r$)rr r!r"r;ryrzs@r%rr sB  r$rceZdZdZeZedZedZdfd Z e dZ dZ dZ d Zd Zd Z ddZxZS)_RestrictedWrappera^Base class to wrap a Deb822 object, restricting write access to some keys. The underlying data is hidden internally. Subclasses may keep a reference to the data before giving it to this class's constructor, if necessary, but RestrictedField should cover most use-cases. The dump method from Deb822 is directly proxied. Typical usage:: class Foo(object): def __init__(self, ...): # ... @staticmethod def from_str(self, s): # Parse s... return Foo(...) def to_str(self): # Return in string format. return ... class MyClass(deb822._RestrictedWrapper): def __init__(self): data = Deb822ParagraphElement.new_empty_paragraph() data['Bar'] = 'baz' super(MyClass, self).__init__(data) foo = deb822.RestrictedField( 'Foo', from_str=Foo.from_str, to_str=Foo.to_str) bar = deb822.RestrictedField('Bar', allow_none=False) d = MyClass() d['Bar'] # returns 'baz' d['Bar'] = 'quux' # raises RestrictedFieldError d.bar = 'quux' d.bar # returns 'quux' d['Bar'] # returns 'quux' d.foo = Foo(...) d['Foo'] # returns string representation of foo cg}|D]\\}}t|trB||j|||]t||_dSr+) itemsrMrr?rlower)_RestrictedWrapper__init_restricted_field frozenset%_RestrictedWrapper__restricted_fields)r new_attrsrestricted_fields attr_namevals r%rz_RestrictedWrapper._class_initQs'oo// < r$c hfd}fd}t||t||djdS)Nc~|jj}j|S|Sr+)_RestrictedWrapper__datagetrrrCrfields r%getterz:_RestrictedWrapper.__init_restricted_field..getter\s7+//%*--C~)~~c***Jr$c|j|}|5jrj|jvr|jj=dSdSt d||jj<dS)Nzvalue must not be None)r allow_nonerrrNrs r%rxz:_RestrictedWrapper.__init_restricted_field..settercs~5<#;ll3''{#>zT[00 K 33310$$<===*- EJ'''r$)setattrrwr)rrrrrxs ` r%__init_restricted_fieldz*_RestrictedWrapper.__init_restricted_fieldZs_      . . . . . Yuz J JKKKKKr$Tctt||r>t|ts)t dt |jjz||_ dS)zEInitializes the wrapper over 'data', a Deb822ParagraphElement object.z!Paragraph has duplicated fields: N) r:rr;rMr ValueErrorr>rIr!rrCdata_internal_validaterIs r%r;z_RestrictedWrapper.__init__rsj  $''00222  ej7^&_&_ e@3t~GbCcCccdd d r$c|jSr+rrKs r%rBz(_RestrictedWrapper._underlying_paragraphzs {r$c|j|Sr+rrCkeys r% __getitem__z_RestrictedWrapper.__getitem__s{3r$ct||jvrtd|z||j|<dSNz<%s may not be modified directly; use the associated propertyrrrr)rCrvalues r% __setitem__z_RestrictedWrapper.__setitem__sK 99;;$2 2 2&!"## #! Cr$cp||jvrtd|z|j|=dSrrrs r% __delitem__z_RestrictedWrapper.__delitem__sI 99;;$2 2 2&!"## # K   r$c$d|jDS)Nc34K|]}t|VdSr+)r>)rSks r%rTz._RestrictedWrapper.__iter__..s(,,1A,,,,,,r$rrKs r%rYz_RestrictedWrapper.__iter__s,, ,,,,r$c*t|jSr+)rrrKs r%__len__z_RestrictedWrapper.__len__s4;r$NFc||,|s*|jtd|dS|j}|7td|||n%|r#td||dS|jS)zcCalls dump() on the underlying data object. See Deb822.dump for more information. Nz IO[bytes]zIO[str])rrsrrtencode)rCfdr6 text_modeas_strs r%rsz_RestrictedWrapper.dumps >    k2!6!6777t[%%''F#["%%++FMM(,C,CDDDD 2Y##))&1114{!!!r$T)NNF)rr r!r"rrrrrr;rwrBrrrrYrrsryrzs@r%rr"s**X$)++??[?LL[L.X   !!!---   """"""""r$r) metaclassceZdZdZejdZdfd ZedZ dZ dZ e de je jd Ze d d Ze d ejejd Ze dZxZS)rzRepresents a Files paragraph of a debian/copyright file. This kind of paragraph is used to specify the copyright and license for a particular set of files in the package. rTctt||||rRd|vrtdd|vrt d|d|vrt d||jst d|d|jf|_dS) Nr8z"Files" field requiredr2z'Files paragraph missing Copyright fieldr9z%Files paragraph missing License fieldz%Files paragraph has empty Files fieldr)r:rr;r)r0files _default_re!_FilesParagraph__cached_files_pat)rCrrr/rIs r%r;zFilesParagraph.__init__s nd##,,T3EFFF  Kd""01IJJJ$&&CVLLL$$A6JJJ: KA6JJJ#%t'7"8r$cl|tjd}||_||_||_|S)zCreate a new FilesParagraph from its required parts. :param files: The list of file globs. :param copyright: The copyright for the files (free-form text). :param license: The Licence for the files. Fr)rnew_empty_paragraphr copyrightlicense)rrrrrFs r%createzFilesParagraph.creates= C&:<>>IoG,W^Goi((GGGGGr$rceZdZdZd fd ZedZedej ej dZ edZ ed Z xZS) ra Represents a standalone license paragraph of a debian/copyright file. Minimally, this kind of paragraph requires a 'License' field and has no 'Files' field. It is used to give a short name to a license text, which can be referred to from the header or files paragraphs. Tctt||||r&d|vrtdd|vrtddSdS)Nr9z"License" field requiredr8z%input appears to be a Files paragraph)r:rr;r)rs r%r;zLicenseParagraph.__init__ sv %%..t5GHHH  =$$01KLLL$0;===  = =r$ct|tstd|tjd}||_|S)z2Returns a LicenseParagraph with the given license.z"license must be a License instanceFr )rMr9rNrr r)rrris r%rzLicenseParagraph.createsS '7++ B@AA AC.BDDY^___ # r$r9Frrr8r)rr r!r"r;rrrr9rrrr_LicenseParagraph__filesryrzs@r%rrs======[oG,W^Goi((Gog&&GGGGGr$rc~eZdZdZdfd ZdZdZededZ ed e Z ed e j e j Zed ZedZedZedej ej ZedZede j e j Zede j e j ZxZS)rzRepresents the header paragraph of a debian/copyright file. Property values are all immutable, such that in order to modify them you must explicitly set them (rather than modifying a returned reference). Ncl|tj}t|d<d|vr(td|d|d<|d=t t ||t}|j }|tkrh|f| ds|dz }| dr d|ddz}|tvr!td ||_ |td |tvrtd |dSdS) zInitializer. :param data: A Deb822ParagraphElement object for underlying data. If None, a new one will be created. NFormatzFormat-SpecificationzEuse of deprecated "Format-Specification" field; rewriting as "Format"/zhttp:zhttps:%szFixing Format URLz0input is not a machine-readable debian/copyrightzformat not known: %r)rr _CURRENT_FORMATr,r-r:rr;r>formatendswithr_KNOWN_FORMATSr')rCrfmtrIs r%r;zHeader.__init__5sQ <)=??D,DN !T ) ) NN4 5 5 5!"89DN+, fd$$T***eek / ! !co<<$$ s ~~g&& + 3qrr7*n$$2333! ;)BDD D n $ $ NN13 7 7 7 7 7 % $r$c|jtvS)z%Returns True iff the format is known.)r$r&rKs r% known_formatzHeader.known_format]s{n,,r$c"|jtkS)z2Returns True iff the format is the current format.)r$r#rKs r%current_formatzHeader.current_formatbs{o--r$r F)rrz Upstream-Name)rzUpstream-Contact)rrSource Disclaimerrr9r2zFiles-ExcludedzFiles-Includedr+)rr r!r"r;r)r+rr~r$ upstream_namerrrupstream_contactsource disclaimerrr9rrfiles_excludedfiles_includedryrzs@r%rr.sl &8&8&8&8&8&8P--- ... _%999F$O ...M'Z%8 """_X & &F ..Joi((GoG,W^EEEG  ,,I$_:#6 """N%_:#6 """NNNNNr$r)@r" collectionsrUloggingrrtypingrrrrrrr r r r r rrrParagraphTypesAllParagraphTypes ImportErrordebian._deb822_reprorrrrr debian.deb822rrrr#rr& getLoggerrr, Exceptionrr'rr)r0objectr2r~rrrrrr namedtupler9rtyperrrrrr#r$r%rAs  6  ?@NLMMMM @??????? -------   D I  8 $ $44444I444QQQQQeQQQ zzzzzzzzz   ((((((((VfB 2 2 2     2 2 2 *PPPPP$k$Y@@PPPF+@+@+@\T0N"N"N"N"N">N"N"N"N"bN)N)N)N)N)'N)N)N)b'''''''''')''''''TU"U"U"U"U" U"U"U"U"U"s#0A AA-A44A<;A<