Files
Home-AssistantConfig/deps/cherrypy/process/__pycache__/plugins.cpython-34.pyc
T

236 lines
23 KiB
Plaintext
Raw Normal View History

2016-10-11 16:42:06 +00:00
î
²¾ûWhã
@dZddlZddlZddlZddlZddlZddlZddlm Z m
Z
m Z ddlm Z m
Z
mZejƒZGdddeƒZGdddeƒZyddlZddlZWnek
d\ZZYnXGd d
d
eƒZGd d d eƒZGd
ddeƒZGddde
ƒZGdddeejƒZGdddeƒZGdddeƒZGdddeƒZdS)z2Site services for use with a Web Site Process Bus.éN)Ú
text_or_bytesÚ
get_daemonÚget_thread_ident)ÚntobÚTimerÚSetDaemonPropertyc@s@eZdZdZdZddZddZddZdS) Ú SimplePluginzCPlugin base class which auto-subscribes methods for known channels.NcCs
||_dS)N)Úbus)Úselfr ©r ú</tmp/pip-build-fa7b9uy2/cherrypy/cherrypy/process/plugins.pyÚ__init__(szSimplePlugin.__init__cCsOxH|jjD]:}t||dƒ}|dk r
|jj||ƒq
q
WdS)z>Register this object as a (multi-channel) listener on the bus.N)r Ú listenersÚgetattrÚ subscribe)r
ÚchannelÚmethodr r r r+s zSimplePlugin.subscribecCsOxH|jjD]:}t||dƒ}|dk r
|jj||ƒq
q
WdS)z0Unregister this object as a listener on the bus.N)r rrÚ unsubscribe)r
rrr r r r3s zSimplePlugin.unsubscribe)Ú__name__Ú
__module__Ú __qualname__Ú__doc__r r
rrr r r r r s
  rc@eZdZdZiZiZxLeeƒjƒD]8\Z Z
e j dƒr1e j dƒ r1e ee
<q1q1W[ [
ddZ ddddZ
d d
Zd d Zd
dZdddZddddZddZdS)Ú
SignalHandlerañRegister bus channels (and listeners) for system signals.
You can modify what signals your application listens for, and what it does
when it receives signals, by modifying :attr:`SignalHandler.handlers`,
a dict of {signal name: callback} pairs. The default set is::
handlers = {'SIGTERM': self.bus.exit,
'SIGHUP': self.handle_SIGHUP,
'SIGUSR1': self.bus.graceful,
}
The :func:`SignalHandler.handle_SIGHUP`` method calls
:func:`bus.restart()<cherrypy.process.wspbus.Bus.restart>`
if the process is daemonized, but
:func:`bus.exit()<cherrypy.process.wspbus.Bus.exit>`
if the process is attached to a TTY. This is because Unix window
managers tend to send SIGHUP to terminal windows when the user closes them.
Feel free to add signals which are not available on every platform.
The :class:`SignalHandler` will ignore errors raised from attempting
to register handlers for unknown signals.
ZSIGZSIG_cC||_i|jjd6|jd6|jjd6|_tjdddkr|jd=|jj|jd<|jjdƒ|j|jd<ni|_ t
j ƒ|_ dS) NÚSIGTERMÚSIGHUPÚSIGUSR1éÚjavaÚSIGUSR2zASIGUSR1 cannot be set on the JVM platform. Using SIGUSR2 instead.ÚSIGINT)
r ÚexitÚ
handle_SIGHUPÚgracefulÚhandlersÚsysÚplatformÚlogÚ_jython_SIGINT_handlerÚ_previous_handlersÚosÚgetpidÚ
_original_pid)r
r r r r r
`s 

 zSignalHandler.__init__NcCs!|jjdƒ|jjƒdS)Nz%Keyboard Interrupt: shutting down bus)r r&r )r
ÚsignumÚframer r r r'tsz$SignalHandler._jython_SIGINT_handlercCs:|jtjƒkr2tjtjjƒƒ r2dSdSdS)Return boolean indicating if the current process is
running as a daemon.
The criteria to determine the `daemon` condition is to verify
if the current pid is not the same as the one that got used on
the initial construction of the plugin *and* the stdin is not
connected to a terminal.
The sole validation of the tty is not enough when the plugin
is executing inside other process like in a CI tool
(Buildbot, Jenkins).
TFN)r+r)r*Úisattyr$ÚstdinÚfileno)r
r r r Ú_is_daemonizedys
zSignalHandler._is_daemonizedc CsMxF|jjƒD]5\}}y|j||ƒWqtk
rDYqXqWdS)z#Subscribe self.handlers to signals.N)r#ÚitemsÚ set_handlerÚ
ValueError)r
ÚsigÚfuncr r r rs

zSignalHandler.subscribec
C|jjƒD]ß\}}|j|}|dkrU|jjd|ƒtj}n|jjd||fƒyEtj||ƒ}|dk|jjd||fddƒnWqtk
|jjd||fddd d
ƒYqXqWdS) z'Unsubscribe self.handlers from signals.Nz Restoring %s handler to SIG_DFL.zRestoring %s handler %r.z?Restored old %s handler %r, but our handler was not registered.Úleveléz Unable to restore %s handler %r.é(Ú tracebackT) r(r2Úsignalsr r&Ú_signalÚSIG_DFLÚsignalr4)r
r,ÚhandlerÚsignameZ our_handlerr r r rs
    
 zSignalHandler.unsubscribec Ct|tƒrItt|dƒ}|dkr@td|ƒn|}n<y|j|}Wn"tk
r~td|ƒYnX|}tj||jƒ}||j |<|dk |j
j d|ƒ|j
j ||ƒndS)a=Subscribe a handler for the given signal (number or name).
If the optional 'listener' argument is provided, it will be
subscribed as a listener for the given signal's channel.
If the given signal name or number is not available on the current
platform, ValueError is raised.
NzNo such signal: %rzListening for %s.)
Ú
isinstancerrr<r4r;ÚKeyErrorr>Ú_handle_signalr(r r&r)r
r>Zlistenerr,r@Úprevr r r r3ªs   

 zSignalHandler.set_handlercCs5|j|}|jjd|ƒ|jj|ƒdS)z?Python signal handler (self.set_handler subscribes it for you).zCaught signal %s.N)r;r r&Úpublish)r
r,r-r@r r r rCÆs
zSignalHandler._handle_signalcCsM|jƒr,|jjdƒ|jjƒn|jjdƒ|jjƒdS)z!Restart if daemonized, else exit.z+SIGHUP caught while daemonized. Restarting.z*SIGHUP caught but not daemonized. Exiting.N)r1r r&Úrestartr )r
r r r r!Ìs
 zSignalHandler.handle_SIGHUP)rrrrr#r;Úvarsr<r2Ú
startswithr
r'r1rrr3rCr!r r r r r<s     rc@eZdZdZdddddZddZddZeeed d
ƒZd d Z d
dZ
ee e
d dƒZ ddZ ddZ
ee e
d dƒZddZde_dS)ÚDropPrivilegesz§Drop privileges. uid/gid arguments not available on Windows.
Special thanks to `Gavin Baker <http://antonym.org/2005/12/dropping-privileges-in-python.html>`_
NcCs8tj||ƒd|_||_||_||_dS)NF)rr
Ú finalizedÚuidÚgidÚumask)r
r rOrMrNr r r r
ås
   zDropPrivileges.__init__cCs|jS)N)Ú_uid)r
r r r Ú_get_uidìszDropPrivileges._get_uidcCsl|dk r_tdkr7|jjdddƒd}q_t|tƒr_tj|ƒd}q_n||_dS)Nz'pwd module not available; ignoring uid.r7r8é)Úpwdr r&rArÚgetpwnamrP)r
Úvalr r r Ú_set_uidïs   zDropPrivileges._set_uidÚdocz/The uid under which to run. Availability: Unix.cCs|jS)N)Ú_gid)r
r r r Ú_get_gidûszDropPrivileges._get_gidcCsl|dk r_tdkr7|jjdddƒd}q_t|tƒr_tj|ƒd}q_n||_dS)Nz'grp module not available; ignoring gid.r7r8rR)Úgrpr r&rArÚgetgrnamrX)r
rUr r r Ú_set_gidþs   zDropPrivileges._set_gidz/The gid under which to run. Availability: Unix.cCs|jS)N)Ú_umask)r
r r r Ú
_get_umask
szDropPrivileges._get_umaskc CsX|dk rKy tjWqKtk
rG|jjdddƒd}YqKXn||_dS)Nz-umask function not available; ignoring umask.r7r8)r)rOÚAttributeErrorr r&r])r
rUr r r Ú
_set_umask
s  
zDropPrivileges._set_umaskz½The default permission mode for newly created files and
directories.
Usually expressed in octal format, for example, ``0644``.
Availability: Unix, Windows.
cCdd}|jrP|jdko0|jdks|jjd|ƒƒq|jdkr|jdkrtsztr|jjdddƒqn|jjd|ƒƒ|jdk tj|jƒtj gƒn|jdk tj
|jƒn|jjd|ƒƒ|jrJ|j dk |jjd |j ƒnW|j dkrr|jjd
ddƒn/tj |j ƒ}|jjd ||j fƒd |_dS)
NcSsZd\}}tr.tjtjƒƒd}ntrPtjtjƒƒd}n||fS)z+Return the current (uid, gid) if available.Nr)NN)rSÚgetpwuidr)ÚgetuidrZÚgetgrgidÚgetgid)ÚnameÚgroupr r r Ú current_ids"s  z)DropPrivileges.start.<locals>.current_idsz"Already running as uid: %r gid: %rzuid/gid not setr7r8zStarted as uid: %r gid: %rzRunning as uid: %r gid: %rzumask already set to: %03oz
umask not setzumask old: %03o, new: %03oT) rLrMrNr r&rSrZr)ÚsetgidÚ setgroupsÚsetuidrO)r
rgZ old_umaskr r r Ústart s0     zDropPrivileges.startéM)rrrrr
rQrVÚpropertyrMrYr\rNr^r`rOrkÚpriorityr r r r rKÞs$           ,rKc@s@eZdZdZdddddZddZde_dS) Ú
Daemonizera¿Daemonize the running script.
Use this with a Web Site Process Bus via::
Daemonizer(bus).subscribe()
When this component finishes, the process is completely decoupled from
the parent environment. Please note that when this component is used,
the return code from the parent process will still be 0 if a startup
error occurs in the forked children. Errors in the initial daemonizing
process still return proper exit codes. Therefore, if you use this
plugin to daemonize, don't use the return code as an accurate indicator
of whether the process fully started. In fact, that return code only
indicates if the process succesfully finished the first fork.
z /dev/nullcCs8tj||ƒ||_||_||_d|_dS)NF)rr
r/ÚstdoutÚstderrrL)r
r r/rprqr r r r
as
   zDaemonizer.__init__cCsY|jr|jjdƒntjƒdkrQ|jjdtjƒddƒntjjƒtj jƒy<t
j ƒ}|dkr‰n|jjdƒt
j dƒWnIt
k
tjƒd}tjdtjd|j|jfƒYnXt
jƒy<t
j ƒ}|dkr8|jjd ƒt
j dƒnWnIt
k
r„tjƒd}tjd
tjd|j|jfƒYnXt
jd ƒt
jdƒt|jd ƒ}t|jd
ƒ}t|j d
ƒ}t
j|jƒtjjƒƒt
j|jƒtjjƒƒt
j|jƒtj jƒƒ|jjdt
jƒƒd|_dS)NzAlready deamonized.ézHThere are %r active threads. Daemonizing now may cause strange failures.r7r8rz
Forking once.z%s: fork #1 failed: (%d) %s
zForking twice.z%s: fork #2 failed: (%d) %s
úrza+zDaemonized to PID: %sT)rLr r&Ú threadingÚ activeCountÚ enumerater$rpÚflushrqr)ÚforkÚ_exitÚOSErrorÚexc_infor ÚargvÚerrnoÚstrerrorÚsetsidÚchdirrOÚopenr/Údup2r0r*)r
ÚpidÚexcÚsiÚsoÚser r r rkisJ  

  
 #
  
 #

zDaemonizer.startéAN)rrrrr
rkrnr r r r roOs
 Croc@sCeZdZdZddZddZde_ddZd S)
ÚPIDFilez!Maintain a PID file via a WSPBus.cCs&tj||ƒ||_d|_dS)NF)rr
ÚpidfilerL)r
r rr r r r
³s zPIDFile.__init__cCs…tjƒ}|jr5|jjd||jfƒnLt|jdƒjtd|dƒƒ|jjd||jfƒd|_dS)NzPID %r already written to %r.Úwbz%s
Úutf8zPID %r written to %r.T) r)r*rLr r&rrÚwriter)r
r„r r r rk¸s    &z
PIDFile.startéFc CsTy+tj|jƒ|jjd|jƒWn"ttfk
rHYnYnXdS)NzPID file removed: %r.)r)Úremoverr r&ÚKeyboardInterruptÚ
SystemExit)r
r r r r Âsz PIDFile.exitN)rrrrr
rkrnr r r r r ¯s
   cs4eZdZdZfddZddZS)ÚPerpetualTimeraA responsive subclass of threading.Timer whose run() method repeats.
Use this timer only when you really need a very interruptible timer;
this checks its 'finished' condition up to 20 times a second, which can
results in pretty high CPU usage
cs2|jddƒ|_tt|ƒj||ŽdS)z:Override parent constructor to allow 'bus' to be provided.r N)Úpopr Úsuperr“r
)r
ÚargsÚkwargs)Ú __class__r r r
ÕszPerpetualTimer.__init__c Cs|jj|jƒ|jjƒr)dSy|j|j|jŽWqtk
r‰|jr|jj d|jddddƒnYqXqdS)Nz,Error in perpetual timer thread function %r.r7r9r:T)
ÚfinishedÚwaitÚintervalÚisSetÚfunctionrr—Ú Exceptionr r&)r
r r r ÚrunÚs
  zPerpetualTimer.run)rrrrr
r r )r˜r r“Ìs r“c@sCeZdZdZgidddZddZddZdS) ÚBackgroundTaska¥A subclass of threading.Thread whose run() method repeats.
Use this class for most repeating tasks. It uses time.sleep() to wait
for each interval, which isn't very responsive; that is, even if you call
self.cancel(), you'll have to wait until the sleep() call finishes before
the thread stops. To compensate, it defaults to being daemonic, which means
it won't delay stopping the whole process.
NcCsStjj|ƒ||_||_||_||_d|_||_d|_ dS)NFT)
ruÚThreadr
r›rrr—Úrunningr Údaemon)r
rrrr—r r r r r
õs      zBackgroundTask.__init__cCs
d|_dS)NF))r
r r r ÚcancelszBackgroundTask.cancelc Csd|_x|jrtj|jƒ|js2dSy|j|j|jŽWq tk
r|jr|jj d|jddddƒnYq Xq WdS)NTz,Error in background task thread function %r.r7r9r:)
ÚtimeÚsleeprrrr—r r&)r
r r r s   
  zBackgroundTask.run)rrrrr