Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
dmapi / usr / lib / python2.7 / dist-packages / dmapi / utils.pyc
Size: Mime:
ó
Éec@sødZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlmZddlmZddlZddlmZddlmZddlmZddlmZ ddl!Z"dd	l#m$Z$dd
l#m%Z%ddl#m&Z&ddl#m'Z'dd
l#m(Z(ddl#m)Z)ddl*Z*ddl+m,Z,ddl-Z.ddl.m/Z/ddl0m1Z1m2Z2m3Z3m4Z4ddl.m5Z5e&j6dƒZ7e.j8j9Z9e j:e;ƒZ<dZ>ej?dƒZ@dZAd]ZBd^ZCd_ZDiaEeFed"ƒrŒejGZHn	ejHZHd#„ZId$eJfd%„ƒYZKd&eKfd'„ƒYZLd(„ZMd)„ZNd*„ZOd+d,„ZPddd-„ZQd.„ZRd/„ZSd0„ZTd1„ZUd2„ZVd3„ZWd4„ZXdd5„ZYejZd6„ƒZ[ejZdd7„ƒZ\ejZd8„ƒZ]dd9„Z^d:eJfd;„ƒYZ_de`d<„Zae`d=„Zbd>„Zcd?„Zdd@„ZedA„ZfdBeJfdC„ƒYZgddDddE„ZhdddF„ZidG„ZjdH„ZkdI„ZldJ„ZmdK„ZndddL„ZodM„ZpddN„ZqdO„ZrdP„ZsdQ„ZtdR„ZudS„ZvddT„ZwdU„Zxe`dV„ZydW„ZzddX„Z{dY„Z|dZ„Z}d[„Z~d\„ZdS(`sUtilities and helper functions.iÿÿÿÿN(t
exceptions(tloading(t	lockutils(tprocessutils(tcontext(tlog(tencodeutils(texcutils(timportutils(tstrutils(t	timeutils(tunits(trange(t	exception(t_t_LEt_LIt_LW(t
safe_utilssosprofiler.profilersdmapi-timage_tmin_ramtmin_disktdisk_formattcontainer_formattmappingstblock_device_mappingtimg_mappingstimg_block_device_mappingtimage_idt
image_nametsizetchecksumtgetfullargspeccCs&tjjrd}n
dtj}|S(Ntsudossudo dmapi-rootwrap %s(tCONFtworkaroundstdisable_rootwraptrootwrap_config(tcmd((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_root_helperUs	
tRootwrapProcessHelpercBseZd„Zd„ZRS(cOstƒ|d<tj||ŽS(Ntroot_helper(R'Rttrycmd(tselfR&tkwargs((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR*^s
cOstƒ|d<tj||ŽS(NR)(R'Rtexecute(R+R&R,((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR-bs
(t__name__t
__module__R*R-(((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR(]s	tRootwrapDaemonHelpercBs>eZiZedƒd„ƒZd„Zd„Zd„ZRS(sdaemon-client-lockcCs]y|j|SWnGtk
rXddlm}|jdd|gƒ}||j|<|SXdS(Niÿÿÿÿ(tclientR!sdmapi-rootwrap-daemon(t_clientstKeyErrort
oslo_rootwrapR1tClient(tclsR%R1t
new_client((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt_get_clientjs

cCs|j|ƒ|_dS(N(R8R1(R+R%((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt__init__uscOs|jdtƒ}y"|j||Ž\}}t}Wn2tjk
rh}dtj|ƒ}}t}nX|r…|r…|r…d}n||fS(Ntdiscard_warningst(tpoptFalseR-RtProcessExecutionErrortsixt	text_typetTrue(R+targsR,R:toutterrtfailedtexn((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR*xs

	c	Osg|D]}t|ƒ^q}|jdtjƒ}|jddƒ}|jddƒ}|jdtƒ}|jddƒ}|jddgƒ}	t}
t|	tƒr¹|	}
dg}	nt|	t	ƒrÔ|	g}	nt
jd	j|ƒƒ}t
jtd
ƒi|d6|d6ƒx÷|dkr	|d8}yátjƒ}t
j|td
ƒ|ƒ|jj||ƒ\}
}}tjƒ|}t
j|di|d6|
d6|d6ƒ|
rÿ|
|	krÿt
j|ƒ}t
j|ƒ}tjd|
d|d|d|ƒ‚n||fSWqtjk
r}|tjksD|tjkr˜|r˜tdƒ}t
j||i|jd6|jd6|jd6|jd6|jd6ƒn|s½t
j|tdƒ|ƒ‚qt
j|tdƒ|ƒ|rtjtj ƒj!ddƒdƒqqXqWdS(Ntloglevelt
log_errorst
process_inputtdelay_on_retrytattemptsitcheck_exit_codeit sHExecuting RootwrapDaemonHelper.execute cmd=[%(cmd)r] kwargs=[%(kwargs)r]R&R,sRunning cmd (subprocess): %ssECMD "%(sanitized_cmd)s" returned: %(return_code)s in %(end_time)0.3fst
sanitized_cmdtreturn_codetend_timet	exit_codetstdouttstderrsS%(desc)r
command: %(cmd)r
exit code: %(code)r
stdout: %(stdout)r
stderr: %(stderr)rtdesctcodes%r failed. Not Retrying.s%r failed. Retrying.iiÈgY@("tstrR<tloggingtDEBUGtNoneRAR=t
isinstancetbooltintR	t
mask_passwordtjointLOGtinfoRttimeRRR1R-RR>tLOG_ALL_ERRORStLOG_FINAL_ERRORtdescriptionR&RQRRRStsleeptrandomtSystemRandomtrandint(R+R&R,tcRGRHRIRJRKRLtignore_exit_codeRNt
start_timet
returncodeRCRDRPtformat((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR-…st
		
	


(R.R/R2tsynchronizedR8R9R*R-(((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR0gs
		
cOscd|krS|jdƒrStjr=ttjƒj||ŽStƒj||ŽSntj||ŽS(s3Convenience wrapper around oslo's execute() method.trun_as_root(tgetR"tuse_rootwrap_daemonR0R%R-R(R(R&R,((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR-Ós	
cOs6dddg}|j|ƒ|j|ƒt||ŽS(s+Convenience wrapper to execute ssh command.tsshs-os
BatchMode=yes(tappendtextendR-(tdestR&R,tssh_cmd((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytssh_executeÞs

cOsZ|jdtƒrJtjr4ttjƒj||ŽStƒj||ŽSntj||ŽS(s2Convenience wrapper around oslo's trycmd() method.Ro(	RpR=R"RqR0R%R*R(R(RBR,((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR*æs	
icCsKd}gt|ƒD]}tjƒj|ƒ^q}d|dj|ƒfS(Nt%01234567890abcdefghijklmnopqrstuvwxyzs%s-%sR;(RRfRgtchoiceR^(ttopicRt
characterst_xtchoices((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytgenerate_uidñs.c	CsR|stj}nd}d|krK|jddƒ\}}t|ƒ}n|dk	r`|}ntjƒ}|dkrttdƒƒ‚n|dkr¦|dkr®d}nt	j	d|d|j
d|jƒ}||krB|j}d|j
kr|d8}d	|j
d}n
|j
d}t	j	d|d|d|ƒ}n|j}d|j
krx|d8}d	|j
d}n
|j
d}t	j	d|d|d|ƒ}n¢|dkri|dkrÇd}nt	j	ddd|d|jƒ}||krAt	j	ddd|d|jdƒ}t	j	ddd|d|jd
ƒ}qHt	j	ddd|d|jdƒ}nß|dkràt	j	d|d|jd|j
d|jƒ}||krÇ|t	j
ddƒ}n|t	j
ddƒ}nh|dkrH|jd|d
dddƒ}||kr/|t	j
ddƒ}n|t	j
ddƒ}n||fS(s§This method gives you the most recently *completed* audit period.

    arguments:
            units: string, one of 'hour', 'day', 'month', 'year'
                    Periods normally begin at the beginning (UTC) of the
                    period unit (So a 'day' period begins at midnight UTC,
                    a 'month' unit on the 1st, a 'year' on Jan, 1)
                    unit string may be appended with an optional offset
                    like so:  'day@18'  This will begin the period at 18:00
                    UTC.  'month@15' starts a monthly period on the 15th,
                    and year@3 begins a yearly one on March 1st.
            before: Give the audit period most recently completed before
                    <timestamp>. Defaults to now.


    returns:  2 tuple of datetimes (begin, end)
              The begin timestamp of this audit period is the same as the
              end of the previous.
    it@itmonthtdaytyearthours,Time period must be hour, day, month or yeariitdaystminutetsecondtmicrosecondthoursN(smonthsdaysyearshour(R"tinstance_usage_audit_periodtsplitR\RYR
tutcnowt
ValueErrorRtdatetimeR€R‚Rt	timedeltatreplace(tunittbeforetoffsettrightnowtendR‚R€tbegin((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytlast_completed_audit_period÷sx				

	

!	!		cCsS|dkst|tjƒr"|St|tjƒsFtj|ƒ}n|jdƒS(s³Try to turn a string into utf-8 if possible.

    The original code was copied from the utf8 function in
    http://github.com/facebook/tornado/blob/master/tornado/escape.py

    sutf-8N(RYRZR?tbinary_typeR@tencode(tvalue((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytutf8Ts
cCsÖyžtj|ƒr|dfS|jdƒdkr_|jdddƒjdƒ\}}||fS|jdƒdkr~|dfS|jdƒ\}}||fSWn1ttjfk
rÑtjt	dƒ|ƒd	SXdS(
sôParses the given server_string and returns a tuple of host and port.
    If it's not a combination of host part and port, the port element
    is an empty string. If the input is invalid expression, return a tuple of
    two empty strings.
    R;s]:iÿÿÿÿt[it:sInvalid server_string: %sN(R;R;(
tnetaddrt
valid_ipv6tfindRRŠRŒtAddrFormatErrorR_terrorR(t
server_strtaddresstport((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytparse_server_stringds
$

cCs%tj|ddƒ}t|jƒƒS(Ntversioni(Rt	IPAddressRVtipv6(R£taddr((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_shortened_ipv6scCs"tj|ddƒ}t|jƒS(NR¦i(Rt	IPNetworkRVtcidr(R£tnet((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_shortened_ipv6_cidr†scCsEy$tj|ƒjdkr#d|SWnttjfk
r@nX|S(sŽTransform ip string to "safe" format.

    Will return ipv4 addresses unchanged, but will nest ipv6 addresses
    inside square brackets.
    is[%s](RR§R¦t	TypeErrorR (tip((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytsafe_ip_format‹scCs$|dkr|Sdt|ƒ|fS(sÌReturns remote path in format acceptable for scp/rsync.

    If host is IPv6 address literal, return '[host]:path', otherwise
    'host:path' is returned.

    If host is None, only path is returned.
    s%s:%sN(RYR±(thosttpath((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytformat_remote_pathšscCstjs
dStjdƒtjr/tj}n	d„}x@tjD]5}|j	dƒ\}}t
j|ƒ}t|ƒt
j|ƒ}xït|jƒƒD]Û\}}t|t
jƒrt
jd||fƒ}xItj||ƒD]2\}	}
t||	|d|||	f|
ƒƒqâWnt|t
jƒr˜t
jd||fƒ}
ttj|||d||f|
ƒƒq˜q˜WqBWdS(sÿDEPRECATED: If the CONF.monkey_patch set as True,
    this function patches a decorator
    for all functions in specified modules.
    You can set decorators for each modules
    using CONF.monkey_patch_modules.
    The format is "Module path:Decorator function".
    Example:
    'dmapi.api.ec2.cloud:dmapi.notifications.notify_decorator'

    Parameters of the decorator is as follows.
    (See dmapi.notifications.notify_decorator)

    name - name of the function
    function - object of the function
    Ns0Monkey patching dmapi is deprecated for removal.cSstj|ƒptj|ƒS(N(tinspecttismethodt
isfunction(tobj((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt	is_methodÀsRœs%s.%ss%s.%s.%s(R"tmonkey_patchR_twarningR?tPY2RµR¶tmonkey_patch_modulesRŠRtimport_classt
__import__tpyclbrt
readmodule_extlisttitemsRZtClasst
getmemberstsetattrtFunctiontsystmodules(R¹tmodule_and_decoratortmoduletdecorator_namet	decoratortmodule_datatkeyR™tclztmethodtfunc((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRº©s*	
		
	$cCsÂd„}t|tjƒrK|jddƒ}tjrK|jdƒ}qKn||ƒ}tjdd|ƒ}tjdd|ƒ}|jƒ}|j	dƒ}|dkr¾|d	k	r¾||ƒS|S(
ssReturn a hostname which conforms to RFC-952 and RFC-1123 specs except
       the length of hostname.

       Window, Linux, and Dnsmasq has different limitation:

       Windows: 255 (net_bios limits to 15, but window will truncate it)
       Linux: 64
       Dnsmasq: 63

       Due to dmapi-network will leverage dnsmasq to set hostname, so we chose
       63.

       cSsEt|ƒdkr=tjtdƒi|d6|d d6ƒn|d S(Ni?sJHostname %(hostname)s is longer than 63, truncate it to %(truncated_name)sthostnamettruncated_name(tlenR_R»R(tname((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyttruncate_hostnameèsslatin-1tignores[ _]t-s[^\w.-]+R;s.-N(RZR?R@R˜tPY3tdecodetretsubtlowertstripRY(RÓtdefault_nameR×((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytsanitize_hostnameÙs		
c
+sîd„‰‡fd†}‡fd†}‡fd†}tƒ}i}xFt|jƒƒD]2\}}||||ƒ||<||||ƒqXWz	dVWdxLt|jƒƒD]8\}}	|	|krÕ|||ƒq­||||	ƒq­WXdS(s4Temporarily set the attr on a particular object to a given value then
    revert when finished.

    One use of this is to temporarily set the read_deleted flag on a context
    object:

        with temporary_mutation(context, read_deleted="yes"):
            do_something_that_needed_deleted_objects()
    cSst|dƒpt|tƒS(Nthas_key(thasattrRZtdict(tthing((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytis_dict_likescs0ˆ|ƒr|j||ƒSt|||ƒSdS(N(Rptgetattr(Råtattrtdefault(Ræ(s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRpscs-ˆ|ƒr|||<nt|||ƒdS(N(RÆ(RåRètval(Ræ(s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt	set_values
cs'ˆ|ƒr||=n
t||ƒdS(N(tdelattr(RåRè(Ræ(s//usr/lib/python2.7/dist-packages/dmapi/utils.pytdeletes
N(tobjectRÂRÃ(
R¸R,RpRëRítNOT_PRESENTt
old_valuesRèt	new_valuet	old_value((Ræs//usr/lib/python2.7/dist-packages/dmapi/utils.pyttemporary_mutations			ccsŽ|dkrtjƒ}ntj|ƒj}||krUtjjj|d|ƒnz	dVWd||kr‰tjjj|d|ƒnXdS(sgTemporarily chown a path.

    :param owner_uid: UID of temporary owner (defaults to current user)
    tuidN(	RYtostgetuidtstattst_uidtdmapitprivsepR³tchown(R³t	owner_uidtorig_uid((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyttemporary_chown6s	cks…|jƒ}d|kr(tj|d<ntj|}z	|VWdytj|ƒWn)tk
r}tj	t
dƒ|ƒnXXdS(NtdirsCould not remove tmpdir: %s(tcopyR"ttempdirttempfiletmkdtemptshutiltrmtreetOSErrorR_R¡R(R,targdictttmpdirte((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRJs	ccsj|sg}nxT|jƒD]F}||kr|j|ƒxt||ƒD]}|VqKW|VqqWdS(s:Walk class hierarchy, yielding most derived classes first.N(t__subclasses__Rstwalk_class_hierarchy(tclazztencounteredtsubclasstsubsubclass((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRYs	
	tUndoManagercBs5eZdZd„Zd„Zd„Zdd„ZRS(siProvides a mechanism to facilitate rolling back a series of actions
    when an exception is raised.
    cCs
g|_dS(N(t
undo_stack(R+((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR9jscCs|jj|ƒdS(N(RRs(R+t	undo_func((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt	undo_withmscCs%xt|jƒD]
}|ƒqWdS(N(treversedR(R+R((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt	_rollbackpscKs:tjƒ(|r&tj||n|jƒWdQXdS(s¦Rollback a series of actions then re-raise the exception.

        .. note:: (sirp) This should only be called within an
                  exception handler.
        N(Rtsave_and_reraise_exceptionR_R
R(R+tmsgR,((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytrollback_and_reraisets
N(R.R/t__doc__R9RRRYR(((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRfs
			cCsž|dkrdg}ndd|g}|dkrF|jdgƒn|r}|dkrad}nd}|j||gƒn|j|ƒtd
||ŒdS(sçFormat a file or block device

    :param fs: Filesystem type (examples include 'swap', 'ext3', 'ext4'
               'btrfs', etc.)
    :param path: Path to file or block device to format
    :param label: Volume label to use
    tswaptmkswaptmkfss-ttext3text4tntfss-Ftmsdostvfats-ns-LRoN(RRR(R R!(RtRsR-(tfsR³tlabelRoRBt	label_opt((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRs	
cCsIi}x<|D]4}|r/|jdƒr/q
n|d||d<q
W|S(NtdeletedR™RÏ(Rp(tmetadatatinclude_deletedtresulttitem((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytmetadata_to_dictšs
cCsIg}x<t|jƒƒD](\}}|jtd|d|ƒƒqW|S(NRÏR™(RÂRÃRsRä(R&R(RÏR™((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytdict_to_metadata£s cCs-t|dtƒr|dSt|dƒSdS(NR&(RZRäR*(tinstance((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt
instance_metaªscCsF|jdƒsiSt|dtƒr.|dSt|ddtƒSdS(Ntsystem_metadataR'(RpRZRäR*RA(R,((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytinstance_sys_meta±s
cs‡fd†}|S(Ncs%tjˆƒ‡‡fd†ƒ}|S(Ncs€tj|ƒ}tj|ƒ\}}}}|sN|sNtˆƒt|ƒkrXˆ|ƒStdi|jd6ˆjd6ƒ‚dS(Ns^Decorated function %(f_name)s does not have the arguments expected by the decorator %(d_name)stf_nametd_name(Rtget_wrapped_functionRµt
getargspectsetR¯R.(tftbase_ft	arg_namestatkwt_default(RBtdec(s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt
_decorator½s$

(t	functoolstwraps(R;R<(RB(R;s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt_decorator_checker¼s!((RBR?((RBs//usr/lib/python2.7/dist-packages/dmapi/utils.pytexpects_func_args»stExceptionHelpercBs eZdZd„Zd„ZRS(srClass to wrap another and translate the ClientExceptions raised by its
    function calls to the actual ones.
    cCs
||_dS(N(t_target(R+ttarget((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR9Õscs4t|j|ƒ‰tjˆƒ‡fd†ƒ}|S(Ncs>yˆ||ŽSWn&tjk
r9}tj|jŒnXdS(N(t	messagingtExpectedExceptionR?treraisetexc_info(RBR,R	(RÒ(s//usr/lib/python2.7/dist-packages/dmapi/utils.pytwrapperÛs(RçRBR=R>(R+RÖRH((RÒs//usr/lib/python2.7/dist-packages/dmapi/utils.pyt__getattr__Øs(R.R/RR9RI(((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRAÐs	icCsût|tjƒsR|d	kr-tdƒ}ntdƒ|}tjd|ƒ‚n|d	krg|}nt|ƒ|kr¬tdƒi|d6|d6}tjd|ƒ‚n|r÷t|ƒ|kr÷tdƒi|d6|d6}tjd|ƒ‚nd	S(
sãCheck the length of specified string
    :param value: the value of the string
    :param name: the name of the string
    :param min_length: the min_length of the string
    :param max_length: the max_length of the string
    s$The input is not a string or unicodes%s is not a string or unicodetmessages?%(name)s has a minimum character requirement of %(min_length)s.RÖt
min_lengths1%(name)s has more than %(max_length)s characters.t
max_lengthN(RZR?tstring_typesRYRR
tInvalidInputRÕ(R™RÖRKRLR((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytcheck_string_lengthäs			cCsOytj||||ƒSWn.tk
rJ}tjdtj|ƒƒ‚nXdS(sAMake sure that value is a valid integer, potentially within range.

    :param value: value of the integer
    :param name: name of the integer
    :param min_value: min_value of the integer
    :param max_value: max_value of the integer
    :returns: integer
    :raise: InvalidInput If value is not a valid integer
    treasonN(R	tvalidate_integerRŒR
RNR?R@(R™RÖt	min_valuet	max_valueR	((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRQs
cCsSts
dStjƒ}d}|rOi|jd6|jƒd6|jƒd6}n|S(Nthmac_keytbase_idt	parent_id(tprofilerRYRpRTtget_base_idtget_id(tproft
trace_info((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt_serialize_profile_infos

csLtjƒ‰tƒ‰tjˆƒ‡‡‡fd†ƒ}tj|||ŽS(smPassthrough method for eventlet.spawn.

    This utility exists so that it can be stubbed for testing without
    interfering with the service spawns.

    It will also grab the context from the threadlocal store and add it to
    the store on the new thread.  This allows for continuity in logging the
    context when using this method to spawn a new thread.
    csBˆdk	rˆjƒnˆr5tr5tjˆnˆ||ŽS(N(RYtupdate_storeRWtinit(RBR,(t_contextRÒt
profiler_info(s//usr/lib/python2.7/dist-packages/dmapi/utils.pytcontext_wrapper-s

(tcommon_contexttget_currentR\R=R>teventlettspawn(RÒRBR,Ra((R_RÒR`s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRe s
	$
csPtjƒ‰tƒ‰tjˆƒ‡‡‡fd†ƒ}tj|||ŽdS(soPassthrough method for eventlet.spawn_n.

    This utility exists so that it can be stubbed for testing without
    interfering with the service spawns.

    It will also grab the context from the threadlocal store and add it to
    the store on the new thread.  This allows for continuity in logging the
    context when using this method to spawn a new thread.
    csFˆdk	rˆjƒnˆr5tr5tjˆnˆ||ŽdS(N(RYR]RWR^(RBR,(R_RÒR`(s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRaGs

N(RbRcR\R=R>Rdtspawn_n(RÒRBR,Ra((R_RÒR`s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRf:s
	$
cCs&t|tjƒstS|jƒdkS(s/Check if a string represents a None value.
    tnone(RZR?RMR=RÞ(Rê((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytis_none_stringTscCs@t}|dk	r<|jƒjƒ}|dkr<t}q<n|S(Ntdisabled(R=RYRßRÞRA(tauto_disk_config_rawtauto_disk_config_disabledtadc_lowered((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytis_auto_disk_config_disabled]scCs(|dkrt|ƒ}n|jdƒS(Ntimage_auto_disk_config(RYR/Rp(R,tsys_meta((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt"get_auto_disk_config_from_instancefscCs
|jdƒS(Ntauto_disk_config(Rp(timage_properties((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt%get_auto_disk_config_from_image_propslscCsi}td}xdt|jdiƒjƒƒD]D\}}|tkrMq/nttj|ƒdƒ}||||<q/WxtD]…}|j|ƒ}|dkrã|rã|jdƒdkrÇ|d}qãt	|pÓd|dƒ}n|dkrõq~n||||<q~W|S(	Ns%st
propertiesiÿRRtvhdtroot_gbi(tSM_IMAGE_PROP_PREFIXRÂRpRÃtSM_SKIP_KEYSt
safe_truncateR?R@tSM_INHERITABLE_KEYStmaxRY(t
image_metatflavortsystem_metat
prefix_formatRÏR™Rñ((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_system_metadata_from_imageps"
+

cCsÊi}i}t|tƒs0t|dtƒ}nx‰t|jƒƒD]u\}}|dkraqCn|jtƒrƒ|t	tƒ}n|t
kr•qCn|tkr®|||<qC|||<qCW||d<|S(NR'Rt(RZRäR*RARÂRÃRYt
startswithRwRÕRxRz(R~R|RtRÏR™((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_image_from_system_metadataŒs 

cCs›tj|jdiƒƒ}i|d6}|jddƒtj|d<xEtD]=}|j|dƒ}|d	krLt|p|dƒ||<qLqLWd|d<|S(
Ntvolume_image_metadataRtRiRRtactivetstatus(Rsmin_disk(RRpRtGitVIM_IMAGE_ATTRIBUTESR<RYR\(tvolumeRtR|RèRê((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_image_metadata_from_volume©s


cCs7t|tjƒr$|jdƒ}ntj|ƒjƒS(sƒReturns string that represents MD5 hash of base_str (in hex format).

    If base_str is a Unicode string, encode it to UTF-8.
    sutf-8(RZR?R@R˜thashlibtmd5t	hexdigest(tbase_str((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_hash_strÂscCs7t|tjƒr$|jdƒ}ntj|ƒjƒS(s@Returns string that represents sha256 hash of base_str (in hex format).

    sha1 and md5 are known to be breakable, so sha256 is a better option
    when the hash is being used for security purposes. If hashing passwords
    or anything else that needs to be retained for a long period a salted
    hash is better.
    sutf-8(RZR?R@R˜RŠtsha256RŒ(R((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_sha256_strÌscCs.t|ƒ}tjs*tj|dƒ}n|S(s½Returns a string representation of an object converted to unicode.

    In the case of python 3, this just returns the repr() of the object,
    else it converts the repr() to unicode.
    sutf-8(treprR?RÚR@(R¸tobj_repr((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_obj_repr_unicodeÙs	cst|tƒr|g}n‡fd†‰d„‰‡‡fd†}g}x¶|D]®}ˆdkrŽ|d	kryd}n|j|iƒ}nx |D]}||||ƒ}q•WxLt|jƒƒD]8\}	}
|ji|	d6|
d6ˆ|ƒdˆ6ƒqÄWqRW|S(
smGet all metadata for a list of resources after filtering.

    Search_filts is a list of dictionaries, where the values in the dictionary
    can be string or regex string, or a list of strings/regex strings.

    Let's call a dict a 'filter block' and an item in the dict
    a 'filter'. A tag is returned if it matches ALL the filters in
    a filter block. If more than one values are specified for a
    filter, a tag is returned if it matches ATLEAST ONE value of the filter. If
    more than one filter blocks are specified, the tag should match ALL the
    filter blocks.

    For example:

        search_filts = [{'key': ['key1', 'key2'], 'value': 'val1'},
                        {'value': 'val2'}]

    The filter translates to 'match any tag for which':
        ((key=key1 AND value=val1) OR (key=key2 AND value=val1)) AND
            (value=val2)

    This example filter will never match a tag.

        :param resource_type: The resource type as a string, e.g. 'instance'
        :param resource_list: List of resource objects
        :param search_filts: Filters to filter metadata to be returned. Can be
            dict (e.g. {'key': 'env', 'value': 'prod'}, or a list of dicts
            (e.g. [{'key': 'env'}, {'value': 'beta'}]. Note that the values
            of the dict can be regular expressions.
        :param metadata_type: Provided to search for a specific metadata type
            (e.g. 'system_metadata')

        :returns: List of dicts where each dict is of the form {'key':
            'somekey', 'value': 'somevalue', 'instance_id':
            'some-instance-uuid-aaa'} if resource_type is 'instance'.
    csˆdkr|jdƒSdS(NR,tuuid(Rp(tresource(t
resource_type(s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt_get_idscSsDt|tƒr|g}ntg|D]}tj||ƒ^q%ƒS(N(RZRVtanyRÜtmatch(tpattern_listtstringtpattern((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt
_match_anysc	sí|jdgƒ}|jdgƒ}|jdgƒ}i}|rXˆ|ƒ|krXiSxŽt|jƒƒD]z\}}|r©|r©ˆ||ƒr©ˆ||ƒr©qkn2|r¿ˆ||ƒsk|rۈ||ƒrÛqkn|||<qkW|S(Ntresource_idRÏR™(RpRÂRÃ(	R•tsearch_filttinput_metadatatidstkeys_filtert
values_filtertoutput_metadatatktv(R—R(s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt_filter_metadatas R,R&RÏR™s%s_idN(RZRäRYRpRÂRÃRs(R–t
resource_listtsearch_filtst
metadata_typeR§tformatted_metadata_listtresR&tfiltR¥R¦((R—RR–s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt#filter_and_format_resource_metadataås"'	
	
cCsatj|ƒ| }t}xA|s\ytj|ƒ}t}Wqtk
rX|d }qXqW|S(spSafely truncates unicode strings such that their encoded length is
    no greater than the length provided.
    iÿÿÿÿ(Rtsafe_encodeR=tsafe_decodeRAtUnicodeDecodeError(R™tlengthtb_valuet	decode_oktu_value((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRyGs	

cCs²|rt|ƒnt}tjj|ƒ}tj|iƒ}|s\||jddƒkr¤tj	d|ƒt
|ƒ}|jƒ|d<WdQX||d<t}n||dfS(s¿Read from a file if it has been modified.

    :param force_reload: Whether to reload the file.
    :returns: A tuple with a boolean specifying if the data is fresh
              or not.
    tmtimeisReloading cached file %stdataN(
tdelete_cached_fileR=RõR³tgetmtimet_FILE_CACHEt
setdefaultRpR_tdebugtopentreadRA(tfilenametforce_reloadtreloadedR¶t
cache_infotfap((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytread_cached_file[s	

	cCs|tkrt|=ndS(sLDelete cached file if present.

    :param filename: filename to delete
    N(Rº(R¿((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyR¸tscCse|stjƒ}n|jdƒ}|jr?|jjdƒnd}||dkrZdn|7}|S(ssCurrent time as ISO string,
    as timeutils.isotime() is deprecated

    :returns: Current time in ISO format
    s%Y-%m-%dT%H:%M:%StUTCs	UTC+00:00tZN(sUTCs	UTC+00:00(R
R‹tstrftimettzinfottznameRY(tattdate_stringttz((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytisotimes!cCs
|jdƒS(Ns%Y-%m-%dT%H:%M:%S.%f(RÇ(RÊ((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytstrtimescCsÑ|jr|jS|jdkrGy|jƒjSWqGtk
rCqGXny|jƒSWntjk
rknXt|j	ƒ}x<|D]4}||_	y|jƒSWq‚tjk
rµq‚Xq‚Wtjd|ƒ‚dS(sñGet the endpoint URL represented by a keystoneauth1 Adapter.

    This method is equivalent to what

        ksa_adapter.get_endpoint()

    should do, if it weren't for a panoply of bugs.

    :param ksa_adapter: keystoneauth1.adapter.Adapter, appropriately set up
                        with an endpoint_override; or service_type, interface
                        (list) and auth/service_catalog.
    :return: String endpoint URL.
    :raise EndpointNotFound: If endpoint discovery fails.
    timagesICould not find requested endpoint for any of the following interfaces: %sN(
tendpoint_overridetservice_typetget_endpoint_datatcatalog_urltAttributeErrortget_endpointtks_exctEndpointNotFoundRÂt	interface(tksa_adaptert
interfacesRØ((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyRÕ‘s*	


	c	Cs×ttdƒs tjdƒtStjj|dƒ}t}d}z>ywtj	|tj
tjBtjBƒ}d}t
j
d|ƒ}|jd|ƒtj||ƒtjdi|d6ƒWnÀtk
r:}|jtjkrtjd	i|d6|d
6ƒt}q~tjƒ#tjdi|d6|d
6ƒWdQXnDtk
r}}tjƒ#tjdi|d6|d
6ƒWdQXnXWd|dk	ržtj|ƒnytj|ƒWn tk
rÑ}tj|ƒnXX|S(NtO_DIRECTs/This python runtime does not support direct I/Os.directio.testiiÿÿÿÿtxs#Path '%(path)s' supports direct I/OR³s5Path '%(path)s' does not support direct I/O: '%(ex)s'texs7Error on '%(path)s' while checking direct I/O: '%(ex)s'(RãRõR_R¼R=R³R^RARYR½tO_CREATtO_WRONLYRÛtmmaptwriteRterrnotEINVALRRR¡t	ExceptiontclosetunlinkR
(tdirpathttestfilethasDirectIOtfdt
align_sizetmR	RÝ((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytsupports_direct_ioÆsF
#			
	
	#
cCs/tjrtj|ƒdStj|ƒdSdS(Ni(R?R¼RµR3R (RÒ((s//usr/lib/python2.7/dist-packages/dmapi/utils.pytget_func_valid_keysös	(smin_ramsmin_disksdisk_formatR(RRRR(RRssizeRRsdisk_formatsmin_ramsmin_disk(€Rt
contextlibRRRâR=RŠRµRàRõRÀRfRÜRRÈRRaRdt
keystoneauth1RRÖRt
ks_loadingRtoslo_concurrencyRRtoslo_contextRRbtoslo_logRRWtoslo_messagingRDt
oslo_utilsRRRR	R
RR?t	six.movesRt
dmapi.confRùR
t
dmapi.i18nRRRRRt
try_importRWtconfR"t	getLoggerR.R_RYt_IS_NEUTRONtsynchronized_with_prefixRnRwRzRxR‡RºRãR R3R'RîR(R0R-RwR*R~R–RšR¥RªR®R±R´RºRátcontextmanagerRóRþRRRR=RR*R+R-R/R@RARORQR\ReRfRhRmRpRsR€R‚R‰RŽRR“R®RyRÄR¸RÍRÎRÕRíRî(((s//usr/lib/python2.7/dist-packages/dmapi/utils.pyt<module>sÒ"		
l			]							0(5
				
												
	
	
a				5	0