<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import os <EOL> import stat <EOL> import salt . ext . six as six <EOL> import salt . utils <EOL> log = logging . getLogger ( __name__ ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> return salt . utils . is_linux ( ) <EOL> def attr ( key , value = None ) : <EOL> '''<STR_LIT>''' <EOL> key = target ( key ) <EOL> if key is False : <EOL> return False <EOL> elif os . path . isdir ( key ) : <EOL> return key <EOL> elif value is not None : <EOL> return write ( key , value ) <EOL> else : <EOL> return read ( key ) <EOL> def write ( key , value ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> key = target ( key ) <EOL> log . trace ( '<STR_LIT>' . format ( value , key ) ) <EOL> with salt . utils . fopen ( key , '<STR_LIT:w>' ) as twriter : <EOL> twriter . write ( '<STR_LIT>' . format ( value ) ) <EOL> return True <EOL> except : <EOL> return False <EOL> def read ( key , root = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( key , six . string_types ) : <EOL> res = { } <EOL> for akey in key : <EOL> ares = read ( os . path . join ( root , akey ) ) <EOL> if ares is not False : <EOL> res [ akey ] = ares <EOL> return res <EOL> key = target ( os . path . join ( root , key ) ) <EOL> if key is False : <EOL> return False <EOL> elif os . path . isdir ( key ) : <EOL> keys = interfaces ( key ) <EOL> result = { } <EOL> for subkey in keys [ '<STR_LIT:r>' ] + keys [ '<STR_LIT>' ] : <EOL> subval = read ( os . path . join ( key , subkey ) ) <EOL> if subval is not False : <EOL> subkeys = subkey . split ( '<STR_LIT:/>' ) <EOL> subkey = subkeys . pop ( ) <EOL> subresult = result <EOL> if len ( subkeys ) : <EOL> for skey in subkeys : <EOL> if skey not in subresult : <EOL> subresult [ skey ] = { } <EOL> subresult = subresult [ skey ] <EOL> subresult [ subkey ] = subval <EOL> return result <EOL> else : <EOL> try : <EOL> log . trace ( '<STR_LIT>' . format ( key ) ) <EOL> with os . fdopen ( os . open ( key , os . O_RDONLY | os . O_NONBLOCK ) ) as treader : <EOL> val = treader . read ( ) . strip ( ) <EOL> if not val : <EOL> return False <EOL> try : <EOL> val = int ( val ) <EOL> except : <EOL> try : <EOL> val = float ( val ) <EOL> except : <EOL> pass <EOL> return val <EOL> except : <EOL> return False <EOL> def target ( key , full = True ) : <EOL> '''<STR_LIT>''' <EOL> if not key . startswith ( '<STR_LIT>' ) : <EOL> key = os . path . join ( '<STR_LIT>' , key ) <EOL> key = os . path . realpath ( key ) <EOL> if not os . path . exists ( key ) : <EOL> log . debug ( '<STR_LIT>' . format ( key ) ) <EOL> return False <EOL> elif full : <EOL> return key <EOL> else : <EOL> return os . path . basename ( key ) <EOL> def interfaces ( root ) : <EOL> '''<STR_LIT>''' <EOL> root = target ( root ) <EOL> if root is False or not os . path . isdir ( root ) : <EOL> log . error ( '<STR_LIT>' . format ( root ) ) <EOL> return False <EOL> readwrites = [ ] <EOL> reads = [ ] <EOL> writes = [ ] <EOL> for path , _ , files in os . walk ( root , followlinks = False ) : <EOL> for afile in files : <EOL> canpath = os . path . join ( path , afile ) <EOL> if not os . path . isfile ( canpath ) : <EOL> continue <EOL> stat_mode = os . stat ( canpath ) . st_mode <EOL> is_r = bool ( stat . S_IRUSR & stat_mode ) <EOL> is_w = bool ( stat . S_IWUSR & stat_mode ) <EOL> relpath = os . path . relpath ( canpath , root ) <EOL> if is_w : <EOL> if is_r : <EOL> readwrites . append ( relpath ) <EOL> else : <EOL> writes . append ( relpath ) <EOL> elif is_r : <EOL> reads . append ( relpath ) <EOL> else : <EOL> log . warning ( '<STR_LIT>' . format ( canpath ) ) <EOL> return { <EOL> '<STR_LIT:r>' : reads , <EOL> '<STR_LIT:w>' : writes , <EOL> '<STR_LIT>' : readwrites <EOL> } </s>
