Commit 112a5af4 authored by thomas.forbriger's avatar thomas.forbriger

[TASK] (util): provide new python program template

parent c8338dba
#!/usr/bin/env python
# this is <csbusortdirlist.py>
# ----------------------------------------------------------------------------
#
# Copyright (c) 2016 by Thomas Forbriger (BFO Schiltach)
#
# read a directory list and create a sorted table
#
# REVISIONS and CHANGES
# 10/06/2016 V1.0 Thomas Forbriger
#
# ============================================================================
#
"""
This program takes a directory list like created by csbulistcsfiles.sh and
creates a sorted table which should support a completeness check.
"""
# ----------------------------------------------------------------------------
__version__ = "csbusortdirlist 2016-06-10"
__author__ = "Thomas Forbriger"
# ----------------------------------------------------------------------------
import sys
import os
import string
import getopt
# ============================================================================
def usage():
"""
print usage information
"""
print(__version__)
print('Usage: '+sys.argv[0]+' [-v] [-D] [-o] listfile tablefile')
print(' or: '+sys.argv[0]+' --help|-h')
# ----------------------------------------------------------------------------
def help():
"""
print detailed usage information (online help)
"""
usage()
print(
"""
This program takes a directory list like created by csbulistcsfiles.sh and
creates a sorted table which should support a completeness check.
listfile list of subdirectory path names
tablefile name of file to write table to
-v be verbose
-o overwrite output file
-D produce debug output
""")
print('Call "pydoc csbusortdirlist" to learn more')
# ============================================================================
# ============================================================================
def main(argv=None):
argv = sys.argv
(opts, args)=getopt.gnu_getopt(sys.argv[1:], 'voDh', ('help'))
verbose=False
overwrite=False
for opt in opts:
if (opt[0]=='-v'):
verbose=True
elif (opt[0]=='-D'):
global DEBUG
DEBUG=True
elif (opt[0]=='-o'):
overwrite=True
elif (opt[0]=='-h') or (opt[0]=='--help'):
help()
exit(0)
else :
usage()
exit(0)
if len(args) < 1:
usage()
exit(0)
inputfilename=args[0]
outputfilename=args[1]
# check for existing file
# if an exception is raised here, this is just fine: we do not want to
# overwrite an existing file
if not overwrite:
try:
output=open(outputfilename, 'r')
except IOError:
if verbose:
print('create new file %s' % outputfilename)
else:
print('ERROR: output file %s exists' % outputfilename)
exit(ERR_ERROR)
if verbose:
print('write to file %s' % outputfilename)
# try to open for output
try:
output=open(outputfilename, 'w')
except IOError as err:
print('ERROR: could not open file %s for output' % outputfilename)
raise err
# ----------------------------------------------------------------------------
if __name__ == '__main__':
sys.exit(main())
# ----- END OF csbusortdirlist.py -----
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment