示例#1
0
def get_galaxy_connection(args, file=None, log=None, login_required=True):
    """
    Return a Galaxy connection, given a user or an API key.
    If not given gets the arguments from the file.
    If either is missing raise ValueError.
    """
    if file:
        file_content = load_yaml_file(file)
    else:
        file_content = dict()

    url = args.galaxy or file_content.get('galaxy_instance')
    galaxy_url = check_url(url, log)
    api_key = args.api_key or file_content.get('api_key')

    if args.user and args.password:
        return galaxy.GalaxyInstance(url=galaxy_url,
                                     email=args.user,
                                     password=args.password)
    elif api_key:
        return galaxy.GalaxyInstance(url=galaxy_url, key=api_key)
    elif not login_required:
        return galaxy.GalaxyInstance(url=galaxy_url)
    else:
        raise ValueError(
            "Missing api key or user & password combination, in order to make a galaxy connection."
        )
    def test_dataset_permissions(self):
        admin_user_id = self.gi.users.get_current_user()['id']
        user_id = self.gi.users.create_local_user('newuser3',
                                                  '*****@*****.**',
                                                  'secret')['id']
        user_api_key = self.gi.users.create_user_apikey(user_id)
        anonymous_gi = galaxy.GalaxyInstance(url=self.gi.base_url, key=None)
        user_gi = galaxy.GalaxyInstance(url=self.gi.base_url, key=user_api_key)
        sharing_role = self.gi.roles.create_role(
            'sharing_role', 'sharing_role', [user_id, admin_user_id])['id']

        self.gi.datasets.publish_dataset(self.dataset_id, published=False)
        with self.assertRaises(ConnectionError):
            anonymous_gi.datasets.show_dataset(self.dataset_id)
        self.gi.datasets.publish_dataset(self.dataset_id, published=True)
        # now dataset is public, i.e. accessible to anonymous users
        self.assertEqual(
            anonymous_gi.datasets.show_dataset(self.dataset_id)['id'],
            self.dataset_id)
        self.gi.datasets.publish_dataset(self.dataset_id, published=False)

        with self.assertRaises(ConnectionError):
            user_gi.datasets.show_dataset(self.dataset_id)
        self.gi.datasets.update_permissions(self.dataset_id,
                                            access_ids=[sharing_role],
                                            manage_ids=[sharing_role])
        self.assertEqual(
            user_gi.datasets.show_dataset(self.dataset_id)['id'],
            self.dataset_id)
        # anonymous access now fails because sharing is only with the shared user role
        with self.assertRaises(ConnectionError):
            anonymous_gi.datasets.show_dataset(self.dataset_id)
def main():

    parent = get_common_args()
    parser = argparse.ArgumentParser(
        parents=[parent],
        description='Populate the Galaxy data library with test data.')

    parser.add_argument('-i',
                        '--infile',
                        required=True,
                        type=argparse.FileType('r'))
    args = parser.parse_args()

    if args.user and args.password:
        gi = galaxy.GalaxyInstance(url=args.galaxy,
                                   email=args.user,
                                   password=args.password)
    elif args.api_key:
        gi = galaxy.GalaxyInstance(url=args.galaxy, key=args.api_key)
    else:
        sys.exit(
            'Please specify either a valid Galaxy username/password or an API key.'
        )

    if args.verbose:
        log.basicConfig(level=log.DEBUG)

    setup_data_libraries(gi, args.infile)
示例#4
0
def get_galaxy_connection():
    """
        Given access to the configuration dict that galaxy passed us, we try and connect to galaxy's API.

        First we try connecting to galaxy directly, using an IP address given
        us by docker (since the galaxy host is the default gateway for docker).
        Using additional information collected by galaxy like the port it is
        running on and the application path, we build a galaxy URL and test our
        connection by attempting to get a history listing. This is done to
        avoid any nasty network configuration that a SysAdmin has placed
        between galaxy and us inside docker, like disabling API queries.

        If that fails, we failover to using the URL the user is accessing
        through. This will succeed where the previous connection fails under
        the conditions of REMOTE_USER and galaxy running under uWSGI.
    """
    conf = _get_conf()
    try:
        # Remove trailing slashes
        app_path = conf['galaxy_url'].rstrip('/')
        # Remove protocol+host:port if included
        app_path = ''.join(app_path.split('/')[3:])
        # Now obtain IP address from a netstat command.

        cmd_netstat = ['netstat', '-nr']
        p1 = subprocess.Popen(cmd_netstat, stdout=subprocess.PIPE)
        cmd_grep = ['grep', '^0\.0\.0\.0']
        p2 = subprocess.Popen(cmd_grep,
                              stdin=p1.stdout,
                              stdout=subprocess.PIPE)
        cmd_awk = ['awk', '{ print $2 }']
        p3 = subprocess.Popen(cmd_awk, stdin=p2.stdout, stdout=subprocess.PIPE)
        # Now we have an ip address to connect to galaxy on.
        galaxy_ip = p3.stdout.read()
        # We should be able to find a port to connect to galaxy on via this
        # conf var: galaxy_paster_port
        galaxy_port = conf['galaxy_paster_port']

        if not galaxy_port:
            # We've failed to detect a port in the config we were given by
            # galaxy, so we won't be able to construct a valid URL
            raise Exception("No port")

        built_galaxy_url = 'http://%s:%s/%s' % (galaxy_ip.strip(), galaxy_port,
                                                app_path.strip())
        gi = galaxy.GalaxyInstance(url=built_galaxy_url.rstrip('/'),
                                   key=conf['api_key'])
        gi.histories.get_histories()
    except:
        try:
            gi = galaxy.GalaxyInstance(url=conf['galaxy_url'],
                                       key=conf['api_key'])
            gi.histories.get_histories()
        except:
            raise Exception(
                "Could not connect to a galaxy instance. Please contact your SysAdmin for help with this error"
            )
    return gi
示例#5
0
def _gi(args):
    gi = galaxy.GalaxyInstance(args.host, key=args.api_key)
    name = "wftest-user-%d" % random.randint(0, 1000000)

    user = gi.users.create_local_user(name, "*****@*****.**" % name, "pass123")
    user_id = user["id"]
    api_key = gi.users.create_user_apikey(user_id)
    user_gi = galaxy.GalaxyInstance(args.host, api_key)
    return user_gi
示例#6
0
def get_galaxy_connection(args):
    """
    Return a Galaxy connection, given a user or an API key.
    """
    if args.user and args.password:
        gi = galaxy.GalaxyInstance(url=args.galaxy, email=args.user, password=args.password)
    elif args.api_key:
        gi = galaxy.GalaxyInstance(url=args.galaxy, key=args.api_key)

    return gi or False
def main():
    """
    load histories, workflows and trigger tool dependency installation. Well, try.
    Keep this idiom handy:
    #x=gi.make_get_request(f"http://nginx/api/tools/{tfid}/dependencies", params={'api_key':'fakekey'})
    """
    args = _parser().parse_args()
    if args.key:
        connected = False
        started = time.time()
        while not connected:
            try:
                gi = galaxy.GalaxyInstance(url=args.galaxy, key=args.key)
                connected = True
            except Exception:
                print('install-history: No gi yet...%d seconds' %
                      int(time.time() - started))
                time.sleep(5)
    else:
        print('Need a key passed to install-history - no default found')
        sys.exit(1)
    job_check(gi)
    wfpaths = args.wfpaths
    for wfpath in wfpaths:
        x = gi.workflows.import_workflow_from_local_path(wfpath, publish=True)
        job_check(gi)
        print('Installed %s, res= %s' % (wfpath, x))
    hists = args.history_path
    for hf in hists:
        if os.path.isdir(hf):
            for fp in os.listdir(hf):
                hp = os.path.join(hf, fp)
                if os.path.isfile(hp):
                    x = gi.histories.import_history(file_path=hp, url=None)
                    print('installed ', hp, 'res=', x)
                    job_check(gi)

        else:
            x = gi.histories.import_history(file_path=hf, url=None)
            print('installed', hf, 'res=', x)
            job_check(gi)
    x = ''
    gi = galaxy.GalaxyInstance(url=args.galaxy, key=args.key)
    for tfid in args.toolid:
        try:
            x = gi.tools.install_dependencies(tfid,
                                              resolver_type="singularity")
        except Exception:
            print('Attempt to install %s failed' % tfid)
        job_check(gi)
        print('tried installing dependencies for', tfid, 'dependencies, res=',
              x)
示例#8
0
def get_galaxy_client(args):
    if args.create_user:
        master_client = galaxy.GalaxyInstance(args.url, key=args.master_api_key)
        user_info = master_client.users.create_local_user(args.user,
                '*****@*****.**' % args.user, args.user)
        api_key = master_client.users.create_user_apikey(user_info.get('id'))
        sys.stderr.write('Created user %s with api key %s\n'
                % (args.user, api_key))

    else:
        api_key = args.api_key

    return galaxy.GalaxyInstance(args.url, key=api_key)
示例#9
0
def main():
    args = _parser().parse_args()
    if args.user and args.password:
        gi = galaxy.GalaxyInstance(url=args.galaxy, email=args.user, password=args.password)
    elif args.api_key:
        gi = galaxy.GalaxyInstance(url=args.galaxy, key=args.api_key)
    else:
        sys.exit('Please specify either a valid Galaxy username/password or an API key.')

    if args.verbose:
        log.basicConfig(level=log.DEBUG)

    setup_data_libraries(gi, args.infile, training=args.training, legacy=args.legacy)
示例#10
0
 def install_deps(self, tool_id):
     gi = galaxy.GalaxyInstance(url='http://galaxy-server', key='fakekey')
     try:
         res = gi.tools.install_dependencies(tool_id)
         logging.info('Tried installing dependencies for %s. Got %s' % (tool_id,res))
     except Exception:
         logging.warning('Attempt to install %s failed' % tool_id)
 def configure_galaxy_api_key(self, driver):
     """Make a new Galaxy admin API key and configure the tool to use it"""
     gal = galaxy.GalaxyInstance(self.GALAXY_URL,
                                 email=self.EMAIL,
                                 password=self.GALAXY_PASSWORD)
     self.configure_tool('Galaxy', 'admin_key', gal.key)
     print 'key:' + gal.key
示例#12
0
    def send_result_to_galaxy(self):
        """Send a result file to Galaxy"""
        filename = "AskOmics_result_{}.tsv".format(self.id)

        galaxy_instance = galaxy.GalaxyInstance(self.session["user"]["galaxy"]["url"], self.session["user"]["galaxy"]["apikey"])
        last_history = galaxy_instance.histories.get_most_recently_used_history()
        galaxy_instance.tools.upload_file(self.file_path, last_history['id'], file_name=filename, file_type='tabular')
示例#13
0
文件: utils.py 项目: simonbray/gxwf
def _login():
    login_dict = _read_configfile()
    cnfg = login_dict['logins'][login_dict['active_login']]
    aliases = login_dict['aliases']
    gi = galaxy.GalaxyInstance(cnfg['url'], cnfg['api_key'])
    gi.histories.get_histories()  # just to check the connection
    return gi, cnfg, aliases
示例#14
0
def __main__():
    parser = argparse.ArgumentParser(description="""Script to run all workflows mentioned in workflows_to_test.
    It will import the shared workflows are create histories for each workflow run, prefixed with ``TEST_RUN_<date>:``
    Make sure the yaml has file names identical to those in the data library.""")

    parser.add_argument('-k', '--api-key', '--key', dest='key', metavar='your_api_key',
                        help='The account linked to this key needs to have admin right to upload by server path',
                        required=True)
    parser.add_argument('-u', '--url', dest='url', metavar="http://galaxy_url:port",
                        help="Be sure to specify the port on which galaxy is running",
                        default="http://usegalaxy.org")
    parser.add_argument('-x', '--xunit-output', dest="xunit_output", type=argparse.FileType('w'), default='report.xml',
                        help="""Location to store xunit report in""")
    args = parser.parse_args()

    gi = galaxy.GalaxyInstance(args.url, args.key)
    hist = gi.histories.create_history('Load All Student Genomes')


    org_names = ('Soft', '2ww-3119', 'ISA', 'Inf_Still_Creek', 'J76', 'K6',
                 'K7', 'K8', 'MIS1-LT2', 'MIS3-3117', 'MP16', 'Pin', 'SCI',
                 'SCS', 'SL-Ken', 'ScaAbd', 'ScaApp', 'Sw1_3003', 'Sw2-Ken',
                 'UDP')

    test_suites = []
    for name in org_names:
        ts = retrieve_and_rename(gi, hist, name)
        test_suites.append(ts)
    args.xunit_output.write(xunit_dump(test_suites))
def main():
    """
        This script uses bioblend to import .ga workflow files into a running instance of Galaxy
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("-w", "--workflow_path", help='Path to workflow file')
    parser.add_argument(
        "-g",
        "--galaxy",
        dest="galaxy_url",
        help="Target Galaxy instance URL/IP address (required "
        "if not defined in the tools list file)",
    )
    parser.add_argument(
        "-a",
        "--apikey",
        dest="api_key",
        help="Galaxy admin user API key (required if not "
        "defined in the tools list file)",
    )
    args = parser.parse_args()

    gi = galaxy.GalaxyInstance(url=args.galaxy_url, key=args.api_key)

    with open(args.workflow_path, 'r') as wf_file:
        import_uuid = json.load(wf_file).get('uuid')
    existing_uuids = [
        d.get('latest_workflow_uuid') for d in gi.workflows.get_workflows()
    ]
    if import_uuid not in existing_uuids:
        gi.workflows.import_workflow_from_local_path(args.workflow_path)
示例#16
0
def check_instance(galaxy_url, tutorial_path):
    """ Check if a given tutorial can be run on a given Galaxy instance """
    # TODO: support specific revision checking
    # TODO: discussion: for suites, encourage to list all tools actually needed in yaml, not just the suite?

    gi = galaxy.GalaxyInstance(url=galaxy_url)

    # find tools needed for the tutorial
    with open(os.path.join(tutorial_path, 'tools.yaml')) as f:
        tool_yaml = yaml.safe_load(f)

    tools_required = []
    for tool in tool_yaml['tools']:
        tools_required.append(tool['name'])

    # check if all tools are installed
    tools_list = gi.tools.get_tools()
    tools_available = []
    for t in tools_list:
        try:
            tools_available.append(t['tool_shed_repository']['name'])
        except KeyError:
            pass

    tool_requirements_satisfied = set(tools_required) < set(tools_available)

    # output what is installed and what is missing
    print "This Galaxy instance (" + galaxy_url + ") can run my tutorial: " + str(
        tool_requirements_satisfied)

    for tool in tools_required:
        print " - " + tool + ": " + ("present" if tool in tools_available else
                                     "not present")

    return tool_requirements_satisfied
示例#17
0
def cli(ctx, url=None, api_key=None, admin=False, **kwds):
    """Help initialize global configuration (in home directory)
    """
    # TODO: prompt for values someday.
    click.echo("""
Welcome to
      ____   ____ _   _____   _____  ___   _____
     / __ \ / __ `/  / ___/  / ___/ / _ \ / ___/
    / /_/ // /_/ /  / /     (__  ) /  __// /__
   / .___/ \__,_/  /_/     /____/  \___/ \___/
  /_/ Galaxy at the Speed of Light
""")
    config_path = config.global_config_path()

    if os.path.exists(config_path):
        info("Your parsec configuration already exists. Please edit it instead: %s" % config_path)
        return 0

    while True:
        galaxy_url = url
        if url is None or len(url) == 0 :
            galaxy_url = click.prompt("Please entry your Galaxy's URL")
        galaxy_key = api_key
        if api_key is None or len(api_key) == 0 :
            galaxy_key = click.prompt("Please entry your Galaxy API Key")
        info("Testing connection...")
        gi = galaxy.GalaxyInstance(galaxy_url, galaxy_key)
        try:
            conf = gi.config.get_config()
            if 'version_major' in conf:
                # Ok, success
                info("Ok! Everything looks good.")
                break
            else:
                warn("Error, we could not access the configuration data for your instance.")
                should_break = click.prompt("Continue despite inability to contact this Galaxy Instance? [y/n]")
                if should_break in ('Y', 'y'):
                    break
        except Exception as e:
            warn_msg = ("Error, we could not access the configuration data for your instance. "
                        "The server responded with error code %s. If your galaxy uses remote_user "
                        "authentication (i.e. as username + password is requested if you try to curl "
                        "or wget %s.) If this is the case, you may need to add an exception for the "
                        "Galaxy API. Please see https://galaxyproject.org/admin/config/apache-external-user-auth/ "
                        "or https://galaxyproject.org/admin/config/nginx-external-user-auth/ for more information.)")
            warn(warn_msg, e.status_code, galaxy_url)
            should_break = click.prompt("Continue despite inability to contact this Galaxy Instance? [y/n]")
            if should_break in ('Y', 'y'):
                break

    if os.path.exists(config_path):
        warn("File %s already exists, refusing to overwrite." % config_path)
        return -1
    with open(config_path, "w") as f:
        f.write(
            CONFIG_TEMPLATE % {
                'key': galaxy_key,
                'url': galaxy_url,
            })
        info(SUCCESS_MESSAGE)
示例#18
0
def main():
    if len(sys.argv) < 2:
        print "USAGE:\n\tpython {} api_key.txt <history name (optional) >\n\n\tNOTE: if not history name is given" \
              " all histories will be downloaded".format(sys.argv[0])
        sys.exit(1)

    logger.info("############")
    apiFile = open(sys.argv[1])
    url, key = apiFile.read().strip().split(',')

    gi = galaxy.GalaxyInstance(url=url, key=key)

    logger.info("Getting history IDs")
    hist_name = None if len(sys.argv) < 3 else sys.argv[2]
    histories = get_history(gi, hist_name)

    logger.info("Creating the Download dir (if necessary)")
    down_dir = os.getcwd() + "/download"
    mkpath(os.getcwd() + "/download")

    jehas = prepare_download(gi, histories)

    for item in jehas:
        dfile = open(down_dir + "/" + item.name + ".tar.gz", "wb")
        dhist = downlad_history(gi, item.id, item.jeha, dfile)

        logger.info("The Download of {} is {}".format(item.name, dhist))
        dfile.close()
示例#19
0
def export_user_histories(api_key, GalaxyURL):
    gui = galaxy.GalaxyInstance(url=GalaxyURL, key=api_key)
    histories = gui.histories.get_histories()
    for history in histories:
        print("exporting history %s" % history[u'id'])
        print(gui.histories.export_history(history[u'id'],
                                           include_hidden=True))
示例#20
0
    def send_query_to_galaxy(self):
        """Send the json query to a galaxy dataset"""
        galaxy_instance = galaxy.GalaxyInstance(self.session["user"]["galaxy"]["url"], self.session["user"]["galaxy"]["apikey"])
        last_history_id = galaxy_instance.histories.get_most_recently_used_history()['id']

        # Name of the json file
        name = "AskOmics_query_{}.json".format(self.id)

        # Load the file into Galaxy
        galaxy_instance.tools.paste_content(json.dumps(self.get_graph_state(formated=True)), last_history_id, file_type='json', file_name=name)
示例#21
0
    def send_to_history(self, path, name, filetype):
        """Send a file into the most recent Galaxy history

        :param path: path of file to load into Galaxy
        :type path: string
        """

        galaxy_instance = galaxy.GalaxyInstance(self.url, self.apikey)
        last_history = galaxy_instance.histories.get_most_recently_used_history()
        galaxy_instance.tools.upload_file(path, last_history['id'], file_name=name, file_type=filetype)
    def __init__(self, url, api, library_id, folder_id, should_link,
                 non_local):
        self.gi = galaxy.GalaxyInstance(url=url, key=api)
        self.library_id = library_id
        self.folder_id = folder_id
        self.should_link = should_link
        self.non_local = non_local

        self.memo_path = {}
        self.prepopulate_memo()
示例#23
0
def __main__():
    args = _parse_cli_options()
    ADMIN_KEY = args.api_key
    GalaxyURL = args.admin_galaxy_url
    gi = galaxy.GalaxyInstance(url=GalaxyURL, key=ADMIN_KEY)
    all_users = gi.users.get_users()
    selected_users = extract_users(args.emails, all_users)
    user_api_keys = get_user_apikeys(gi, selected_users)
    for api_key in user_api_keys:
        export_user_histories(api_key, GalaxyURL)
示例#24
0
    def __init__(self, settings, request):
        """constructor"""
        self.settings = settings
        self.request = request

        # Start a connection with galaxy
        self.galaxy_instance = galaxy.GalaxyInstance(
            self.settings['askomics.testing_galaxy_url'],
            self.settings['askomics.testing_galaxy_key'])

        self.history_id = ''
示例#25
0
def main(data):
    """
    Load files into a Galaxy data library.
    By default all test-data tools from all installed tools
    will be linked into a data library.
    """

    log.info("Importing data libraries.")

    url = "http://*****:*****@galaxy.org')
    admin_pass = os.environ.get('GALAXY_DEFAULT_ADMIN_PASSWORD', 'admin')

    # Establish connection to galaxy instance
    gi = galaxy.GalaxyInstance(url=url, email=admin_email, password=admin_pass)

    jc = galaxy.jobs.JobsClient(gi)

    folders = dict()

    libraries = yaml.load(data)
    for lib in libraries['libraries']:
        folders[lib['name']] = lib['files']

    if folders:
        log.info("Create 'Test Data' library.")
        lib = gi.libraries.create_library('Training Data',
                                          'Data pulled from online archives.')
        lib_id = lib['id']

        for fname, urls in folders.items():
            log.info("Creating folder: %s" % fname)
            folder = gi.libraries.create_folder(lib_id, fname)
            for url in urls:
                gi.libraries.upload_file_from_url(
                    lib_id,
                    url,
                    folder_id=folder[0]['id'],
                )

        no_break = True
        while True:
            no_break = False
            for job in jc.get_jobs():
                if job['state'] != 'ok':
                    no_break = True
            if not no_break:
                break
            time.sleep(3)

        time.sleep(20)
        log.info("Finished importing test data.")
示例#26
0
def get_installed_tool_version(args):
    gi = galaxy.GalaxyInstance(url=args.gi_url, key=args.api_key)
    ts_repositories = gi.toolShed.get_repositories()

    for repo in ts_repositories:
        test = (str(repo['name']) == args.tool_name)
        test &= (not repo['deleted'])
        test &= (str(repo['tool_shed']) == args.tool_shed)
        if test:
            print repo['installed_changeset_revision']
            print repo['owner']
示例#27
0
def __main__():
    parser = argparse.ArgumentParser(
        description=
        """Script to run all workflows mentioned in workflows_to_test.
    It will import the shared workflows are create histories for each workflow run, prefixed with ``TEST_RUN_<date>:``
    Make sure the yaml has file names identical to those in the data library."""
    )

    parser.add_argument(
        '-k',
        '--api-key',
        '--key',
        dest='key',
        metavar='your_api_key',
        help=
        'The account linked to this key needs to have admin right to upload by server path',
        required=True)
    parser.add_argument(
        '-u',
        '--url',
        dest='url',
        metavar="http://galaxy_url:port",
        help="Be sure to specify the port on which galaxy is running",
        default="http://usegalaxy.org")
    parser.add_argument('-w',
                        '--workflow-output',
                        dest="workflow_output",
                        type=str,
                        default='out.ga',
                        help="""Location to store Galaxy workflow in""")
    parser.add_argument('-i', '--workflow-id', dest="workflow_id", type=str)
    args = parser.parse_args()

    gi = galaxy.GalaxyInstance(args.url, args.key)
    wf = gi.workflows.get_workflows(workflow_id=args.workflow_id)[0]

    gi.workflows.export_workflow_to_local_path(args.workflow_id,
                                               args.workflow_output,
                                               use_default_filename=False)
    # Get the current invocations
    invocations = gi.workflows.get_invocations(args.workflow_id)
    workflow_states = {}
    for invocation in tqdm(invocations):
        invoke = gi.workflows.show_invocation(wf['id'], invocation['id'])

        workflow_states[invocation['id']] = {}
        for step in invoke['steps']:
            sid = step['workflow_step_uuid']
            if sid == 'None':
                sid = step['order_index']

            workflow_states[invocation['id']][sid] = step['state']

    print(json.dumps(workflow_states, sort_keys=True, indent=2))
示例#28
0
    def get_datasets_and_histories(self, allowed_files, history_id=None):
        """Get Galaxy datasets of current history
        and all histories of a user

        :returns: a list of datasets
        :rtype: dict
        """

        galaxy_instance = galaxy.GalaxyInstance(self.url, self.apikey)
        results = {}

        # get current history
        if not history_id:
            history_id = galaxy_instance.histories.get_most_recently_used_history(
            )['id']

        # Get all available history id and name
        histories = galaxy_instance.histories.get_histories()
        histories_list = []
        for history in histories:
            if history['id'] == history_id:
                history_dict = {
                    "name": history['name'],
                    "id": history['id'],
                    "selected": True
                }
            else:
                history_dict = {
                    "name": history['name'],
                    "id": history['id'],
                    "selected": False
                }
            histories_list.append(history_dict)

        # Get datasets of selected history
        dataset_list = []
        history_content = galaxy_instance.histories.show_history(history_id,
                                                                 contents=True)
        for dataset in history_content:
            if 'extension' not in dataset:
                continue
            if dataset['extension'] not in allowed_files:
                continue
            if 'deleted' in dataset:
                # Don't show deleted datasets
                if dataset['deleted']:
                    continue
            dataset_list.append(dataset)

        results['datasets'] = dataset_list
        results['histories'] = histories_list

        return results
def main(master_api_key, api_url, galaxy_admin_user, galaxy_admin_pass, workflows_dir):
    gi = galaxy.GalaxyInstance(url=api_url, key=master_api_key)

    #adding new user
    admin_key = create_user(gi, galaxy_admin_user.split('@')[0], galaxy_admin_user, galaxy_admin_pass)

    print "Galaxy API key [%s] created for user [%s]." % (admin_key,galaxy_admin_user)

    print "Signing in as admin user."
    gi = galaxy.GalaxyInstance(url=api_url, key=admin_key)

    for (dir_name, sub_dir, file_names) in os.walk(workflows_dir):
        for xml_file in filter(lambda f: f == 'repository_dependencies.xml', file_names):
            xml_path = os.path.join(dir_name, xml_file)
            print "Going to parse " + xml_path
            xml_doc = ET.parse(xml_path)
            xml_root = xml_doc.getroot()
            for repository in xml_root.findall('.//repository'):
                name = repository.get('name')
                owner = repository.get('owner')
                url = repository.get('toolshed')
                revision = repository.get('changeset_revision')
                tool_info = ", ".join([name, owner, url, revision])

                print "Going to install %s" % tool_info
                try:
                    gi.toolShed.install_repository_revision(url, name, owner, revision, install_tool_dependencies = True, install_repository_dependencies = True)
                except galaxy.client.ConnectionError as e:
                    error_body = json.loads(e.body)
                    if (error_body['err_code'] == 400008):
                        print "%s is already installed, skipping." % tool_info
                    else:
                        print "%s failed to install, check the galaxy logs." % tool_info
                        raise

        for workflow_file in filter(lambda f: f.endswith('.ga'), file_names):
            workflow_path = os.path.join(dir_name, workflow_file)
            print "Going to load " + workflow_path
            gi.workflows.import_workflow_from_local_path(workflow_path)
    return
def main():
    parser = argparse.ArgumentParser(
        description='Populate the Galaxy data library with test data.')
    parser.add_argument("-v",
                        "--verbose",
                        help="Increase output verbosity.",
                        action="store_true")
    parser.add_argument('-i', '--infile', type=argparse.FileType('r'))
    parser.add_argument("-g",
                        "--galaxy",
                        help="Target Galaxy instance URL/IP address.")
    parser.add_argument("-u", "--user", help="Galaxy user name")
    parser.add_argument("-p",
                        "--password",
                        help="Password for the Galaxy user")
    parser.add_argument(
        "-a",
        "--api_key",
        dest="api_key",
        help=
        "Galaxy admin user API key (required if not defined in the tools list file)"
    )

    args = parser.parse_args()

    if args.user and args.password:
        gi = galaxy.GalaxyInstance(url=args.galaxy,
                                   email=args.user,
                                   password=args.password)
    elif args.api_key:
        gi = galaxy.GalaxyInstance(url=args.galaxy, key=args.api_key)
    else:
        sys.exit(
            'Please specify either a valid Galaxy username/password or an API key.'
        )

    if args.verbose:
        log.basicConfig(level=log.DEBUG)

    setup_data_libraries(gi, args.infile)