Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/test/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyc


=Nc@sZdZddlZddlZddlZddlZddlZddlmZddlm	Z	ddl
mZmZm
Z
ddlmZddlmZddlmZd	efd
YZdefdYZd
e	jfdYZdZdZdZde	jfdYZde	jfdYZde	jfdYZdS(s"
Tests for Twisted plugin system.
iN(t	Interface(tunittest(ttextFromEventDicttaddObservertremoveObserver(tFilePath(tmergeFunctionMetadata(tplugintITestPlugincBseZdZRS(sS
    A plugin for use by the plugin system's unit tests.

    Do not use this.
    (t__name__t
__module__t__doc__(((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyRstITestPlugin2cBseZdZRS(s
    See L{ITestPlugin}.
    (R	R
R(((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR stPluginTestCasecBseZdZdZdZedZdZdZdZ	ee	Z	dZ
ee
Z
dZeeZd	ZeeZd
Z
ee
Z
dZeeZdZeeZd
ZRS(s_
    Tests which verify the behavior of the current, active Twisted plugins
    directory.
    cCstj|_tjj|_t|j|_|jj	|jj
d|_|jj	|jj
djdtt
jdj|jj
dd|_tjjd|jjdd	l}||_d	S(
sV
        Save C{sys.path} and C{sys.modules}, and create a package for tests.
        t	mypackages__init__.pytsplugin_basic.pys
testplugin.pyt
testpluginiiN(tsystpathtoriginalPathtmodulestcopytsavedModulesRtmktemptroottcreateDirectorytchildtpackaget
setContentt__file__tsiblingtcopyTotoriginalPlugintinsertRtmodule(tselfR((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pytsetUp-s


	cCs1|jtj(tjjtjj|jdS(sR
        Restore C{sys.path} and C{sys.modules} to their original values.
        N(RRRRtcleartupdateR(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttearDownDs

cCs|jjd}dj|d }|d}ttj||tj|j=xmddg|rkdgpngD]L}ytj|j|Wqpt	k
r}|j
t
jkrqqpXqpWdS(Nt.itctoR(R	tsplittjointdelattrRRtostremoveRtOSErrorterrnotENOENT(R#R"tdeleteSourcet
modulePathtpackageNamet
moduleNametexttose((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt_unimportPythonModuleMs

&cCs|jjdjdS(s;
        Remove the plugins B{droping.cache} file.
        sdropin.cacheN(RRR/(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt_clearCache\scsfd}t|S(s
        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.
        cs6|||j||dS(N(R:(R#(tmeth(s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pytwrappedjs




(R(R;R<((R;s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt_withCachenesscscCs/tj|j}||j}|j|jd|jf|jd|jg|jD]}t	|j
krY|^qYd}|j|j||j|j
d|j|jjd|j|j
t	tjg|j}|j|tjd|jfjddlj}|j||jdS(s	
        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.
        smypackage.%ssI'm a test drop-in.it
TestPlugins*A plugin used solely for testing purposes.iN(RtgetCacheR"R tassertEqualR6tassertIntdescriptiontpluginsRtprovidedtassertIdenticaltdropintnametstriptIPlugintloadRRR>tmypackage.testpluginR(R#tcacheRFtptp1t
realPluginttp((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt
test_cachess$
/cCslttjt|j}|jt|dddg}x(|D] }|j|j|j	qDWdS(s
        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.
        itAnotherTestPlugintThirdTestPluginN(
tlistRt
getPluginsRR"R@tlenR/R	ttest(R#RCtnamesRM((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_pluginss
cCsttjdj|jjdz|jdtj|j	t
tjdddttj
t|j}|jt|ddd	g}x(|D] }|j|j|jqWWd
|jtjdtXd
S(se
        Check that L{plugin.getPlugins} is able to detect plugins added at
        runtime.
        splugin_extra1.pyspluginextra.pysmypackage.pluginextraRtpluginextras&mypackage still has pluginextra moduleiR>tFourthTestPluginN(RRRRRRtfailIfInRRtfailIfthasattrRTRRURR"R@RVR/R	ttest1R9tTrue(R#tplgsRXRM((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_detectNewFiless

cCs#ttjdj|jjdzttjt	|j
}|jt|dttjdj|jjd|j
tjdttjt	|j
}|jt|dddd	g}x(|D] }|j|j|jqWWd
|j
tjdtXd
S(s
        Check that if the content of a plugin change, L{plugin.getPlugins} is
        able to detect the new plugins added.
        splugin_extra1.pyspluginextra.pyisplugin_extra2.pysmypackage.pluginextraiR>R[tFifthTestPluginN(RRRRRRRTRRURR"R@RVR9RRR/R	R_R`(R#RaRXRM((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_detectFilesChangeds"

cCsttjdj|jjdzttjt	|j
Wd|jtj
dtXttjt	|j
}|jdt|dS(ss
        Check that when a dropin file is removed, L{plugin.getPlugins} doesn't
        return it anymore.
        splugin_extra1.pyspluginextra.pyNsmypackage.pluginextrai(RRRRRRRTRRURR"R9RRR`R@RV(R#Ra((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_detectFilesRemoveds
cCs|j}|jtjj||jjj|z5tt	j
t|j}|jt
|dWd|jjj|XdS(sy
        Test that getCache skips over any entries in a plugin package's
        C{__path__} which do not exist.
        iN(RR]R.RtexistsR"t__path__tappendRTRRURR@RVR/(R#RRa((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_nonexistentPathEntryscCst|j}|j|j|j|jdj}|jjj	|z5t
tjt
|j}|jt|dWd|jjj|XdS(s
        Test that getCache skips over any entries in a plugin package's
        C{__path__} which refer to children of paths which are not directories.
        ttest_packageiN(RRR]RfttouchRRR"RgRhRTRRURR@RVR/(R#RRRa((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_nonDirectoryChildEntrys
cCsXtj|j|jjd}ttjdj|jjdt	j
|jjdt	j
|jd|jt	j
|jjd|jt	j
|jdg}t
|j|jt|jtj|j}|jd||j|j|d|jtjf}x8|D]}|t|kr Pq q W|jd	|fd
S(s
        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.
        sdropin.cachesplugin_extra1.pyspluginextra.pyi@iiRZs3Unable to write to plugin cache %s: error number %ds;Did not observe unwriteable cache warning in log events: %rN(RR?R"RRRRRRR.tchmodRt
addCleanupRRhRRAR R1tEPERMRtfail(R#t	cachepathteventsRLtexpectedtevent((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_deployedMode(s,

(R	R
RR$R'tFalseR9R:R=RQRYRbRdReRiRlRu(((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR
's*						#			 			sj
from twisted.plugin import pluginPackagePaths
__path__.extend(pluginPackagePaths(__name__))
__all__ = []
cCsd|fS(Nsfrom zope.interface import classProvides
from twisted.plugin import IPlugin
from twisted.test.test_plugin import ITestPlugin

class %s(object):
    classProvides(IPlugin, ITestPlugin)
((RG((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pytpluginFileContents[scCs|j|jd}|j|rB|jdjdn|jd}|j|rz|jdjtn|j|dj||S(s'
    Create a plugindummy package.
    tplugindummys__init__.pyRRCs.py(RRRtpluginInitFile(t	entrypatht
pluginContenttrealtpluginModuletpkgtplugs((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt_createPluginDummyes


tDeveloperSetupTestscBsheZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZRS(s
    These tests verify things about the plugin system without actually
    interacting with the deployed 'twisted.plugins' package, instead creating a
    temporary package.
    cCstj|_tjj|_t|j|_|jj	|jj
d|_|jj
d|_|jj
d|_
t|jtdtd|_t|jtdtd|_t|j
tdtd|_tjjg|j|j
gD]}|j^q|j|jj
d	j
d
|_|jj
d|_tj}tj|jj
dj|d
fdtj|jj|dfd|j|jdS(s7
        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.
        tsystem_pathtdevelopment_pathtapplication_pathtsystemtplugindummy_builtintdevtapptplugindummy_appRxRCsdropin.cachesplugindummy_builtin.pyiiiN(RRt	savedPathRRRRRtfakeRootRRt
systemPathtdevPathtappPathRRwR`t
systemPackaget
devPackageRvt
appPackagetextendt
getAllPluginstsysplugtsyscachettimeR.tutimet
lockSystemtresetEnvironment(R#txtnow((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR$~s8

 
!
cCs0tj|jjdtj|jjddS(sW
        Lock the system directories, as if they were unwritable by this user.
        imN(R.RmRRR(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyRscCs0tj|jjdtj|jjddS(sW
        Unlock the system directories, as if they were writable by this user.
        iN(R.RmRRR(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pytunlockSystemscCsAddl}ttjt|j}g|D]}|j^q.S(sl
        Get all the plugins loadable from our dummy package, and return their
        short names.
        iN(tplugindummy.pluginsRTRRURRCR	(R#RxRtplug((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyRscCsF|jtjjg|j|j|jgD]}|j^q,dS(sc
        Change the environment to what it should be just as the test is
        starting.
        N(tunsetEnvironmentRRRRRR(R#R((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyRs
cCs1tjjtjj|j|jtj(dS(sh
        Change the Python environment back to what it was before the test was
        started.
        N(RRR%R&RRR(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyRs
cCs|j|jdS(s
        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(RR(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR's
cCsGx@tdD]2}|j}|j|j|ddgq
WdS(s
        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.
        iRRN(trangeRtsortR@(R#RRX((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt"test_developmentPluginAvailabilitys	
cCs|jjd}|jtdtjd}tj|j||f|jd}t	j
|jjddtj|j||f|j|j|j
d|j|jd|j|j|jtd|jd|j|j
d|jdS(	s
        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.
        sstale.pytoneis	stale.pyctquietittwoN(RRRRwRR.RRRt
compilealltcompile_dirR/RRARR\(R#tmypathRtpyc((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_freshPyReplacesStalePycs


cCs|j|jjdjtd|jtjj|j	jg}t
|j|jt
|j|jd|jd|jjtjf}x8|D]}|t|krPqqW|jd|fdS(sF
        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".
        snewstuff.pyRs3Unable to write to plugin cache %s: error number %ds;Did not observe unwriteable cache warning in log events: %rN(RRRRRwRRRR/RRRhRnRRARRR1RoRRp(R#RrRsRt((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_newPluginsOnReadOnlyPaths 	



(
R	R
RR$RRRRRR'RRR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyRws	.				
	
			tAdjacentPackageTestscBs;eZdZdZdZdZdZdZRS(s
    Tests for the behavior of the plugin system when there are multiple
    installed copies of the package containing the plugins being loaded.
    cCs#tj|_tjj|_dS(sS
        Save the elements of C{sys.path} and the items of C{sys.modules}.
        N(RRRRRR(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR$@s
cCs1|jtj(tjjtjj|jdS(sR
        Restore C{sys.path} and C{sys.modules} to their original values.
        N(RRRRR%R&R(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR'Hs

cCs|j|}|jd}|j|jdjd|jd}|j|jdjt|j|d}|jt||S(s`
        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.
        tdummys__init__.pyRRCs.py(RtmakedirsRRyRw(R#RRGt
pluginNamet	directoryRRCR}((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pytcreateDummyPackageQs

cCst|j}|j|j|dd}|j|dd}tjj|jtjj|jddl}tt	j
t|j}|j
dgg|D]}|j^qdS(s
        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.
        tfirstt
someplugintsecondiN(RRRRRRRht
dummy.pluginsRTRRURRCR@R	(R#RtfirstDirectorytsecondDirectoryRRCRM((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt.test_hiddenPackageSamePluginModuleNameObscuredqs
cCst|j}|j|j|dd}|j|dd}tjj|jtjj|jddl}tt	j
t|j}|j
dgg|D]}|j^qdS(s
        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.
        Rt
thispluginRt
thatpluginiN(RRRRRRRhRRTRRURRCR@R	(R#RRRRRCRM((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt3test_hiddenPackageDifferentPluginModuleNameObscureds
(R	R
RR$R'RRR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR:s				 	tPackagePathTestscBs2eZdZdZdZdZdZRS(sg
    Tests for L{plugin.pluginPackagePaths} which constructs search paths for
    plugin packages.
    cCstj|_dS(s3
        Save the elements of C{sys.path}.
        N(RRR(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR$scCs|jtj(dS(s<
        Restore C{sys.path} to its original value.
        N(RRR(R#((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyR'scCsztd}td}|j|jgt_|jtjd|jdjdj|jdjdjgdS(s
        L{plugin.pluginPackagePaths} should return a list containing each
        directory in C{sys.path} with a suffix based on the supplied package
        name.
        tfootbars
dummy.pluginsRRCN(RRRR@RtpluginPackagePathsR(R#RR((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_pluginDirectoriesscCst|j}|jdjdjd}|j|jdjd|jdj|jdjgt_|jtj	d|jdjdjdjgdS(	s
        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.
        RRRCs__init__.pyRRs
dummy.pluginsN(
RRRRRRRR@RR(R#RR((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyttest_pluginPackagesExcludeds	!
'(R	R
RR$R'RR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyRs
			(RRR1R.RRtzope.interfaceRt
twisted.trialRttwisted.python.logRRRttwisted.python.filepathRttwisted.python.utilRttwistedRRRtTestCaseR
RyRwRRRR(((s</usr/lib/python2.7/dist-packages/twisted/test/test_plugin.pyt<module>s$0	3	
	b