HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/test/__pycache__/test_plugin.cpython-38.pyc
U


W[f�@s2dZddlmZmZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
ddlmZmZddlmZmZmZddlmZdd	lmZer�dd
lmZndd�ZGd
d�de�ZGdd�de�ZGdd�de
j�ZdZ dd�Z!dd�Z"Gdd�de
j�Z#Gdd�de
j�Z$Gdd�de
j�Z%dS)z"
Tests for Twisted plugin system.
�)�absolute_import�divisionN)�	Interface)�unittest)�_PY3�_PYPY)�textFromEventDict�addObserver�removeObserver)�FilePath)�plugin)�invalidate_cachescCsdS)zJ
        On python 2, import caches don't need to be invalidated.
        N�rrr�:/usr/lib/python3/dist-packages/twisted/test/test_plugin.py�invalidateImportCachessrc@seZdZdZdS)�ITestPluginzS
    A plugin for use by the plugin system's unit tests.

    Do not use this.
    N��__name__�
__module__�__qualname__�__doc__rrrrr"src@seZdZdZdS)�ITestPlugin2z
    See L{ITestPlugin}.
    Nrrrrrr+src@s�eZdZdZdd�Zdd�Zd dd�Zd	d
�Zdd�Zd
d�Z	ee	�Z	dd�Z
dd�Zee�Zdd�Zee�Zdd�Z
ee
�Z
dd�Zee�Zdd�Zee�Zdd�Zee�Zdd�ZdS)!�PluginTestsz_
    Tests which verify the behavior of the current, active Twisted plugins
    directory.
    cCs�tjdd�|_tj��|_t|���|_|j�	�|j�
d�|_|j�	�|j�
d��d�tt
��d��|j�
d��d|_tj�d|jj�ddl}||_dS)	zV
        Save C{sys.path} and C{sys.modules}, and create a package for tests.
        N�	mypackage�__init__.py�zplugin_basic.pyz
testplugin.py�
testpluginr)�sys�path�originalPath�modules�copy�savedModulesr�mktemp�root�createDirectory�child�package�
setContent�__file__�sibling�copyTo�originalPlugin�insertr�module)�selfrrrr�setUp8s


�zPluginTests.setUpcCs,|jtjdd�<tj��tj�|j�dS�zR
        Restore C{sys.path} and C{sys.modules} to their original values.
        N�rrrr �clear�updater"�r/rrr�tearDownOs
zPluginTests.tearDownFcCs�|j�d�}d�|dd��}|d}ttj||�tj|j=ddg|rPdgpRgD]J}zt�|j|�WqVt	k
r�}z|j
t
jkr��W5d}~XYqVXqVdS)N�.����c�o�)r�split�join�delattrrr �os�remover)�OSError�errnoZENOENT)r/r.ZdeleteSourceZ
modulePathZpackageName�
moduleNameZextZoserrr�_unimportPythonModuleXs
z!PluginTests._unimportPythonModulecCs|j�d���dS)z;
        Remove the plugins B{droping.cache} file.
        �dropin.cacheN)r'r&r@r5rrr�_clearCachegszPluginTests._clearCachecst����fdd��}|S)z�
        This is a paranoid test wrapper, that calls C{meth} 2 times, clear the
        cache, and calls it 2 other times. It's supposed to ensure that the
        plugin system behaves correctly no matter what the state of the cache
        is.
        cs,�|��|�|���|��|�dS)N)rFr5��methrr�wrappedus
z+PluginTests._withCacheness.<locals>.wrapped)�	functools�wraps)rHrIrrGr�_withCachenessnszPluginTests._withCachenesscCs�t�|j�}||j}|�|jd|jf�|�d|j�dd�|jD�d}|�	|j
|�|�|jd�|�|j��d�|�|j
ttjg�|��}|�	|tjd|jfj�ddlm}|�	||j�dS)	a	
        Check that the cache returned by L{plugin.getCache} hold the plugin
        B{testplugin}, and that this plugin has the properties we expect:
        provide L{TestPlugin}, has the good name and description, and can be
        loaded successfully.
        zmypackage.%szI'm a test drop-in.cSsg|]}t|jkr|�qSr)r�provided��.0�prrr�
<listcomp>�s
z*PluginTests.test_cache.<locals>.<listcomp>r�
TestPluginz*A plugin used solely for testing purposes.N)r�getCacher.r,�assertEqualrC�assertInZdescription�pluginsZassertIs�dropin�name�striprMrZIPlugin�loadrr rRZmypackage.testpluginr)r/�cacherWZp1Z
realPlugin�tprrr�
test_cache�s*

���zPluginTests.test_cachecCs>t�|j�|j}tdd�|jD��d}|�t|�d�dS)zm
        L{CachedPlugin} has a helpful C{repr} which contains relevant
        information about it.
        css|]}|jdkr|VqdS)rRN�rXrNrrr�	<genexpr>�s
�z-PluginTests.test_cacheRepr.<locals>.<genexpr>rzT<CachedPlugin 'TestPlugin'/'mypackage.testplugin' (provides 'ITestPlugin, IPlugin')>N)rrSr.r,�listrVrT�repr)r/ZcachedDropinZcachedPluginrrr�test_cacheRepr�s��zPluginTests.test_cacheReprcCsLtt�t|j��}|�t|�d�ddg}|D]}|�|j�|�	�q.dS)a
        L{plugin.getPlugins} should return the list of plugins matching the
        specified interface (here, L{ITestPlugin2}), and these plugins
        should be instances of classes with a C{test} method, to be sure
        L{plugin.getPlugins} load classes correctly.
        �ZAnotherTestPluginZThirdTestPluginN)
r`r�
getPluginsrr.rT�lenr@rZtest)r/rV�namesrPrrr�test_plugins�szPluginTests.test_pluginsc
Cs�tt��d��|j�d��zr|�	dtj�|�
ttjdd�d�tt
�t|j��}|�t|�d�d	d
g}|D]}|�|j�|��qrW5|�tjdd�XdS)ze
        Check that L{plugin.getPlugins} is able to detect plugins added at
        runtime.
        �plugin_extra1.py�pluginextra.py�mypackage.pluginextraTr�pluginextraz&mypackage still has pluginextra modulercrR�FourthTestPluginN)rr)r*r+r'r&rDrr �failIfIn�assertFalse�hasattrr`rrdrr.rTrer@r�test1�r/�plgsrfrPrrr�test_detectNewFiles�s$
���zPluginTests.test_detectNewFilesc
Cs�tt��d��|j�d��z�t	t
�t|j
��}|�t|�d�tt��d��|j�d��|�tjd�t	t
�t|j
��}|�t|�d�dd	d
g}|D]}|�|j�|��q�W5|�tjdd�XdS)z�
        Check that if the content of a plugin change, L{plugin.getPlugins} is
        able to detect the new plugins added.
        rhrirjTrczplugin_extra2.py�rRrlZFifthTestPluginN)rr)r*r+r'r&rDrr r`rrdrr.rTrer@rrprqrrr�test_detectFilesChanged�s(
�
�
�z#PluginTests.test_detectFilesChangedc
Csntt��d��|j�d��zt	t
�t|j
��W5|�tjdd�Xt	t
�t|j
��}|�dt|��dS)zs
        Check that when a dropin file is removed, L{plugin.getPlugins} doesn't
        return it anymore.
        rhrirjT�N)rr)r*r+r'r&rDrr r`rrdrr.rTre)r/rrrrr�test_detectFilesRemoved
s
��z#PluginTests.test_detectFilesRemovedc	Csd|��}|�tj�|��|jj�|�z&t	t
�t|j��}|�
t|�d�W5|jj�|�XdS)zy
        Test that getCache skips over any entries in a plugin package's
        C{__path__} which do not exist.
        rvN)r#rnr?r�existsr.�__path__�appendr@r`rrdrrTre)r/rrrrrr�test_nonexistentPathEntrysz%PluginTests.test_nonexistentPathEntryc	Csxt|���}|�|���|��|�d�j}|jj�	|�z&tt�
t|j��}|�t|�d�W5|jj�
|�XdS)z�
        Test that getCache skips over any entries in a plugin package's
        C{__path__} which refer to children of paths which are not directories.
        Ztest_packagervN)rr#rnrxZtouchr&rr.ryrzr@r`rrdrrTre)r/rr&rrrrr�test_nonDirectoryChildEntry1sz'PluginTests.test_nonDirectoryChildEntrycCs�t�|j�|j�d�}tt��d��|j�d��t	�t
�|jjd�t
�|jd�|�
t
j|jjd�|�
t
j|jd�g}t|j�|�
t|j�t�|j�}|�d|�|�|j|�d|jtjf}|D]}|t|�kr�q�q�|�d	|f�d
S)z�
        The C{dropin.cache} file may not be writable: the cache should still be
        attainable, but an error should be logged to show that the cache
        couldn't be updated.
        rErhrii@�i�rk�3Unable to write to plugin cache %s: error number %d�;Did not observe unwriteable cache warning in log events: %rN)rrSr.r'r&rr)r*r+rr?�chmodr�
addCleanupr	rzr
rUr,rB�EPERMr�fail)r/Z	cachepath�eventsr[�expected�eventrrr�test_deployedModeDs8
�
���zPluginTests.test_deployedModeN)F)rrrrr0r6rDrFrLr]rbrgrsrurwr{r|r�rrrrr2s,	
# rsj
from twisted.plugin import pluginPackagePaths
__path__.extend(pluginPackagePaths(__name__))
__all__ = []
cCsd�|��d�S)Nz�from zope.interface import provider
from twisted.plugin import IPlugin
from twisted.test.test_plugin import ITestPlugin

@provider(IPlugin, ITestPlugin)
class {0}(object):
    pass
�ascii)�format�encoder^rrr�pluginFileContentsxs���r�cCsl|��|�d�}|��|r.|�d��d�|�d�}|��|rT|�d��t�|�|d��|�|S)z'
    Create a plugindummy package.
    �plugindummyrrrV�.py)r%r&r(�pluginInitFile)Z	entrypathZ
pluginContent�real�pluginModule�pkgZplugsrrr�_createPluginDummy�s

r�c@sneZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Ze
rbesbde_dd�ZdS)�DeveloperSetupTestsz�
    These tests verify things about the plugin system without actually
    interacting with the deployed 'twisted.plugins' package, instead creating a
    temporary package.
    cCs>tjdd�|_tj��|_t|���|_|j�	�|j�
d�|_|j�
d�|_|j�
d�|_
t|jtd�dd�|_t|jtd�dd�|_t|j
td	�d
d�|_tj�dd
�|j|j
fD��|��|j�
d��
d�|_|j�
d�|_t��}t�|j�
d�j|dfd�t�|jj|dfd�|��|��dS)a7
        Create a complex environment with multiple entries on sys.path, akin to
        a developer's environment who has a development (trunk) checkout of
        Twisted, a system installed version of Twisted (for their operating
        system's tools) and a project which provides Twisted plugins.
        NZsystem_pathZdevelopment_pathZapplication_path�systemTZplugindummy_builtin�dev�appFZplugindummy_appcSsg|]
}|j�qSr�r�rO�xrrrrQ�sz-DeveloperSetupTests.setUp.<locals>.<listcomp>r�rVrEzplugindummy_builtin.pyi�rci�)rr�	savedPathr r!r"rr#ZfakeRootr%r&�
systemPath�devPath�appPathr�r�Z
systemPackageZ
devPackage�
appPackage�extend�
getAllPlugins�sysplug�syscache�timer?�utime�
lockSystem�resetEnvironment)r/Znowrrrr0�sN
����
�zDeveloperSetupTests.setUpcCs$t�|jjd�t�|jjd�dS)zW
        Lock the system directories, as if they were unwritable by this user.
        imN�r?r�r�rr�r5rrrr��szDeveloperSetupTests.lockSystemcCs$t�|jjd�t�|jjd�dS)zW
        Unlock the system directories, as if they were writable by this user.
        i�Nr�r5rrr�unlockSystem�sz DeveloperSetupTests.unlockSystemcCs(ddl}tt�t|j��}dd�|D�S)zl
        Get all the plugins loadable from our dummy package, and return their
        short names.
        rNcSsg|]
}|j�qSr�r)rOZplugrrrrQ�sz5DeveloperSetupTests.getAllPlugins.<locals>.<listcomp>)Zplugindummy.pluginsr`rrdrrV)r/r�r�rrrr��sz!DeveloperSetupTests.getAllPluginscCs.|��tj�dd�|j|j|jfD��dS)zc
        Change the environment to what it should be just as the test is
        starting.
        cSsg|]
}|j�qSrr�r�rrrrQ�sz8DeveloperSetupTests.resetEnvironment.<locals>.<listcomp>N)�unsetEnvironmentrrr�r�r�r�r5rrrr��s
�z$DeveloperSetupTests.resetEnvironmentcCs2t�tj��tj�|j�|jtjdd�<dS)zh
        Change the Python environment back to what it was before the test was
        started.
        N)rrr r3r4r"r�rr5rrrr��s
z$DeveloperSetupTests.unsetEnvironmentcCs|��|��dS)z�
        Reset the Python environment to what it was before this test ran, and
        restore permissions on files which were marked read-only so that the
        directory may be cleanly cleaned up.
        N)r�r�r5rrrr6�szDeveloperSetupTests.tearDowncCs2td�D]$}|��}|��|�|ddg�qdS)a
        Plugins added in the development path should be loadable, even when
        the (now non-importable) system path contains its own idea of the
        list of plugins for a package.  Inversely, plugins added in the
        system path should not be available.
        rtr�r�N)�ranger��sortrT)r/r�rfrrr�"test_developmentPluginAvailability
s	z6DeveloperSetupTests.test_developmentPluginAvailabilitycCs�|j�d�}|�td��t��d}t�|j||f�|�d�}t	rRt
dd�}nt
�}tj|jjfddi|��t�|j||f�|�
�|��|�d|���|�d	|���|��|�td	��|�d|���|�d	|���d
S)z�
        Verify that if a stale .pyc file on the PYTHONPATH is replaced by a
        fresh .py file, the plugins in the new .py are picked up rather than
        the stale .pyc, even if the .pyc is still around.
        zstale.py�onei�z	stale.pycT)Zlegacy�quietrvZtwoN)r�r&r(r�r�r?r�rr*r�dict�
compileallZcompile_dirr@r�rUr�rm)r/Zmypathr�ZpycZextrarrr�test_freshPyReplacesStalePycs$
z0DeveloperSetupTests.test_freshPyReplacesStalePycz/PyPy2 will not normally import lone .pyc files.cCs�|��|j�d��td��|��tj�|j	j�g}t
|j�|�t
|j�|�d|���d|jjtjf}|D]}|t|�krxq�qx|�d|f�dS)aF
        Verify that a failure to write the dropin.cache file on a read-only
        path will not affect the list of plugins returned.

        Note: this test should pass on both Linux and Windows, but may not
        provide useful coverage on Windows due to the different meaning of
        "read-only directory".
        znewstuff.pyr�r~rN)r�r�r&r(r�r�rrr@r�r	rzr�r
rUr�r�rBr�rr�)r/r�r�r�rrr�test_newPluginsOnReadOnlyPathBs(	
���z1DeveloperSetupTests.test_newPluginsOnReadOnlyPathN)rrrrr0r�r�r�r�r�r6r�r�rr�skipr�rrrrr��s.
$�r�c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�AdjacentPackageTestsz�
    Tests for the behavior of the plugin system when there are multiple
    installed copies of the package containing the plugins being loaded.
    cCs tjdd�|_tj��|_dS)zS
        Save the elements of C{sys.path} and the items of C{sys.modules}.
        N)rrrr r!r"r5rrrr0mszAdjacentPackageTests.setUpcCs,|jtjdd�<tj��tj�|j�dSr1r2r5rrrr6us
zAdjacentPackageTests.tearDowncCsn|�|�}|�d�}|��|�d��d�|�d�}|��|�d��t�|�|d�}|�t|��|S)a`
        Create a directory containing a Python package named I{dummy} with a
        I{plugins} subpackage.

        @type root: L{FilePath}
        @param root: The directory in which to create the hierarchy.

        @type name: C{str}
        @param name: The name of the directory to create which will contain
            the package.

        @type pluginName: C{str}
        @param pluginName: The name of a module to create in the
            I{dummy.plugins} package.

        @rtype: L{FilePath}
        @return: The directory which was created to contain the I{dummy}
            package.
        �dummyrrrVr�)r&�makedirsr(r�r�)r/r$rXZ
pluginNameZ	directoryr'rVr�rrr�createDummyPackage~s


z'AdjacentPackageTests.createDummyPackagecCs�t|���}|��|�|dd�}|�|dd�}tj�|j�tj�|j�ddl}tt	�
t|j��}|�
dgdd�|D��dS)z�
        Only plugins from the first package in sys.path should be returned by
        getPlugins in the case where there are two Python packages by the same
        name installed, each with a plugin module by a single name.
        �firstZ
someplugin�secondrNcSsg|]
}|j�qSrr�rNrrrrQ�szWAdjacentPackageTests.test_hiddenPackageSamePluginModuleNameObscured.<locals>.<listcomp>�rr#r�r�rrrzZ
dummy.pluginsr`rrdrrVrT�r/r$ZfirstDirectoryZsecondDirectoryr�rVrrr�.test_hiddenPackageSamePluginModuleNameObscured�szCAdjacentPackageTests.test_hiddenPackageSamePluginModuleNameObscuredcCs�t|���}|��|�|dd�}|�|dd�}tj�|j�tj�|j�ddl}tt	�
t|j��}|�
dgdd�|D��dS)	z�
        Plugins from the first package in sys.path should be returned by
        getPlugins in the case where there are two Python packages by the same
        name installed, each with a plugin module by a different name.
        r�Z
thispluginr�Z
thatpluginrNcSsg|]
}|j�qSrr�rNrrrrQ�sz\AdjacentPackageTests.test_hiddenPackageDifferentPluginModuleNameObscured.<locals>.<listcomp>r�r�rrr�3test_hiddenPackageDifferentPluginModuleNameObscured�szHAdjacentPackageTests.test_hiddenPackageDifferentPluginModuleNameObscuredN)	rrrrr0r6r�r�r�rrrrr�gs	 r�c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�PackagePathTestszg
    Tests for L{plugin.pluginPackagePaths} which constructs search paths for
    plugin packages.
    cCstjdd�|_dS)z3
        Save the elements of C{sys.path}.
        N)rrrr5rrrr0�szPackagePathTests.setUpcCs|jtjdd�<dS)z<
        Restore C{sys.path} to its original value.
        N)rrrr5rrrr6�szPackagePathTests.tearDowncCsTtd�}td�}|j|jgt_|�t�d�|�d��d�j|�d��d�jg�dS)z�
        L{plugin.pluginPackagePaths} should return a list containing each
        directory in C{sys.path} with a suffix based on the supplied package
        name.
        �foo�bar�
dummy.pluginsr�rVN)rrrrTr�pluginPackagePathsr&)r/r�r�rrr�test_pluginDirectories�s��z'PackagePathTests.test_pluginDirectoriescCs�t|���}|�d��d��d�}|��|�d��d�|�d�j|�d�jgt_|�t�	d�|�d��d��d�jg�dS)	a�
        L{plugin.pluginPackagePaths} should exclude directories which are
        Python packages.  The only allowed plugin package (the only one
        associated with a I{dummy} package which Python will allow to be
        imported) will already be known to the caller of
        L{plugin.pluginPackagePaths} and will most commonly already be in
        the C{__path__} they are about to mutate.
        r�r�rVrrr�r�N)
rr#r&r�r(rrrTrr�)r/r$r�rrr�test_pluginPackagesExcluded�s	�z,PackagePathTests.test_pluginPackagesExcludedN)rrrrr0r6r�r�rrrrr��s
r�)&rZ
__future__rrrrBr?r�r�rJZzope.interfacerZ
twisted.trialrZtwisted.python.compatrrZtwisted.python.logrr	r
Ztwisted.python.filepathrZtwistedr�	importlibr
rrrZTestCaserr�r�r�r�r�r�rrrr�<module>s2 	BRb