示例#1
0
def init(loc='.'):

    fp = os.path.abspath(os.path.join(loc, '.hs_auth'))
    if os.path.exists(fp):
        print(f'Auth already exists: {fp}')
        remove = input('Do you want to replace it [Y/n]')
        if remove.lower() == 'n':
            sys.exit(0)
        os.remove(fp)

    usr = input('Enter HydroShare Username: '******'Enter HydroShare Password: '******'usr': usr,
           'pwd': pwd}
    cred_json_string = str.encode(json.dumps(dat))
    cred_encoded = base64.b64encode(cred_json_string)
    with open(fp, 'w') as f:
        f.write(cred_encoded.decode('utf-8'))

    try:
        hydroshare.hydroshare(authfile=fp)
    except Exception:
        print('Authentication Failed')
        os.remove(fp)
        sys.exit(1)

    print(f'Auth saved to: {fp}')
示例#2
0
def main(args):

    if args.v:
        log.set_verbose()
    if args.q:
        log.set_quiet()

    title = ' '.join(args.title)
    abstract = ' '.join(args.abstract)

    # make sure input files exist
    for f in args.files:
        if not os.path.exists(f):
            raise Exception(f'Could not find file: {f}')
            sys.exit(1)

    # connect to hydroshare
    hs = hydroshare.hydroshare()
    if hs is None:
        raise Exception(f'Connection to HydroShare failed')
        sys.exit(1)

    # get the hydroshare data
    resource_id = create_resource(hs,
                                  abstract,
                                  title,
                                  keywords=args.keywords,
                                  content_files=args.files)

    # return the path to the data
    logger.info(f'\nhttps://hydroshare.org/resource/{resource_id}')
示例#3
0
    def test_file_doesnt_exist(self):
        title = 'unit testing'
        abstract = 'this is a resource created by a unittest'
        keywords = ['test']

        # test that an exception is raised if an input file doesn't exist
        hs = hydroshare.hydroshare(authfile=self.authfile)
        with self.assertRaises(Exception):
            resid = hs.createResource(abstract,
                                      title,
                                      keywords,
                                      content_files=['wrong_name.txt'])
        hs.close()
示例#4
0
    def test_add_file_to_resource(self):

        # test that resource is created successfully without files
        hs = hydroshare.hydroshare(authfile=self.authfile)
        resid = hs.createResource(self.abstract,
                                  self.title,
                                  self.keywords,
                                  content_files=[])
        self.assertTrue(resid is not None)

        resid = hs.addContentToExistingResource(resid, self.testfile)
        self.assertTrue(resid is not None)

        hs.hs.deleteResource(resid)
        hs.close()
示例#5
0
    def test_set_directory(self):
        """
        tests that the download directory is set properly
        """

        # test no download directory set
        hs = hydroshare.hydroshare(authfile=self.authfile)
        self.assertTrue(hs.download_dir == '.')
        hs.close()

        # test that an exception is raised if the save directory doesn't exist
        with self.assertRaises(Exception) as context:
            hs = hydroshare.hydroshare(authfile=self.authfile,
                                       save_dir='/dir_doesnt_exist')
        self.assertTrue('does not exist' in str(context.exception))
        hs.close()

        # test with directory as input
        d = 'test_directory_please_remove'
        os.makedirs(d)
        hs = hydroshare.hydroshare(authfile=self.authfile, save_dir=d)
        self.assertTrue(hs.download_dir == d)
        hs.close()
        os.rmdir(d)
示例#6
0
    def test_file_already_exists(self):

        hs = hydroshare.hydroshare(authfile=self.authfile)
        resid = hs.createResource(self.abstract,
                                  self.title,
                                  self.keywords,
                                  content_files=[self.testfile])
        self.assertTrue(resid is not None)

        # test adding file that already exists in the resource
        with self.assertRaises(Exception):
            files = [self.testfile]
            resid = hs.addContentToExistingResource(resid, files)

        hs.hs.deleteResource(resid)
        hs.close()
示例#7
0
    def test_add_wrong_path(self):

        hs = hydroshare.hydroshare(authfile=self.authfile)
        resid = hs.createResource(self.abstract,
                                  self.title,
                                  self.keywords,
                                  content_files=[])
        self.assertTrue(resid is not None)

        # test that an exception is raised if an input file doesn't exist
        with self.assertRaises(Exception):
            f = 'file_that_doesnt_exist.txt'
            resid = hs.addContentToExistingResource(resid, f)

        hs.hs.deleteResource(resid)
        hs.close()
示例#8
0
    def test_one_file(self):

        title = 'unit testing'
        abstract = 'this is a resource created by a unittest'
        keywords = ['test']

        # test that resource is created successfully
        hs = hydroshare.hydroshare(authfile=self.authfile)
        resid = hs.createResource(abstract,
                                  title,
                                  keywords,
                                  content_files=[self.testfile])
        self.assertTrue(resid is not None)

        scimeta = hs.getResourceMetadata(resid)
        self.assertTrue(type(scimeta) == ResourceMetadata)
        hs.hs.deleteResource(resid)
        hs.close()
示例#9
0
def main(args):

    if args.v:
        log.set_verbose()
    if args.q:
        log.set_quiet()

    # connect to hydroshare
    hs = hydroshare.hydroshare()
    if hs is None:
        raise Exception(f'Connection to HydroShare failed')
        sys.exit(1)

    for resid in args.resource_id:
        try:
            delete_resource(hs, resid)
        except Exception as e:
            print(f'  {str(e)}')
示例#10
0
    def test_get_file(self):

        # create a temp dir
        d = os.path.join(os.path.dirname(__file__),
                         'test_directory_please_remove')
        os.makedirs(d)

        # instantiate HS
        hs = hydroshare.hydroshare(authfile=self.authfile, save_dir=d)
        self.assertTrue(hs.download_dir == d)

        # download a published resource
        resid = '1be4d7902c87481d85b93daad99cf471'
        hs.getResource(resid)
        self.assertTrue(os.path.exists(os.path.join(d, f'{resid}')))

        # clean up
        hs.close()
        shutil.rmtree(d)
示例#11
0
    def test_no_files(self):

        title = 'unit testing'
        abstract = 'this is a resource created by a unittest'
        keywords = ['test']
        # test that resource is created successfully without files
        hs = hydroshare.hydroshare(authfile=self.authfile)
        try:
            resid = hs.createResource(abstract,
                                      title,
                                      keywords,
                                      content_files=[])
            self.assertTrue(resid is not None)
        except Exception as e:
            print(e.status_msg)

        scimeta = hs.getResourceMetadata(resid)
        self.assertTrue(type(scimeta) == ResourceMetadata)
        hs.hs.deleteResource(resid)
        hs.close()
示例#12
0
    def test_add_multiple(self):

        hs = hydroshare.hydroshare(authfile=self.authfile)
        resid = hs.createResource(self.abstract,
                                  self.title,
                                  self.keywords,
                                  content_files=[])
        self.assertTrue(resid is not None)

        # test that resource is created successfully with list input
        files = [self.testfile, self.testfile2]
        for f in files:
            resid = hs.addContentToExistingResource(resid, f)
            self.assertTrue(resid is not None)

        # get file from resource, make sure it exists in the resource
        filenames = [r['file_name'] for r in hs.getResourceFiles(resid)]
        self.assertTrue(self.testfile in filenames)

        hs.hs.deleteResource(resid)
        hs.close()
示例#13
0
文件: ls.py 项目: Castronova/hstools
def main(args):

    # check filter
    filters = {}
    if args.filter:
        for f in args.filter:
            pf = parse_filter(f)
            if pf is not None:
                filters.update(pf)
            else:
                sys.exit(1)

    # connect to hydroshare
    hs = hydroshare.hydroshare()
    userinfo = hs.userInfo()

    print_resource_list(hs,
                        userinfo['username'],
                        filter_dict=filters,
                        count=args.n,
                        long_format=args.l,
                        size=args.s)
示例#14
0
文件: get.py 项目: Castronova/hstools
def main(args):

    if args.v:
        log.set_verbose()
    if args.q:
        log.set_quiet()

    # create output directory if it doesn't already exist
    try:
        if not os.path.exists(args.save_dir):
            os.makedirs(args.save_dir)
    except Exception as e:
        raise Exception(f'Could not create output directory: {e}')
        sys.exit(1)

    # connect to hydroshare
    hs = hydroshare.hydroshare(save_dir=args.save_dir)
    if hs is None:
        sys.exit(1)

    # get the hydroshare data
    get_resource(hs, args.resource_id, args.f)
示例#15
0
def main(args):

    if args.v:
        log.set_verbose()
    if args.q:
        log.set_quiet()

    # connect to hydroshare
    hs = hydroshare.hydroshare()
    if hs is None:
        raise Exception(f'Connection to HydroShare failed')
        sys.exit(1)

    # separate the file source and target paths
    sources = []
    targets = []
    for f in args.files:
        if ':' in f:
            src, tar = f.split(':')

            # make sure the target doesn't contain a beginning
            # or trailing slash
            tar = tar.strip('/')

        else:
            src = f
            tar = os.path.basename(src)

        sources.append(src)
        targets.append(tar)

    # make sure input files exist
    for f in sources:
        if not os.path.exists(f):
            raise Exception(f'Could not find file: {f}')
            sys.exit(1)

    # get the resource files
    try:
        response = hs.getResourceFiles(args.resource_id)
        res_files = []
        if len(response) > 0:
            for r in response:
                # get the filepath from the content url field
                file_path = r['url'].split('contents')[-1]
                # remove slash at beginning
                file_path = file_path[1:] if file_path[0] == '/' else file_path
                res_files.append(file_path)
    except Exception:
        raise Exception('Error connecting to HydroShare resource')

    # make sure files don't already exist in the resource
    file_conflicts = []
    if len(res_files) > 0:
        for rf in res_files:
            if rf in targets:
                file_conflicts.append(rf)

    if len(file_conflicts) > 0:
        if not args.overwrite:
            for fc in file_conflicts:
                print(f' - {fc} already exists, use --overwrite to replace')
                res_files.pop(res_files.index(fc))
                tidx = targets.index(fc)
                sources.pop(tidx)
                targets.pop(tidx)
        else:
            for fc in file_conflicts:
                logger.info(f'- removing: {fc}')
                hs.hs.deleteResourceFile(args.resource_id, fc)

    # create folders if necessary
    res_dirs = [os.path.dirname(p) for p in res_files]
    for i in range(0, len(targets)):
        target_path = os.path.dirname(targets[i])
        if target_path not in res_dirs:
            print(f'+ creating resource directory: {target_path}')

            # need a try/except loop here because hs.getResourceFiles will
            # not return empty directories. There is a chance that the
            # target directory already exists on HS but is empty
            try:
                hs.hs.createResourceFolder(args.resource_id,
                                           pathname=target_path)
            except Exception:
                pass

    # loop through the files and add each individually
    for i in range(0, len(sources)):
        try:
            add_file(hs, args.resource_id, sources[i], targets[i])
        except Exception:
            print(f'- failed to add file {sources[i]:targets[i]}')
示例#16
0
def main(args):

    if args.v:
        log.set_verbose()

    # connect to hydroshare
    hs = hydroshare.hydroshare()
    if hs is None:
        raise Exception(f'Connection to HydroShare failed')
        sys.exit(1)

    if args.resource_id:
        print('-' * 50)

    # loop through input resources
    for r in args.resource_id:
        try:
            meta = hs.getResourceMetadata(r)
            meta_dict = {k: v for k, v in vars(meta).items()
                         if not k.startswith('_')}

            if args.terms:
                # filter based on specified data types
                meta_filtered = {}
                for term in args.terms:
                    if term in meta_dict.keys():
                        meta_filtered[term] = meta_dict[term]
                    else:
                        logger.error(f'  - Unknown metadata term {term}')
                meta_dict = meta_filtered

            # if not verbose, remove some of the metadata
            elif not args.long:
                short_keys = ['abstract',
                              'authors',
                              'creators',
                              'date_created',
                              'title']
                meta_dict = {k: meta_dict[k] for k in short_keys}

                # clean strings
                for k, v in meta_dict.items():
                    if type(v) == type(str):
                        meta_dict[k] = v.replace('\n', '')

                # shorten author and creator data
                meta_dict['authors'] = ';'.join(meta_dict['authors'])

                creator_values = []
                for creator in meta_dict['creators']:
                    creator_values.append(creator['name'])
                meta_dict['creators'] = ';'.join(creator_values)

            if args.yaml:
                class literal(str):
                    pass

                def literal_presenter(dumper, data):
                    return dumper.represent_scalar('tag:yaml.org,2002:str',
                                                   data, style='|')
                yaml.add_representer(literal, literal_presenter)
                v = meta_dict['abstract']
                meta_dict['abstract'] = literal(v)
                print(yaml.dump(meta_dict))

            if args.json:
                # query scientific metadata
                print(json.dumps(meta_dict,
                                 indent=4,
                                 sort_keys=True))

            # organize files for tree printing
            urls = []
            for file_info in hs.getResourceFiles(r):
                rpth = file_info['url'].split('contents/')[-1]
                urls.append(rpth)
            ftree = dict([get_tree('tree', urls, '')])['tree']
            tree_print(ftree)

            print('-' * 50)

        except Exception as e:
            print(e)