Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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