o a`@sdZddlZddlZddlZddlZddlZz&ddlmZmZm Z m Z m Z m Z m Z mZmZmZmZmZedZedZWn eyGdZYnwddlmZdZeegZeeZGd d d eZGd d d eZ Gd ddee!Z"ddZ#Gddde$Z%ddZ&Gddde$Z'Gddde$Z(ddZ)ddZ*ddZ+dd Z,Gd!d"d"e-d"d#Z.d$d%Z/Gd&d'd'ej0Z1Gd(d)d)ej0Z2Gd*d+d+ej0Z3dS),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) AnyCallableIOIterableIteratorListOptionalPatternTextTupleUnion TYPE_CHECKING)FilesParagraphLicenseParagraph)HeaderrrF)deb822zBhttps://www.debian.org/doc/packaging-manuals/copyright-format/1.0/c@eZdZdZdS)Errorz)Base class for exceptions in this module.N__name__ __module__ __qualname____doc__rr2/usr/lib/python3/dist-packages/debian/copyright.pyrGrc@r)NotMachineReadableErrorzFRaised when the input is not a machine-readable debian/copyright file.NrrrrrrKrrc@r)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. NrrrrrrOrrcCs|rt|t|dSN)rloggerwarning)msgstrictrrr _complainWsr#cs~eZdZdZdfdd ZeddZejd dZd d Zd d Z ddZ ddZ ddZ ddZ ddZdddZZS) 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 stt|g|_|durZttjj||d}|stdt |d|_ t dt |D]+}||}d|vrBt ||}|j|q,d|vrRt||}|j|q,td|q,dSt |_ dS) 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)sequenceencodingzno paragraphs in inputrFilesLicensez=Non-header paragraph has neither "Files" nor "License" fields)superr$__init___Copyright__paragraphslistrDeb822iter_paragraphsrr_Copyright__headerrangelenrappendrr#) selfr&r'r" paragraphsippfpl __class__rrr,s,   zCopyright.__init__cCs|jS)zThe file header paragraph.)r1r5rrrheaderszCopyright.headercCst|ts td||_dS)Nzvalue must be a Header object) isinstancer TypeErrorr1)r5hdrrrrr>s  cCst|jgdd|jDS)aReturns an iterator over all paragraphs (header, Files, License). The header (returned first) will be returned as a Header object; file paragraphs as FilesParagraph objects; license paragraphs as LicenseParagraph objects. css|]}|VqdSrr.0r8rrr sz+Copyright.all_paragraphs..) itertoolschainr>r-r=rrrall_paragraphss zCopyright.all_paragraphscCs|S)zXIterate over all paragraphs see all_paragraphs() for more information )rGr=rrr__iter__szCopyright.__iter__cCdd|jDS)z>Returns an iterator over the contained FilesParagraph objects.cs|] }t|tr|VqdSr)r?rrBrrrrDz1Copyright.all_files_paragraphs..r-r=rrrall_files_paragraphszCopyright.all_files_paragraphscCs$d}|D] }||r|}q|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)rMmatches)r5filenameresultr8rrrfind_files_paragraphs   zCopyright.find_files_paragraphcCsNt|ts tdd}t|jD] \}}t|tr|}q|j|d|dS)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 instancer(N)r?rr@ enumerater-insert)r5 paragraphlast_ir7r8rrradd_files_paragraphs  zCopyright.add_files_paragraphcCrI)z=Returns an iterator over standalone LicenseParagraph objects.csrJr)r?rrBrrrrDrKz3Copyright.all_license_paragraphs..rLr=rrrall_license_paragraphsrNz Copyright.all_license_paragraphscCs"t|ts td|j|dS)zoAdds a LicenceParagraph to this object. The paragraph is inserted after any other paragraphs. z-paragraph must be a LicenseParagraph instanceN)r?rr@r-r4)r5rVrrradd_license_paragraphs zCopyright.add_license_paragraphcCs\d}|dur d}t}|jj|dd|jD]}|d|j|ddq|r,|SdS)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. FNT) text_mode )ioStringIOr>dumpr-writegetvalue)r5f return_stringr8rrrr_s   zCopyright.dump)Nr%Tr)rrrrr,propertyr>setterrGrHrMrRrXrYrZr_ __classcell__rrr;rr$^s$*      r$cCsd|vrtd|S)zNReturns s if it is a single line; otherwise raises MachineReadableFormatError.r\zmust be single line)rsrrr _single_linesric@s(eZdZdZeddZeddZdS) _LineBasedz@Namespace for conversion methods for line-based lists as tuples.cCs(tdddd|p dDDS)z?Returns the lines in 's', with whitespace stripped, as a tuple.cs|]}|r|VqdSrrrCvrrrrD$sz&_LineBased.from_str..css|]}|VqdSr)strip)rClinerrrrD%)tuplern splitlinesrgrrrfrom_str sz_LineBased.from_strcCs\t|}|sdSdd}t|dkr||dSdg}|D] }|d||qd|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. NcSs(|}|s tdd|vrtd|S)Nvalues must not be emptyr\z values must not contain newlines)rnrrgrrrprocess_and_validate4sz/_LineBased.to_str..process_and_validater(rrq r\)r.r3r4join)seqlrvtmprhrrrto_str(s  z_LineBased.to_strN)rrrr staticmethodrtr|rrrrrjs  rjc@s2eZdZdZedZeddZe ddZ dS)_SpaceSeparatedzENamespace for conversion methods for space-separated lists as tuples.z\scCstdd|pdDS)z>Returns the values in s as a tuple (empty if only whitespace).csrkrrrlrrrrDQrpz+_SpaceSeparated.from_str..rq)rrsplitrgrrrrtMsz_SpaceSeparated.from_strcCsZt|}|sdSg}|D]}|j|rtd|}|s"td||q d|S)zAReturns the sequence as a space-separated string (None if empty).Nz"values must not contain whitespacerurw)r. _has_spacesearchrrnr4rx)clsryrzr{rhrrrr|Ss   z_SpaceSeparated.to_strN) rrrrrecompilerr}rt classmethodr|rrrrr~Gs  r~cCs|durdSt|S)zFormats multiline text for insertion in a Deb822 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_linesrsrgrrrformat_multilinehs rcCsFg}t|D]\}}|dkr|sd}d|}||qd|S)z?Same as format_multline, but taking input pre-split into lines.r.rwr\)rTrnr4rx)lines out_linesr7rorrrrts  rcCs|durdSdt|S)agInverse of format_multiline. Technically it can't be a perfect inverse, since format_multline must replace all-whitespace lines with ' .'. Specifically, this function: - Does nothing to the first line - Removes first character (which must be ' ') from each proceeding line. - Replaces any line that is '.' with an empty line. Nr\)rxparse_multiline_as_linesrgrrrparse_multilines rcCs\|}t|D]#\}}|dkrq|dr|dd}ntd|dkr'd}|||<q|S)zpSame as parse_multiline, but returns a list of lines. (This is the inverse of format_multiline_lines.) rrwr(Nz"continued line must begin with " "rrq)rsrT startswithr)rhrr7rorrrrs  rcs6eZdZdZd fdd ZeddZddZZS) r*z7Represents the contents of a License field. Immutable.rqcstt|j|t||p ddS)aCreates a new License object. :param synopsis: The short name of the license, or an expression giving alternatives. (The first line of a License field.) :param text: The full text of the license, if any (may be None). The lines should not be mangled for "deb822"-style wrapping - i.e. they should not have whitespace prefixes or single '.' for empty lines. rq)synopsistext)r+r*__new__ri)rrrr;rrrs zLicense.__new__c Cs@|durdSt|}|s|dS||ddt|dddS)Nrqrr\r()r)rrxrEislice)rrhrrrrrts  zLicense.from_strcCst|jg|jSr)rrrrsr=rrrr|szLicense.to_str)rq) rrrrrrrtr|rfrrr;rr*s   r*z synopsis textcCst}t|D]g\}}|dkr|dd}t|}||kro||}|d7}|dkr1|dn:|dkr;|dn0|dkrc||krL||}|d7}ntd |d vr]|t|ntd ||t|||ksq|d t| tj tj BS) a^Returns an re object for the given globs. Only * and ? wildcards are supported. Literal * and ? may be matched via \* and \?, respectively. A literal backslash is matched \\. Any other character after a backslash is forbidden. Empty globs match nothing. Raises MachineReadableFormatError if any of the globs is illegal. r|r(*z.*?r\z#single backslash not allowed at endz\?*zinvalid escape sequence: \%sz\Z) r]r^rTr`r3rrescaperra MULTILINEDOTALL)globsbufr7globncrrr globs_to_res<      rcseZdZdZedZdfdd ZeddZ dd Z d d Z e j d ejejd dZe j dd dZe j dejejd dZe dZZS)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. rqTcshtt|||r,d|vrtdd|vrtd|d|vr$td||js,td|d|jf|_dS) Nr)z"Files" field requiredr$z'Files paragraph missing Copyright fieldr*z%Files paragraph missing License fieldz%Files paragraph has empty Files fieldrq)r+rr,rr#files _default_re!_FilesParagraph__cached_files_pat)r5data_internal_validater"r;rrr,s   zFilesParagraph.__init__cCs&|tdd}||_||_||_|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)rr/r copyrightlicense)rrrrr8rrrcreates zFilesParagraph.createcCs0|d}|jd|kr|t|jf|_|jdS)zReturns a regular expression equivalent to the Files globs. Caches the result until files is set to a different value. Raises ValueError if any of the globs are invalid. rrr()rrr)r5 files_strrrr files_pattern&s zFilesParagraph.files_patterncCs"|}|dur dS||duS)z8Returns True iff filename is matched by a glob in Files.NF)rmatch)r5rPpatrrrrO3szFilesParagraph.matchesr)Frtr| allow_noner$)rr*Comment)TT)rrrrrrrr,rrrrOrRestrictedFieldr~rtr|rrr*rcommentrfrrr;rrs$    rcsXeZdZdZd fdd ZeddZejde j e j dd Z ed Z ed ZZS) 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. Tcs<tt|||rd|vrtdd|vrtddSdS)Nr*z"License" field requiredr)z%input appears to be a Files paragraph)r+rr,r)r5rrr;rrr,PszLicenseParagraph.__init__cCs,t|ts td|tdd}||_|S)z2Returns a LicenseParagraph with the given license.z"license must be a License instanceFr)r?r*r@rr/r)rrrVrrrrZs zLicenseParagraph.creater*Frrr))T)rrrrr,rrrrr*rtr|rr_LicenseParagraph__filesrfrrr;rrHs   rcseZdZdZdfdd ZddZddZejd e d d Z ejd e d Z ejde j e jdZedZedZedZejdej ejdZedZejde j e jdZejde j e jdZZS)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). Ncs|dur t}t|d<d|vrtd|d|d<|d=tt||t}|j }|tkrV|durV| ds=|d7}| drJd|dd}|t vrVtd ||_ |dur^t d |t vrjtd |dSdS) zInitializer. :param data: A deb822.Deb822 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_FORMATrr r+rr,strformatendswithr_KNOWN_FORMATSr)r5rfmtr;rrr,ys2     zHeader.__init__cCs |jtvS)z%Returns True iff the format is known.)rrr=rrr known_format zHeader.known_formatcCs |jtkS)z2Returns True iff the format is the current format.)rrr=rrrcurrent_formatrzHeader.current_formatrF)r|rz Upstream-Name)r|zUpstream-Contact)rtr|Source Disclaimerrr*r$zFiles-ExcludedzFiles-Includedr)rrrrr,rrrrrir upstream_namerjrtr|upstream_contactsource disclaimerrr*rrfiles_excludedfiles_includedrfrrr;rrrs<(     r)4r collectionsrEloggingr]rtypingrrrrrrrr r r r r ParagraphTypesAllParagraphTypes ImportErrordebianrr frozensetr getLoggerrr Exceptionrr ValueErrorrr#objectr$rirjr~rrrr namedtupler*rRestrictedWrapperrrrrrrrsL8    7+!  #.Q*