Skip to content

Instantly share code, notes, and snippets.

Avatar
🏡
Staying home

Chris Adams acdha

🏡
Staying home
View GitHub Profile
@acdha
acdha / simple_cors_server.py
Last active Feb 14, 2021
Python 3: serve the current directory as HTTP while setting CORS headers for XHR debugging
View simple_cors_server.py
#!/usr/bin/env python3
# encoding: utf-8
"""Use instead of `python3 -m http.server` when you need CORS"""
from http.server import HTTPServer, SimpleHTTPRequestHandler
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
View eternal-march.py
from datetime import date
ordinal = lambda n: "%d%s" % (n, "tsnrhtdd" [(n // 10 % 10 != 1) * (n % 10 < 4) * n % 10::4])
dom = date.today() - date(year=2020, month=3, day=1)
print(f"Today is March {ordinal(dom.days)}, 2020")
@acdha
acdha / pre-commit
Last active Dec 8, 2020
Git pre-commit hook which runs various code linters. Install this to .git/hooks/pre-commit inside your favorite repos
View pre-commit
#!/usr/bin/env PYTHONIOENCODING=utf-8 python
# encoding: utf-8
"""Git pre-commit hook which lints Python, JavaScript, SASS and CSS"""
from __future__ import absolute_import, print_function, unicode_literals
import os
import subprocess
import sys
@acdha
acdha / paramiko-using-ssh-config.py
Created Jul 23, 2013
Connecting with paramiko using the user's OpenSSH config
View paramiko-using-ssh-config.py
client = paramiko.SSHClient()
client._policy = paramiko.WarningPolicy()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_config = paramiko.SSHConfig()
user_config_file = os.path.expanduser("~/.ssh/config")
if os.path.exists(user_config_file):
with open(user_config_file) as f:
ssh_config.parse(f)
View register-qualys-assetview-connector-accounts.py
#!/usr/bin/env python3
"""
Bulk register AWS Accounts with Qualys AssetView
Given a list of account IDs, registers them and prints CSV output of the Account
ID and External ID in the format expected by the account setup Terraform code.
A list of accounts to register can be obtained like this::
$ grep -vf <(csvgrep --invert-match --columns 'External ID' --regex '^$' account-setup/qualys-assetview-accounts.csv | csvcut -c 'Account ID' | sed 1d) \
@acdha
acdha / custom-log-filtering-and-formatting.py
Created Feb 26, 2014
Example of how to filter or apply custom formatting using Python's logging library
View custom-log-filtering-and-formatting.py
#!/usr/bin/env python
# encoding: utf-8
from pprint import pformat, pprint
import logging
class PasswordMaskingFilter(logging.Filter):
"""Demonstrate how to filter sensitive data:"""
@acdha
acdha / ocr-file.py
Created Mar 17, 2014
Fragment of code used to process images with Tesseract OCR
View ocr-file.py
def ocr_file(filename, languages, output_base, temp_dir):
log.info("Launching tesseract on %s", filename)
output = subprocess.check_output(['tesseract', filename, output_base,
'-l', '+'.join(languages), TESSERACT_CONFIG],
cwd=temp_dir,
stderr=subprocess.STDOUT)
with OCR_STORAGE.open('%s/%s/%s.log' % (item_id, group, index), 'w') as log_f:
log_f.write(output)
@acdha
acdha / test-coverage.py
Created Jan 28, 2010
A custom Django test runner with support for coverage.py and graceful handling for app selection and various testing gotchas
View test-coverage.py
#!/usr/bin/env python
"""
Run Django Tests with full test coverage
This starts coverage early enough to get all of the model loading &
other startup code. It also allows you to change the output location
from $PROJECT_ROOT/coverage by setting the $TEST_COVERAGE_OUTPUT_DIR
environmental variable.
"""
View eternal-march.py
from datetime import date
ordinal = lambda n: "%d%s" % (n, "tsnrhtdd" [(n // 10 % 10 != 1) * (n % 10 < 4) * n % 10::4])
dom = date.today() - date(year=2020, month=3, day=1)
print(f"Today is March {ordinal(dom.days)}, 2020")
View ansible.cfg
[defaults]
interpreter_python = auto_silent
vault_password_file=get_vault_password_from_keyring
[ssh_connection]
# Enabling pipelining reduces the number of SSH operations required to
# execute a module on the remote server. This can result in a significant
# performance improvement when enabled, however when using "sudo:" you must
# first disable 'requiretty' in /etc/sudoers
#