<s> import os <EOL> import http <EOL> import json <EOL> import asyncio <EOL> import aiohttp <EOL> from boto . glacier . layer2 import Layer2 <EOL> from boto . glacier . exceptions import UnexpectedHTTPResponseError <EOL> from waterbutler . core import signing <EOL> from waterbutler . core . utils import async_retry <EOL> from waterbutler . providers . osfstorage import settings <EOL> from waterbutler . providers . osfstorage . tasks import utils <EOL> def get_vault ( credentials , settings ) : <EOL> layer2 = Layer2 ( <EOL> aws_access_key_id = credentials [ '<STR_LIT>' ] , <EOL> aws_secret_access_key = credentials [ '<STR_LIT>' ] , <EOL> ) <EOL> return layer2 . get_vault ( settings [ '<STR_LIT>' ] ) <EOL> @ utils . task <EOL> def _push_file_archive ( self , local_path , version_id , callback_url , <EOL> credentials , settings ) : <EOL> _ , name = os . path . split ( local_path ) <EOL> with utils . RetryUpload ( self ) : <EOL> vault = get_vault ( credentials , settings ) <EOL> try : <EOL> glacier_id = vault . upload_archive ( local_path , description = name ) <EOL> except UnexpectedHTTPResponseError as error : <EOL> if error . status == <NUM_LIT> : <EOL> payload = json . loads ( error . body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if payload . get ( '<STR_LIT:message>' ) == '<STR_LIT>' : <EOL> return <EOL> raise <EOL> metadata = { <EOL> '<STR_LIT>' : vault . name , <EOL> '<STR_LIT>' : glacier_id , <EOL> } <EOL> _push_archive_complete . delay ( version_id , callback_url , metadata ) <EOL> @ utils . task <EOL> def _push_archive_complete ( self , version_id , callback_url , metadata ) : <EOL> signer = signing . Signer ( settings . HMAC_SECRET , settings . HMAC_ALGORITHM ) <EOL> with utils . RetryHook ( self ) : <EOL> data = signing . sign_data ( <EOL> signer , <EOL> { <EOL> '<STR_LIT:version>' : version_id , <EOL> '<STR_LIT>' : metadata , <EOL> } , <EOL> ) <EOL> future = aiohttp . request ( <EOL> '<STR_LIT>' , <EOL> callback_url , <EOL> data = json . dumps ( data ) , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } , <EOL> ) <EOL> loop = asyncio . get_event_loop ( ) <EOL> response = loop . run_until_complete ( future ) <EOL> if response . status != http . client . OK : <EOL> raise Exception ( '<STR_LIT>' . format ( response . status ) ) <EOL> @ async_retry ( retries = <NUM_LIT:5> , backoff = <NUM_LIT:5> ) <EOL> def main ( local_path , version_id , callback_url , credentials , settings ) : <EOL> return _push_file_archive . delay ( local_path , version_id , callback_url , credentials , settings ) </s>
