java/sandbox/data-cat/src/main/python/hpsdatacat
--- java/sandbox/data-cat/src/main/python/hpsdatacat/util.py (rev 0)
+++ java/sandbox/data-cat/src/main/python/hpsdatacat/util.py 2014-06-02 20:04:00 UTC (rev 648)
@@ -0,0 +1,86 @@
+import getpass, socket, subprocess
+
+# location of data catalog script at SLAC
+__datacat_script = '~srs/datacat/prod/datacat-hps'
+
+# commands that have script wrappers for them
+__valid_commands = ( 'rm', 'registerDataset', 'addLocation', 'addMetaData', 'find' )
+
+# default site for data catalog search
+__default_site = 'SLAC'
+
+# default base path for datacatalog search
+__default_search_path = '/HPS'
+
+"""
+Get the default site.
+"""
+def get_default_site():
+ return __default_site
+
+"""
+Get the default search path.
+"""
+def get_default_search_path():
+ return __default_search_path
+
+"""
+Simple utility to return the full script command.
+This function will check if the command is valid.
+"""
+def get_datacat_command(command):
+ if command not in __valid_commands:
+ raise Exception("Unknown command: " % command)
+ return '%s %s' % (__datacat_script, command)
+
+
+"""
+Get an SSH connection_string string for the SLAC or JLAB sites.
+This function will return null if not running at those sites,
+in which case the caller needs to provide their own (usually
+through a command line argument to one of the scripts).
+"""
+def get_ssh_connection_string():
+
+ # setup default connection_string
+ connection_string = None
+ domainname = socket.getfqdn()
+
+ if 'slac' in domainname:
+ username = getpass.getuser()
+ elif 'jlab' in domainname:
+ username = 'hpsdatacat'
+ else:
+ username = None
+
+ if username != None:
+ connection_string = [log in to unmask] % username
+
+ return connection_string
+
+"""
+Run a process in a shell and return the output lines, errors, and return value (in that order).
+"""
+def run_process(cmd, printToScreen=True):
+
+ process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+ errors = []
+ lines = []
+ for line in process.stdout.readlines():
+ if printToScreen == True:
+ print line,
+ if 'Exception' in line:
+ errors.append(line)
+ lines.append(line)
+
+ retval = process.wait()
+
+ return lines, errors, retval
+
+"""
+Escape characters for the SSH command.
+These include double quotes, spaces, and ampersands.
+"""
+def escape_characters(raw_string):
+ return raw_string.replace('"', '\\"').replace(' ', '\\ ').replace('&', '\\&')
+
\ No newline at end of file