def move(fileService, path, destinationFileService, destinationPath, replaceExisting=True, doCopy=True): """ Moves or copies a file or folder. :param fileService: name of fileService (string), or object (dictionary) that defines a file service. A list of these kind of objects available to the user is returned by the function Files.getFileServices(). :param path: String defining the origin path (in the remote fileService) of the file or directory to be copied/moved, starting from the root volume level. Example: rootVolume/userVolumeOwner/userVolume/fileToBeMoved.txt. :param destinationFileService: name of fileService (string), or object (dictionary) that defines a destination file service (where the file is moved/copied into). A list of these kind of objects available to the user is returned by the function Files.getFileServices(). :param destinationRelativePath: String defining the destination path (in the remote destinationFileService) of the file or directory to be copied/moved, starting from the root volume level. Example: rootVolume/userVolumeOwner/userVolume/recentlyMovedFile.txt. :param replaceExisting: If set to False, it will throw an error if the file already exists, If set to True, it will not throw and eeror in that case. :param doCopy: if set to True, then it will copy the file or folder. If set to False, then the file or folder will be moved. :raises: Throws an exception if the user is not logged into SciServer (use Authentication.login for that purpose). Throws an exception if the HTTP request to the FileService API returns an error. :example: fileServices = Files.getFileServices(); isDownloaded = Files.upload("/myUploadedFile.txt","persistent","myUserName", fileServices[0], localFilePath="/myDownloadedFile.txt"); .. seealso:: Files.getFileServices(), Files.getFileServiceFromName, Files.createDir, Files.delete, Files.upload, Files.dirList """ token = Authentication.getToken() if token is not None and token != "": if Config.isSciServerComputeEnvironment(): taskName = "Compute.SciScript-Python.Files.Move" else: taskName = "SciScript-Python.Files.Move" (rootVolume, userVolumeOwner, userVolume, relativePath) = splitPath(path); (destinationRootVolume, destinationUserVolumeOwner, destinationUserVolume, destinationRelativePath) = splitPath(destinationPath); if userVolumeOwner is None: userVolumeOwner = Authentication.getKeystoneUserWithToken(token).userName; if destinationUserVolumeOwner is None: destinationUserVolumeOwner = Authentication.getKeystoneUserWithToken(token).userName; if type(fileService) == str: fileService = getFileServiceFromName(fileService) if type(destinationFileService) == str: destinationFileService = getFileServiceFromName(destinationFileService) url = __getFileServiceAPIUrl(fileService) + "api/data/" + rootVolume + "/" + userVolumeOwner + "/" + userVolume + "/" + relativePath + "?replaceExisting=" + str(replaceExisting) + "&doCopy=" + str(doCopy) + "&TaskName=" + taskName; headers = {'X-Auth-Token': token, "Content-Type": "application/json"} if type(destinationFileService) == dict: destinationFileService = destinationFileService['name'] jsonDict = {'destinationPath': destinationRelativePath, 'destinationRootVolume': destinationRootVolume, 'destinationUserVolume':destinationUserVolume, 'destinationOwnerName': destinationUserVolumeOwner, 'destinationFileService': destinationFileService}; data = json.dumps(jsonDict).encode() res = requests.put(url, stream=True, headers=headers, json=jsonDict) if res.status_code < 200 or res.status_code >= 300: raise Exception("Error when moving '" + str(path) + "' in file service '" + fileService.get("name") + "' to '" + str(path) + "' in file service '" + destinationFileService.get("name)") + "'. \nHttp Response from FileService API returned status code " + str(res.status_code) + ":\n" + res.content.decode()); else: raise Exception("User token is not defined. First log into SciServer.")
def login(username, password): token1 = Authentication.login(username, password) user = Authentication.getKeystoneUserWithToken(token1) print("userName="******"id=" + user.id) iden = Authentication.identArgIdentifier() print("ident=" + iden)
def getSchemaName(): """ Returns the WebServiceID that identifies the schema for a user in MyScratch database with CasJobs. :return: WebServiceID of the user (string). :raises: Throws an exception if the user is not logged into SciServer (use Authentication.login for that purpose). Throws an exception if the HTTP request to the CasJobs API returns an error. :example: wsid = CasJobs.getSchemaName() .. seealso:: CasJobs.getTables. """ token = Authentication.getToken() if token is not None and token != "": keystoneUserId = Authentication.getKeystoneUserWithToken(token).id usersUrl = Config.CasJobsRESTUri + "/users/" + keystoneUserId headers = {'X-Auth-Token': token, 'Content-Type': 'application/json'} getResponse = requests.get(usersUrl, headers=headers) if getResponse.status_code != 200: raise Exception( "Error when getting schema name. Http Response from CasJobs API returned status code " + str(getResponse.status_code) + ":\n" + getResponse.content.decode()) jsonResponse = json.loads(getResponse.content.decode()) return "wsid_" + str(jsonResponse["WebServicesId"]) else: raise Exception("User token is not defined. First log into SciServer.")
def test_Authentication_allMethods(self): newToken1 = "myToken1" newToken2 = "myToken2" token1 = Authentication.login(Authentication_loginName, Authentication_loginPassword) token2 = Authentication.getToken() token3 = Authentication.getKeystoneToken() token4 = Authentication.token.value user = Authentication.getKeystoneUserWithToken(token1) iden = Authentication.identArgIdentifier() self.assertEqual(iden, "--ident=") self.assertNotEqual(token1, "") self.assertIsNot(token1, None) self.assertEqual(token1, token2) self.assertEqual(token1, token3) self.assertEqual(token1, token4) self.assertEqual(user.userName, Authentication_loginName) self.assertIsNot(user.id, None) self.assertNotEqual(user.id, "") Authentication.setToken(newToken1) self.assertEqual(newToken1, Authentication.getToken()) Authentication.setKeystoneToken(newToken2) self.assertEqual(newToken2, Authentication.getKeystoneToken())
def getSchemaName(token=""): if token == "": userToken = Authentication.getToken() else: userToken = token keystoneUserId = Authentication.getKeystoneUserWithToken(userToken).id usersUrl = Config.CasJobsRESTUri + "/users/" + keystoneUserId headers = {"X-Auth-Token": userToken, "Content-Type": "application/json"} getResponse = requests.get(usersUrl, headers=headers) jsonResponse = json.loads(getResponse.content.decode()) return "wsid_" + str(jsonResponse["WebServicesId"])
def __setupserver(self): print("Setting up SkyServer...") Authentication_loginName = self.username Authentication_loginPassword = self.password token1 = Authentication.login(Authentication_loginName, Authentication_loginPassword) token2 = Authentication.getToken() token3 = Authentication.getKeystoneToken() token4 = Authentication.token.value user = Authentication.getKeystoneUserWithToken(token1) iden = Authentication.identArgIdentifier()
def createDir(fileService, path, quiet=True): """ Create a directory. :param fileService: name of fileService (string), or object (dictionary) that defines a file service. A list of these kind of objects available to the user is returned by the function Files.getFileServices(). :param path: path (in the remote file service) to the directory (string), starting from the root volume level. Example: rootVolume/userVolumeOwner/userVolume/directory :param quiet: If set to False, it will throw an error if the directory already exists. If set to True. it will not throw an error. :raises: Throws an exception if the user is not logged into SciServer (use Authentication.login for that purpose). Throws an exception if the HTTP request to the FileService API returns an error. :example: fileServices = Files.getFileServices(); Files.createDir(fileServices[0], "myRootVolume","myUserVolume", "myNewDir"); .. seealso:: Files.getFileServices(), Files.getFileServiceFromName, Files.delete, Files.upload, Files.download, Files.dirList """ token = Authentication.getToken() if token is not None and token != "": if Config.isSciServerComputeEnvironment(): taskName = "Compute.SciScript-Python.Files.createDir" else: taskName = "SciScript-Python.Files.createDir" (rootVolume, userVolumeOwner, userVolume, relativePath) = splitPath(path); if not relativePath.startswith("/"): relativePath = "/" + relativePath; if userVolumeOwner is None: userVolumeOwner = Authentication.getKeystoneUserWithToken(token).userName; if type(fileService) == str: fileService = getFileServiceFromName(fileService) url = __getFileServiceAPIUrl(fileService) + "api/folder/" + rootVolume + "/" + userVolumeOwner + "/" + userVolume + "/" + relativePath + "?quiet=" + str(quiet) + "&TaskName=" + taskName; headers = {'X-Auth-Token': token} res = requests.put(url, headers=headers) if res.status_code >= 200 and res.status_code < 300: pass; else: raise Exception("Error when creating directory '" + str(path) + "' in file service '" + fileService.get('name') + "'.\nHttp Response from FileService API returned status code " + str(res.status_code) + ":\n" + res.content.decode()); else: raise Exception("User token is not defined. First log into SciServer.")
def authenticate_user(): try: token = Authentication.getToken() user = Authentication.getKeystoneUserWithToken(token) USERNAME = user.userName except requests.exceptions.ConnectionError as e: print("Connection Error: Authentication Token was not generated.") print(" Please log back in to SciServer and try again.") sys.exit(1) except Exception as e: if "Error when getting the keystone user with token" in str(e.args[0]): print( "Connection Error: Authentication failed. User not signed in." ) print( " Please log back in to SciServer, reboot terminal, and try again." ) else: print("Unexpected error:", sys.exc_info()[0]) raise sys.exit(1) else: return (token, USERNAME)
token1 = Authentication.login(Authentication_loginName, Authentication_loginPassword); token2 = Authentication.getToken() token3 = Authentication.getKeystoneToken() token4 = Authentication.token.value print("token1=" + token1)# print("token2=" + token2)# print("token3=" + token3)# print("token4=" + token4)# # In[ ]: #getting curent user info user = Authentication.getKeystoneUserWithToken(token1) print("userName="******"id=" + user.id) iden = Authentication.identArgIdentifier() print("ident="+iden) # In[ ]: #reseting the current token to another value: Authentication.setToken("myToken1") token5 = Authentication.getToken() Authentication.setKeystoneToken("myToken2") token6 = Authentication.getKeystoneToken()
#|emphysema| does the patient have a diagnosis for Emphysema | #|acute_bronchitist| does the patient have a diagnosis for Acute Bronchitis | #|cystic_fibrosis| does the patient have a diagnosis for Cystic Fibrosis | #|duration| duration in days between earliest and latest encounter| # Import Libraries & connect to DB import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline import seaborn as sns import getpass from SciServer import Authentication myUserName = Authentication.getKeystoneUserWithToken(Authentication.getToken()).userName passwd = getpass.getpass('Password for ' + myUserName + ': ') user = "******" + myUserName import sqlalchemy import urllib.parse #SQL Driver driver="FreeTDS" tds_ver="8.0" # Database host_ip="ESMPMDBPR4.WIN.AD.JHU.EDU" # Update this accordingly db_port="1433" db="CAMP_PMCoe_Projection" # Update this accordingly # Create Connection String conn_str=("DRIVER={};Server={};PORT={};DATABASE={};UID={};PWD={};TDS_VERSION={}" .format(driver, host_ip, db_port, db, user, passwd, tds_ver))