|
1 | 1 | # File src/library/tools/R/license.R
|
2 | 2 | # Part of the R package, https://www.R-project.org
|
3 | 3 | #
|
4 |
| -# Copyright (C) 1995-2019 The R Core Team |
| 4 | +# Copyright (C) 1995-2015 The R Core Team |
5 | 5 | #
|
6 | 6 | # This program is free software; you can redistribute it and/or modify
|
7 | 7 | # it under the terms of the GNU General Public License as published by
|
@@ -931,3 +931,66 @@ function(file, keep = TRUE)
|
931 | 931 | licenses =
|
932 | 932 | select(x, - c(1L, pos), fields_in_license_para, keep))
|
933 | 933 | }
|
| 934 | + |
| 935 | +spdx2r <- |
| 936 | +function(spdx) |
| 937 | +{ |
| 938 | + if(!is.character(spdx)) { |
| 939 | + stop("Input must be character") |
| 940 | + } |
| 941 | + |
| 942 | + db <- read.dcf(file.path(R.home("share"), "licenses", "license.db")) |
| 943 | + |
| 944 | + ## output = basename(File) if available, else SPDX identifier |
| 945 | + r_format <- ifelse(!is.na(db[, "File"]), |
| 946 | + basename(db[, "File"]), |
| 947 | + db[, "SPDX"]) |
| 948 | + names(r_format) <- db[, "SPDX"] |
| 949 | + |
| 950 | + convert_single <- function(single_id) { |
| 951 | + single_id <- toupper(trimws(single_id)) |
| 952 | + if(is.na(single_id) || !nzchar(single_id)) |
| 953 | + return(NA_character_) |
| 954 | + |
| 955 | + single_id <- sub("-only", "", single_id) |
| 956 | + |
| 957 | + ## Return if perfect match to license.db |
| 958 | + idx <- match(toupper(single_id), toupper(names(r_format))) |
| 959 | + if (!is.na(idx)) |
| 960 | + return(r_format[idx]) |
| 961 | + |
| 962 | + ## -or-later + license.db does not always retain trailing zeros |
| 963 | + if(grepl("-or-later$", single_id, ignore.case = TRUE)) { |
| 964 | + base_id <- sub("-or-later", "", |
| 965 | + single_id, ignore.case = TRUE) |
| 966 | + base_id_nozero <- |
| 967 | + c(base_id, sub("-(\\d)\\.0", "-\\1", base_id)) |
| 968 | + full_id <- sub("-(\\d)\\.0-or-later", " (>= \\1)", |
| 969 | + single_id, ignore.case = TRUE) |
| 970 | + full_id <- sub("-(\\d\\.\\d)-or-later", " (>= \\1)", |
| 971 | + full_id, ignore.case = TRUE) |
| 972 | + if(any(toupper(base_id_nozero) %in% toupper(r_format))) |
| 973 | + return(full_id) |
| 974 | + } |
| 975 | + |
| 976 | + return(NA_character_) |
| 977 | + } |
| 978 | + |
| 979 | + convert_compound <- function(compound_id) { |
| 980 | + compound_id <- trimws(compound_id) |
| 981 | + ## OR -> | |
| 982 | + if(grepl(" OR ", compound_id, ignore.case = TRUE)) { |
| 983 | + parts <- strsplit(compound_id, " [Oo][Rr] ", perl = TRUE)[[1L]] |
| 984 | + converted_parts <- |
| 985 | + vapply(parts, convert_single, "", USE.NAMES = FALSE) |
| 986 | + if(any(is.na(converted_parts))) |
| 987 | + return(NA_character_) |
| 988 | + return(paste(converted_parts, collapse = " | ")) |
| 989 | + } |
| 990 | + convert_single(compound_id) |
| 991 | + } |
| 992 | + |
| 993 | + result <- vapply(spdx, convert_compound, "", USE.NAMES = FALSE) |
| 994 | + names(result) <- NULL |
| 995 | + result |
| 996 | +} |
0 commit comments