-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathmeta.R
211 lines (203 loc) · 6.29 KB
/
meta.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#' @title Compute the initial pre-build metadata of a target or import.
#' @description The metadata helps determine if the
#' target is up to date or outdated. The metadata of imports
#' is used to compute the metadata of targets.
#' @details Target metadata is computed
#' with `drake_meta()`, and then
#' `drake:::store_outputs()` completes the metadata
#' after the target is built.
#' In other words, the output of `drake_meta()` corresponds
#' to the state of the target immediately before [make()]
#' builds it.
#' See [diagnose()] to read the final metadata of a target,
#' including any errors, warnings, and messages in the last build.
#' @seealso [diagnose()], [dependency_profile()], [make()]
#' @export
#' @return A list of metadata on a target. Does not include
#' the file modification time if the target is a file.
#' That piece is computed later in [make()] by
#' `drake:::store_outputs()`.
#' @param target Character scalar, name of the target
#' to get metadata.
#' @param config Master internal configuration list produced
#' by [drake_config()].
#' @examples
#' \dontrun{
#' test_with_dir("Quarantine side effects.", {
#' # This example is not really a user-side demonstration.
#' # It just walks through a dive into the internals.
#' # Populate your workspace and write 'report.Rmd'.
#' load_mtcars_example() # Get the code with drake_example("mtcars").
#' # Create the master internal configuration list.
#' config <- drake_config(my_plan)
#' # Optionally, compute metadata on 'small',
#' # including a hash/fingerprint
#' # of the dependencies. If meta is not supplied,
#' # drake_build() computes it automatically.
#' meta <- drake_meta(target = "small", config = config)
#' # Should not yet include 'small'.
#' cached()
#' # Build 'small'.
#' # Equivalent to just drake_build(target = "small", config = config).
#' drake_build(target = "small", config = config, meta = meta)
#' # Should now include 'small'
#' cached()
#' readd(small)
#' })
#' }
drake_meta <- function(target, config = drake::read_drake_config()) {
layout <- config$layout[[target]]
meta <- list(
name = target,
target = target,
imported = layout$imported %||% TRUE,
missing = !target_exists(target = target, config = config),
seed = seed_from_basic_types(config$seed, target)
)
if (meta$imported) {
meta$isfile <- is_encoded_path(target)
meta$trigger <- trigger(condition = TRUE)
} else {
meta$isfile <- FALSE
meta$trigger <- as.list(layout$trigger)
}
# For imported files.
if (meta$isfile) {
meta$mtime <- file.mtime(decoded_path(target, config))
}
if (meta$trigger$command) {
meta$command <- layout$command_standardized
}
if (meta$trigger$depend) {
meta$dependency_hash <- dependency_hash(target = target, config = config)
}
if (meta$trigger$file) {
meta$input_file_hash <- input_file_hash(target = target, config = config)
meta$output_file_hash <- output_file_hash(target = target, config = config)
}
if (!is.null(meta$trigger$change)) {
deps <- layout$deps_change[c("globals", "namespaced", "loadd", "readd")]
ensure_loaded(unlist(deps), config = config)
meta$trigger$value <- eval(meta$trigger$change, config$eval)
}
meta
}
dependency_hash <- function(target, config) {
x <- config$layout[[target]]$deps_build
deps <- c(x$globals, x$namespaced, x$loadd, x$readd)
if (is_imported(target, config)) {
deps <- c(deps, x$file_in, x$knitr_in)
}
deps <- unlist(deps)
deps <- as.character(deps)
deps <- unique(deps)
deps <- sort(deps)
out <- self_hash(deps, config)
out <- paste(out, collapse = "")
digest::digest(out, algo = config$long_hash_algo, serialize = FALSE)
}
input_file_hash <- function(
target,
config,
size_cutoff = rehash_file_size_cutoff
) {
deps <- config$layout[[target]]$deps_build
files <- sort(unique(as.character(c(deps$file_in, deps$knitr_in))))
out <- vapply(
X = files,
FUN = file_hash,
FUN.VALUE = character(1),
config = config,
size_cutoff = size_cutoff
)
out <- paste(out, collapse = "")
digest::digest(out, algo = config$long_hash_algo, serialize = FALSE)
}
output_file_hash <- function(
target,
config,
size_cutoff = rehash_file_size_cutoff
) {
deps <- config$layout[[target]]$deps_build
files <- sort(unique(as.character(deps$file_out)))
out <- vapply(
X = files,
FUN = file_hash,
FUN.VALUE = character(1),
config = config,
size_cutoff = size_cutoff
)
out <- paste(out, collapse = "")
digest::digest(out, algo = config$long_hash_algo, serialize = FALSE)
}
self_hash <- Vectorize(function(target, config) {
if (kernel_exists(target = target, config = config)) {
config$cache$get_hash(target, namespace = "kernels")
} else {
NA_character_
}
},
"target", USE.NAMES = FALSE)
rehash_file <- function(target, config) {
file <- decoded_path(target, config) %||% target
if (!file.exists(file) || file.info(file)$isdir) {
return(NA_character_)
}
digest::digest(
object = file,
algo = config$long_hash_algo,
file = TRUE,
serialize = FALSE
)
}
safe_rehash_file <- function(target, config) {
if (file.exists(decoded_path(target, config))) {
rehash_file(target = target, config = config)
} else {
NA_character_
}
}
should_rehash_file <- function(filename, new_mtime, old_mtime,
size_cutoff) {
do_rehash <- file.size(filename) < size_cutoff | new_mtime > old_mtime
if (safe_is_na(do_rehash)) {
do_rehash <- TRUE
}
do_rehash
}
file_hash <- function(
target,
config,
size_cutoff = rehash_file_size_cutoff
) {
filename <- decoded_path(target, config) %||% target
if (!file.exists(filename))
return(NA_character_)
old_mtime <- ifelse(
exists_in_subspace(
key = target,
subspace = "mtime",
namespace = "meta",
cache = config$cache
),
get_from_subspace(
key = target,
subspace = "mtime",
namespace = "meta",
cache = config$cache
),
-Inf
)
new_mtime <- file.mtime(filename)
do_rehash <- should_rehash_file(
filename = filename,
new_mtime = new_mtime,
old_mtime = old_mtime,
size_cutoff = size_cutoff)
old_hash_exists <- config$cache$exists(key = target, namespace = "kernels")
if (do_rehash || !old_hash_exists) {
rehash_file(target = target, config = config)
} else {
config$cache$get(key = target, namespace = "kernels")
}
}