<s> from functools import wraps <EOL> from glob import glob <EOL> from hashlib import sha1 <EOL> import os , shutil , sys , tempfile <EOL> from django . template . loader import render_to_string <EOL> from fabric . state import env <EOL> from fabric . operations import run , sudo , put <EOL> from fabric . context_managers import cd , settings , hide <EOL> from fabric . contrib . files import exists <EOL> from fabric . contrib . project import rsync_project <EOL> def _backup_file ( path ) : <EOL> """<STR_LIT>""" <EOL> backup_base = '<STR_LIT>' <EOL> backup_path = '<STR_LIT>' . join ( [ backup_base , path ] ) <EOL> if not exists ( backup_path ) : <EOL> directory = '<STR_LIT>' . join ( [ backup_base , os . path . split ( path ) [ <NUM_LIT:0> ] ] ) <EOL> sudo ( '<STR_LIT>' % directory ) <EOL> sudo ( '<STR_LIT>' % ( path , backup_path ) ) <EOL> def _restore_file ( path , delete_backup = True ) : <EOL> """<STR_LIT>""" <EOL> backup_base = '<STR_LIT>' <EOL> backup_path = '<STR_LIT>' . join ( [ backup_base , path ] ) <EOL> if exists ( backup_path ) : <EOL> if delete_backup : <EOL> sudo ( '<STR_LIT>' % ( backup_path , path ) ) <EOL> else : <EOL> sudo ( '<STR_LIT>' % ( backup_path , path ) ) <EOL> def _get_local_files ( local_dir , pattern = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> local_files = { } <EOL> if pattern : <EOL> cwd = os . getcwd ( ) <EOL> os . chdir ( local_dir ) <EOL> patterns = pattern . split ( '<STR_LIT:|>' ) <EOL> local_list = set ( [ ] ) <EOL> for p in patterns : local_list = local_list | set ( glob ( p ) ) <EOL> for path in local_list : <EOL> dir , file = os . path . split ( path ) <EOL> if os . path . isfile ( path ) : <EOL> local_files [ dir ] = local_files . get ( dir , [ ] ) + [ file ] <EOL> elif os . path . isdir ( path ) : <EOL> local_files [ file ] = local_files . get ( dir , [ ] ) <EOL> os . chdir ( cwd ) <EOL> return local_files <EOL> def _stage_local_files ( local_dir , local_files = { } ) : <EOL> """<STR_LIT>""" <EOL> staging_dir = os . path . join ( tempfile . mkdtemp ( ) , os . path . basename ( local_dir ) ) <EOL> os . mkdir ( staging_dir ) <EOL> for root , dirs , files in os . walk ( local_dir ) : <EOL> relative_tree = root . replace ( local_dir , '<STR_LIT>' ) <EOL> if relative_tree : <EOL> relative_tree = relative_tree [ <NUM_LIT:1> : ] <EOL> if local_files : <EOL> files = local_files . get ( relative_tree , [ ] ) <EOL> for file in files : <EOL> if relative_tree : <EOL> filepath = os . path . join ( relative_tree , file ) <EOL> if not os . path . exists ( os . path . join ( staging_dir , relative_tree ) ) : <EOL> os . mkdir ( os . path . join ( staging_dir , relative_tree ) ) <EOL> else : filepath = file <EOL> shutil . copy2 ( os . path . join ( root , file ) , os . path . join ( staging_dir , filepath ) ) <EOL> return staging_dir <EOL> def deploy_files ( local_dir , remote_dir , pattern = '<STR_LIT>' , rsync_exclude = [ '<STR_LIT>' , '<STR_LIT>' ] , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> if local_dir [ - <NUM_LIT:1> ] == os . sep : local_dir = local_dir [ : - <NUM_LIT:1> ] <EOL> if remote_dir [ - <NUM_LIT:1> ] == '<STR_LIT:/>' : remote_dir = remote_dir [ : - <NUM_LIT:1> ] <EOL> created_list = [ ] <EOL> staging_dir = local_dir <EOL> local_files = _get_local_files ( local_dir , pattern ) <EOL> if local_files : staging_dir = _stage_local_files ( local_dir , local_files ) <EOL> remote_staging_dir = '<STR_LIT>' % env . user <EOL> if not exists ( remote_staging_dir ) : <EOL> run ( '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , remote_staging_dir ] ) ) . split ( '<STR_LIT:\n>' ) <EOL> created_list = [ remote_staging_dir ] <EOL> rsync_project ( local_dir = staging_dir , remote_dir = remote_staging_dir , exclude = rsync_exclude , delete = True ) <EOL> created_dir_list = mkdirs ( remote_dir , use_sudo ) <EOL> if not os . listdir ( staging_dir ) : return created_list <EOL> func = use_sudo and sudo or run <EOL> remote_base_path = '<STR_LIT:/>' . join ( [ remote_staging_dir , os . path . basename ( local_dir ) , '<STR_LIT:*>' ] ) <EOL> copy_file_list = func ( '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , remote_base_path , remote_dir ] ) ) . split ( '<STR_LIT:\n>' ) <EOL> if copy_file_list [ <NUM_LIT:0> ] : created_list += [ file . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:2> ] [ <NUM_LIT:1> : - <NUM_LIT:1> ] for file in copy_file_list if file ] <EOL> if staging_dir < > local_dir : <EOL> shutil . rmtree ( staging_dir , ignore_errors = True ) <EOL> return created_list <EOL> def mkdirs ( remote_dir , use_sudo = False ) : <EOL> """<STR_LIT>""" <EOL> func = use_sudo and sudo or run <EOL> result = func ( '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , remote_dir ] ) ) . split ( '<STR_LIT:\n>' ) <EOL> if result [ <NUM_LIT:0> ] : result = [ dir . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:3> ] [ <NUM_LIT:1> : - <NUM_LIT:1> ] for dir in result if dir ] <EOL> return result <EOL> def upload_template ( filename , destination , context = { } , use_sudo = False , backup = True , modified_only = False ) : <EOL> """<STR_LIT>""" <EOL> basename = os . path . basename ( filename ) <EOL> text = render_to_string ( filename , context ) <EOL> func = use_sudo and sudo or run <EOL> if modified_only : <EOL> hashfile_dir , hashfile = os . path . split ( destination ) <EOL> hashfile_dir = '<STR_LIT>' . join ( [ '<STR_LIT>' , hashfile_dir ] ) <EOL> hashfile = '<STR_LIT>' % hashfile <EOL> hashfile_path = os . path . join ( hashfile_dir , hashfile ) <EOL> hashed = sha1 ( text ) . hexdigest ( ) <EOL> if hashfile : <EOL> if not exists ( hashfile_dir ) : sudo ( '<STR_LIT>' % hashfile_dir ) <EOL> sudo ( '<STR_LIT>' % hashfile_path ) <EOL> previous_hashed = sudo ( '<STR_LIT>' % hashfile_path ) . strip ( ) <EOL> if previous_hashed == hashed : <EOL> return False <EOL> else : sudo ( '<STR_LIT>' % ( hashed , hashfile_path ) ) <EOL> temp_destination = '<STR_LIT>' + basename <EOL> tempfile_fd , tempfile_name = tempfile . mkstemp ( ) <EOL> output = open ( tempfile_name , "<STR_LIT>" ) <EOL> output . write ( text ) <EOL> output . close ( ) <EOL> put ( tempfile_name , temp_destination ) <EOL> os . close ( tempfile_fd ) <EOL> os . remove ( tempfile_name ) <EOL> if backup : <EOL> to_backup = destination <EOL> with settings ( hide ( '<STR_LIT>' ) , warn_only = True ) : <EOL> if func ( '<STR_LIT>' % to_backup ) . failed : <EOL> to_backup = destination + '<STR_LIT:/>' + basename <EOL> if exists ( to_backup ) : <EOL> _backup_file ( to_backup ) <EOL> func ( "<STR_LIT>" % ( temp_destination , destination ) ) <EOL> return True </s>
