Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Library Permissions #187

Merged
merged 16 commits into from Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.6']
python-version: ['3.7']
tox-action:
- lint
- pytest
Expand Down Expand Up @@ -37,7 +37,7 @@ jobs:
submodules: true
- uses: actions/setup-python@v1
with:
python-version: '3.6'
python-version: '3.7'
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip setuptools
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -5,3 +5,4 @@ Jinja2
galaxy-tool-util>=20.9.1
galaxy-util>=20.9.0
pysam
rich
5 changes: 4 additions & 1 deletion setup.py
Expand Up @@ -37,6 +37,7 @@ def get_var(var_name):
setup-data-libraries=ephemeris.setup_data_libraries:main
galaxy-wait=ephemeris.sleep:main
install_tool_deps=ephemeris.install_tool_deps:main
set_library_permissions=ephemeris.set_library_permissions:main
'''
PACKAGE_DATA = {
# Be sure to update MANIFEST.in for source dist.
Expand Down Expand Up @@ -76,6 +77,7 @@ def get_var(var_name):
install_requires=requirements,
license="AFL",
zip_safe=False,
python_requires=">=3.7",
keywords='galaxy',
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand All @@ -88,9 +90,10 @@ def get_var(var_name):
'Topic :: Software Development :: Testing',
'Natural Language :: English',
"Programming Language :: Python :: 3",
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
],
test_suite=TEST_DIR,
tests_require=test_requirements
Expand Down
132 changes: 132 additions & 0 deletions src/ephemeris/set_library_permissions.py
@@ -0,0 +1,132 @@
#!/usr/bin/env python
"""Tool to set permissions for all datasets of a given Galaxy Data Library"""

import argparse
import logging as log
import sys

from bioblend import galaxy
from rich.progress import Progress

from .common_parser import get_common_args

# Print iterations progress


def get_datasets(gi, library_id) -> [str]:
objects = gi.libraries.show_dataset(library_id=library_id, dataset_id="")
datasets = []
for index in range(len(objects)):
if objects[index]["type"] == "file":
datasets.append(objects[index]["id"])
if datasets == []:
sys.exit("No datasets in library!")
else:
return datasets


def set_permissions(gi, library_id, role_ids, auto):
log.info("Your library_id is %s", library_id)
log.info("Your roles are: %s", " ".join(role_ids))
datasets = get_datasets(gi, library_id)
total = len(datasets)
est = total * 3 / 60
# Give User time to abort
log.info(
"\nSuccess! %d datasets found. Processing can take up to %0.02f min\n",
total,
est,
)
if auto:
hexylena marked this conversation as resolved.
Show resolved Hide resolved
for current in range(total):
log.debug(
"Processing dataset %d of %d, ID=%s", current, total, datasets[current]
)
gi.libraries.set_dataset_permissions(
dataset_id=datasets[current],
access_in=role_ids,
modify_in=role_ids,
manage_in=role_ids,
)
else:
if input("Do you want to continue? (y/n) ") == "y":
with Progress() as progress:
task = progress.add_task("[green]Processing datasets...", total=total)
for current in range(total):
log.debug(
"Processing dataset %d of %d, ID=%s",
current,
total,
datasets[current],
)
gi.libraries.set_dataset_permissions(
dataset_id=datasets[current],
access_in=role_ids,
modify_in=role_ids,
manage_in=role_ids,
)
progress.update(task, advance=1)
else:
log.info("Operation cancelled by user. No changes were applied.\n")


def _parser():
"""Constructs the parser object"""
parent = get_common_args()
parser = argparse.ArgumentParser(
parents=[parent], description="Populate the Galaxy data library with data."
)
parser.add_argument("library", help="Specify the data library ID")
parser.add_argument("--roles", nargs="+", help="Specify a list of comma separated role IDs")
parser.add_argument(
"-y",
"--yes",
default=False,
action="store_true",
help="Set the -y flag for auto-accept and skip manual approvement",
)
parser.add_argument(
"-s",
"--silent",
default=False,
action="store_true",
help="sets loglevel to ERROR",
)
return parser


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)
elif args.silent:
log.basicConfig(level=log.ERROR)
else:
log.basicConfig(level=log.INFO)

if args.roles and args.library:
args.roles = [r.strip() for r in args.roles.split(",")]
else:
sys.exit(
"Specify library ID (--library myLibraryID) and (list of) role(s) (--roles roleId1,roleId2)"
)
set_permissions(gi, library_id=args.library, role_ids=args.roles, auto=args.yes)
log.info(
"\nThis script uses bioblend to update ALL permissions of ALL datasets in a"
"specified library to the given roles. Be careful and cancel if unsure\n"
)


if __name__ == "__main__":
main()
10 changes: 5 additions & 5 deletions tox.ini
@@ -1,21 +1,21 @@
# TODO: implement doc linting
[tox]
envlist = py{36}-lint, py{36}-pytest, py{36}, py{36}-integration
envlist = py{37}-lint, py{37}-pytest, py{37}, py{37}-integration
source_dir = src/ephemeris
test_dir = tests

[testenv]
commands = {envpython} setup.py nosetests []
whitelist_externals = bash

[testenv:py36-lint]
[testenv:py37-lint]
commands = flake8 {[tox]source_dir} {[tox]test_dir}
skip_install = True
deps =
flake8
flake8-import-order

[testenv:py36-pytest]
[testenv:py37-pytest]
deps =
-r requirements.txt
pytest
Expand All @@ -32,12 +32,12 @@ commands =
# Unfortunately this has to run in the tox env to have access to envsitepackagesdir
sed -i 's#{envsitepackagesdir}#src#' coverage.xml

[testenv:py36]
[testenv:py37]
deps =
-r requirements.txt
commands = bash {[tox]test_dir}/test.sh

[testenv:py36-integration]
[testenv:py37-integration]
deps =
-r requirements.txt
commands = bash {[tox]test_dir}/test.sh