Skip to content

Commit

Permalink
v0.11.0: new auto_change_directory
Browse files Browse the repository at this point in the history
  • Loading branch information
cihga39871 committed Aug 27, 2024
1 parent 33f7ce3 commit 7012253
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Pipelines"
uuid = "ef544631-5c6f-4e9b-994c-12e7a4cd724c"
authors = ["Jiacheng Chuan <jiacheng_chuan@outlook.com>"]
version = "0.10.6"
version = "0.11.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
1 change: 1 addition & 0 deletions docs/src/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ isok

## Redirection
```@docs
Pipelines.auto_change_directory
redirect_to_files
restore_stdout
restore_stderr
Expand Down
4 changes: 4 additions & 0 deletions docs/src/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## v0.11.0

- Feat/**Breaking**: new method `auto_change_directory(b::Bool)`. It is necessary because changing directory is not thread-safe in Julia. It was set to `false` in v0.11.0. To make your code compatible with previous version, you can add `Pipelines.auto_change_directory(true)` at the beginning of your code, or use full paths through out your code (recommended).

## v0.10.6

- Fix: `run(::Program)`: wrap `pwd()` in try-catch block in case the dir no longer exists. It happens because workding dir is not thread safe in Julia. If other program delete the directory, it will fail.
Expand Down
52 changes: 37 additions & 15 deletions src/Program.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,39 @@ function Base.run(p::Program, inputs; kwarg...)
run(p; inputs=inputs, kwarg...)
end

AUTO_CHANGE_DIRECTORY::Bool = false

"""
auto_change_directory(b::Bool)
When running a `Program`, whether automatically change to the directory.
Default is false since Pipelines v0.11.
Changing directory is not thread-safe in Julia.
Cannot set it in versions <= v0.10.6.
"""
function auto_change_directory(b::Bool)
global AUTO_CHANGE_DIRECTORY = b
end

function Base.run(p::Program;
dir::AbstractString = "", retry::Int = 0,
stdout = nothing, stderr = nothing, stdlog = stderr, append::Bool = false, _do_parse_program_args::Bool = true, kwarg...
)
if dir != ""
dir_backup = try
# in case the dir no longer exists. happens because workding dir is not thread safe in Julia. If other program delete the directory, it will fail.
pwd()
catch
""
)

if AUTO_CHANGE_DIRECTORY
if dir != ""
dir_backup = try
# in case the dir no longer exists. happens because workding dir is not thread safe in Julia. If other program delete the directory, it will fail.
pwd()
catch
""
end
dir = abspath(dir)
cd(dir) # go to working directory
end
dir = abspath(dir)
cd(dir) # go to working directory
end

if _do_parse_program_args
Expand All @@ -288,11 +308,13 @@ function Base.run(p::Program;
n_try += 1
end

if dir != ""
try
# in case the dir no longer exists. happens because workding dir is not thread safe in Julia. If other program delete the directory, it will fail.
cd(dir_backup)
catch
if AUTO_CHANGE_DIRECTORY
if dir != ""
try
# in case the dir no longer exists. happens because workding dir is not thread safe in Julia. If other program delete the directory, it will fail.
cd(dir_backup)
catch
end
end
end
res
Expand Down Expand Up @@ -322,7 +344,7 @@ Return `(success::Bool, outputs::Dict{String})`
- elements in `p.arg_inputs` and `p.arg_outputs`. They will merge to positional arguments `inputs` and `outputs`.
- `dir::AbstractString = ""`: working directory to run the program and store `run_id_file`.
- `dir::AbstractString = ""`: directory to store `run_id_file`. If set `Pipelines.auto_change_directory(true)`, Program will change to this directory before running. However, changing directory is not thread safe, so it is not recommended.
- `check_dependencies::Bool = true`: check dependencies for `p` (`p.cmd_dependencies`).
Expand Down

0 comments on commit 7012253

Please sign in to comment.