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 # # 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 ) } # this list is based in the licenses.json file from the stack dataset # see: https://huggingface.co/datasets/bigcode/the-stack-dedup/blob/main/licenses.json allowed_license_codes <- c("MIT", "Apache", "BSD", "CC-BY", "Artistic", "CC0", "FreeBSD") # 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) permissive_licenses <- license_codes%>% mutate(permissive = license_code %in% allowed_license_codes) %>% group_by(license) %>% summarise(permissive = any(permissive)) %>% filter(permissive) # We then filter the allowed packages. allowed_packages <- licenses %>% filter(license %in% permissive_licenses$license) arrow::write_parquet(allowed_packages, "packages.parquet") # the distribution of packages per license code licenses %>% left_join(license_codes, by = "license", relationship = "many-to-many") %>% count(license_code) %>% mutate(n / nrow(licenses)) %>% arrange(desc(n)) %>% print(n = 100) # # A tibble: 24 × 3 # license_code n `n/nrow(licenses)` # # 1 GPL 18440 0.726 # 2 MIT 4533 0.179 # 3 LGPL 556 0.0219 # 4 Apache 428 0.0169 # 5 BSD-Clause 343 0.0135 # 6 CC0 261 0.0103 # 7 AGPL 232 0.00914 # 8 Artistic 158 0.00622 # 9 NA 96 0.00378 # 10 Unlimited 76 0.00299 # 11 CC-BY 56 0.00221 # 12 MPL 52 0.00205 # 13 EUPL 35 0.00138 # 14 CC-BY-SA 34 0.00134 # 15 CeCILL 34 0.00134 # 16 BSD 33 0.00130 # 17 CC-BY-NC-SA 27 0.00106 # 18 FreeBSD 18 0.000709 # 19 BSL 10 0.000394 # 20 CC-BY-NC 6 0.000236 # 21 EPL 6 0.000236 # 22 ACM 5 0.000197 # 23 CPL 3 0.000118 # 24 CC-BY-NC-ND 1 0.0000394