Skip to content

Commit 989c1bf

Browse files
committed
Merge pull request geekcomputers#2 from geekcomputers/master
2
2 parents dd72214 + d43cfce commit 989c1bf

12 files changed

+261
-177
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ In the scripts the comments etc are lined up correctly when they are viewed in [
3535
- `script_listing.py` - This will list all the files in the given directory, it will also go through all the subdirectories as well.
3636

3737
- `testlines.py` - This very simple script open a file and prints out 100 lines of whatever is set for the line variable.
38+
39+
- `serial_scanner.py` contains a method called ListAvailablePorts which returns a list with the names of the serial ports that are in use in our computer, this method works only on Linux and Windows (can be extended for mac osx). If no port is found, an empty list is returned.

backup_automater_services.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
# Script Name : backup_automater_services.py
22
# Author : Craig Richards
33
# Created : 24th October 2012
4-
# Last Modified :
5-
# Version : 1.0
4+
# Last Modified : 13th February 2016
5+
# Version : 1.0.1
66

7-
# Modifications :
7+
# Modifications : 1.0.1 - Tidy up the comments and syntax
88

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

1111
import shutil # Load the library module
1212
import datetime # Load the library module
13-
import os # Load the library module
13+
import os # Load the library module
1414

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

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

check_file.py

+25-26
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,28 @@
99
# Description : Check a file exists and that we can read the file
1010

1111
import sys # Import the Modules
12-
import os # Import the Modules
13-
14-
# Readfile Functions which open the file that is passed to the script
15-
16-
def readfile(filename):
17-
f = open(filename, 'r')
18-
line = f.read()
19-
print line
20-
21-
def main():
22-
if len(sys.argv) == 2: # Check the arguments passed to the script
23-
filename = sys.argv[1] # The filename is the first argument
24-
if not os.path.isfile(filename): # Check the File exists
25-
print '[-] ' + filename + ' does not exist.'
26-
exit(0)
27-
if not os.access(filename, os.R_OK): # Check you can read the file
28-
print '[-] ' + filename + ' access denied'
29-
exit(0)
30-
else:
31-
print '[-] Usage: ' + str(sys.argv[0]) + ' <filename>' # Print usage if not all parameters passed/Checked
32-
exit(0)
33-
print '[+] Reading from : ' + filename # Display Message and read the file contents
34-
readfile(filename)
35-
36-
if __name__ == '__main__':
37-
main()
12+
import os # Import the Modules
13+
14+
# Readfile Functions which open the file that is passed to the script
15+
16+
def readfile(filename):
17+
line = open(filename, 'r').read()
18+
print line
19+
20+
def main():
21+
if len(sys.argv) == 2: # Check the arguments passed to the script
22+
filename = sys.argv[1] # The filename is the first argument
23+
if not os.path.isfile(filename): # Check the File exists
24+
print '[-] ' + filename + ' does not exist.'
25+
exit(0)
26+
if not os.access(filename, os.R_OK): # Check you can read the file
27+
print '[-] ' + filename + ' access denied'
28+
exit(0)
29+
else:
30+
print '[-] Usage: ' + str(sys.argv[0]) + ' <filename>' # Print usage if not all parameters passed/Checked
31+
exit(0)
32+
print '[+] Reading from : ' + filename # Display Message and read the file contents
33+
readfile(filename)
34+
35+
if __name__ == '__main__':
36+
main()

check_for_sqlite_files.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Script Name : check_for_sqlite_files.py
22
# Author : Craig Richards
33
# Created : 07 June 2013
4-
# Last Modified :
5-
# Version : 1.0
4+
# Last Modified : 14 February 2016
5+
# Version : 1.0.1
66

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

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

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

2524
if Header[0:16] == 'SQLite format 3\000':

create_dir_if_not_there.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
# Script Name : create_dir_if_not_there.py
2-
# Author : Craig Richards
3-
# Created : 09th January 2012
4-
# Last Modified : 22nd October 2015
5-
# Version : 1.0
6-
# Modifications : Added exceptions
1+
# Script Name : create_dir_if_not_there.py
2+
# Author : Craig Richards
3+
# Created : 09th January 2012
4+
# Last Modified : 22nd October 2015
5+
# Version : 1.0.1
6+
# Modifications : Added exceptions
7+
# : 1.0.1 Tidy up comments and syntax
8+
#
9+
# Description : Checks to see if a directory exists in the users home directory, if not then create it
710

8-
# Description : Checks to see if a directory exists in the users home directory, if not then create it
9-
10-
import os # Import the OS module
11+
import os # Import the OS module
1112
try:
12-
home=os.path.expanduser("~") # Set the variable home by expanding the users set home directory
13-
print home # Print the location
14-
if not os.path.exists(home+'/testdir'):
15-
os.makedirs(home+'/testdir') # If not create the directory, inside their home directory
16-
except Exceptions as e:
17-
print e
13+
home = os.path.expanduser("~") # Set the variable home by expanding the users set home directory
14+
print home # Print the location
15+
16+
if not os.path.exists(home+'/testdir'):
17+
os.makedirs(home+'/testdir') # If not create the directory, inside their home directory
18+
except Exceptions as e:
19+
print e

daily_checks.py

+42-36
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
# Script Name : daily_checks.py
2-
# Author : Craig Richards
3-
# Created : 07th December 2011
4-
# Last Modified : 01st May 2013
5-
# Version : 1.4
1+
# Script Name : daily_checks.py
2+
# Author : Craig Richards
3+
# Created : 07th December 2011
4+
# Last Modified : 01st May 2013
5+
# Version : 1.5
6+
#
7+
# Modifications : 1.1 Removed the static lines for the putty sessions, it now reads a file, loops through and makes the connections.
8+
# : 1.2 Added a variable filename=sys.argv[0] , as when you use __file__ it errors when creating an exe with py2exe.
9+
# : 1.3 Changed the server_list.txt file name and moved the file to the config directory.
10+
# : 1.4 Changed some settings due to getting a new pc
11+
# : 1.5 Tidy comments and syntax
12+
#
13+
# Description : This simple script loads everything I need to carry out the daily checks for our systems.
614

7-
# Modifications : 1.1 Removed the static lines for the putty sessions, it now reads a file, loops through and makes the connections.
8-
# : 1.2 Added a variable filename=sys.argv[0] , as when you use __file__ it errors when creating an exe with py2exe.
9-
# : 1.3 Changed the server_list.txt file name and moved the file to the config directory.
10-
# : 1.4 Changed some settings due to getting a new pc
11-
12-
# Description : This simple script loads everything I need to carry out the daily checks for our systems.
13-
14-
import platform # Load the Library Module
15-
import os # Load the Library Module
16-
import subprocess # Load the Library Module
17-
import sys # Load the Library Module
15+
import platform # Load Modules
16+
import os
17+
import subprocess
18+
import sys
1819

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

21-
def clear_screen(): # Function to clear the screen
22-
if os.name == "posix": # Unix/Linux/MacOS/BSD/etc
23-
os.system('clear') # Clear the Screen
22+
def clear_screen(): # Function to clear the screen
23+
if os.name == "posix": # Unix/Linux/MacOS/BSD/etc
24+
os.system('clear') # Clear the Screen
2425
elif os.name in ("nt", "dos", "ce"): # DOS/Windows
25-
os.system('CLS') # Clear the Screen
26+
os.system('CLS') # Clear the Screen
2627

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

32-
def putty_sessions(): # Function to load the putty sessions I need
33-
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
34-
subprocess.Popen(('putty -load '+server)) # Open the PuTTY sessions - 1.1
33+
def putty_sessions(): # Function to load the putty sessions I need
34+
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
35+
subprocess.Popen(('putty -load '+server)) # Open the PuTTY sessions - 1.1
3536

3637
def rdp_sessions():
3738
print "Loading RDP Sessions:"
38-
subprocess.Popen("mstsc eclr.rdp") # Open up a terminal session connection and load the euroclear session
39+
subprocess.Popen("mstsc eclr.rdp") # Open up a terminal session connection and load the euroclear session
3940

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

4647
# Start of the Main Program
48+
def main():
49+
filename = sys.argv[0] # Create the variable filename
50+
confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable - 1.3
51+
conffile = ('daily_checks_servers.conf') # Set the variable conffile - 1.3
52+
conffilename = os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together - 1.3
53+
clear_screen() # Call the clear screen function
54+
55+
# The command below prints a little welcome message, as well as the script name, the date and time and where it was run from.
56+
print "Good Morning " + os.getenv('USERNAME') + ", " + filename, "ran at", strftime("%Y-%m-%d %H:%M:%S"), "on",platform.node(), "run from",os.getcwd()
57+
58+
print_docs() # Call the print_docs function
59+
putty_sessions() # Call the putty_session function
60+
rdp_sessions() # Call the rdp_sessions function
61+
euroclear_docs() # Call the euroclear_docs function
4762

48-
filename=sys.argv[0] # Create the variable filename
49-
confdir = os.getenv("my_config") # Set the variable confdir from the OS environment variable - 1.3
50-
conffile = ('daily_checks_servers.conf') # Set the variable conffile - 1.3
51-
conffilename=os.path.join(confdir, conffile) # Set the variable conffilename by joining confdir and conffile together - 1.3
52-
clear_screen() # Call the clear screen function
53-
# The command below prints a little welcome message, as well as the script name, the date and time and where it was run from.
54-
print "Good Morning " + os.getenv('USERNAME') + ", " + filename, "ran at", strftime("%Y-%m-%d %H:%M:%S"), "on",platform.node(), "run from",os.getcwd()
55-
print_docs() # Call the print_docs function
56-
putty_sessions() # Call the putty_session function
57-
rdp_sessions() # Call the rdp_sessions function
58-
euroclear_docs() # Call the euroclear_docs function
63+
if __name__ == '__main__':
64+
main()

env_check.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
# Script Name : env_check.py
2-
# Author : Craig Richards
3-
# Created : 14th May 2012
4-
# Last Modified :
5-
# Version : 1.0
1+
# Script Name : env_check.py
2+
# Author : Craig Richards
3+
# Created : 14th May 2012
4+
# Last Modified : 14 February 2016
5+
# Version : 1.0.1
66

7-
# Modifications :
7+
# Modifications : 1.0.1 - Tidy up comments and syntax
88

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

1111
import os
1212

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

17-
for env_check in open(conffilename): # Open the config file and read all the settings
18-
env_check = env_check.strip() # Set the variable as itsself, but strip the extra text out
19-
print '[{}]'.format(env_check) # Format the Output to be in Square Brackets
20-
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
21-
if newenv is None: # If it doesn't exist
22-
print env_check, 'is not set' # Print it is not set
23-
else: # Else if it does exist
17+
for env_check in open(conffilename): # Open the config file and read all the settings
18+
env_check = env_check.strip() # Set the variable as itsself, but strip the extra text out
19+
print '[{}]'.format(env_check) # Format the Output to be in Square Brackets
20+
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
21+
22+
if newenv is None: # If it doesn't exist
23+
print env_check, 'is not set' # Print it is not set
24+
else: # Else if it does exist
2425
print 'Current Setting for {}={}\n'.format(env_check, newenv) # Print out the details

folder_size.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
# Script Name : folder_size.py
2-
# Author : Craig Richards
3-
# Created : 19th July 2012
4-
# Last Modified :
5-
# Version : 1.0
1+
# Script Name : folder_size.py
2+
# Author : Craig Richards
3+
# Created : 19th July 2012
4+
# Last Modified : 22 February 2016
5+
# Version : 1.0.1
66

7-
# Modifications :
7+
# Modifications : Modified the Printing method and added a few comments
88

9-
# Description : This will scan the current directory and all subdirectories and display the size.
9+
# Description : This will scan the current directory and all subdirectories and display the size.
1010

11-
import os # Load the library module
11+
import os # Load the library module
12+
directory = '.' # Set the variable directory to be the current directory
13+
dir_size = 0 # Set the size to 0
1214

13-
directory = '.' # Set the variable directory to be the current directory
14-
dir_size = 0 # Set the size to 0
15-
for (path, dirs, files) in os.walk(directory): # Walk through all the directories
16-
for file in files: # Get all the files
17-
filename = os.path.join(path, file)
18-
dir_size += os.path.getsize(filename) # Get the sizes, the following lines print the sizes in bytes, Kb, Mb and Gb
19-
print "Folder Size in Bytes = %0.2f Bytes" % (dir_size)
20-
print "Folder Size in Kilobytes = %0.2f KB" % (dir_size/1024.0)
21-
print "Folder Size in Megabytes = %0.2f MB" % (dir_size/1024/1024.0)
22-
print "Folder Size in Gigabytes = %0.2f GB" % (dir_size/1024/1024/1024.0)
15+
fsizedicr = {'Bytes': 1, 'Kilobytes': float(1)/1024, 'Megabytes': float(1)/(1024*1024), 'Gigabytes': float(1)/(1024*1024
16+
*
17+
1024)}
18+
19+
for (path, dirs, files) in os.walk(directory): # Walk through all the directories. For each iteration, os.walk returns the folders, subfolders and files in the dir.
20+
for file in files: # Get all the files
21+
filename = os.path.join(path, file)
22+
dir_size += os.path.getsize(filename) # Add the size of each file in the root dir to get the total size.
23+
24+
for key in fsizedicr: #iterating through the dictionary
25+
print ("Folder Size: " + str(round(fsizedicr[key]*dir_size, 2)) + " " + key) # round function example: round(4.2384, 2) ==> 4.23

0 commit comments

Comments
 (0)