<s> """<STR_LIT>""" <EOL> from pipes import quote <EOL> import os <EOL> from fabric . api import ( <EOL> abort , <EOL> env , <EOL> hide , <EOL> run , <EOL> settings , <EOL> sudo , <EOL> warn , <EOL> ) <EOL> from fabric . contrib . files import upload_template as _upload_template <EOL> from fabric . contrib . files import exists <EOL> from fabtools . utils import run_as_root <EOL> def is_file ( path , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' ) , warn_only = True ) : <EOL> return func ( '<STR_LIT>' % locals ( ) ) . succeeded <EOL> def is_dir ( path , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' ) , warn_only = True ) : <EOL> return func ( '<STR_LIT>' % locals ( ) ) . succeeded <EOL> def is_link ( path , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' ) , warn_only = True ) : <EOL> return func ( '<STR_LIT>' % locals ( ) ) . succeeded <EOL> def owner ( path , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' ) , warn_only = True ) : <EOL> result = func ( '<STR_LIT>' % locals ( ) ) <EOL> if result . failed and '<STR_LIT>' in result : <EOL> return func ( '<STR_LIT>' % locals ( ) ) <EOL> else : <EOL> return result <EOL> def group ( path , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' ) , warn_only = True ) : <EOL> result = func ( '<STR_LIT>' % locals ( ) ) <EOL> if result . failed and '<STR_LIT>' in result : <EOL> return func ( '<STR_LIT>' % locals ( ) ) <EOL> else : <EOL> return result <EOL> def mode ( path , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' ) , warn_only = True ) : <EOL> result = func ( '<STR_LIT>' % locals ( ) ) <EOL> if result . failed and '<STR_LIT>' in result : <EOL> return func ( '<STR_LIT>' % locals ( ) ) <EOL> else : <EOL> return result <EOL> def umask ( use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> return func ( '<STR_LIT>' ) <EOL> def upload_template ( filename , destination , context = None , use_jinja = False , <EOL> template_dir = None , use_sudo = False , backup = True , <EOL> mirror_local_mode = False , mode = None , <EOL> mkdir = False , chown = False , user = None ) : <EOL> """<STR_LIT>""" <EOL> if mkdir : <EOL> remote_dir = os . path . dirname ( destination ) <EOL> if use_sudo : <EOL> sudo ( '<STR_LIT>' % quote ( remote_dir ) , user = user ) <EOL> else : <EOL> run ( '<STR_LIT>' % quote ( remote_dir ) ) <EOL> _upload_template ( <EOL> filename = filename , <EOL> destination = destination , <EOL> context = context , <EOL> use_jinja = use_jinja , <EOL> template_dir = template_dir , <EOL> use_sudo = use_sudo , <EOL> backup = backup , <EOL> mirror_local_mode = mirror_local_mode , <EOL> mode = mode , <EOL> ) <EOL> if chown : <EOL> if user is None : <EOL> user = env . user <EOL> run_as_root ( '<STR_LIT>' % ( user , quote ( destination ) ) ) <EOL> def md5sum ( filename , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> warn_only = True ) : <EOL> if exists ( u'<STR_LIT>' ) : <EOL> res = func ( u'<STR_LIT>' % locals ( ) ) <EOL> elif exists ( u'<STR_LIT>' ) : <EOL> res = func ( u'<STR_LIT>' % locals ( ) ) <EOL> elif exists ( u'<STR_LIT>' ) : <EOL> res = func ( u'<STR_LIT>' % locals ( ) ) <EOL> elif exists ( u'<STR_LIT>' ) : <EOL> res = func ( u'<STR_LIT>' % locals ( ) ) <EOL> else : <EOL> md5sum = func ( u'<STR_LIT>' ) <EOL> md5 = func ( u'<STR_LIT>' ) <EOL> if exists ( md5sum ) : <EOL> res = func ( '<STR_LIT>' % locals ( ) ) <EOL> elif exists ( md5 ) : <EOL> res = func ( '<STR_LIT>' % locals ( ) ) <EOL> else : <EOL> abort ( '<STR_LIT>' ) <EOL> if res . succeeded : <EOL> parts = res . split ( ) <EOL> _md5sum = len ( parts ) > <NUM_LIT:0> and parts [ <NUM_LIT:0> ] or None <EOL> else : <EOL> warn ( res ) <EOL> _md5sum = None <EOL> return _md5sum <EOL> class watch ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filenames , callback = None , use_sudo = False ) : <EOL> if isinstance ( filenames , basestring ) : <EOL> self . filenames = [ filenames ] <EOL> else : <EOL> self . filenames = filenames <EOL> self . callback = callback <EOL> self . use_sudo = use_sudo <EOL> self . digest = dict ( ) <EOL> self . changed = False <EOL> def __enter__ ( self ) : <EOL> with settings ( hide ( '<STR_LIT>' ) ) : <EOL> for filename in self . filenames : <EOL> self . digest [ filename ] = md5sum ( filename , self . use_sudo ) <EOL> return self <EOL> def __exit__ ( self , type , value , tb ) : <EOL> for filename in self . filenames : <EOL> if md5sum ( filename , self . use_sudo ) != self . digest [ filename ] : <EOL> self . changed = True <EOL> break <EOL> if self . changed and self . callback : <EOL> self . callback ( ) <EOL> def uncommented_lines ( filename , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = run_as_root if use_sudo else run <EOL> res = func ( '<STR_LIT>' % quote ( filename ) , quiet = True ) <EOL> if res . succeeded : <EOL> return [ line for line in res . splitlines ( ) <EOL> if line and not line . startswith ( '<STR_LIT:#>' ) ] <EOL> else : <EOL> return [ ] <EOL> def getmtime ( path , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> return int ( func ( '<STR_LIT>' % locals ( ) ) . strip ( ) ) <EOL> def copy ( source , destination , recursive = False , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> options = '<STR_LIT>' if recursive else '<STR_LIT>' <EOL> func ( '<STR_LIT>' . format ( options , quote ( source ) , quote ( destination ) ) ) <EOL> def move ( source , destination , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> func ( '<STR_LIT>' . format ( quote ( source ) , quote ( destination ) ) ) <EOL> def symlink ( source , destination , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> func ( '<STR_LIT>' . format ( quote ( source ) , quote ( destination ) ) ) <EOL> def remove ( path , recursive = False , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and run_as_root or run <EOL> options = '<STR_LIT>' if recursive else '<STR_LIT>' <EOL> func ( '<STR_LIT>' . format ( options , quote ( path ) ) ) </s>
