#!/usr/bin/python # import sys import os import sdms import re # # Tokens # CAL = 'CAL' ID = 'ID' EQUALS = '=' OPAREN = '(' CPAREN = ')' COMMA = ',' SPACE = ' ' TAB = '\t' NEWLINE = '\n' CR = '\r' COMMENT = '#' SQUOTE = "'" DQUOTE = '"' ESCAPE = '\\' DASH = '-' # # Literals for navigating in output structures # DATA = 'DATA' RECORD = 'RECORD' COMMENT = 'COMMENT' CMTTYPE = 'COMMENTTYPE' TABLE = 'TABLE' TAG = 'TAG' DESC = 'DESCRIPTION' objlist = [ 'job definition system.examples.e0010_singlejob.singlejob', "JOB DEFINITION SYSTEM.'EXAMPLES'.'E0015_PARAMETERS'.'PARAMETERS'", ] newtag = "'Test Tag'" newdesc = """'this is a new description that will be added to the end of the list of comment entries'""" # ===================================================================================================================== # P R O C E S S S D M S H R C # ===================================================================================================================== def parseSdmshrcEntry(row): key = '' value = '' gotValue = False seenEquals = False if row[0] == COMMENT: return None for i in range(len(row)): if row[i] == '=': seenEquals = True continue if row[i] in [ SPACE, TAB, CR, NEWLINE ]: if gotValue: break continue if seenEquals: value = value + row[i] gotValue = True else: key = key + row[i] return [ key.upper(), value ] def readSdmshrc(): server = { 'HOST' : 'localhost', 'PORT' : '2506' } HOME = os.environ['HOME'] BSCONF= os.environ['BICSUITECONFIG'] sdmshrc = HOME + '/.sdmshrc' genrc = BSCONF + '/etc/sdmshrc' genarr = [] usrarr = [] try: f = open(genrc) genarr = f.readlines(); f.close() except: pass # doesn't seem to exist; no problem try: f = open(sdmshrc) usrarr = f.readlines(); f.close() except: pass # doesn't seem to exist for arr in [ genarr, usrarr ]: for row in arr: val = parseSdmshrcEntry(row) if val != None: if (server.has_key(val[0])): server[val[0]] = val[1] else: server.update({ val[0] : val[1] }) return server # ===================================================================================================================== # C O N N E C T T O B I C S U I T E # ===================================================================================================================== def connectToBICsuite(): server = readSdmshrc() conn = sdms.SDMSConnectionOpenV2(server, server['USER'], server['PASSWORD'], "Ryan's Utility") try: while conn.has_key('ERROR'): sleep(15) conn = sdms.SDMSConnectionOpenV2(server, server['USER'], server['PASSWORD'], "Ryan's Utility") except: pass # conn is now a socket return conn # ===================================================================================================================== # P R O C E S S C O M M A N D L I N E A R G U M E N T S # ===================================================================================================================== def processParms(): return # ===================================================================================================================== # M A I N # ===================================================================================================================== def main(): rc = 0 processParms() conn = connectToBICsuite() for obj in objlist: # I'm using "show object" instead of "show comment on object" here, because # the "show comment" could return a Not Found, whereas the "sho object" won't # (as far as the referenced object exists) stmt = 'show ' + obj resultstmt = 'create or alter comment on ' + obj + ' with\n' result = sdms.SDMSCommandWithSoc(conn, stmt) if result.has_key('ERROR'): print str(result) rc = 1 break # fail fast cmts = result[DATA][RECORD][COMMENT][TABLE] cmttype = result[DATA][RECORD][CMTTYPE] if (cmttype.upper() == 'URL'): print("WARNING: " + obj + " has a comment type URL which cannot be extended") continue; sep = '' for cmt in cmts: tag = cmt[TAG] if (tag == None or tag == ''): tag = 'NONE' else: tag = "'" + tag + "'" desc = "'" + cmt[DESC] + "'" resultstmt = resultstmt + sep + 'TAG = ' + tag + ", " + 'TEXT = ' + desc sep = ',\n' # Instead of executing the statements by themselves, they could be sored in an array # in order to execute the as a multicommand, which turns the action into a null-action # in case of errors, and the action can be repeated without adding multiple comment lines resultstmt = resultstmt + sep + 'TAG = ' + newtag + ", " + 'TEXT = ' + newdesc result = sdms.SDMSCommandWithSoc(conn, resultstmt) if result.has_key('ERROR'): print str(result) rc = 1 break # fail fast else: print("Successfully created comment line for " + obj) if conn != None: sdms.SDMSConnectionClose(conn) return rc rc = main() exit(rc)