-
Notifications
You must be signed in to change notification settings - Fork 6
/
gha_python_versions.R
76 lines (75 loc) Β· 2.85 KB
/
gha_python_versions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#' GitHub Actions python versions
#'
#' Retrieve all python versions available for GitHub Actions runners.
#' @inheritParams construct_runners
#' @returns Yaml with info for all versions of python,
#' or a character string of one or more versions of python.
#'
#' @keywords internal
#' @importFrom tools R_user_dir
#' @importFrom utils download.file
gha_python_versions <- function(
python_version=NULL,
## versions_explicit must be TRUE for setup-miniconda to recognize it
versions_explicit=TRUE,
cache_dir=tools::R_user_dir(package = "rworkflows",
which = "cache"),
verbose=TRUE){
# devoptera::args2vars(gha_python_versions)
#### Download versions path ####
versions_url <- paste0(
"https://raw.githubusercontent.com/actions/python-versions/",
"main/versions-manifest.json")
versions_path <- file.path(cache_dir,basename(versions_url))
if(!file.exists(versions_path)){
messager("Downloding python versions json.", v=verbose)
dir.create(cache_dir, showWarnings = FALSE, recursive = TRUE)
utils::download.file(url = versions_url, destfile = versions_path)
}
### Read yaml ####
messager("Reading yaml.", v=verbose)
yml <- yaml::read_yaml(versions_path)
#### Check python_version ####
if(!is.null(python_version)){
messager("Validating python_version.",v=verbose)
python_version <- as.character(python_version)
#### Get all versions (minor and major) ####
versions <- sapply(yml, function(x){x$version})
versions_major <- unique(sapply(strsplit(versions,'\\.'),
function(x)paste(x[1],x[2],sep="."))
)
versions_x <- unique(sapply(strsplit(versions,'\\.'),
function(x)paste(x[1],"x",sep="."))
)
versions_opts <- c(versions,versions_major,versions_x)
#### Get devel version ####
if(python_version %in% c("devel","dev")){
if(isTRUE(versions_explicit)){
python_version <- versions[sapply(yml, function(x){x$stable==FALSE})][1]
} else {
python_version <- "devel"
}
#### Get release version ####
} else if (python_version %in% c("latest","release")){
if(isTRUE(versions_explicit)){
python_version <- versions[sapply(yml, function(x){x$stable==TRUE})][1]
} else {
python_version <- "latest"
}
#### Get specific version ####
} else if(python_version=="3"){
python_version <- "3.x"
}else {
if(!python_version %in% versions_opts){
stopper(paste0("python_version=",shQuote(python_version),
"is not available.",
"Must be one of:",
paste("\n - ",shQuote(versions_opts),collapse = "")))
}
}
return(python_version)
#### Or return entire yaml with all version info ####
} else {
return(yml)
}
}