Skip to content

Commit

Permalink
Ignore more files (#700)
Browse files Browse the repository at this point in the history
* Ignore temporary backup files. Fixes #111.
* Ignore python virtual envs. Fixes #632
* Don't special case root directory
  • Loading branch information
hadley authored Feb 26, 2023
1 parent bcd6ce3 commit 2a578e5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# rsconnect 0.8.30 (development version)

* `deployApp()` excludes temporary backup files (names starting or ending
with `~`) when automatically determining files to bundle (#111) and
excludes directories that are likely to be python virtual environments
(#632). Additionally, ignore rules are always now applied to all directories;
previously some (like `.Rproj.user` and `"manifest.json"`) were only applied
to the root directory.

* `deployApp()` is more aggressive about saving deployment data, which should
make it less likely that you need to repeat yourself after a failed
deployment. In particular, it now saves both before and after uploading the
Expand Down
47 changes: 27 additions & 20 deletions R/bundleFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,11 @@ bundleFiles <- function(appDir) {

recursiveBundleFiles <- function(dir,
rootDir = dir,
depth = 0,
totalFiles = 0,
totalSize = 0) {
# generate a list of files at this level
contents <- list.files(dir, all.files = TRUE, no.. = TRUE, include.dirs = TRUE)
contents <- ignoreBundleFiles(dir, contents, depth = depth)
contents <- ignoreBundleFiles(dir, contents)

# Info for each file lets us know to recurse (directories) or aggregate (files).
is_dir <- dir.exists(file.path(dir, contents))
Expand All @@ -147,8 +146,7 @@ recursiveBundleFiles <- function(dir,
dir = file.path(dir, name),
rootDir = rootDir,
totalFiles = totalFiles,
totalSize = totalSize,
depth = depth + 1
totalSize = totalSize
)

children <- append(children, file.path(name, out$contents))
Expand All @@ -170,23 +168,27 @@ recursiveBundleFiles <- function(dir,
)
}

ignoreBundleFiles <- function(dir, contents, depth = 0) {
# exclude some well-known files/directories at root level
if (depth == 0) {
contents <- contents[!grepl(glob2rx("*.Rproj"), contents)]
contents <- setdiff(
contents,
c("manifest.json", "rsconnect", "packrat", "app_cache", ".Rproj.user")
)
}

# exclude renv files, knitr cache dirs, and another well-known files
contents <- setdiff(contents, c("renv", "renv.lock"))
contents <- contents[!isKnitrCacheDir(contents)]
contents <- setdiff(
contents,
c(".DS_Store", ".git", ".gitignore", ".quarto", ".Rhistory", ".svn")
ignoreBundleFiles <- function(dir, contents) {
ignore <- c(
# rsconnect packages
"rsconnect", "rsconnect-python", "manifest.json",
# packrat + renv,
"renv", "renv.lock", "packrat",
# version control
".git", ".gitignore", ".svn",
# R/RStudio
".Rhistory", ".Rproj.user",
# python virtual envs
# https://github.com/rstudio/rsconnect-python/blob/94dbd28797ee503d66411f736da6edc29fcf44ed/rsconnect/bundle.py#L37-L50
".env", "env", ".venv", "venv", "__pycache__/",
# other
".DS_Store", ".quarto", "app_cache"
)
contents <- setdiff(contents, ignore)
contents <- contents[!isKnitrCacheDir(contents)]
contents <- contents[!isPythonEnv(dir, contents)]
contents <- contents[!grepl("^~|~$", contents)]
contents <- contents[!grepl(glob2rx("*.Rproj"), contents)]

# remove any files lines listed .rscignore
if (".rscignore" %in% contents) {
Expand All @@ -206,6 +208,11 @@ isKnitrCacheDir <- function(files) {
ifelse(is_cache, has_rmd, FALSE)
}

# https://github.com/rstudio/rsconnect-python/blob/94dbd28797ee503d6/rsconnect/bundle.py#L541-L543
isPythonEnv <- function(dir, files) {
file.exists(file.path(dir, files, "bin", "python"))
}

enforceBundleLimits <- function(appDir, totalFiles, totalSize) {
maxSize <- getOption("rsconnect.max.bundle.size")
maxFiles <- getOption("rsconnect.max.bundle.files")
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-bundleFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ test_that("ignores files listed in .rscignore", {
expect_setequal(bundleFiles(dir), character())
})

test_that("ignores temporary files", {
ignored <- ignoreBundleFiles(
dir = ".",
contents = c("foo.xlsx", "~$foo.xlsx", "foo.csv", "foo.csv~")
)
expect_equal(ignored, c("foo.xlsx", "foo.csv"))
})

test_that("ignores python virtual envs", {
dir <- withr::local_tempdir()
dir.create(file.path(dir, "test", "bin"), recursive = TRUE)
file.create(file.path(dir, "test", "bin", "python"))
dir.create(file.path(dir, "venv"))
file.create(file.path(dir, "venv", "somefile"))

expect_equal(bundleFiles(dir), character())
})

# explodeFiles ------------------------------------------------------------

test_that("returns relative paths", {
Expand Down

0 comments on commit 2a578e5

Please sign in to comment.