07. May 2014 · Comments Off · Categories: Snippets

An update to my Python file comparison utility, now with bidirectional support. This is an alternative to using diff that provides, in my opinion, cleaner output. Also, unlike diff, because this is written in Python it should be able to run on just about any platform.

#!/usr/bin/python
# audit-tool.py 2.0 - A simple file comparison utility.
# Copyright 2014 13Cubed. All rights reserved. Written by: Richard Davis

import sys

def compareFiles(filename1, filename2, ignorecase, bidirectional):
  """
  Given two filenames and an ignorecase booelean, compares filename1
  against filename2 and returns list of the differences and a count of
  how many were found. If ignorecase is 1, the contents of both files
  are read in as lowercase so that case differences are ignored. If
  bidirectional is 1, filename1 is compared to filename2 and vice-versa.
  """
  results = []

  try:
    f1 = open(filename1, 'rU')
  except IOError:
    print 'Could not find the specified file:', filename1
    sys.exit(1)

  try:
    f2 = open(filename2, 'rU')
  except IOError:
    print 'Could not find the specified file:', filename2
    sys.exit(1)

  list1 = f1.readlines()
  list2 = f2.readlines()

  f2.close()
  f1.close()

  if ignorecase == 1:
    for i in range(0,len(list1)):
      list1[i] = list1[i].lower()
    for i in range(0,len(list2)):
      list2[i] = list2[i].lower()

  diffs = set(list1) - set(list2)

  if bidirectional == 1:
    reverseDiffs = set(list2) - set(list1)

  diffcount = 0

  results.append('\n' + filename1 + ' -> ' + filename2 + ':\n')

  for diff in diffs:
    results.append(diff)
    diffcount = diffcount + 1

  if bidirectional == 1:
    results.append('\n' + filename1 + ' <- ' + filename2 + ':\n')

    for diff in reverseDiffs:
      results.append(diff)
      diffcount = diffcount + 1

  return results, diffcount

def main():
  if (len(sys.argv) < 3) or (len(sys.argv) > 5):
    print 'usage: audit-tool.py filename1 filename2 [--ignorecase] [--bidirectional]'
    sys.exit(1)

  ignorecase = 0
  bidirectional = 0

  filename1 = sys.argv[1]
  filename2 = sys.argv[2]

  if len(sys.argv) == 4:
    option1 = sys.argv[3]
    if option1 == '--ignorecase':
      ignorecase = 1
    elif option1 == '--bidirectional':
      bidirectional = 1
    else:
      print 'unknown option: ' + option1
      sys.exit(1)

  elif len(sys.argv) == 5:
    option1 = sys.argv[3]
    option2 = sys.argv[4]

    if option1 == '--ignorecase':
      ignorecase = 1
    elif option1 == '--bidirectional':
      bidirectional = 1
    else:
      print 'unknown option: ' + option1
      sys.exit(1)

    if option2 == '--ignorecase':
      ignorecase = 1
    elif option2 == '--bidirectional':
      bidirectional = 1
    else:
      print 'unknown option: ' + option2
      sys.exit(1)

  (results, diffcount) = compareFiles(filename1, filename2, ignorecase, bidirectional)

  if diffcount:
    print '\n%d difference(s) found.' % (diffcount)
    for line in results:
      print line,
  else:
    print '\nNo differences -- files are identical.'

  print '\nCopyright (C) 2014 13Cubed. All rights reserved.'

if __name__ == '__main__':
  main()
28. February 2014 · Comments Off · Categories: Linux

Suppose you needed to run a cron job that ran only on the first x-day of the month. It’s easy to create a cron that runs every x-day, but how would you specify the first? The solution is pretty nifty. Simply create a cron that would run a job every x-day, but then have that cron execute a script that evaluates whether it is indeed the first occurrence of that day in a given month. If yes, it will execute your script. If no, it will do nothing.

Example for running a cron the first Wednesday of every month at midnight:
0 0 * * 3 [ "$(date '+\%d')" -le 7 ] && /usr/local/bin/myscript.sh > /dev/null

The cron will indeed run every Wednesday, but the conditional statement will only execute myscript.sh if the current day of the month is less than or equal to seven. Perfect!

12. October 2013 · Comments Off · Categories: iOS

Introducing Pingy.

Pingy is a free ping (ICMP echo request/reply) utility with a clean interface and no ads. It’s designed to be a lightweight, quick, and easy to use utility to check for basic network connectivity. It calculates sent/received counts, packet loss, and minimum, maximum, average, and standard deviation statistics for round trip time. It also features a ping sound option that will allow you to hear when a ping is successfully received.

Download Now!

18. September 2013 · Comments Off · Categories: iOS

What’s New in Version 1.6
– Completely redesigned to match the look and feel of iOS 7
– Added new Code Folding section under Miscellaneous
– Fixed minor errors

Please note that this update is only for iOS 7 and will not be available to users running older versions of iOS.

Download Now!

18. September 2013 · Comments Off · Categories: iOS

What’s New in Version 1.5
– Completely redesigned to match the look and feel of iOS 7
– Fixed minor errors in word list

Please note that this update is only for iOS 7 and will not be available to users running older versions of iOS.

Download Now!