From 762c7062113ecabd74e679b6d38d51e88a92622c Mon Sep 17 00:00:00 2001
From: Florian Atteneder <florian.atteneder@uni-jena.de>
Date: Mon, 5 Aug 2024 10:51:08 +0000
Subject: [PATCH] dg1d: Avoid generating nested output folders in case
 parameter file comes from inside an output folder
 (https://git.tpi.uni-jena.de/dg/dg1d.jl/-/merge_requests/192)

---
 src/dg1d.jl                      | 14 +---------
 src/main.jl                      | 47 ++++++++++++++++----------------
 src/utils.jl                     |  2 +-
 test/UnitTests/src/test_utils.jl | 15 ++++++----
 4 files changed, 36 insertions(+), 42 deletions(-)

diff --git a/src/dg1d.jl b/src/dg1d.jl
index e0218414..a881e5dc 100644
--- a/src/dg1d.jl
+++ b/src/dg1d.jl
@@ -27,19 +27,7 @@ using WriteVTK
 
 include("sandbox.jl")
 
-export # parameter handling
-       query, splitallext, trim_keys, match_keys, prepend_keys,
-       # type utils
-       concretetypes,
-       # parameter files and file system operations
-       mk_outputdir, dirname_path, flatten,
-       # get_parameter_file, get_parameter_file_index,
-       # select_parameter_files, gather_output_directories, gather_parameter_files,
-       match_properties,
-       # find_duplicated_parameter_files,
-       # run_projects, analyze_projects,
-       TODO, @TODO, @stringify_names,
-       indent
+export TODO, @TODO
 include("utils.jl")
 
 export Cartoon, DoubleCartoon
diff --git a/src/main.jl b/src/main.jl
index 052a2adf..2e2f7a42 100644
--- a/src/main.jl
+++ b/src/main.jl
@@ -20,24 +20,10 @@ end
 
 
 function normalize_parfile(filename)
-  path = if startswith(filename, r"@test/")
-    fname, ext = splitext(replace(filename, "@test/" => ""))
-    path = normpath(joinpath(@__DIR__, "..", "test", "IntegrationTests", "refs", fname, "$fname.toml"))
-    if !isfile(path)
-      error("failed to locate test parfile $path")
-    end
-    path
-  else
-    nr = tryparse(Int64, filename)
-    if !isnothing(nr)
-      return parse_parfile(nr)
-    end
-    filename
+  if !isfile(filename)
+    error("Cannot locate parameter file '$filename'")
   end
-  if !isfile(path)
-    error("Cannot locate parameter file '$path'")
-  end
-  return abspath(path)
+  return filename
 end
 function normalize_parfile(nr::Integer)
   testpars = get_testparfiles()
@@ -46,7 +32,7 @@ function normalize_parfile(nr::Integer)
   end
   parfile = testpars[nr]
   path = normpath(joinpath(@__DIR__, "..", "test", "IntegrationTests", "refs", parfile, "$parfile.toml"))
-  return abspath(path)
+  return path
 end
 
 
@@ -56,17 +42,32 @@ end
 Run program specified by a `parfile` in TOML format.
 """
 function main(parfile)
-  env, project, bdryconds, prms = setup_env(parfile)
-  # backup any previous output dir and setup a fresh one
-  # TODO Rename this to make_outputdir
-  outputdir = mk_outputdir(parfile)
-  cp(parfile, joinpath(outputdir, basename(parfile)))
+
+  parfile = abspath(parfile)
+  dir = dirname(parfile)
+  dummy_parfile = parfile
+  if basename(dir) == splitext(basename(parfile))[1]
+    # we are running parameter file contained in an output folder
+    # so we backup that folder and run the parameter file one directory above
+    dummy_parfile = string(dir, ".toml")
+  end
+  # backup any previous output dir and set up a fresh one
+  outputdir = make_outputdir(dummy_parfile)
+  if dummy_parfile != parfile
+    # we moved the initial parfile with the backup of the output folder
+    cp(joinpath(string(dir,".previous"),string(basename(dir),".toml")),
+       joinpath(outputdir, basename(parfile)))
+  else
+    cp(parfile, joinpath(outputdir, basename(parfile)))
+  end
   cp(joinpath(@__DIR__,"..","Manifest.toml"), joinpath(outputdir,"Manifest.toml"))
   open(joinpath(outputdir, "dirty"), "w") do file
     println(file, "git commit hash: $(githash())")
     println(file, '='^100)
     println(file, gitdirty())
   end
+
+  env, project, bdryconds, prms = setup_env(parfile)
   rhs_fn, timestep_fn = setup_callbacks(env, project, bdryconds, prms, outputdir)
   # TODO Use PrettyTables.jl for summary
   # TODO Dump versioninfo() to some file
diff --git a/src/utils.jl b/src/utils.jl
index c7c9f66d..ddacc763 100644
--- a/src/utils.jl
+++ b/src/utils.jl
@@ -181,7 +181,7 @@ Generate an output directory in root directory of `path`. Directory is only crea
 If a folder with the same name is already present, then it is backed up by moving it
 and appending .previous to its name.
 """
-function mk_outputdir(path)
+function make_outputdir(path)
   path = abspath(path)
 
   # analyze extensions
diff --git a/test/UnitTests/src/test_utils.jl b/test/UnitTests/src/test_utils.jl
index e7b3c75c..15a18fe4 100644
--- a/test/UnitTests/src/test_utils.jl
+++ b/test/UnitTests/src/test_utils.jl
@@ -1,3 +1,8 @@
+import dg1d: make_outputdir, flatten, dirname_path, query,
+             splitallext, trim_keys, match_keys, prepend_keys,
+             concretetypes, match_properties, indent
+
+
 @testset "Utilities" begin
 
 
@@ -256,7 +261,7 @@ end
   ## flatten dicts
   nested_dict = Dict("d1"=>Dict("a"=>1, "b"=>2),
                      "d2"=>Dict("c"=>3, "d"=>4))
-  flattened_dict = dg1d.flatten(nested_dict)
+  flattened_dict = flatten(nested_dict)
   expected = Dict( "d1.a" => 1, "d1.b" => 2, "d2.c" => 3, "d2.d" => 4)
   @test flattened_dict == expected
 
@@ -332,21 +337,21 @@ end
     dummyparfiles = [ dummyparfile1, dummyparfile2 ]
 
     for dp in dummyparfiles
-      path = mk_outputdir(dp)
+      path = dg1d.make_outputdir(dp)
       @test isdir(path)
       # call to already existing should generate a backup at $path.previous
-      path = mk_outputdir(dp)
+      path = dg1d.make_outputdir(dp)
       path_previous = "$(path).previous"
       @test isdir(path)
       @test isdir(path_previous)
     end
 
     nofilethere = joinpath(dir, "nofilethere.toml")
-    @test_throws ErrorException mk_outputdir(nofilethere)
+    @test_throws ErrorException dg1d.make_outputdir(nofilethere)
 
     wrongextensionfile = joinpath(dir, "wrongextension.toml.blabla")
     touch(wrongextensionfile)
-    @test_throws ErrorException mk_outputdir(wrongextensionfile)
+    @test_throws ErrorException dg1d.make_outputdir(wrongextensionfile)
 
 
     #### mock a project directory
-- 
GitLab