diff --git a/pyproject.toml b/pyproject.toml index 5eea633..f2956dd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "rda_python_common" -version = "2.0.2" +version = "2.0.3" authors = [ { name="Zaihua Ji", email="zji@ucar.edu" }, ] @@ -33,4 +33,3 @@ pythonpath = [ [project.scripts] pgpassword = "rda_python_common.pgpassword:main" -pg_pass = "rda_python_common.pg_pass:main" diff --git a/src/rda_python_common/pg_pass.py b/src/rda_python_common/pg_pass.py deleted file mode 100644 index eedbae8..0000000 --- a/src/rda_python_common/pg_pass.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python3 -# -################################################################################## -# -# Title: pg_pass -# Author: Zaihua Ji, zji@ucar.edu -# Date: 2025-10-27 -# 2025-12-02 convert to class PgPassword -# Purpose: python script to retrieve passwords for postgrsql login to connect a -# gdex database from inside an python application -# -# Github: https://github.com/NCAR/rda-python-common.git -# -################################################################################## - -import sys -import re -from .pg_dbi import PgDBI - -class PgPassword(PgDBI): - - def __init__(self): - super().__init__() # initialize parent class - self.DBFLDS = { - 'd' : 'dbname', - 'c' : 'scname', - 'h' : 'dbhost', - 'p' : 'dbport', - 'u' : 'lnname' - } - self.DBINFO = { - 'dbname' : "", - 'scname' : "", - 'lnname' : "", - 'dbhost' : "", - 'dbport' : 5432 - } - self.dbopt = False - self.password = '' - - # read in command line parameters - def read_parameters(self): - argv = sys.argv[1:] - opt = None - dohelp = True - for arg in argv: - if re.match(r'^-\w+$', arg): - opt = arg[1:] - elif opt: - if opt == 'l': - self.PGDBI['BAOURL'] = arg - elif opt == 'k': - self.PGDBI['BAOTOKEN'] = arg - elif opt in self.DBFLDS: - self.dbopt = True - self.DBINFO[self.DBFLDS[opt]] = arg - else: - self.pglog(arg + ": Unknown option", self.LGEREX) - dohelp = False - else: - self.pglog(arg + ": Value provided without option", self.LGEREX) - if dohelp: - print("Usage: pg_pass [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\") - print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]") - print(" -l OpenBao URL to retrieve passwords") - print(" -k OpenBao Token Name to retrieve passwords") - print(" -d PostgreSQL Database Name") - print(" -c PostgreSQL Schema Name") - print(" -u PostgreSQL Login User Name") - print(" -h PostgreSQL Server Host Name") - print(" -p PostgreSQL Port Number") - sys.exit(0) - - # get the pgpassword - def start_actions(self): - if self.dbopt: - self.default_scinfo(self.DBINFO['dbname'], self.DBINFO['scname'], self.DBINFO['dbhost'], - self.DBINFO['lnname'], None, self.DBINFO['dbport']) - self.password = self.get_baopassword() - if not self.password: self.password = self.get_pg_pass() - -# main function to excecute this script -def main(): - object = PgPassword() - object.read_parameters() - object.start_actions() - print(object.password) - sys.exit(0) - -# call main() to start program -if __name__ == "__main__": main() diff --git a/src/rda_python_common/pg_password.py b/src/rda_python_common/pg_password.py new file mode 100644 index 0000000..c24b598 --- /dev/null +++ b/src/rda_python_common/pg_password.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +# +################################################################################## +# +# Title: pgpassword +# Author: Zaihua Ji, zji@ucar.edu +# Date: 2025-10-27 +# Purpose: python script to retrieve passwords for postgrsql login to connect a +# gdex database from inside an python application +# +# Github: https://github.com/NCAR/rda-python-common.git +# +################################################################################## + +import os +import sys +import re +import pwd +import hvac +from . import PgLOG +from . import PgDBI + +DBFLDS = { + 'd' : 'dbname', + 'c' : 'scname', + 'h' : 'dbhost', + 'p' : 'dbport', + 'u' : 'lnname' +} + +DBINFO = { + 'dbname' : "", + 'scname' : "", + 'lnname' : "", + 'dbhost' : "", + 'dbport' : 5432 +} + +# +# main function to excecute this script +# +def main(): + + permit = False + aname = 'pgpassword' + argv = sys.argv[1:] + opt = None + dohelp = True + dbopt = False + + for arg in argv: + if re.match(r'^-\w+$', arg): + opt = arg[1:] + elif opt: + if opt == 'l': + PgDBI.PGDBI['BAOURL'] = arg + elif opt == 'k': + PgDBI.PGDBI['BAOTOKEN'] = arg + elif opt in DBFLDS: + dbopt = True + DBINFO[DBFLDS[opt]] = arg + else: + PgLOG.pglog(arg + ": Unknown option", PgLOG.LGEREX) + dohelp = False + else: + PgLOG.pglog(arg + ": Value provided without option", PgLOG.LGEREX) + + if dohelp: + print("Usage: pgpassword [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\") + print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]") + print(" -l OpenBao URL to retrieve passwords") + print(" -k OpenBao Token Name to retrieve passwords") + print(" -d PostgreSQL Database Name") + print(" -c PostgreSQL Schema Name") + print(" -u PostgreSQL Login User Name") + print(" -h PostgreSQL Server Host Name") + print(" -p PostgreSQL Port Number") + sys.exit(0) + + if dbopt: + PgDBI.default_scinfo(DBINFO['dbname'], DBINFO['scname'], DBINFO['dbhost'], + DBINFO['lnname'], None, DBINFO['dbport']) + + pwname = PgDBI.get_baopassword() + if not pwname: pwname = PgDBI.get_pgpassword() + print(pwname) + sys.exit(0) + +# +# call main() to start program +# +if __name__ == "__main__": main() diff --git a/src/rda_python_common/pgpassword.py b/src/rda_python_common/pgpassword.py index c24b598..eedbae8 100644 --- a/src/rda_python_common/pgpassword.py +++ b/src/rda_python_common/pgpassword.py @@ -2,9 +2,10 @@ # ################################################################################## # -# Title: pgpassword +# Title: pg_pass # Author: Zaihua Ji, zji@ucar.edu # Date: 2025-10-27 +# 2025-12-02 convert to class PgPassword # Purpose: python script to retrieve passwords for postgrsql login to connect a # gdex database from inside an python application # @@ -12,81 +13,79 @@ # ################################################################################## -import os import sys import re -import pwd -import hvac -from . import PgLOG -from . import PgDBI +from .pg_dbi import PgDBI -DBFLDS = { - 'd' : 'dbname', - 'c' : 'scname', - 'h' : 'dbhost', - 'p' : 'dbport', - 'u' : 'lnname' -} +class PgPassword(PgDBI): -DBINFO = { - 'dbname' : "", - 'scname' : "", - 'lnname' : "", - 'dbhost' : "", - 'dbport' : 5432 -} + def __init__(self): + super().__init__() # initialize parent class + self.DBFLDS = { + 'd' : 'dbname', + 'c' : 'scname', + 'h' : 'dbhost', + 'p' : 'dbport', + 'u' : 'lnname' + } + self.DBINFO = { + 'dbname' : "", + 'scname' : "", + 'lnname' : "", + 'dbhost' : "", + 'dbport' : 5432 + } + self.dbopt = False + self.password = '' -# -# main function to excecute this script -# -def main(): - - permit = False - aname = 'pgpassword' - argv = sys.argv[1:] - opt = None - dohelp = True - dbopt = False - - for arg in argv: - if re.match(r'^-\w+$', arg): - opt = arg[1:] - elif opt: - if opt == 'l': - PgDBI.PGDBI['BAOURL'] = arg - elif opt == 'k': - PgDBI.PGDBI['BAOTOKEN'] = arg - elif opt in DBFLDS: - dbopt = True - DBINFO[DBFLDS[opt]] = arg + # read in command line parameters + def read_parameters(self): + argv = sys.argv[1:] + opt = None + dohelp = True + for arg in argv: + if re.match(r'^-\w+$', arg): + opt = arg[1:] + elif opt: + if opt == 'l': + self.PGDBI['BAOURL'] = arg + elif opt == 'k': + self.PGDBI['BAOTOKEN'] = arg + elif opt in self.DBFLDS: + self.dbopt = True + self.DBINFO[self.DBFLDS[opt]] = arg + else: + self.pglog(arg + ": Unknown option", self.LGEREX) + dohelp = False else: - PgLOG.pglog(arg + ": Unknown option", PgLOG.LGEREX) - dohelp = False - else: - PgLOG.pglog(arg + ": Value provided without option", PgLOG.LGEREX) + self.pglog(arg + ": Value provided without option", self.LGEREX) + if dohelp: + print("Usage: pg_pass [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\") + print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]") + print(" -l OpenBao URL to retrieve passwords") + print(" -k OpenBao Token Name to retrieve passwords") + print(" -d PostgreSQL Database Name") + print(" -c PostgreSQL Schema Name") + print(" -u PostgreSQL Login User Name") + print(" -h PostgreSQL Server Host Name") + print(" -p PostgreSQL Port Number") + sys.exit(0) - if dohelp: - print("Usage: pgpassword [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\") - print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]") - print(" -l OpenBao URL to retrieve passwords") - print(" -k OpenBao Token Name to retrieve passwords") - print(" -d PostgreSQL Database Name") - print(" -c PostgreSQL Schema Name") - print(" -u PostgreSQL Login User Name") - print(" -h PostgreSQL Server Host Name") - print(" -p PostgreSQL Port Number") - sys.exit(0) + # get the pgpassword + def start_actions(self): + if self.dbopt: + self.default_scinfo(self.DBINFO['dbname'], self.DBINFO['scname'], self.DBINFO['dbhost'], + self.DBINFO['lnname'], None, self.DBINFO['dbport']) + self.password = self.get_baopassword() + if not self.password: self.password = self.get_pg_pass() - if dbopt: - PgDBI.default_scinfo(DBINFO['dbname'], DBINFO['scname'], DBINFO['dbhost'], - DBINFO['lnname'], None, DBINFO['dbport']) - - pwname = PgDBI.get_baopassword() - if not pwname: pwname = PgDBI.get_pgpassword() - print(pwname) +# main function to excecute this script +def main(): + object = PgPassword() + object.read_parameters() + object.start_actions() + print(object.password) sys.exit(0) -# # call main() to start program -# if __name__ == "__main__": main() diff --git a/test/test_common.py b/test/test_common.py index 4d9fe98..3e370b8 100644 --- a/test/test_common.py +++ b/test/test_common.py @@ -14,7 +14,6 @@ def test_common(): import rda_python_common.PgSIG import rda_python_common.PgSplit import rda_python_common.PgUtil - import rda_python_common.pgpassword import rda_python_common.pg_cmd import rda_python_common.pg_dbi import rda_python_common.pg_file @@ -24,4 +23,4 @@ def test_common(): import rda_python_common.pg_sig import rda_python_common.pg_split import rda_python_common.pg_util - import rda_python_common.pg_pass + import rda_python_common.pgpassword