<s> from __future__ import absolute_import <EOL> import contextlib <EOL> import errno <EOL> import logging <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import time <EOL> import salt . utils <EOL> import salt . modules . selinux <EOL> from salt . exceptions import CommandExecutionError , FileLockError , MinionError <EOL> from salt . ext import six <EOL> log = logging . getLogger ( __name__ ) <EOL> def recursive_copy ( source , dest ) : <EOL> '''<STR_LIT>''' <EOL> for root , _ , files in os . walk ( source ) : <EOL> path_from_source = root . replace ( source , '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) <EOL> target_directory = os . path . join ( dest , path_from_source ) <EOL> if not os . path . exists ( target_directory ) : <EOL> os . makedirs ( target_directory ) <EOL> for name in files : <EOL> file_path_from_source = os . path . join ( source , path_from_source , name ) <EOL> target_path = os . path . join ( target_directory , name ) <EOL> shutil . copyfile ( file_path_from_source , target_path ) <EOL> def copyfile ( source , dest , backup_mode = '<STR_LIT>' , cachedir = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if not os . path . isfile ( source ) : <EOL> raise IOError ( <EOL> '<STR_LIT>' . format ( source ) <EOL> ) <EOL> if not os . path . isdir ( os . path . dirname ( dest ) ) : <EOL> raise IOError ( <EOL> '<STR_LIT>' . format ( dest ) <EOL> ) <EOL> bname = os . path . basename ( dest ) <EOL> dname = os . path . dirname ( os . path . abspath ( dest ) ) <EOL> tgt = salt . utils . mkstemp ( prefix = bname , dir = dname ) <EOL> shutil . copyfile ( source , tgt ) <EOL> bkroot = '<STR_LIT>' <EOL> if cachedir : <EOL> bkroot = os . path . join ( cachedir , '<STR_LIT>' ) <EOL> if backup_mode == '<STR_LIT>' or backup_mode == '<STR_LIT>' and bkroot : <EOL> if os . path . exists ( dest ) : <EOL> salt . utils . backup_minion ( dest , bkroot ) <EOL> if backup_mode == '<STR_LIT>' or backup_mode == '<STR_LIT>' and bkroot : <EOL> pass <EOL> fstat = None <EOL> if not salt . utils . is_windows ( ) : <EOL> try : <EOL> fstat = os . stat ( dest ) <EOL> except OSError : <EOL> pass <EOL> shutil . move ( tgt , dest ) <EOL> if fstat is not None : <EOL> os . chown ( dest , fstat . st_uid , fstat . st_gid ) <EOL> os . chmod ( dest , fstat . st_mode ) <EOL> rcon = salt . utils . which ( '<STR_LIT>' ) <EOL> if rcon : <EOL> policy = False <EOL> try : <EOL> policy = salt . modules . selinux . getenforce ( ) <EOL> except ( ImportError , CommandExecutionError ) : <EOL> pass <EOL> if policy == '<STR_LIT>' : <EOL> with salt . utils . fopen ( os . devnull , '<STR_LIT:w>' ) as dev_null : <EOL> cmd = [ rcon , dest ] <EOL> subprocess . call ( cmd , stdout = dev_null , stderr = dev_null ) <EOL> if os . path . isfile ( tgt ) : <EOL> try : <EOL> os . remove ( tgt ) <EOL> except Exception : <EOL> pass <EOL> def rename ( src , dst ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> os . rename ( src , dst ) <EOL> except OSError as exc : <EOL> if exc . errno != errno . EEXIST : <EOL> raise <EOL> try : <EOL> os . remove ( dst ) <EOL> except OSError as exc : <EOL> if exc . errno != errno . ENOENT : <EOL> raise MinionError ( <EOL> '<STR_LIT>' . format ( <EOL> dst , <EOL> exc . strerror <EOL> ) <EOL> ) <EOL> os . rename ( src , dst ) <EOL> def process_read_exception ( exc , path ) : <EOL> '''<STR_LIT>''' <EOL> if exc . errno == errno . ENOENT : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( path ) ) <EOL> elif exc . errno == errno . EACCES : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( path ) <EOL> ) <EOL> else : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' . format ( <EOL> exc . errno , path , exc . strerror <EOL> ) <EOL> ) <EOL> @ contextlib . contextmanager <EOL> def wait_lock ( path , lock_fn = None , timeout = <NUM_LIT:5> , sleep = <NUM_LIT:0.1> , time_start = None ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( path , six . string_types ) : <EOL> raise FileLockError ( '<STR_LIT>' ) <EOL> if lock_fn is None : <EOL> lock_fn = path + '<STR_LIT>' <EOL> if time_start is None : <EOL> time_start = time . time ( ) <EOL> obtained_lock = False <EOL> def _raise_error ( msg , race = False ) : <EOL> '''<STR_LIT>''' <EOL> raise FileLockError ( msg , time_start = time_start ) <EOL> try : <EOL> if os . path . exists ( lock_fn ) and not os . path . isfile ( lock_fn ) : <EOL> _raise_error ( <EOL> '<STR_LIT>' . format ( lock_fn ) <EOL> ) <EOL> open_flags = os . O_CREAT | os . O_EXCL | os . O_WRONLY <EOL> while time . time ( ) - time_start < timeout : <EOL> try : <EOL> fh_ = os . open ( lock_fn , open_flags ) <EOL> except ( IOError , OSError ) as exc : <EOL> if exc . errno != errno . EEXIST : <EOL> _raise_error ( <EOL> '<STR_LIT>' <EOL> . format ( exc . errno , lock_fn , exc . strerror ) <EOL> ) <EOL> log . trace ( <EOL> '<STR_LIT>' , lock_fn , sleep <EOL> ) <EOL> time . sleep ( sleep ) <EOL> else : <EOL> with os . fdopen ( fh_ , '<STR_LIT:w>' ) : <EOL> pass <EOL> log . trace ( '<STR_LIT>' , lock_fn ) <EOL> obtained_lock = True <EOL> yield <EOL> break <EOL> else : <EOL> _raise_error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( timeout , lock_fn ) <EOL> ) <EOL> except FileLockError : <EOL> raise <EOL> except Exception as exc : <EOL> _raise_error ( <EOL> '<STR_LIT>' . format ( <EOL> lock_fn , <EOL> exc <EOL> ) <EOL> ) <EOL> finally : <EOL> if obtained_lock : <EOL> os . remove ( lock_fn ) <EOL> log . trace ( '<STR_LIT>' , path , lock_fn ) </s>
