Skip to content

Instantly share code, notes, and snippets.

@William-Hill
William-Hill / install_python27.sh
Created October 18, 2017 18:56
A shell script to install Python 2.7.13 with shared libraries and pip on CentOS
# Start by making sure your system is up-to-date:
yum update
# Compilers and related tools:
yum groupinstall -y "development tools"
# Libraries needed during compilation to enable all features of Python:
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel
# If you are on a clean "minimal" install of CentOS you also need the wget tool:
yum install -y wget
@William-Hill
William-Hill / install_jenkins.sh
Created October 24, 2017 23:34
Install Jenkins on CentOS 6
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum install jenkins
@William-Hill
William-Hill / find_processes.py
Created January 22, 2018 00:22
Find all processes belonging to a user in python
'''Requires psutil package that's available from pip'''
import psutil
def find_processes_by_user(user_name):
user_processes = [proc for proc in psutil.process_iter(attrs=['pid', 'name', 'username']) if proc.info["username"] == user_name]
return user_processes
@William-Hill
William-Hill / backup.py
Created January 22, 2018 07:49
Backup a file in python
'''A simple function to create a backup of a file with a date stamp and backup extension'''
import datetime
import shutil
import os
def create_backup_file(file_name, backup_extension=".bak", date=str(datetime.date.today())):
'''Create a backup of a file using the given backup extension'''
backup_file_name = os.path.join(file_name, backup_extension)
try:
shutil.copyfile(file_name, backup_file_name)
@William-Hill
William-Hill / mkdir_p.py
Created January 22, 2018 07:56
Create directory with parent directories as needed; Mimics mkdir -p
'''Makes a directory; no exception if directory already exists, make parent directories as needed'''
def mkdir_p(path, mode = 0777):
try:
os.makedirs(path, mode)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
print("%s already exists.", path)
else:
raise
@William-Hill
William-Hill / logging_setup.py
Created February 9, 2018 20:12
Python logging setup
'''My preferred logging setup to log the filename, line number, function name, and time in messages'''
import logging
logger = logging.getLogger('metrics_logger')
logger.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(levelname)s - %(filename)s - %(lineno)s - %(funcName)s - %(asctime)s - %(message)s", datefmt='%m/%d/%Y %I:%M:%S %p')
# create console handler with a higher log level
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
@William-Hill
William-Hill / styles.css
Created February 11, 2018 16:49
Responsively center text horizontally and vertically
/* source: https://stackoverflow.com/a/45275695/2154867 */
#text_container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh; }
#text_container p {
margin: 0;
color: white;
@William-Hill
William-Hill / styles.css
Created February 11, 2018 16:54
Responsive background image
html{
width: 100vw; height: 100vh;
}
body {
width: 100vw;
height: 100vh;
/*Replace with some_image with image url/path*/
background: url("some_image.png");
background-repeat: no-repeat;
@William-Hill
William-Hill / x509_subject_string.py
Created April 19, 2018 22:34
Get X509 subject name as string using Pyopenssl
import OpenSSL
def get_x509_subject_string(cert_name):
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, open(cert_name).read())
subject_components = x509.get_subject().get_components()
subject_string = ""
for component in subject_components:
subject_string = subject_string + "/" + component[0] + "=" + component[1]
@William-Hill
William-Hill / insert_dll.py
Created April 28, 2018 21:11
Insert into a sorted doubly linked list
def insert_between_nodes(current_node, new_node):
new_node.next = current_node.next
new_node.prev = current_node
current_node.next.prev = new_node
current_node.next = new_node
def SortedInsert(head, data):
new_node = Node(data)
current_node = head