Skip to content

Remove unecessary line of code and variable, and tidy up comments and syntax. #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions backup_automater_services.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
# Script Name : backup_automater_services.py
# Author : Craig Richards
# Created : 24th October 2012
# Last Modified :
# Version : 1.0
# Last Modified : 13th February 2016
# Version : 1.0.1

# Modifications :
# Modifications : 1.0.1 - Tidy up the comments and syntax

# Description : This will go through and backup all my automator services workflows

import shutil # Load the library module
import datetime # Load the library module
import os # Load the library module
import os # Load the library module

today=datetime.date.today() # Get Today's date
todaystr=today.isoformat() # Format it so we can use the format to create the directory
today = datetime.date.today() # Get Today's date
todaystr = today.isoformat() # Format it so we can use the format to create the directory

confdir=os.getenv("my_config") # Set the variable by getting the value from the OS setting
dropbox=os.getenv("dropbox") # Set the variable by getting the value from the OS setting
conffile = ('services.conf') # Set the variable as the name of the configuration file
conffilename=os.path.join(confdir, conffile) # Set the variable by combining the path and the file name
sourcedir=os.path.expanduser('~/Library/Services/') # Source directory of where the scripts are located
destdir=os.path.join(dropbox, "My_backups"+"/"+"Automater_services"+todaystr+"/") # Combine several settings to create the destination backup directory

for file_name in open(conffilename): # Walk through the configuration file
fname = file_name.strip() # Strip out the blank lines from the configuration file
if fname: # For the lines that are not blank
sourcefile=os.path.join(sourcedir, file_name.strip()) # Get the name of the source files to backup
destfile=os.path.join(destdir, file_name.strip()) # Get the name of the destination file names
shutil.copytree(sourcefile, destfile) # Copy the directories
confdir = os.getenv("my_config") # Set the variable by getting the value from the OS setting
dropbox = os.getenv("dropbox") # Set the variable by getting the value from the OS setting
conffile = ('services.conf') # Set the variable as the name of the configuration file
conffilename = os.path.join(confdir, conffile) # Set the variable by combining the path and the file name
sourcedir = os.path.expanduser('~/Library/Services/') # Source directory of where the scripts are located
destdir = os.path.join(dropbox, "My_backups"+"/"+"Automater_services"+todaystr+"/") # Combine several settings to create

# the destination backup directory
for file_name in open(conffilename): # Walk through the configuration file
fname = file_name.strip() # Strip out the blank lines from the configuration file
if fname: # For the lines that are not blank
sourcefile = os.path.join(sourcedir, file_name.strip()) # Get the name of the source files to backup
destfile = os.path.join(destdir, file_name.strip()) # Get the name of the destination file names
shutil.copytree(sourcefile, destfile) # Copy the directories
51 changes: 25 additions & 26 deletions check_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@
# Description : Check a file exists and that we can read the file

import sys # Import the Modules
import os # Import the Modules

# Readfile Functions which open the file that is passed to the script

def readfile(filename):
f = open(filename, 'r')
line = f.read()
print line

def main():
if len(sys.argv) == 2: # Check the arguments passed to the script
filename = sys.argv[1] # The filename is the first argument
if not os.path.isfile(filename): # Check the File exists
print '[-] ' + filename + ' does not exist.'
exit(0)
if not os.access(filename, os.R_OK): # Check you can read the file
print '[-] ' + filename + ' access denied'
exit(0)
else:
print '[-] Usage: ' + str(sys.argv[0]) + ' <filename>' # Print usage if not all parameters passed/Checked
exit(0)
print '[+] Reading from : ' + filename # Display Message and read the file contents
readfile(filename)

if __name__ == '__main__':
main()
import os # Import the Modules

# Readfile Functions which open the file that is passed to the script

def readfile(filename):
line = open(filename, 'r').read()
print line

def main():
if len(sys.argv) == 2: # Check the arguments passed to the script
filename = sys.argv[1] # The filename is the first argument
if not os.path.isfile(filename): # Check the File exists
print '[-] ' + filename + ' does not exist.'
exit(0)
if not os.access(filename, os.R_OK): # Check you can read the file
print '[-] ' + filename + ' access denied'
exit(0)
else:
print '[-] Usage: ' + str(sys.argv[0]) + ' <filename>' # Print usage if not all parameters passed/Checked
exit(0)
print '[+] Reading from : ' + filename # Display Message and read the file contents
readfile(filename)

if __name__ == '__main__':
main()
9 changes: 4 additions & 5 deletions check_for_sqlite_files.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Script Name : check_for_sqlite_files.py
# Author : Craig Richards
# Created : 07 June 2013
# Last Modified :
# Version : 1.0
# Last Modified : 14 February 2016
# Version : 1.0.1

# Modifications :
# Modifications : 1.0.1 - Remove unecessary line and variable on Line 21

# Description : Scans directories to check if there are any sqlite files in there

Expand All @@ -18,8 +18,7 @@ def isSQLite3(filename):
if getsize(filename) < 100: # SQLite database file header is 100 bytes
return False
else:
fd = open(filename, 'rb')
Header = fd.read(100)
Header = open(filename, 'rb').read(100)
fd.close()

if Header[0:16] == 'SQLite format 3\000':
Expand Down
32 changes: 17 additions & 15 deletions create_dir_if_not_there.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Script Name : create_dir_if_not_there.py
# Author : Craig Richards
# Created : 09th January 2012
# Last Modified : 22nd October 2015
# Version : 1.0
# Modifications : Added exceptions
# Script Name : create_dir_if_not_there.py
# Author : Craig Richards
# Created : 09th January 2012
# Last Modified : 22nd October 2015
# Version : 1.0.1
# Modifications : Added exceptions
# : 1.0.1 Tidy up comments and syntax
#
# Description : Checks to see if a directory exists in the users home directory, if not then create it

# Description : Checks to see if a directory exists in the users home directory, if not then create it

import os # Import the OS module
import os # Import the OS module
try:
home=os.path.expanduser("~") # Set the variable home by expanding the users set home directory
print home # Print the location
if not os.path.exists(home+'/testdir'):
os.makedirs(home+'/testdir') # If not create the directory, inside their home directory
except Exceptions as e:
print e
home = os.path.expanduser("~") # Set the variable home by expanding the users set home directory
print home # Print the location

if not os.path.exists(home+'/testdir'):
os.makedirs(home+'/testdir') # If not create the directory, inside their home directory
except Exceptions as e:
print e
78 changes: 42 additions & 36 deletions daily_checks.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
# Script Name : daily_checks.py
# Author : Craig Richards
# Created : 07th December 2011
# Last Modified : 01st May 2013
# Version : 1.4
# Script Name : daily_checks.py
# Author : Craig Richards
# Created : 07th December 2011
# Last Modified : 01st May 2013
# Version : 1.5
#
# Modifications : 1.1 Removed the static lines for the putty sessions, it now reads a file, loops through and makes the connections.
# : 1.2 Added a variable filename=sys.argv[0] , as when you use __file__ it errors when creating an exe with py2exe.
# : 1.3 Changed the server_list.txt file name and moved the file to the config directory.
# : 1.4 Changed some settings due to getting a new pc
# : 1.5 Tidy comments and syntax
#
# Description : This simple script loads everything I need to carry out the daily checks for our systems.

# Modifications : 1.1 Removed the static lines for the putty sessions, it now reads a file, loops through and makes the connections.
# : 1.2 Added a variable filename=sys.argv[0] , as when you use __file__ it errors when creating an exe with py2exe.
# : 1.3 Changed the server_list.txt file name and moved the file to the config directory.
# : 1.4 Changed some settings due to getting a new pc

# Description : This simple script loads everything I need to carry out the daily checks for our systems.

import platform # Load the Library Module
import os # Load the Library Module
import subprocess # Load the Library Module
import sys # Load the Library Module
import platform # Load Modules
import os
import subprocess
import sys

from time import strftime # Load just the strftime Module from Time

def clear_screen(): # Function to clear the screen
if os.name == "posix": # Unix/Linux/MacOS/BSD/etc
os.system('clear') # Clear the Screen
def clear_screen(): # Function to clear the screen
if os.name == "posix": # Unix/Linux/MacOS/BSD/etc
os.system('clear') # Clear the Screen
elif os.name in ("nt", "dos", "ce"): # DOS/Windows
os.system('CLS') # Clear the Screen
os.system('CLS') # Clear the Screen

def print_docs(): # Function to print the daily checks automatically
def print_docs(): # Function to print the daily checks automatically
print "Printing Daily Check Sheets:"
# The command below passes the command line string to open word, open the document, print it then close word down
subprocess.Popen(["C:\\Program Files (x86)\Microsoft Office\Office14\winword.exe", "P:\\\\Documentation\\Daily Docs\\Back office Daily Checks.doc", "/mFilePrintDefault", "/mFileExit"]).communicate()

def putty_sessions(): # Function to load the putty sessions I need
for server in open(conffilename): # Open the file server_list.txt, loop through reading each line - 1.1 -Changed - 1.3 Changed name to use variable conffilename
subprocess.Popen(('putty -load '+server)) # Open the PuTTY sessions - 1.1
def putty_sessions(): # Function to load the putty sessions I need
for server in open(conffilename): # Open the file server_list.txt, loop through reading each line - 1.1 -Changed - 1.3 Changed name to use variable conffilename
subprocess.Popen(('putty -load '+server)) # Open the PuTTY sessions - 1.1

def rdp_sessions():
print "Loading RDP Sessions:"
subprocess.Popen("mstsc eclr.rdp") # Open up a terminal session connection and load the euroclear session
subprocess.Popen("mstsc eclr.rdp") # Open up a terminal session connection and load the euroclear session

def euroclear_docs():
# The command below opens IE and loads the Euroclear password document
Expand All @@ -44,15 +45,20 @@ def euroclear_docs():
# End of the functions

# Start of the Main Program
def main():
filename = sys.argv[0] # Create the variable filename
confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable - 1.3
conffile = ('daily_checks_servers.conf') # Set the variable conffile - 1.3
conffilename = os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together - 1.3
clear_screen() # Call the clear screen function

# The command below prints a little welcome message, as well as the script name, the date and time and where it was run from.
print "Good Morning " + os.getenv('USERNAME') + ", " + filename, "ran at", strftime("%Y-%m-%d %H:%M:%S"), "on",platform.node(), "run from",os.getcwd()

print_docs() # Call the print_docs function
putty_sessions() # Call the putty_session function
rdp_sessions() # Call the rdp_sessions function
euroclear_docs() # Call the euroclear_docs function

filename=sys.argv[0] # Create the variable filename
confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable - 1.3
conffile = ('daily_checks_servers.conf') # Set the variable conffile - 1.3
conffilename=os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together - 1.3
clear_screen() # Call the clear screen function
# The command below prints a little welcome message, as well as the script name, the date and time and where it was run from.
print "Good Morning " + os.getenv('USERNAME') + ", " + filename, "ran at", strftime("%Y-%m-%d %H:%M:%S"), "on",platform.node(), "run from",os.getcwd()
print_docs() # Call the print_docs function
putty_sessions() # Call the putty_session function
rdp_sessions() # Call the rdp_sessions function
euroclear_docs() # Call the euroclear_docs function
if __name__ == '__main__':
main()
35 changes: 18 additions & 17 deletions env_check.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# Script Name : env_check.py
# Author : Craig Richards
# Created : 14th May 2012
# Last Modified :
# Version : 1.0
# Script Name : env_check.py
# Author : Craig Richards
# Created : 14th May 2012
# Last Modified : 14 February 2016
# Version : 1.0.1

# Modifications :
# Modifications : 1.0.1 - Tidy up comments and syntax

# Description : This script will check to see if all of the environment variables I require are set
# Description : This script will check to see if all of the environment variables I require are set

import os

confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable
conffile = 'env_check.conf' # Set the variable conffile
conffilename=os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together
confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable
conffile = 'env_check.conf' # Set the variable conffile
conffilename = os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together

for env_check in open(conffilename): # Open the config file and read all the settings
env_check = env_check.strip() # Set the variable as itsself, but strip the extra text out
print '[{}]'.format(env_check) # Format the Output to be in Square Brackets
newenv = os.getenv(env_check) # Set the variable newenv to get the settings from the OS what is currently set for the settings out the configfile
if newenv is None: # If it doesn't exist
print env_check, 'is not set' # Print it is not set
else: # Else if it does exist
for env_check in open(conffilename): # Open the config file and read all the settings
env_check = env_check.strip() # Set the variable as itsself, but strip the extra text out
print '[{}]'.format(env_check) # Format the Output to be in Square Brackets
newenv = os.getenv(env_check) # Set the variable newenv to get the settings from the OS what is currently set for the settings out the configfile

if newenv is None: # If it doesn't exist
print env_check, 'is not set' # Print it is not set
else: # Else if it does exist
print 'Current Setting for {}={}\n'.format(env_check, newenv) # Print out the details
31 changes: 16 additions & 15 deletions folder_size.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# Script Name : folder_size.py
# Author : Craig Richards
# Created : 19th July 2012
# Last Modified :
# Version : 1.0
# Script Name : folder_size.py
# Author : Craig Richards
# Created : 19th July 2012
# Last Modified : 14 February 2016
# Version : 1.0.1

# Modifications :
# Modifications : 1.0.1 - Tidy up comments and syntax, add main() function

# Description : This will scan the current directory and all subdirectories and display the size.
# Description : This will scan the current directory and all subdirectories and display the size.

import os # Load the library module
import os # Load the library module

directory = '.' # Set the variable directory to be the current directory
dir_size = 0 # Set the size to 0
for (path, dirs, files) in os.walk(directory): # Walk through all the directories
for file in files: # Get all the files
filename = os.path.join(path, file)
dir_size += os.path.getsize(filename) # Get the sizes, the following lines print the sizes in bytes, Kb, Mb and Gb
directory = '.' # Set the variable directory to be the current directory
dir_size = 0 # Set the size to 0

for (path, dirs, files) in os.walk(directory): # Walk through all the directories
for file in files: # Get all the files
filename = os.path.join(path, file)
dir_size += os.path.getsize(filename) # Get the sizes, the following lines print the sizes in bytes, Kb, Mb and Gb
print "Folder Size in Bytes = %0.2f Bytes" % (dir_size)
print "Folder Size in Kilobytes = %0.2f KB" % (dir_size/1024.0)
print "Folder Size in Megabytes = %0.2f MB" % (dir_size/1024/1024.0)
print "Folder Size in Gigabytes = %0.2f GB" % (dir_size/1024/1024/1024.0)
print "Folder Size in Gigabytes = %0.2f GB" % (dir_size/1024/1024/1024.0)
Loading