From 1f4e5a8c6631a83ee73bb7fdd80cb858c33a0e86 Mon Sep 17 00:00:00 2001
From: Auke Klazema <klazema@astron.nl>
Date: Tue, 5 Mar 2019 12:24:10 +0000
Subject: [PATCH] SW-598: Add username, password, port options to
 create_CDB_objects.py

---
 .../data/Coordinates/create_CDB_objects.py    | 94 +++++++++++++------
 1 file changed, 66 insertions(+), 28 deletions(-)

diff --git a/MAC/Deployment/data/Coordinates/create_CDB_objects.py b/MAC/Deployment/data/Coordinates/create_CDB_objects.py
index 65472f890e3..e3f265edb36 100755
--- a/MAC/Deployment/data/Coordinates/create_CDB_objects.py
+++ b/MAC/Deployment/data/Coordinates/create_CDB_objects.py
@@ -1,63 +1,101 @@
 #!/usr/bin/env python
-import re,sys,pg
-from database import *
+import re
+import pg
+from optparse import OptionParser
+import getpass
 
-# get info from database.py
-dbName=getDBname()
-dbHost=getDBhost()
 
 #
 # findStationInfo(stationName)
 #
-def findStationInfo(stationName):
+def find_station_info(station_name):
     """
     Return all basic station info (eg. nr RSPboards) from a station.
     """
-    pattern=re.compile("^"+stationName+"[ \t].*", re.IGNORECASE | re.MULTILINE)
+    pattern = re.compile("^"+station_name+"[ \t].*", re.IGNORECASE | re.MULTILINE)
     match = pattern.search(open("../StaticMetaData/StationInfo.dat").read())
     if not match:
-        raise "\nFatal error: "+stationName+" is not defined in file 'StationInfo.dat'"
+        raise "\nFatal error: "+station_name+" is not defined in file 'StationInfo.dat'"
     return match.group().split()
 
+
 #
 # getStationList
 #
-def getStationList():
+def get_station_list():
     """
     Returns a list containing all stationnames
     """
-    pattern=re.compile("^[A-Z]{2}[0-9]{3}[ \t].*", re.IGNORECASE | re.MULTILINE)
-    return [ station.split()[0] for station in pattern.findall(open("../StaticMetaData/StationInfo.dat").read())]
+    pattern = re.compile("^[A-Z]{2}[0-9]{3}[ \t].*", re.IGNORECASE | re.MULTILINE)
+    return [station.split()[0] for station in
+            pattern.findall(open("../StaticMetaData/StationInfo.dat").read())]
+
 
 #
 # MAIN
 #
 if __name__ == '__main__':
+    parser = OptionParser("Usage: %prog [options]")
+
+    parser.add_option("-D", "--database",
+                      dest="dbName",
+                      type="string",
+                      default="StationCoordinates",
+                      help="Name of StationCoordinates database to use")
+
+    parser.add_option("-H", "--host",
+                      dest="dbHost",
+                      type="string",
+                      default="sasdb.control.lofar",
+                      help="Hostname of StationCoordinates database")
+
+    parser.add_option("-P", "--port",
+                      dest="dbPort",
+                      type="int",
+                      default="5432",
+                      help="Port of StationCoordinates database")
+
+    parser.add_option("-U", "--user",
+                      dest="dbUser",
+                      type="string",
+                      default="postgres",
+                      help="Username of StationCoordinates database")
+
+    # parse arguments
+
+    (options, args) = parser.parse_args()
+
+    dbName = options.dbName
+    dbHost = options.dbHost
+    dbPort = options.dbPort
+    dbUser = options.dbUser
+
+    dbPassword = getpass.getpass()
+
     print "Connecting to database ", dbName
-    db = pg.connect(user="postgres", host=dbHost, dbname=dbName)
-    
-    pol = 2 # number of polarizations
-    for station in getStationList():
-        print findStationInfo(station)
-        if (len(findStationInfo(station)) < 13):
+    db = pg.connect(user=dbUser, host=dbHost, dbname=dbName, port=dbPort, passwd=dbPassword)
+
+    pol = 2  # number of polarizations
+    for station in get_station_list():
+        print find_station_info(station)
+        if (len(find_station_info(station)) < 13):
             continue
-        (name, stationID, stnType, long, lat, height, nrRSP, nrTBB, nrLBA, nrHBA, nrPowecs, HBAsplit, LBAcal, Aartfaac ) = findStationInfo(station)
+        (name, stationID, stnType, long, lat, height, nrRSP, nrTBB,
+         nrLBA, nrHBA, nrPowecs, HBAsplit, LBAcal, Aartfaac) = find_station_info(station)
         if height[0] != '0':
             print "updating %s to the coordinate database " % station
             for lba in xrange(0, int(nrLBA)*2):
-                db.query("select * from add_object('%s', '%s', %d)" % ( name, "LBA", lba ))
-            db.query("select * from add_object('%s', '%s', %d)" % ( name, "CLBA", -1 ))
+                db.query("select * from add_object('%s', '%s', %d)" % (name, "LBA", lba))
+            db.query("select * from add_object('%s', '%s', %d)" % (name, "CLBA", -1))
             if HBAsplit == 'Yes':
                 for hba in xrange(0, int(nrHBA)):
-                    db.query("select * from add_object('%s', '%s', %d)" % ( name, "HBA0", hba ))
-                db.query("select * from add_object('%s', '%s', %d)" % ( name, "CHBA0", -1 ))
+                    db.query("select * from add_object('%s', '%s', %d)" % (name, "HBA0", hba))
+                db.query("select * from add_object('%s', '%s', %d)" % (name, "CHBA0", -1))
                 for hba in xrange(int(nrHBA), int(nrHBA)*2):
-                    db.query("select * from add_object('%s', '%s', %d)" % ( name, "HBA1", hba ))
-                db.query("select * from add_object('%s', '%s', %d)" % ( name, "CHBA1", -1 ))
+                    db.query("select * from add_object('%s', '%s', %d)" % (name, "HBA1", hba))
+                db.query("select * from add_object('%s', '%s', %d)" % (name, "CHBA1", -1))
             else:
                 for hba in xrange(0, int(nrHBA)*2):
-                    db.query("select * from add_object('%s', '%s', %d)" % ( name, "HBA", hba ))
-                db.query("select * from add_object('%s', '%s', %d)" % ( name, "CHBA", -1 ))
-            
-
+                    db.query("select * from add_object('%s', '%s', %d)" % (name, "HBA", hba))
+                db.query("select * from add_object('%s', '%s', %d)" % (name, "CHBA", -1))
 # ... to be continued
-- 
GitLab