diff --git a/defaults/main.yml b/defaults/main.yml index ead56f6..b8aa984 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -18,3 +18,4 @@ r_environment: {} r_scripts_install_path: /usr/local/bin r_scripts_install_owner: root r_scripts_install_group: root +r_list_packages_once: false diff --git a/tasks/get_list_packages.yml b/tasks/get_list_packages.yml new file mode 100644 index 0000000..7bf9c78 --- /dev/null +++ b/tasks/get_list_packages.yml @@ -0,0 +1,16 @@ +--- + +- name: packages | list installed packages + command: > + R-list-installed-packages {{ r_packages_lib }} + environment: "{{ r_environment }}" + register: _r_installed_packages + changed_when: false + tags: + - r-list-installed-packages + +- name: packages | set_fact list installed packages + set_fact: + r_installed_packages: "{{ _r_installed_packages.stdout_lines }}" + tags: + - r-list-installed-packages diff --git a/tasks/packages.yml b/tasks/packages.yml index 3231073..592703c 100644 --- a/tasks/packages.yml +++ b/tasks/packages.yml @@ -8,19 +8,23 @@ group: "{{ r_scripts_install_group }}" mode: 0755 with_items: + - src: R-list-installed-packages.j2 + dest: R-list-installed-packages - src: R-install-package.j2 dest: R-install-package - src: R-remove-package.j2 dest: R-remove-package - src: R-update-package.j2 dest: R-update-package - - src: R-update-packages.j2 - dest: R-update-packages - src: R-install-requirements.j2 dest: R-install-requirements tags: - r-packages-copy-r-scripts +- name: packages | init the package lists + include: get_list_packages.yml + when: r_list_packages_once + - name: packages | install command: > {{ r_scripts_install_path }}/R-install-package @@ -37,7 +41,9 @@ r_packages_from_bioconductor | ternary ((r_version is version('35', '>=') | ternary (r_preset_package_bioconductor, [])), []) + r_packages }}" - when: item.state is undefined or item.state == 'present' + when: + - not r_list_packages_once or item.name not in r_installed_packages + - item.state is undefined or item.state == 'present' tags: - r-packages-install @@ -51,6 +57,7 @@ changed_when: "_r_remove_package.stdout_lines[-1] is defined and _r_remove_package.stdout_lines[-1] == 'changed'" with_items: "{{ r_packages }}" when: + - not r_list_packages_once or item.name in r_installed_packages - item.state is defined - item.state == 'absent' tags: diff --git a/templates/R-install-package.j2 b/templates/R-install-package.j2 index 970cc16..94c7dbd 100644 --- a/templates/R-install-package.j2 +++ b/templates/R-install-package.j2 @@ -16,7 +16,9 @@ if (type == 'github') { package = argv[1]; } +{% if r_list_packages_once == false %} if (!(package %in% installed.packages(lib.loc = lib)[, 'Package'])) { +{% endif %} if (type == 'cran') { repos = ifelse(is.na(argv[4]), '{{ r_packages_repos.rstrip('/') }}', argv[4]); @@ -52,4 +54,6 @@ if (!(package %in% installed.packages(lib.loc = lib)[, 'Package'])) { cat("Unrecognised type\n"); q(status = 1); } +{% if r_list_packages_once == false %} } +{% endif %} diff --git a/templates/R-list-installed-packages.j2 b/templates/R-list-installed-packages.j2 new file mode 100644 index 0000000..843f33d --- /dev/null +++ b/templates/R-list-installed-packages.j2 @@ -0,0 +1,12 @@ +#!/usr/bin/env Rscript + +argv = commandArgs(trailingOnly = TRUE); +if (is.null(argv) | length(argv) < 1) { + cat("Usage: R-list-installed-packages [lib]\n"); + q(status = 1); +} + +lib = ifelse(is.na(argv[1]), '{{ r_packages_lib }}', argv[1]); + +cat(paste(installed.packages(lib.loc = lib)[, 'Package'], collapse="\n")) +