Skip to content

Instantly share code, notes, and snippets.

View echohack's full-sized avatar
🎥
twitch.tv/echohack

echohack echohack

🎥
twitch.tv/echohack
View GitHub Profile
# Convert all flac files in all subdirs to mp3 with ffmpeg
find . -type f -name "*.flac" -print0 | while read -d $'\0' a; do
< /dev/null ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
done
@echohack
echohack / chef_env.sh
Last active August 29, 2015 14:06
Shell script for quickly changing which chef server your toolset points to
#! /usr/bin/env bash
# Only tested on Mac OSX 10.10
if [ -d ~/.chef_pp ]; then
echo "Changing to Pre-Production Chef Environment."
mv ~/.chef ~/.chef_sandbox
mv ~/.berkshelf ~/.berkshelf_sandbox
mv ~/.chef_pp ~/.chef
mv ~/.berkshelf_pp ~/.berkshelf
@echohack
echohack / SSLAdapter.py
Created June 11, 2013 21:17
Improved SSLAdapter
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
class SSLAdapter(HTTPAdapter):
"""A HTTPS Transport Adapter that uses an arbitrary SSL version."""
def __init__(self, ssl_version=None, **kwargs):
self.ssl_version = ssl_version
super(SSLAdapter, self).__init__(**kwargs)
@echohack
echohack / artifactory_secret_api.py
Last active December 17, 2015 20:09
Artifactory Secret Build Info API.
def create_build_info(self, build_name, build_number, dependencies, build_dependencies):
"""
Returns a build info dictionary which is formated to correctly deploy
a new build to artifactory.
Make a put request with this build info to api/build
"""
build_info = {'version': '1.0.1',
'name': build_name,
'number': str(build_number),
'type': 'GENERIC',
@echohack
echohack / jsonschemaTestValidator.py
Created May 28, 2013 18:07
Jsonschema test validator.
import jsonschema
import json
import nose.tools
def test_schema_generator():
test_setup_data = [
{"schema": "../schema/1.0/build-manifest.json", "tests": "build-manifest-validtests.json", "assert_function": nose.tools.assert_true},
{"schema": "../schema/1.0/build-manifest.json", "tests": "build-manifest-invalidtests.json", "assert_function": nose.tools.assert_false}]
for test_setup in test_setup_data:
@echohack
echohack / json_validator.py
Last active December 17, 2015 05:19
A snippet that shows how to validate json against a schema using jsonschema.
import json
import jsonschema
import requests
def get_valid_json(self, data, json_schema_url):
"""
Validates a json artifact against a published schema.
"""
s = requests.get(url=json_schema_url)
schema = s.json()
@echohack
echohack / ProviderAPIWrapper.py
Last active December 16, 2015 09:48
An example of a pattern I use when using requests to wrap an api.
import requests
import json
class AbstractDataProvider():
"""A list of methods that data providers should implement or extend."""
def __init__(self, base_url, username=None, password=None):
self.base_url = base_url.rstrip('/')
self.username = username
self.password = password
class APIWrapper:
#snippet...
def poll_api(self, tries, initial_delay, delay, backoff, success_list, apifunction, *args):
time.sleep(initial_delay)
for n in range(tries):
try:
status = self.get_status()
if status not in success_list:
import socket
import inject_exception2
import requests
from mock import patch
import nose.tools
# ...snippet
def test_api_call():
with patch.object(requests, "get") as get_mock:
import socket
# ...snippet
class ExceededRetries(StopIteration):
pass
def call_api(url):
try:
requests.get(url="url")