o
    bI                     @   s$  d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ g dZg d	Zd
d ZG dd de
ZeeG dd dZdd ZG dd dZdd Zdd ZG dd deZG dd dZG dd dZdd ZG d d! d!eZG d"d# d#ZG d$d% d%Z dS )&aL  
Twisted's automated release system.

This module is only for use within Twisted's release system. If you are anyone
else, do not use it. The interface and behaviour will change without notice.

Only Linux is supported by this code.  It should not be used by any tools
which must run on multiple platforms (eg the setup.py script).
    N)STDOUTCalledProcessErrorcheck_output)Dict)	Interfaceimplementer)execfile)FilePath)docbugfixmiscfeatureremoval)
z%https://docs.python.org/3/objects.invz-https://cryptography.io/en/latest/objects.invz6https://pyopenssl.readthedocs.io/en/stable/objects.invz6https://hyperlink.readthedocs.io/en/stable/objects.invz/https://twisted.org/constantly/docs/objects.invz0https://twisted.org/incremental/docs/objects.invz@https://python-hyper.org/projects/hyper-h2/en/stable/objects.invz5https://priority.readthedocs.io/en/stable/objects.invz:https://zopeinterface.readthedocs.io/en/latest/objects.invz4https://automat.readthedocs.io/en/latest/objects.invc                 K   s   t |d< t| fi |S )a  Execute a vector of arguments.

    This is a wrapper around L{subprocess.check_output}, so it takes
    the same arguments as L{subprocess.Popen} with one difference: all
    arguments after the vector must be keyword arguments.

    @param args: arguments passed to L{subprocess.check_output}
    @param kwargs: keyword arguments passed to L{subprocess.check_output}
    @return: command output
    @rtype: L{bytes}
    stderr)r   r   )argskwargs r   9/usr/lib/python3/dist-packages/twisted/python/_release.py
runCommand)   s   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )IVCSCommandz(
    An interface for VCS commands.
    c                 C      dS )z
        Ensure that C{path} is a working directory of this VCS.

        @type path: L{twisted.python.filepath.FilePath}
        @param path: The path to check.
        Nr   pathr   r   r   ensureIsWorkingDirectory>       z$IVCSCommand.ensureIsWorkingDirectoryc                 C   r   )
        Return the Git status of the files in the specified path.

        @type path: L{twisted.python.filepath.FilePath}
        @param path: The path to get the status from (can be a directory or a
            file.)
        Nr   r   r   r   r   isStatusCleanF   r   zIVCSCommand.isStatusCleanc                 C   r   )z
        Remove the specified path from a the VCS.

        @type path: L{twisted.python.filepath.FilePath}
        @param path: The path to remove from the repository.
        Nr   r   r   r   r   removeO   r   zIVCSCommand.removec                 C   r   )a  
        Export the content of the VCSrepository to the specified directory.

        @type fromDir: L{twisted.python.filepath.FilePath}
        @param fromDir: The path to the VCS repository to export.

        @type exportDir: L{twisted.python.filepath.FilePath}
        @param exportDir: The directory to export the content of the
            repository to. This directory doesn't have to exist prior to
            exporting the repository.
        Nr   fromDir	exportDirr   r   r   exportToW   r   zIVCSCommand.exportToN)__name__
__module____qualname____doc__r   r   r   r!   r   r   r   r   r   9   s    	r   c                   @   s@   e Zd ZdZedd Zedd Zedd Zedd	 Zd
S )
GitCommandzJ
    Subset of Git commands to release Twisted from a Git repository.
    c              	   C   s<   zt ddg| jd W dS  ttfy   t| j dw )z
        Ensure that C{path} is a Git working directory.

        @type path: L{twisted.python.filepath.FilePath}
        @param path: The path to check.
        gitz	rev-parsecwdz( does not appear to be a Git repository.N)r   r   r   OSErrorNotWorkingDirectoryr   r   r   r   r   k   s   
z#GitCommand.ensureIsWorkingDirectoryc                 C   s    t dd| jddg }|dkS )r   r'   -Cstatusz--short    )r   r   strip)r   r-   r   r   r   r   z   s   	zGitCommand.isStatusCleanc                 C   s   t dd|  d| jg dS )z
        Remove the specified path from a Git repository.

        @type path: L{twisted.python.filepath.FilePath}
        @param path: The path to remove from the repository.
        r'   r,   rmN)r   dirnamer   r   r   r   r   r      s   zGitCommand.removec              
   C   s$   t dd| jdddd|jd g dS )	a  
        Export the content of a Git repository to the specified directory.

        @type fromDir: L{twisted.python.filepath.FilePath}
        @param fromDir: The path to the Git repository to export.

        @type exportDir: L{twisted.python.filepath.FilePath}
        @param exportDir: The directory to export the content of the
            repository to. This directory doesn't have to exist prior to
            exporting the repository.
        r'   r,   zcheckout-indexz--allz--forcez--prefix/N)r   r   r   r   r   r   r!      s   zGitCommand.exportToN)	r"   r#   r$   r%   staticmethodr   r   r   r!   r   r   r   r   r&   e   s    


	r&   c              	   C   s8   zt |  t W S  ttfy   Y nw td| j )a  
    Detect the VCS used in the specified directory and return a L{GitCommand}
    if the directory is a Git repository. If the directory is not git, it
    raises a L{NotWorkingDirectory} exception.

    @type directory: L{FilePath}
    @param directory: The directory to detect the VCS used from.

    @rtype: L{GitCommand}

    @raise NotWorkingDirectory: if no supported VCS can be found from the
        specified directory.
    z!No supported VCS can be found in )r&   r   r+   r*   r   	directoryr   r   r   getRepositoryCommand   s   
r6   c                   @   s.   e Zd ZdZdd ZdefddZdd Zd	S )
ProjectaC  
    A representation of a project that has a version.

    @ivar directory: A L{twisted.python.filepath.FilePath} pointing to the base
        directory of a Twisted-style Python package. The package should contain
        a C{_version.py} file and a C{newsfragments} directory that contains a
        C{README} file.
    c                 C   
   || _ d S Nr4   )selfr5   r   r   r   __init__      
zProject.__init__returnc                 C   s   | j j d| jdS )N())	__class__r"   r5   )r:   r   r   r   __repr__   s   zProject.__repr__c                 C   sT   i }| j }|s&|jdkrtd| dks| }n	t|dj| |r|d S )z
        @return: A L{incremental.Version} specifying the version number of the
            project based on live python modules.
        r2   zNot inside a Twisted project.twistedz_version.py__version__)r5   r   	Exceptionbasenameparentr   child)r:   	namespacer5   r   r   r   
getVersion   s   

zProject.getVersionN)r"   r#   r$   r%   r;   strrA   rI   r   r   r   r   r7      s
    	r7   c                 C   s8   g }|   D ]}| dkr| }|t| q|S )z
    Find all Twisted-style projects beneath a base directory.

    @param baseDirectory: A L{twisted.python.filepath.FilePath} to look inside.
    @return: A list of L{Project}.
    newsfragments)walkrE   rF   appendr7   )baseDirectoryprojectsfilePathprojectDirectoryr   r   r   findTwistedProjects   s   rR   c                 C   s   t | | d  t| d }| }W d   n1 sw   Y  | D ]
\}}|||}q&t| d d}|| W d   n1 sHw   Y  t | d |  t | d  dS )zP
    I replace the text `oldstr' with `newstr' in `filename' using science.
    z.bakNz.neww)osrenameopenreaditemsreplacewriteunlink)filenameoldToNewfdkvr   r   r   replaceInFile   s   
rb   c                   @      e Zd ZdZdS )NoDocumentsFoundz3
    Raised when no input documents are found.
    Nr"   r#   r$   r%   r   r   r   r   rd         rd   c                   @   s   e Zd ZdZdd ZdS )
APIBuilderz
    Generate API documentation from source files using
    U{pydoctor<https://github.com/twisted/pydoctor>}.  This requires
    pydoctor to be installed and usable.
    c                 C   s   g }t D ]}|d || qddlm} tt d}	d|d|ddd	| jd
|	jd|d|jdddg| }
|
|j ||
 dS )ah  
        Call pydoctor's entry point with options which will generate HTML
        documentation for the specified package's API.

        @type projectName: C{str}
        @param projectName: The name of the package for which to generate
            documentation.

        @type projectURL: C{str}
        @param projectURL: The location (probably an HTTP URL) of the project
            on the web.

        @type sourceURL: C{str}
        @param sourceURL: The location (probably an HTTP URL) of the root of
            the source browser for the project.

        @type packagePath: L{FilePath}
        @param packagePath: The path to the top-level of the package named by
            C{projectName}.

        @type outputPath: L{FilePath}
        @param outputPath: An existing directory to which the generated API
            documentation will be written.
        z--intersphinxr   )main_pydoctortemplatesz--project-namez--project-urlz--system-classz&twisted.python._pydoctor.TwistedSystemz--project-base-dirz--template-dirz--html-viewsource-basez--html-outputz--quietz--make-htmlz--warnings-as-errorsN)	intersphinxURLsrM   pydoctor.driverrh   r	   __file__rF   rG   r   )r:   projectName
projectURL	sourceURLpackagePath
outputPathintersphinxesintersphinxrh   templatesPathr   r   r   r   build  s8   
zAPIBuilder.buildN)r"   r#   r$   r%   ru   r   r   r   r   rg   
  s    rg   c                   @   s"   e Zd ZdZdd ZdddZdS )	SphinxBuildera  
    Generate HTML documentation using Sphinx.

    Generates and runs a shell command that looks something like::

        sphinx-build -b html -d [BUILDDIR]/doctrees
                                [DOCDIR]/source
                                [BUILDDIR]/html

    where DOCDIR is a directory containing another directory called "source"
    which contains the Sphinx source files, and BUILDDIR is the directory in
    which the Sphinx output will be created.
    c                 C   s>   |  t|d d}|rtjd| d tddS )a]  
        Build the main documentation.

        @type args: list of str
        @param args: The command line arguments to process.  This must contain
            one string argument: the path to the root of a Twisted checkout.
            Additional arguments will be ignored for compatibility with legacy
            build infrastructure.
        r   docszUnclean build:

   N)ru   r	   rG   sysstdoutrZ   exit)r:   r   outputr   r   r   rh   Z  s
   

zSphinxBuilder.mainN c           	   	   C   s   |du r|  d}|d}tddddd|j|j|jgd	}|  | D ],}| d
krV||}|}|rE||	d}|s;|  
 sQ|    || q*|S )a  
        Build the documentation in C{docDir} with Sphinx.

        @param docDir: The directory of the documentation.  This is a directory
            which contains another directory called "source" which contains the
            Sphinx "conf.py" file and sphinx source documents.
        @type docDir: L{twisted.python.filepath.FilePath}

        @param buildDir: The directory to build the documentation in.  By
            default this will be a child directory of {docDir} named "build".
        @type buildDir: L{twisted.python.filepath.FilePath}

        @param version: The version of Twisted to set in the docs.
        @type version: C{str}

        @return: the output produced by running the command
        @rtype: L{str}
        Nr
   doctreeszsphinx-buildz-qz-bhtmlz-dzutf-8manr   )rF   rG   r   r   decoder   rL   rE   segmentsFrompopisdirmakedirscopyTo)	r:   docDirbuildDirversion
doctreeDirr}   r   segmentsdestr   r   r   ru   i  s:   


zSphinxBuilder.build)Nr~   )r"   r#   r$   r%   rh   ru   r   r   r   r   rv   K  s    rv   c                 C   sh   d}| j tj}|j tj}t||D ]\}}||kr"|d7 }q dgt||  }|||d  S )a4  
    Return a list of strings that represent C{destination} as a path relative
    to C{origin}.

    It is assumed that both paths represent directories, not files. That is to
    say, the delta of L{twisted.python.filepath.FilePath} /foo/bar to
    L{twisted.python.filepath.FilePath} /foo/baz will be C{../baz},
    not C{baz}.

    @type origin: L{twisted.python.filepath.FilePath}
    @param origin: The origin of the relative path.

    @type destination: L{twisted.python.filepath.FilePath}
    @param destination: The destination of the relative path.
    r   ry   z..N)r   splitrT   sepziplen)origindestinationcommonItemspath1path2elem1elem2r   r   r   r   filePathDelta  s   
r   c                   @   rc   )r+   zf
    Raised when a directory does not appear to be a repository directory of a
    supported VCS.
    Nre   r   r   r   r   r+     rf   r+   c                   @       e Zd ZdZdd Zdd ZdS )BuildAPIDocsScriptz>
    A thing for building API documentation. See L{main}.
    c                 C   sJ   t |d }| }d|f d }t }|dd||d| dS )a  
        Build the API documentation of Twisted, with our project policy.

        @param projectRoot: A L{FilePath} representing the root of the Twisted
            checkout.
        @param output: A L{FilePath} pointing to the desired output directory.
        rB   z2https://github.com/twisted/twisted/tree/twisted-%sz/srcTwistedzhttps://twistedmatrix.com/N)r7   rG   rI   baserg   ru   )r:   projectRootr}   r   versionStringro   
apiBuilderr   r   r   buildAPIDocs  s    zBuildAPIDocsScript.buildAPIDocsc                 C   s6   t |dkrtd | t|d t|d  dS )a  
        Build API documentation.

        @type args: list of str
        @param args: The command line arguments to process.  This must contain
            two strings: the path to the root of the Twisted checkout, and a
            path to an output directory.
           zAMust specify two arguments: Twisted checkout and destination pathr   ry   N)r   rz   r|   r   r	   )r:   r   r   r   r   rh     s
   	 zBuildAPIDocsScript.mainN)r"   r#   r$   r%   r   rh   r   r   r   r   r     s    r   c                   @   r   )CheckNewsfragmentScriptzE
    A thing for checking whether a checkout has a newsfragment.
    c                 C   r8   r9   )_print)r:   r   r   r   r   r;     r<   z CheckNewsfragmentScript.__init__c           	      C   s  t |dkrtd tjjpd}tj|d }tg d|d	|
 }tg d|d	|
 }|s?| d td |
 tj}| d	 |D ]}| | qN| d
 t |dkrw|d tjg dkrw| d td g }|D ]}tjd tj |v rd|v r|ddd tv r|| q{|dr|r| d td n
| d td tjdddkr|r| d td n
| d td |D ]}| d|  td q| d td dS )z
        Run the script.

        @type args: L{list} of L{str}
        @param args: The command line arguments to process. This must contain
            one string: the path to the root of the Twisted checkout.
        ry   z/Must specify one argument: the Twisted checkoutasciir   )   gits	   rev-parses   --abbrev-refHEADr(   )r   s   diffs   --name-onlys   origin/trunk...s   --diff-filter=dz9On trunk or no diffs from trunk; no need to look at this.zLooking at these files:z----)rw   funzTwisted.Quotesz+Quotes change only; no newsfragment needed.rK   .zrelease-z1No newsfragments should be on the release branch.z/Release branch with no newsfragments, all good.GITHUB_HEAD_REFr~   zpre-commit-ci-update-configz<No newsfragments should be present on an autoupdated branch.z3Autoupdated branch with no newsfragments, all good.zFound z-No newsfragment found. Have you committed it?N)r   rz   r|   r{   encodingrT   r   abspathr   r   r/   r   r   linesepr   joinrsplitNEWSFRAGMENT_TYPESrM   
startswithenvironget)	r:   r   r   locationbranchrfileschangerK   r   r   r   rh     sh   
	














zCheckNewsfragmentScript.mainN)r"   r#   r$   r%   r;   rh   r   r   r   r   r     s    r   )!r%   rT   rz   
subprocessr   r   r   typingr   zope.interfacer   r   twisted.python.compatr   twisted.python.filepathr	   r   rj   r   r   r&   r6   r7   rR   rb   rD   rd   rg   rv   r   r+   r   r   r   r   r   r   <module>   s2   
,G!AR,