Skip to content

Commit 599a9b2

Browse files
authored
use environment variables for tagging (#290)
* use environment variables for tagging Hope I did everything right! * fix tests * changelog
1 parent b8adff1 commit 599a9b2

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# 2.7.0
2+
* DrWatson-related `ENV`ironment variables are now available to globally set the default values for e.g. story git patches, tagging, or safe-saving in various functions like `tagsave` or `produce_or_load`.
13
# 2.6.0
24
* Use `JLD2`'s jldopen in `collect_results!` to speed up loading of metadata.
35
# 2.5.0

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DrWatson"
22
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
33
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
4-
version = "2.6.2"
4+
version = "2.7.0"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/naming.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,9 @@ function savename(prefix::String, c, suffix::String;
9191
sort = true, equals = "=")
9292

9393
if any(sep in prefix for sep in ['/', '\\'])
94-
@warn """
95-
Path separators in `savename` prefixes may break reproducibility on other OS.
96-
The recommended way is using the `*dir()` methods or `joinpath` with
97-
`savename` (e.g. `datadir("path", "to", "folder", savename("prefix", data))`).
98-
"""
94+
@warn "Path separators in `savename` prefixes may break reproducibility on other OS. "*
95+
"The recommended way is using the `*dir()` methods or `joinpath` with "*
96+
"`savename` (e.g. `datadir(\"path\", \"to\", \"folder\", savename(\"prefix\", data))`)."
9997
end
10098
sigdigits = digits === nothing ? sigdigits : nothing
10199
val2string = val_to_string === nothing ? (val -> valtostring(val, digits, sigdigits)) : val_to_string

src/saving_files.jl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ end
2424
2525
## Keywords
2626
* `suffix = "jld2", prefix = default_prefix(config)` : Used in [`savename`](@ref).
27-
* `tag::Bool = istaggable(suffix)` : Save the file using [`tagsave`](@ref) if `true`.
27+
* `tag::Bool = get(ENV, "DRWATSON_TAG", istaggable(suffix))` : Save the file
28+
using [`tagsave`](@ref) if `true` (which is the default).
2829
* `gitpath, storepatch` : Given to [`tagsave`](@ref) if `tag` is `true`.
2930
* `force = false` : If `true` then don't check if file `s` exists and produce
3031
it and save it anyway.
@@ -41,9 +42,13 @@ produce_or_load(c, f; kwargs...) = produce_or_load("", c, f; kwargs...)
4142
produce_or_load(f::Function, c; kwargs...) = produce_or_load(c, f; kwargs...)
4243
produce_or_load(f::Function, path, c; kwargs...) = produce_or_load(path, c, f; kwargs...)
4344
function produce_or_load(path, c, f::Function;
44-
suffix = "jld2", prefix = default_prefix(c),
45-
tag::Bool = istaggable(suffix), gitpath = projectdir(), loadfile = true,
46-
force = false, verbose = true, storepatch = true, wsave_kwargs = Dict(), kwargs...)
45+
suffix = "jld2", prefix = default_prefix(c),
46+
tag::Bool = get(ENV, "DRWATSON_TAG", istaggable(suffix)),
47+
gitpath = projectdir(), loadfile = true,
48+
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", true),
49+
force = false, verbose = true, wsave_kwargs = Dict(),
50+
kwargs...
51+
)
4752

4853
s = joinpath(path, savename(prefix, c, suffix; kwargs...))
4954

@@ -118,21 +123,29 @@ end
118123
# tag saving #
119124
################################################################################
120125
"""
121-
tagsave(file::String, d::AbstractDict; safe = false, gitpath = projectdir(), storepatch = true, force = false, kwargs...)
126+
tagsave(file::String, d::AbstractDict; kwargs...)
122127
First [`tag!`](@ref) dictionary `d` and then save `d` in `file`.
123-
If `safe = true` save the file using [`safesave`](@ref).
124128
125129
"Tagging" means that when saving the dictionary, an extra field
126130
`:gitcommit` is added to establish reproducibility of results using
127-
Git. If the Git repository is dirty, one more field `:gitpatch` is
128-
added that stores the difference string. If a dictionary already
129-
contains a key `:gitcommit`, it is not overwritten, unless,
131+
Git. If the Git repository is dirty and `storepatch=true`, one more field `:gitpatch` is
132+
added that stores the difference string. If a dictionary already
133+
contains a key `:gitcommit`, it is not overwritten, unless
130134
`force=true`. For more details, see [`tag!`](@ref).
131135
136+
Keywords `gitpath, storepatch, force,` are propagated to [`tag!`](@ref).
132137
Any additional keyword arguments are propagated to `wsave`, to e.g.
133138
enable compression.
139+
140+
The keyword `safe = get(ENV, "DRWATSON_SAFESAVE", false)` decides whether
141+
to save the file using [`safesave`](@ref).
134142
"""
135-
function tagsave(file, d; safe::Bool = false, gitpath = projectdir(), storepatch = true, force = false, source = nothing, kwargs...)
143+
function tagsave(file, d;
144+
gitpath = projectdir(),
145+
safe::Bool = get(ENV, "DRWATSON_SAFESAVE", false),
146+
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", true),
147+
force = false, source = nothing, kwargs...
148+
)
136149
d2 = tag!(d, gitpath=gitpath, storepatch=storepatch, force=force, source=source)
137150
if safe
138151
safesave(file, copy(d2); kwargs...)
@@ -200,7 +213,7 @@ function increment_backup_num(filepath)
200213
path, filename = splitdir(filepath)
201214
fname, suffix = splitext(filename)
202215
m = match(r"^(.*)_#([0-9]+)$", fname)
203-
if m == nothing
216+
if m === nothing
204217
return joinpath(path, "$(fname)_#1$(suffix)")
205218
end
206219
newnum = string(parse(Int, m.captures[2]) +1)
@@ -228,7 +241,7 @@ Save each entry in `dicts` into a unique temporary file in the directory `tmp`.
228241
Then return the list of file names (relative to `tmp`) that were used
229242
for saving each dictionary. Each dictionary can then be loaded back by calling
230243
231-
FileIO.load(nth_tmpfilename, "params")
244+
wload(nth_tmpfilename, "params")
232245
233246
`tmp` defaults to `projectdir("_research", "tmp")`.
234247

src/saving_tools.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function gitpatch(path = projectdir(); try_submodule_diff=true)
141141
# Remove the submodule option as it is not supported by older git versions.
142142
return gitpatch(path; try_submodule_diff = false)
143143
else
144-
@warn "`gitpatch` failed with error $(result.err) $(result.exception) and , returning `nothing` instead."
144+
@warn "`gitpatch` failed with error $(result.err) $(result.exception). Returning `nothing` instead."
145145
end
146146
catch er
147147
if isa(er,LibGit2.GitError) && er.code == LibGit2.Error.ENOTFOUND
@@ -160,7 +160,7 @@ end
160160
# Tagging
161161
########################################################################################
162162
"""
163-
tag!(d::AbstractDict; gitpath = projectdir(), storepatch = true, force = false) -> d
163+
tag!(d::AbstractDict; kwargs...) -> d
164164
Tag `d` by adding an extra field `gitcommit` which will have as value
165165
the [`gitdescribe`](@ref) of the repository at `gitpath` (by default
166166
the project's gitpath). Do nothing if a key `gitcommit` already exists
@@ -178,6 +178,12 @@ To restore a repository to the state of a particular model-run do:
178178
1. checkout the relevant commit with `git checkout xyz` where xyz is the value stored
179179
2. apply the patch `git apply patch`, where the string stored in the `gitpatch` field needs to be written to the file `patch`.
180180
181+
## Keywords
182+
* `gitpath = projectdir()`
183+
* `force = false`
184+
* `storepatch = get(ENV, "DRWATSON_STOREPATCH", true)`: Whether to collect and store the
185+
output of [`gitpatch`](@ref) as well.
186+
181187
## Examples
182188
```julia
183189
julia> d = Dict(:x => 3, :y => 4)
@@ -192,7 +198,10 @@ Dict{Symbol,Any} with 3 entries:
192198
:x => 3
193199
```
194200
"""
195-
function tag!(d::AbstractDict{K,T}; gitpath = projectdir(), storepatch = true, force = false, source = nothing) where {K,T}
201+
function tag!(d::AbstractDict{K,T};
202+
gitpath = projectdir(), force = false, source = nothing,
203+
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", true),
204+
) where {K,T}
196205
@assert (K <: Union{Symbol,String}) "We only know how to tag dictionaries that have keys that are strings or symbols"
197206
c = gitdescribe(gitpath)
198207
c === nothing && return d # gitpath is not a git repo
@@ -210,7 +219,7 @@ function tag!(d::AbstractDict{K,T}; gitpath = projectdir(), storepatch = true, f
210219
# Only include patch info if `storepatch` is true and if we can get the info.
211220
if storepatch
212221
patch = gitpatch(gitpath)
213-
if (patch != nothing) && (patch != "")
222+
if (patch !== nothing) && (patch != "")
214223
d[patchname] = patch
215224
end
216225
end
@@ -330,10 +339,13 @@ additional data fields as strings. Currently endings that can do this are:
330339
```
331340
$(TAGGABLE_FILE_ENDINGS)
332341
```
333-
334-
istaggable(x) = x isa AbstractDictionary
335342
"""
336343
istaggable(file::AbstractString) = any(endswith(file, e) for e TAGGABLE_FILE_ENDINGS)
344+
345+
"""
346+
istaggable(x) = x isa AbstractDict
347+
For non-string input the function just checks if input is dictionary.
348+
"""
337349
istaggable(x) = x isa AbstractDict
338350

339351

0 commit comments

Comments
 (0)