From 717d0c1fa111a200a388cf9a721c4120598849a9 Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Fri, 14 Apr 2017 16:13:26 +0100 Subject: [PATCH] Convert `genpw` from Bash to Python. Add a method to install Python modules via Pip to `software-install`. --- bin/genpw | 55 ++++++++++++++++++++++++++++++-------------- bin/software-install | 5 ++++ 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/bin/genpw b/bin/genpw index 638ef29..3973464 100755 --- a/bin/genpw +++ b/bin/genpw @@ -1,24 +1,45 @@ -#!/bin/bash +#!/usr/bin/env python +"""Module to generate a random string -ECHOARGS= -LENGTH=12 +Attributes: + LENGTH (int): Defaults to a length of 16 +""" -while getopts nl: ARG; do - case ${ARG} in - n) ECHOARGS="-n" - ;; +import sys +import logging +import optparse - l) LENGTH=${OPTARG} - ;; +try: + import OpenSSL +except ImportError: + logging.error('Missing PyOpenSSL.') + logging.debug('Try `pip install pyopenssl`') + sys.exit(1) - ?) printf "Usage: %s [-n] [-l X]\n" ${0} - exit 1 - ;; - esac -done +import base64 -PW=$(openssl rand -base64 ${LENGTH} | cut -c1-${LENGTH}) +LENGTH = 16 -echo ${ECHOARGS} ${PW} -exit 0 +def generate(length): + """Simple function to call OpenSSL + + Args: + length (int): length of string to return + """ + return base64.encodestring(OpenSSL.rand.bytes(256))[:length] + + +def main(): + """Main function to call + """ + parser = optparse.OptionParser() + parser.add_option('-n', dest='length', default=LENGTH, type=int) + opts, _ = parser.parse_args() + + print generate(opts.length) + + +if __name__ == '__main__': + main() + sys.exit(0) diff --git a/bin/software-install b/bin/software-install index c23a523..2f7b83e 100755 --- a/bin/software-install +++ b/bin/software-install @@ -83,6 +83,10 @@ function install_Darwin_software { #------------------------------- } +function install_python_modules { + pip install pyopenssl +} + echo "#-------------------------------" echo "# START: $(date)" echo "#-------------------------------" @@ -90,6 +94,7 @@ echo "#-------------------------------" # Call the install function appropriate for this platform install_$(uname -s)_software configure_$(uname -s)_software +install_python_modules echo "#-------------------------------" echo "# END: $(date)"