Exemplo n.º 1
0
def _promptLogin():
    print('New ShapeWorks Portal users: Register an account at ' +
          serverAddress + '#?dialog=register')
    print(
        'Returning ShapeWorks Portal users: Enter your username and password')

    basicAuthToken = None
    while basicAuthToken is None:
        print('')
        username = input("Username: "******"Password: "******"ascii")
        try:
            basicAuthToken = GirderAPI.authenticateBasicAuth(
                serverAddress, usernamePasswordHash)
        except ValueError as e:
            print('Incorrect username or password.')
    try:
        apiKey = GirderAPI.getApiKey(serverAddress, basicAuthToken,
                                     _API_KEY_NAME)
    except ValueError as e:
        apiKey = GirderAPI.createApiKey(serverAddress, basicAuthToken,
                                        _API_KEY_NAME)

    loginState = {'username': username, 'key': apiKey}
    _saveLogin(loginState)
    return loginState
Exemplo n.º 2
0
def _downloadFolder(accessToken, path, folder, parsedFileList=None):
    if not os.path.exists(path):
        os.makedirs(path)
    # 1 download items in this folder
    items = GirderAPI.listItemsInFolder(serverAddress, accessToken,
                                        folder['_id'])
    for item in items:
        subset = None if not parsedFileList else [
            elem for elem in parsedFileList
            if len(elem) == 1 and elem[0] == item['name']
        ]
        if subset is None or len(subset) > 0:
            GirderAPI.downloadItem(serverAddress, accessToken, path, item)

    # 2 for each subfolder, create directory in the file system and download every item in the subfolder
    for subfolder in GirderAPI.getFolderList(serverAddress,
                                             accessToken,
                                             parentType='folder',
                                             parentId=folder['_id']):
        subset = None if not parsedFileList else [
            elem[1:] for elem in parsedFileList
            if len(elem) > 1 and elem[0] == subfolder['name']
        ]
        if subset is None or len(subset) > 0:
            _downloadFolder(accessToken, path + '/' + subfolder['name'],
                            subfolder, subset)
Exemplo n.º 3
0
def downloadDatasetZip(accessToken, datasetName, destinationPath):
    # 1 get info of the use case collection
    useCaseCollection = GirderAPI.getCollectionInfo(serverAddress, accessToken,
                                                    _USE_CASE_DATA_COLLECTION)
    # 2 get info of the dataset folder in that collection
    datasetFolder = GirderAPI.getFolderInfo(serverAddress,
                                            accessToken,
                                            parentType='collection',
                                            parentId=useCaseCollection['_id'],
                                            folderName=datasetName)
    # 3 download the dataset folder
    GirderAPI.downloadFolder(serverAddress,
                             accessToken,
                             path=destinationPath,
                             folderInfo=datasetFolder)
Exemplo n.º 4
0
def downloadDataset(accessToken, datasetName, destinationPath, fileList=None):
    # 1 get info of the use case collection
    useCaseCollection = GirderAPI.getCollectionInfo(serverAddress, accessToken,
                                                    _USE_CASE_DATA_COLLECTION)
    # 2 get info of the dataset folder in that collection
    datasetFolder = GirderAPI.getFolderInfo(serverAddress,
                                            accessToken,
                                            parentType='collection',
                                            parentId=useCaseCollection['_id'],
                                            folderName=datasetName)
    # 3 download every item in the base dataset folder
    if fileList:
        _downloadFolder(accessToken, destinationPath, datasetFolder,
                        [_splitPathIntoParts(path) for path in fileList])
    else:
        _downloadFolder(accessToken, destinationPath, datasetFolder)
Exemplo n.º 5
0
def _loginAndGetAccessToken():

    loginState = _loadLogin()

    if loginState is None:
        print('Login info not found in current directory.')
        loginState = _promptLogin()
        if loginState is None:
            print('Login failed!')
            return (None, None)

    accessToken = GirderAPI.getAccessToken(serverAddress, loginState['key'])
    if accessToken is None:
        # Retry login once
        print('Login info in current directory is invalid.')
        loginState = _promptLogin()
        if loginState is None:
            print('Login failed!')
            return (None, None)
        accessToken = GirderAPI.getAccessToken(serverAddress,
                                               loginState['key'])
    return (loginState, accessToken)
Exemplo n.º 6
0
def login(loginState):
    if loginState is None:
        # interactive login mode
        loginState, accessToken = _loginAndGetAccessToken()
    else:
        # login using provided credentials
        if not _verifyLoginState(loginState):
            raise ValueError('Invalid login state')
        accessToken = GirderAPI.getAccessToken(loginState['key'])
    if accessToken is None:
        raise ValueError('Unable to get access token. %' %
                         _CONTACT_SUPPORT_STRING)
    return accessToken
Exemplo n.º 7
0
def uploadDataset(accessToken, datasetName, datasetPath):
    useCaseCollection = GirderAPI.getCollectionInfo(serverAddress, accessToken,
                                                    _USE_CASE_DATA_COLLECTION)
    if useCaseCollection is None:
        return False

    existingFileList = []
    if datasetName in getDatasetList(accessToken):
        # Get list of existing files to avoid overriding
        existingFileList = [
            os.path.join(datasetPath, f)
            for f in getFileList(accessToken, datasetName)
        ]
    return _uploadFolder(accessToken,
                         datasetName,
                         datasetPath,
                         useCaseCollection['_id'],
                         parentType='collection',
                         existingFileList=existingFileList)
Exemplo n.º 8
0
def _uploadFolder(accessToken,
                  folderName,
                  folderPath,
                  parentId,
                  parentType,
                  existingFileList=[]):
    try:
        folderInfo = GirderAPI.getFolderInfo(serverAddress,
                                             accessToken,
                                             parentType=parentType,
                                             parentId=parentId,
                                             folderName=folderName)
        print('Skip creating folder ' + folderPath)
    except:
        GirderAPI.createFolder(serverAddress,
                               accessToken,
                               parentId,
                               folderName,
                               parentType=parentType)

    folderInfo = GirderAPI.getFolderInfo(serverAddress,
                                         accessToken,
                                         parentType=parentType,
                                         parentId=parentId,
                                         folderName=folderName)
    for item in os.listdir(folderPath):
        # must use / here to match girder file paths
        itempath = os.path.join(folderPath, item)
        if os.path.isfile(itempath):
            if itempath in existingFileList:
                print('Skip uploading ' + itempath)
            else:
                GirderAPI.uploadFile(serverAddress,
                                     accessToken,
                                     folderInfo['_id'],
                                     item,
                                     itempath,
                                     parentType='folder')
        else:
            _uploadFolder(accessToken, item, itempath, folderInfo['_id'],
                          'folder', existingFileList)
Exemplo n.º 9
0
def getFileList(accessToken, datasetName):
    useCaseCollection = GirderAPI.getCollectionInfo(serverAddress, accessToken,
                                                    _USE_CASE_DATA_COLLECTION)
    datasetFolder = GirderAPI.getFolderInfo(serverAddress,
                                            accessToken,
                                            parentType='collection',
                                            parentId=useCaseCollection['_id'],
                                            folderName=datasetName)

    # Add items and folders of base directory
    items = GirderAPI.listItemsInFolder(serverAddress, accessToken,
                                        datasetFolder['_id'])
    subfolders = [
        (sf, '')
        for sf in GirderAPI.getFolderList(serverAddress,
                                          accessToken,
                                          parentType='folder',
                                          parentId=datasetFolder['_id'])
    ]
    itemPaths = [element['name'] for element in items]

    # Iterate through subfolders to get all the items
    while len(subfolders) > 0:
        subfolder = subfolders.pop(0)
        path = subfolder[1] + subfolder[0]['name'] + os.path.sep
        items = GirderAPI.listItemsInFolder(serverAddress, accessToken,
                                            subfolder[0]['_id'])
        itemPaths += [path + element['name'] for element in items]
        subfolders = [
            (sf, path)
            for sf in GirderAPI.getFolderList(serverAddress,
                                              accessToken,
                                              parentType='folder',
                                              parentId=subfolder[0]['_id'])
        ] + subfolders

    return itemPaths
Exemplo n.º 10
0
def getDatasetList(accessToken):
    useCaseCollection = GirderAPI.getCollectionInfo(serverAddress, accessToken,
                                                    _USE_CASE_DATA_COLLECTION)
    jsonList = GirderAPI.getFolderList(serverAddress, accessToken,
                                       'collection', useCaseCollection['_id'])
    return [element['name'] for element in jsonList]