cran-packages / 02-cran-licenses.R
dfalbel's picture
change structure
6cabbde unverified
library(tidyverse)
repos <- jsonlite::read_json("repos.json")
extract_licenses <- function(repo) {
repo_name <- as.character(repo$name)
path <- glue::glue("CRAN/{repo_name}.zip")
files <- try(zip::zip_list(path), silent = TRUE)
if (inherits(files, "try-error")) {
return(NULL)
}
files <- files[endsWith(files$filename, "DESCRIPTION"), ]
if (nrow(files) > 1) {
files <- head(files, 1)
}
if (nrow(files) == 0) {
return(NULL)
}
temp <- tempfile()
on.exit(unlink(temp), add = TRUE)
zip::unzip(path, files = files$filename, junkpaths = TRUE, exdir = temp)
desc <- try(desc::desc(file = fs::path(temp, "DESCRIPTION")), silent = TRUE)
if (inherits(desc, "try-error")) {
return(NULL)
}
tibble(
package = repo_name,
license = desc$get("License")
)
}
library(future)
plan(multisession, workers = 8)
# this lists all packages and the License as it's written in the DESCRIPTION
licenses <- furrr::future_map(
repos,
\(repo) extract_licenses(repo),
.progress = TRUE
)
licenses <- bind_rows(licenses)
# Manually classified all possible licenses into 23 license codes.
# A tibble: 24 × 1
# license_code
# <chr>
# 1 ACM
# 2 AGPL
# 3 Apache
# 4 Artistic
# 5 BSD
# 6 BSD-Clause
# 7 BSL
# 8 CC-BY
# 9 CC-BY-NC
# 10 CC-BY-NC-ND
# 11 CC-BY-NC-SA
# 12 CC-BY-SA
# 13 CC0
# 14 CPL
# 15 CeCILL
# 16 EPL
# 17 EUPL
# 18 FreeBSD
# 19 GPL
# 20 LGPL
# 21 MIT
# 22 MPL
# 23 Unlimited
# 24 NA
# the manual classification ('classif' field) has to be parsed.
licenses_table <- readr::read_csv("licenses.csv")
# some licenses ended up duplicated (I think they have been trimed in excel or something)
licenses_table <- licenses_table %>% distinct()
parse_license <- function(license) {
licenses <- license |>
stringr::str_split(pattern = stringr::fixed("|"))
licenses <- stringr::str_trim(licenses[[1]])
license <- stringr::str_extract(licenses, "(.*?)(?=\\()|.*")
license <- stringr::str_trim(license)
version <- stringr::str_extract(licenses, "\\((.*?)\\)")
tibble(
license_code = license,
version = version
)
}
# we now make the indirection from allowed codes to allowed License as specified
# in the DESCRIPTION.
license_codes <- licenses_table %>%
mutate(
parsed = map(classif, parse_license)
) %>%
unnest(parsed)
# we now map license_codes to the same classification used by github
gh_license_table <- license_codes %>%
mutate(
license_gh = case_when(
classif == "GPL (>=2)" ~ "gpl-3.0",
classif == "GPL (>=3)" ~ "gpl-3.0",
classif == "GPL (==3)" ~ "gpl-3.0",
classif == "GPL (==2)" ~ "gpl-2.0",
classif == "GPL" ~ "gpl-3.0",
classif %in% c("GPL (==3) | LGPL (==2.1)", "GPL (>2)", "GPL (>3)", "GPL (>=1)") ~ "gpl-3.0",
classif %in% c("GPL (<=2)", "GPL (==2) | LGPL (==2.1) | MPL (==1.1)") ~ "gpl-2.0",
classif == "AGPL (>=2)" ~ "agpl-3.0",
classif == "AGPL (>=3)" ~ "agpl-3.0",
classif == "AGPL (==3)" ~ "agpl-3.0",
classif == "AGPL (==2)" ~ "agpl-2.0",
classif == "AGPL" ~ "agpl-3.0",
classif == "LGPL (>=2)" ~ "lgpl-3.0",
classif == "LGPL (>=3)" ~ "lgpl-3.0",
classif %in% c("LGPL (==3)", "LGPL (>2)", "LGPL (>=2.1)") ~ "lgpl-3.0",
classif %in% c("LGPL (==2)", "LGPL (>=2, <3)", "LGPL (>= 2.0, < 3)", "LGPL (==2.1)", "LGPL (>= 2.0, < 3) | MPL") ~ "lgpl-2.0",
classif == "LGPL" ~ "lgpl-3.0",
classif == "BSD" ~ "bsd-3-clause",
classif == "BSD-Clause (==2)" ~ "bsd-2-clause",
classif == "BSD-Clause (==3)" ~ "bsd-3-clause",
classif %in% c("BSL", "BSL (==1)") ~ "bsl-1.0",
str_detect(classif, fixed("CC-BY (==4)")) ~ "cc-by-4.0",
str_detect(classif, fixed("CC-BY-SA (==4)")) ~ "cc-by-sa-4.0",
str_detect(classif, "CC0") ~ "cc0-1.0",
str_detect(classif, "FreeBSD") ~ "bsd-2-clause",
classif %in% c("MPL (==2)", "MPL (>=2)", "MPL") ~ "mpl-2.0",
classif %in% c("EUPL", "EUPL (==1.1)") ~ "eupl-1.1",
classif %in% c("EPL", "EPL (>=1)") ~ "epl-2.0",
str_detect(classif, "Apache") ~ "apache-2.0",
str_detect(classif, "Artistic") ~ "artistic-2.0",
str_detect(classif, "MIT") ~ "mit",
str_detect(classif, fixed("BSD-Clause (==2)")) ~ "bsd-2-clause",
str_detect(classif, fixed("BSD-Clause (==3)")) ~ "bsd-3-clause",
str_detect(classif, fixed("MPL (==2)")) ~ "mpl-2.0",
str_detect(classif, fixed("GPL (>=2")) ~ "gpl-2.0",
str_detect(classif, fixed("GPL (>=3")) ~ "gpl-3.0",
TRUE ~ "unknown"
)
)
# We then filter the allowed packages.
packages <- licenses %>%
mutate(license = stringr::str_trim(license)) %>%
left_join(gh_license %>% select(license, license_gh) %>% distinct(), by = "license") %>%
mutate(license_gh = coalesce(license_gh, "unknown"))
arrow::write_parquet(packages, "packages.parquet")