<s> """<STR_LIT>""" <EOL> import subprocess <EOL> from hashlib import md5 <EOL> import json <EOL> from io import open <EOL> import os <EOL> try : <EOL> from urlparse import urljoin <EOL> except ImportError : <EOL> from urllib . parse import urljoin <EOL> class Deploy ( object ) : <EOL> def __init__ ( self , site , config ) : <EOL> """<STR_LIT>""" <EOL> self . site = site <EOL> self . config = config <EOL> self . bucket = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . profile = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . path = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . md5_path = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . redirects = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . site_url = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . skip_upload = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . new_md5s = dict ( ) <EOL> self . old_md5s = dict ( ) <EOL> self . changed = list ( ) <EOL> self . s3path = "<STR_LIT>" . format ( self . bucket ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . upload_files ( ) <EOL> self . new_md5s = dict ( ) <EOL> self . old_md5s = dict ( ) <EOL> self . changed = list ( ) <EOL> self . upload_redirects ( ) <EOL> def upload_redirects ( self ) : <EOL> """<STR_LIT>""" <EOL> md5_file = "<STR_LIT>" <EOL> if self . redirects == dict ( ) : <EOL> return <EOL> self . generate_redirect_md5s ( ) <EOL> self . load_md5 ( md5_file ) <EOL> self . compare_md5s ( ) <EOL> self . save_md5 ( md5_file ) <EOL> self . redirect ( ) <EOL> def upload_files ( self ) : <EOL> """<STR_LIT>""" <EOL> md5_file = "<STR_LIT>" <EOL> self . generate_site_md5s ( ) <EOL> self . load_md5 ( md5_file ) <EOL> self . compare_md5s ( ) <EOL> self . save_md5 ( md5_file ) <EOL> if self . should_upload : <EOL> self . upload ( ) <EOL> def generate_site_md5s ( self ) : <EOL> """<STR_LIT>""" <EOL> for root , dirs , files in os . walk ( self . path ) : <EOL> for name in files : <EOL> if name . startswith ( "<STR_LIT:.>" ) : <EOL> continue <EOL> filepath = os . path . join ( root , name ) <EOL> with open ( filepath , "<STR_LIT:rb>" ) as infile : <EOL> raw = infile . read ( ) <EOL> self . new_md5s [ filepath ] = md5 ( raw ) . hexdigest ( ) <EOL> def generate_redirect_md5s ( self ) : <EOL> """<STR_LIT>""" <EOL> for redirect in self . redirects : <EOL> self . new_md5s [ redirect ] = md5 ( self . redirects [ redirect ] ) . hexdigest ( ) <EOL> def load_md5 ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> load = os . path . join ( self . md5_path , filename ) <EOL> if not os . path . isfile ( load ) : <EOL> return <EOL> with open ( load , "<STR_LIT:r>" , encoding = "<STR_LIT:utf-8>" ) as infile : <EOL> raw = infile . read ( ) <EOL> self . old_md5s = json . loads ( raw ) <EOL> def save_md5 ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> save = os . path . join ( self . md5_path , filename ) <EOL> with open ( save , "<STR_LIT:w>" , encoding = "<STR_LIT:utf-8>" ) as outfile : <EOL> dumped = json . dumps ( self . new_md5s ) <EOL> outfile . write ( str ( dumped ) ) <EOL> def compare_md5s ( self ) : <EOL> """<STR_LIT>""" <EOL> for key in self . new_md5s : <EOL> if not key in self . old_md5s : <EOL> self . changed . append ( key ) <EOL> continue <EOL> if self . new_md5s [ key ] != self . old_md5s [ key ] : <EOL> self . changed . append ( key ) <EOL> def upload ( self ) : <EOL> """<STR_LIT>""" <EOL> for local in self . changed : <EOL> remote = local . replace ( self . path , self . s3path ) <EOL> process = subprocess . Popen ( <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> local , <EOL> remote , <EOL> "<STR_LIT>" , <EOL> self . profile <EOL> ] , <EOL> cwd = self . path <EOL> ) <EOL> process . communicate ( ) <EOL> @ property <EOL> def should_upload ( self ) : <EOL> """<STR_LIT>""" <EOL> upload = False <EOL> for changed in self . changed : <EOL> url = changed . replace ( self . path , "<STR_LIT>" ) <EOL> if url not in self . skip_upload : <EOL> upload = True <EOL> return upload <EOL> def redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> index = os . path . join ( self . path , "<STR_LIT>" ) <EOL> for redirect in self . changed : <EOL> source = os . path . join ( self . s3path , redirect . lstrip ( "<STR_LIT:/>" ) ) <EOL> destination = urljoin ( self . site_url , self . redirects [ redirect ] ) <EOL> process = subprocess . Popen ( <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> index , <EOL> source , <EOL> "<STR_LIT>" , <EOL> destination , <EOL> "<STR_LIT>" , <EOL> self . profile <EOL> ] , <EOL> cwd = self . path <EOL> ) <EOL> process . communicate ( ) </s>
