예제 #1
0
def main():
    admin_token = create_user(mini_ws_admin)
    admin_ws = Workspace(url=mini_ws_url, token=admin_token)
    versions = load_narrative_type(admin_ws)
    versions = {"old_ver": "1.0", "new_ver": "2.0"}

    user_token = create_user(test_user)
    user_ws = Workspace(url=mini_ws_url, token=user_token)
    loaded_info = load_narrative_test_data(user_ws, versions)
    pprint(loaded_info)
예제 #2
0
    def setUpClass(cls):
        token = environ.get('KB_AUTH_TOKEN', None)
        config_file = environ.get('KB_DEPLOYMENT_CONFIG', None)
        cls.cfg = {}
        config = ConfigParser()
        config.read(config_file)

        for nameval in config.items('kb_cufflinks'):
            cls.cfg[nameval[0]] = nameval[1]
        # Getting username from Auth profile for token
        authServiceUrl = cls.cfg['auth-service-url']
        auth_client = _KBaseAuth(authServiceUrl)
        user_id = auth_client.get_user(token)
        # WARNING: don't call any logging methods on the context object,
        # it'll result in a NoneType error
        cls.ctx = MethodContext(None)
        cls.ctx.update({'token': token,
                        'user_id': user_id,
                        'provenance': [
                            {'service': 'kb_cufflinks',
                             'method': 'please_never_use_it_in_production',
                             'method_params': []
                             }],
                        'authenticated': 1})
        cls.wsURL = cls.cfg['workspace-url']
        cls.wsClient = Workspace(url=cls.wsURL, token=token)
        cls.serviceImpl = kb_cufflinks(cls.cfg)
        cls.scratch = cls.cfg['scratch']
        cls.callback_url = environ.get('SDK_CALLBACK_URL')
        cls.srv_wiz_url = cls.cfg['srv-wiz-url']

        # cls.wsName = 'cufflinks_test_' + user_id  # reuse existing workspace
        suffix = int(time.time() * 1000)
        cls.wsName = "test_kb_cufflinks_" + str(suffix)
        print('workspace_name: ' + cls.wsName)

        try:
            # reuse existing (previously torn down) workspace
            cls.wsClient.undelete_workspace({'workspace': cls.wsName})
            print('reusing old workspace...')
        except BaseException:
            try:
                # create if workspace does not exist
                cls.wsClient.create_workspace({'workspace': cls.wsName})
            except BaseException:
                # get workspace if it exists and was not previously deleted (previously
                # not torn down)
                ws_info = cls.wsClient.get_workspace_info({'workspace': cls.wsName})
                print("creating new workspace: " + str(ws_info))

        cls.dfu = DataFileUtil(cls.callback_url)

        cls.gfu = GenomeFileUtil(cls.callback_url)
        cls.ru = ReadsUtils(cls.callback_url)
        cls.rau = ReadsAlignmentUtils(cls.callback_url)
        cls.set_api = SetAPI(cls.srv_wiz_url, service_ver='dev')

        cls.cufflinks_runner = CufflinksUtils(cls.cfg)

        cls.prepare_data()
예제 #3
0
def get_object_uid(name):
    WS_URL = 'https://ci.kbase.us/services/ws/'
    from biokbase.workspace.client import Workspace
    ws = Workspace(WS_URL)
    info = ws.get_objects(
        [dict(workspace=os.environ['KB_WORKSPACE_ID'], name=name)])[0]['info']
    return '%s/%s/%s' % (info[6], info[0], info[4])
예제 #4
0
def get_genome(genome_id=None,
               workspace_id=None,
               token=None,
               workspace_url=None):
    #download genome object from workspace
    if workspace_url is None:
        workspace_url = 'https://kbase.us/services/ws/'
    if token is None:
        with open('/kb/dev_container/modules/genome_util/mytoken.txt'
                  ) as token_file:
            token = token_file.read()
        token = token.rstrip()

    #print token
    #print genome_id
    #print workspace_id

    workspace_client = Workspace(url=workspace_url, token=token)
    #genome=workspace_client.get_object({'id':genome_id, 'workspace':workspace_id, 'type':'KBaseGenomes.Genome'})
    genome = workspace_client.get_object({
        'id': 'Bifidobacterium_animalis_subsp._lactis_AD011',
        'type': 'KBaseGenomes.Genome',
        'workspace': 'plane83:1436884411390'
    })

    return genome
    def BuildFastaFromSequenceSet(self, ctx, params):
        """
        :param params: instance of type "BuildSeqIn" -> structure: parameter
           "workspace_name" of String, parameter "SequenceSetRef" of String,
           parameter "fasta_outpath" of String
        :returns: instance of type "BuildSeqOut" -> structure: parameter
           "fasta_outpath" of String
        """
        # ctx is the context object
        # return variables are: output
        #BEGIN BuildFastaFromSequenceSet
        #END BuildFastaFromSequenceSet

        # At some point might do deeper type checking...
        dfu = DataFileUtil(self.callback_url)

        bu = BackgroundUtils()
        TU = TestUtils()
        if params['TESTFLAG'] and params['background']:
            targetpath = '/kb/module/work/tmp/testgenome.fa'
            TU.GetGenome(targetpath)
            bu.BuildBackground(targetpath)
        elif params['background']:

            ws = Workspace('https://appdev.kbase.us/services/ws')
            subset = ws.get_object_subset([{
                                         'included':['/features/[*]/location', '/features/[*]/id','/assembly_ref'],
    'ref':params['genome_ref']}])
            aref = subset[0]['data']['assembly_ref']
            assembly_ref = {'ref': aref}
            print('Downloading Assembly data as a Fasta file.')
            assemblyUtil = AssemblyUtil(self.callback_url)
            fasta_file = assemblyUtil.get_assembly_as_fasta(assembly_ref)['path']
            bu.BuildBackground(fasta_file)


        get_objects_params = {'object_refs' : [params['SequenceSetRef']]}

        SeqSet = dfu.get_objects(get_objects_params)['data'][0]['data']
        outFile = open(params['fasta_outpath'],'w')
        for s in SeqSet['sequences']:
            sname = '>' + s['sequence_id'] + '\n'
            outFile.write(sname)
            sseq = s['sequence'] + '\n'
            outFile.write(sseq)
        outFile.close()

        fu=FastaUtils()
        if params['mask_repeats']:
            fu.RemoveRepeats(params['fasta_outpath'],params['fasta_outpath'])

        output = {'fasta_outpath' : params['fasta_outpath']}
        #END BuildFastaFromSequenceSet

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method BuildFastaFromSequenceSet return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]
예제 #6
0
def main():

    endpoint = 'prod'

    ws = Workspace(WS_URL[endpoint])
    target_dir = os.path.abspath('../specs')
    repo = Repo(os.path.abspath('../'))

    # create the manifest
    manifest = Manifest(os.path.abspath('../manifest.json'))
    manifest.set_ws_url(WS_URL[endpoint])

    modules = [
        'DataPalette', 'GenomeComparison', 'KBaseAssembly', 'KBaseBiochem',
        'KBaseCommon', 'KBaseFBA', 'KBaseFeatureValues', 'KBaseFile',
        'KBaseGeneFamilies', 'KBaseGenomeAnnotations', 'KBaseGenomes',
        'KBaseNarrative', 'KBaseOntology', 'KBasePhenotypes', 'KBaseRNASeq',
        'KBaseSets', 'KBaseTrees', 'ProbabilisticAnnotation'
    ]

    for m in modules:
        try:
            mu = ModuleUpdater(ws, m, target_dir, repo)
            mu.update(manifest)
        except Exception as e:
            print(str(e))
예제 #7
0
    def setUpClass(cls):
        token = os.environ.get('KB_AUTH_TOKEN', None)
        # WARNING: don't call any logging methods on the context object,
        # it'll result in a NoneType error
        cls.ctx = MethodContext(None)
        cls.ctx.update({
            'token':
            token,
            'provenance': [{
                'service': 'GenomeAnnotationAPI',
                'method': 'please_never_use_it_in_production',
                'method_params': []
            }],
            'authenticated':
            1
        })

        config_file = os.environ.get('KB_DEPLOYMENT_CONFIG', None)
        config = ConfigParser.ConfigParser()
        config.read(config_file)
        cls.cfg = {n[0]: n[1] for n in config.items('GenomeAnnotationAPI')}
        cls.ws = Workspace(cls.cfg['workspace-url'], token=token)
        cls.impl = GenomeAnnotationAPI(cls.cfg)

        cls.ga_ref = "8020/81/1"
        cls.genome_ref = "8020/83/1"
예제 #8
0
def _init_clients():
    ws_c = Workspace(ws_url)
    # The KBase web-client instantiated below is being deprecated and no longer works for all functions.
    # A local work-around has been added using kb_sdk and fba_tools
    fba_c = fbaModelServices(fba_url)
    gap_c = LocalFbaModelServices
    return ws_c, fba_c, gap_c
예제 #9
0
def delete_narrative(ws_id, auth_token, url=ci_ws):
    """
    Deletes a workspace with the given id. Throws a ServerError if the user given
    by auth_token isn't allowed to do so.
    """
    ws_client = Workspace(url=url, token=auth_token)
    ws_client.delete_workspace({"id": ws_id})
예제 #10
0
def download_workspace_data(ws_url, source_ws, source_obj, working_dir,
                            logger):
    ws = Workspace(ws_url, token=TOKEN)
    objdata = ws.get_objects([{'ref': source_ws + '/' + source_obj}])[0]
    info = objdata['info']
    if info[2].split('-')[0] != 'KBaseFile.AssemblyFile':
        raise ValueError(
            'This method only works on the KBaseFile.AssemblyFile type')
    shock_url = objdata['data']['assembly_file']['file']['url']
    shock_id = objdata['data']['assembly_file']['file']['id']
    ref = str(info[6]) + '/' + str(info[0]) + '/' + str(info[4])
    source = objdata['data'].get('source')

    outfile = os.path.join(working_dir, source_obj)
    shock_node = shock_url + '/node/' + shock_id + '/?download'
    headers = {'Authorization': 'OAuth ' + TOKEN}
    with open(outfile, 'w') as f:
        response = requests.get(shock_node, stream=True, headers=headers)
        if not response.ok:
            try:
                err = json.loads(response.content)['error'][0]
            except:
                logger.error("Couldn't parse response error content: " +
                             response.content)
                response.raise_for_status()
            raise Exception(str(err))
        for block in response.iter_content(1024):
            if not block:
                break
            f.write(block)

    return shock_url, shock_id, ref, source
예제 #11
0
def upload_narrative(nar_file, auth_token, user_id, url=ci_ws, set_public=False):
    """
    Uploads a Narrative from a downloaded object file.
    This file needs to be in JSON format, and it expects all
    data and info that is usually returned by the Workspace.get_objects
    method.

    Returns a dict of three elements:
        ws: the id of the workspace that was created
        obj: the id of the narrative object
        ref: the above two joined together into an object ref (for convenience)
    """

    # read the file
    f = open(nar_file, "r")
    nar = json.loads(f.read())
    f.close()

    # do some setup.
    current_nar_metadata = ws_metadata
    current_nar_metadata["narrative_nice_name"] = nar["data"]["metadata"]["name"]
    ws_client = Workspace(url=url, token=auth_token)

    # create the new workspace for the narrative
    ws_info = ws_client.create_workspace(
        {
            "workspace": "{}:{}".format(user_id, str(time.time()).replace(".", "")),
            "meta": current_nar_metadata,
            "globalread": "r" if set_public else "n",
        }
    )
    ws_id = ws_info[0]

    # setup and save the narrative object
    nar["info"][10]
    ws_save_obj = {
        "type": "KBaseNarrative.Narrative",
        "data": nar["data"],
        "name": nar["info"][1],
        "meta": nar["info"][10],
        "provenance": [
            {
                "script": "upload_narrative_test.py",
                "description": "Temporary Narrative uploaded for automated testing",
            }
        ],
    }
    obj_info = ws_client.save_objects({"id": ws_id, "objects": [ws_save_obj]})

    # tweak the workspace's metadata to properly present its narrative
    ws_client.alter_workspace_metadata(
        {"wsi": {"id": ws_id}, "new": {"narrative": obj_info[0][0]}}
    )
    return {
        "ws": ws_info[0],
        "obj": obj_info[0][0],
        "refstr": "{}/{}".format(ws_info[0], obj_info[0][0]),
        "ref": NarrativeRef({"wsid": ws_info[0], "objid": obj_info[0][0]}),
    }
예제 #12
0
 def __init__(self, config):
     self.ws_url = config["workspace-url"]
     self.callback_url = config['SDK_CALLBACK_URL']
     self.token = config['KB_AUTH_TOKEN']
     self.shock_url = config['shock-url']
     self.dfu = DataFileUtil(self.callback_url)
     self.ws = Workspace(self.ws_url, token=self.token)
     self.scratch = config['scratch']
예제 #13
0
def get_object_from_ref(ref):
    objid = int(ref.split('/')[1])
    WS_URL = 'https://ci.kbase.us/services/ws/'
    from biokbase.workspace.client import Workspace
    ws = Workspace(WS_URL)
    return ws.get_objects(
        [dict(workspace=os.environ['KB_WORKSPACE_ID'],
              objid=objid)])[0]['data']
예제 #14
0
    def create(self, ctx, params):
        # ctx is the context object
        # return variables are: info
        #BEGIN create

        print('Creating KBase Report.')

        # check that the basic parameters are set
        if 'report' not in params:
            raise ValueError('Field "report" must be defined to save a report')
        if 'workspace_name' not in params:
            raise ValueError(
                'Field "workspace_name" must be defined to save a report')

        # setup proper provenance for the report
        provenance = [{}]
        if 'provenance' in ctx:
            provenance = ctx['provenance']

        # generate a random report name
        reportName = 'report_' + str(uuid.uuid4())
        if 'prefix' in params:
            reportName = params['prefix'] + reportName

        print('Report Name' + reportName)

        # let any workspace errors just percolate up for now
        ws = Workspace(self.workspaceURL, token=ctx['token'])
        report_info = ws.save_objects({
            'workspace':
            params['workspace_name'],
            'objects': [{
                'type': 'KBaseReport.Report',
                'data': params['report'],
                'name': reportName,
                'meta': {},
                'hidden': 1,
                'provenance': provenance
            }]
        })[0]

        info = {
            'ref':
            str(report_info[6]) + '/' + str(report_info[0]) + '/' +
            str(report_info[4]),
            'name':
            report_info[1]
        }

        #END create

        # At some point might do deeper type checking...
        if not isinstance(info, dict):
            raise ValueError('Method create return value ' +
                             'info is not type dict as required.')
        # return the results
        return [info]
    def setUp(self):
        # Set configuration variables.
        self._config = get_config(os.environ["KB_TEST_CONFIG"])

        # Get an authorization token for the test user.
        wsClient = Workspace(self._config["workspace_url"],
                             user_id=self._config["test_user"],
                             password=self._config["test_pwd"])
        self._token = wsClient._headers['AUTHORIZATION']
예제 #16
0
def upload_narrative(nar_file, auth_token, user_id, url=ci_ws, set_public=False):
    """
    Uploads a Narrative from a downloaded object file.
    This file needs to be in JSON format, and it expects all
    data and info that is usually returned by the Workspace.get_objects
    method.

    Returns a dict of three elements:
        ws: the id of the workspace that was created
        obj: the id of the narrative object
        ref: the above two joined together into an object ref (for convenience)
    """

    # read the file
    f = open(nar_file, 'r')
    nar = json.loads(f.read())
    f.close()

    # do some setup.
    current_nar_metadata = ws_metadata
    current_nar_metadata['narrative_nice_name'] = nar['data']['metadata']['name']
    ws_client = Workspace(url=url, token=auth_token)

    # create the new workspace for the narrative
    ws_info = ws_client.create_workspace({
        'workspace': '{}:{}'.format(user_id, str(time.time()).replace('.', '')),
        'meta': current_nar_metadata,
        'globalread': 'r' if set_public else 'n'
    })
    ws_id = ws_info[0]

    # setup and save the narrative object
    metadata = nar['info'][10]
    ws_save_obj = {
        'type': 'KBaseNarrative.Narrative',
        'data': nar['data'],
        'name': nar['info'][1],
        'meta': nar['info'][10],
        'provenance': [{
            'script': 'upload_narrative_test.py',
            'description': 'Temporary Narrative uploaded for automated testing'
        }]
    }
    obj_info = ws_client.save_objects({'id': ws_id,
                                       'objects': [ws_save_obj]})

    # tweak the workspace's metadata to properly present its narrative
    ws_client.alter_workspace_metadata({'wsi': {'id': ws_id}, 'new': {'narrative': obj_info[0][0]}})
    return {
        'ws': ws_info[0],
        'obj': obj_info[0][0],
        'refstr': '{}/{}'.format(ws_info[0], obj_info[0][0]),
        'ref': NarrativeRef({'wsid': ws_info[0], 'objid': obj_info[0][0]})
    }
 def __init__(self):
     c = Config()
     c.HTMLExporter.preprocessors = [NarrativePreprocessor]
     c.TemplateExporter.template_path = [
         '.', self._narrative_template_path()
     ]
     c.CSSHTMLHeaderPreprocessor.enabled = True
     self.html_exporter = HTMLExporter(config=c)
     self.html_exporter.template_file = 'narrative'
     self.ws_client = Workspace(URLS.workspace)
     self.narr_fetcher = NarrativeIO()
예제 #18
0
def get_obj_info(logger,ws_url,objects,ws_id,token):
    """
    function to get the workspace object id from a object name
    """
    ret = []
    ws_client=Workspace(url=ws_url, token=token)
    for obj in  objects:
    	try:
            obj_infos = ws_client.get_object_info_new({"objects": [{'name': obj, 'workspace': ws_id}]})
            ret.append("{0}/{1}/{2}".format(obj_infos[0][6],obj_infos[0][0],obj_infos[0][4]))
        except Exception, e:
                     logger.error("Couldn't retrieve %s:%s from the workspace , %s " %(ws_id,obj,e))
    def test_annotate(self):
        ''' Run pa-annotate on a valid Genome object and verify that the job runs and returns a valid ProbAnno object in the expected time.'''

        # Run the annotate() function to generate a ProbAnno object.
        paClient = ProbabilisticAnnotation(self._config["probanno_url"],
                                           token=self._token)
        jobid = paClient.annotate({
            "genome": self._config["genomeid"],
            "genome_workspace": self._config["test_ws"],
            "probanno": self._config["probannoid"],
            "probanno_workspace": self._config["test_ws"]
        })

        # Allow time for the command to run.
        time.sleep(float(self._config["runtime"]))

        # Make sure the job has completed.
        ujsClient = UserAndJobState(self._config['ujs_url'], token=self._token)
        jobList = ujsClient.list_jobs([self._config['test_user']], 'CE')
        jobCompleted = False
        for job in jobList:
            if jobid == job[0]:
                jobCompleted = True
                jobInfo = job
        self.assertTrue(
            jobCompleted, 'Job did not complete before timeout of %s seconds' %
            (self._config['runtime']))

        # See if the job ended in error.
        details = ''
        if jobInfo[11] == 1:
            details = ujsClient.get_detailed_error(jobInfo[0])
        self.assertEqual(jobInfo[11], 0, 'Job ended in error: %s' % (details))

        # Look for the ProbAnno object in the test workspace.
        wsClient = Workspace(self._config["workspace_url"], token=self._token)
        try:
            probannoObjectId = {
                'workspace': self._config['test_ws'],
                'name': self._config['probannoid']
            }
            objectList = wsClient.get_objects([probannoObjectId])
            probannoObject = objectList[0]
            self.assertEqual(
                probannoObject['info'][1], self._config['probannoid'],
                'ProbAnno object id %s is not %s' %
                (probannoObject['info'][1], self._config['probannoid']))
        except WorkspaceServerError as e:
            traceback.print_exc(file=sys.stderr)
            self.fail(
                msg=
                "The expected object %s did not get created in the workspace %s!\n"
                % (self._config["probannoid"], self._config["test_ws"]))
예제 #20
0
 def test_handles(self):
     wsName = self.generatePesudoRandomWorkspaceName()
     self.ws.set_permissions({
         'workspace': wsName,
         'new_permission': 'w',
         'users': [self.ctx2['user_id']]
     })
     temp_shock_file = "/kb/module/work/tmp/shock1.txt"
     with open(temp_shock_file, "w") as f1:
         f1.write("Test Shock Handle")
     token1 = self.ctx['token']
     dfu = DataFileUtil(os.environ['SDK_CALLBACK_URL'], token=token1)
     handle1 = dfu.file_to_shock({
         'file_path': temp_shock_file,
         'make_handle': 1
     })['handle']
     hid1 = handle1['hid']
     genome_name = "Genome.1"
     ws2 = Workspace(self.cfg['workspace-url'], token=token1)
     ws2.save_objects({
         'workspace':
         wsName,
         'objects': [{
             'name': genome_name,
             'type': 'KBaseGenomes.Genome',
             'data': {
                 'id': "qwerty",
                 'scientific_name': "Qwerty",
                 'domain': "Bacteria",
                 'genetic_code': 11,
                 'genbank_handle_ref': hid1
             }
         }]
     })
     genome = self.impl.get_genome_v1(
         self.ctx2, {'genomes': [{
             'ref': wsName + '/' + genome_name
         }]})[0]['genomes'][0]['data']
     self.impl.save_one_genome_v1(self.ctx2, {
         'workspace': wsName,
         'name': genome_name,
         'data': genome
     })[0]
     genome = self.impl.get_genome_v1(
         self.ctx2, {'genomes': [{
             'ref': wsName + '/' + genome_name
         }]})[0]['genomes'][0]['data']
     self.assertTrue('genbank_handle_ref' in genome)
     hid2 = genome['genbank_handle_ref']
     self.assertNotEqual(hid1, hid2)
예제 #21
0
    def __init__(self, ws_url, auth_url, token):
        """
        The data fetcher needs a workspace client and auth client.
        It needs Auth to get the current user id out of the token, so we know what workspaces
        are actually shared as opposed to just visible.

        Args:
            ws_url (str): Workspace service URL
            auth_url (str): Auth service URL
            token (str): auth token
        """
        self._ws = Workspace(url=ws_url, token=token)
        auth = KBaseAuth(auth_url=auth_url)
        self._user = auth.get_user(token)
예제 #22
0
def __init_client(client_name):
    if client_name == 'workspace':
        c = Workspace(URLS.workspace)
    elif client_name == 'job_service':
        c = NarrativeJobService(URLS.job_service)
    elif client_name == 'narrative_method_store':
        c = NarrativeMethodStore(URLS.narrative_method_store)
    elif client_name == 'user_and_job_state':
        c = UserAndJobState(URLS.user_and_job_state)
    elif client_name == 'catalog':
        c = Catalog(URLS.catalog)

    else:
        raise ValueError('Unknown client name "%s"' % client_name)

    __clients[client_name] = c
    return c
예제 #23
0
def find_all_narrative_py2_code(ws_url: str, token: str, min_id: int,
                                max_id: int, outfile: str, report: str):
    assert ws_url
    assert token
    assert min_id and min_id > 0
    assert max_id and max_id >= min_id
    assert outfile

    ws = Workspace(url=ws_url, token=token)
    all_results = {"fail": [], "no_narr": [], "no_change": [], "changes": []}

    avail_fixes = set(get_fixers_from_package("lib2to3.fixes"))
    rt = RefactoringTool(avail_fixes, options={"print_function": False})

    for ws_id in range(min_id, max_id):
        try:
            result = _find_narrative_py2_code(ws_id, ws, rt, verbose=True)
            if result is None:
                all_results["no_narr"].append({"id": ws_id})
            elif result.updated_cells == 0:
                all_results["no_change"].append(result.to_dict())
            else:
                all_results["changes"].append(result.to_dict())
        except baseclient.ServerError as e:
            if "No workspace with id" in str(e):
                print(f"WS:{ws_id} does not exist")
            all_results["fail"].append({"id": ws_id, "error": str(e.message)})
        except TypeError as e:
            print(f"WS:{ws_id} metadata doesn't link to a Narrative type!")
            all_results["fail"].append({"id": ws_id, "error": str(e)})
        except json.JSONDecodeError as e:
            print(f"WS:{ws_id} unable to unpack Narrative - not valid JSON!")
            all_results["fail"].append({
                "id":
                ws_id,
                "error":
                f"Invalid JSON in Narrative object: {str(e)}"
            })
    with open(outfile, "w") as fjson:
        fjson.write(json.dumps(all_results, indent=4))
    print(f"Done. Results in {outfile}")
    if report is not None:
        with open(report, "w") as ftsv:
            ftsv.write("user name\tnarrative id\tlast saved\n")
            for n in all_results["changes"]:
                ftsv.write(f"{n['owner']}\t{n['id']}\t{n['last_saved']}\n")
예제 #24
0
    def manyHellos_runEach(self, ctx, task):
        """
        :param task: instance of type "ManyHellos_task" -> structure:
           parameter "msg" of String, parameter "job_number" of Long,
           parameter "workspace" of String
        :returns: instance of type "ManyHellos_runEachResult" (runEach()) ->
           structure: parameter "message" of String
        """
        # ctx is the context object
        # return variables are: returnVal
        #BEGIN manyHellos_runEach
        print("this is manyHellos_runEach...")
        pprint(["task is ", task])

        res = "{0}: {1}".format(task['job_number'], task['msg'])

        ws_client = Workspace(url=self.config['workspace-url'],
                              token=ctx['token'])
        res_obj = ws_client.save_objects({
            "workspace":
            task['workspace'],
            "objects": [{
                'type':
                'KBaseReport.Report',
                "data": {
                    'objects_created': [],
                    'text_message': res
                },
                "name":
                "{0}_{1}.rpt".format(task['msg'], task['job_number']),
                "meta": {}
            }]
        })
        res = json.dumps(res_obj)

        print("exiting manyHellos_runEach(), res is", res)
        returnVal = {'message': res}
        #END manyHellos_runEach

        # At some point might do deeper type checking...
        if not isinstance(returnVal, dict):
            raise ValueError('Method manyHellos_runEach return value ' +
                             'returnVal is not type dict as required.')
        # return the results
        return [returnVal]
예제 #25
0
def fix_all_workspace_info(ws_url,
                           auth_url,
                           token,
                           max_id,
                           outfile=DEFAULT_OUTPUT_FILE):
    """
    Iterates over all workspaces available at the ws_url endpoint, using the given admin token,
    and applies _fix_single_workspace_info to each.
    ws_url = endpoint for the workspace service to modify
    auth_url = endpoint for the auth service that the workspace talks to
    token = an active auth token for a workspace administrator
    max_id = the max workspace id to fix - should be fairly large, any that are missing up to that point are just ignored.
    """
    assert ws_url
    assert auth_url
    assert token

    user_id = _get_user_id(auth_url, token)
    ws = Workspace(url=ws_url, token=token)
    all_results = {
        "multiple": [],  # more than 1 narrative, not updated
        "update": [],  # list of updated narratives
        "skip": [],  # skipped - likely has 0 narratives
        "fail":
        [],  # failed because either that id doesn't exist, or it was deleted. maybe locked.
    }
    for ws_id in range(1, max_id):
        try:
            result = _fix_single_workspace_info(ws_id,
                                                user_id,
                                                ws,
                                                verbose=True)
            if result.get("multiple") is not None:
                all_results["multiple"].append(result["multiple"])
            if result.get("update") is not None:
                all_results["update"].append(result["update"])
            if result.get("skip") is not None:
                all_results["skip"].append(result["skip"])
        except baseclient.ServerError as e:
            if "No workspace with id" in str(e):
                print("WS:{} does not exist".format(ws_id))
            all_results["fail"].append({"id": ws_id, "error": str(e.message)})
    print("Done. Results in update_results.json")
    with open("update_results.json", "w") as f:
        f.write(json.dumps(all_results, indent=4))
예제 #26
0
def fetch_narrative(nar_id, auth_token, url=ci_ws, file_name=None):
    """
    Fetches a Narrative object with the given reference id (of the form ##/##).
    If a file_name is given, then it is printed to that file.
    If the narrative is found, the jsonized string of it is returned.

    If nothing is found, an empty Dict is returned.
    """
    ws_client = Workspace(url=url, token=auth_token)
    nar_data = ws_client.get_objects([{"ref": nar_id}])
    if len(nar_data) > 0:
        nar_json = json.dumps(nar_data[0])
        if file_name is not None:
            f = open(file_name, "w")
            f.write(nar_json)
            f.close()
        return nar_json
    return {}
예제 #27
0
    def __init__(self, args):
        TransformBase.__init__(self, args)
        self.ws_url = args.ws_url
        self.cfg_name = args.cfg_name
        self.sws_id = args.sws_id
        self.etype = args.etype
        self.opt_args = args.opt_args

        # download ws object and find where the validation script is located
        self.wsd = Workspace(url=self.ws_url, token=self.token)
        self.config = self.wsd.get_object({
            'id': self.cfg_name,
            'workspace': self.sws_id
        })['data']['config_map']

        if self.config is None:
            raise Exception("Object {} not found in workspace {}".format(
                self.cfg_name, self.sws_id))
    def test_cleanup(self):
        ''' Cleanup objects created by tests. '''

        # Delete all of the objects in the test workspace.
        wsClient = Workspace(self._config["workspace_url"], token=self._token)
        listObjectsParams = dict()
        listObjectsParams['workspaces'] = [self._config['test_ws']]
        objectList = wsClient.list_objects(listObjectsParams)
        deleteList = list()
        for object in objectList:
            deleteList.append({
                'wsid': object[6],
                'objid': object[0],
                'ver': object[4]
            })
        wsClient.delete_objects(deleteList)
        objectList = wsClient.list_objects(listObjectsParams)
        for object in objectList:
            print 'After delete Object %s %s' % (object[1], object[4])
    def test_loadGenome(self):
        ''' Load a test Genome object into the test workspace. '''

        # Create the test workspace.
        wsClient = Workspace(self._config["workspace_url"], token=self._token)
        try:
            # See if the workspace exists.
            wsInfo = wsClient.get_workspace_info(
                {"workspace": self._config["test_ws"]})
        except WorkspaceServerError as e:
            # Hopefully this means the workspace does not exist. (It could also mean someone messed up setting up the URLs)
            traceback.print_exc(file=sys.stderr)
            wsInfo = wsClient.create_workspace(
                {"workspace": self._config["test_ws"]})

        # We also need to put in a mapping and a biochemistry object somewhere.
        # To do this, I just create a "dependency workspace" and pull them from there.
        try:
            # See if the workspace exists.
            wsInfo = wsClient.get_workspace_info(
                {"workspace": self._config["dependency_ws"]})
        except WorkspaceServerError as e:
            # Hopefully this means the workspace does not exist. (It could also mean someone messed up setting up the URLs)
            #            traceback.print_exc(file=sys.stderr)
            depWsInfo = wsClient.create_workspace(
                {"workspace": self._config["dependency_ws"]})

        # Load the mapping and biochemistry objects
        testContigSet = json.load(open(self._config['contigset_file'], 'r'))
        contigSetSaveData = dict()
        contigSetSaveData['type'] = 'KBaseGenomes.ContigSet'
        contigSetSaveData['name'] = self._config['contigsetid']
        contigSetSaveData['data'] = testContigSet
        testGenome = json.load(open(self._config["genome_file"], "r"))
        genomeSaveData = dict()
        genomeSaveData['type'] = 'KBaseGenomes.Genome'
        genomeSaveData['name'] = self._config['genomeid']
        genomeSaveData['data'] = testGenome
        wsClient.save_objects({
            'workspace': self._config['test_ws'],
            'objects': [genomeSaveData, contigSetSaveData]
        })
예제 #30
0
def upload_workspace_data(cs, ws_url, source_ref, target_ws, obj_name):
    ws = Workspace(ws_url, token=TOKEN)
    type_ = ws.translate_from_MD5_types([CS_MD5_TYPE])[CS_MD5_TYPE][0]
    ws.save_objects({
        'workspace':
        target_ws,
        'objects': [{
            'name':
            obj_name,
            'type':
            type_,
            'data':
            cs,
            'provenance': [{
                'script': SCRIPT_NAME,
                'script_ver': __VERSION__,
                'input_ws_objects': [source_ref],
            }]
        }]
    })