From f80c032ff2bb027ef46b90b4619159cbd70e5a7d Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Wed, 29 May 2024 20:32:50 -0400 Subject: [PATCH 1/6] Update changeDirectory * Expand "~" to user's home directory * If no input, go to user's home directory * Return the new working directory as a string --- M2/Macaulay2/d/actors5.d | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/M2/Macaulay2/d/actors5.d b/M2/Macaulay2/d/actors5.d index 30a4343add6..9e70a92a2f2 100644 --- a/M2/Macaulay2/d/actors5.d +++ b/M2/Macaulay2/d/actors5.d @@ -1316,11 +1316,6 @@ readlinkfun(e:Expr):Expr := ( else WrongArgString()); setupfun("readlink",readlinkfun); -changeDirectory(e:Expr):Expr := ( - when e is filename:stringCell do if chdir(filename.v) == -1 then buildErrorPacket(syscallErrorMessage("changing directory")) else nullE - else WrongArgString()); -setupfun("changeDirectory",changeDirectory); - realpathfun(e:Expr):Expr := ( when e is f:stringCell do ( when realpath(expandFileName(f.v)) @@ -1732,6 +1727,21 @@ getcwdfun(e:Expr):Expr := ( -- this has to be a function, because getcwd else WrongNumArgs(0)); setupfun("currentDirectory",getcwdfun); +changeDirectory(dir:string):Expr := ( + if chdir(expandFileName(dir)) == -1 + then buildErrorPacket(syscallErrorMessage("changing directory")) + else getcwdfun(emptySequenceE)); + +changeDirectory(e:Expr):Expr := ( + when e + is filename:stringCell do changeDirectory(filename.v) + is a:Sequence do ( + if length(a) == 0 + then changeDirectory("~") + else WrongArg("a string or ()")) + else WrongArg("a string or ()")); +setupfun("changeDirectory",changeDirectory); + export debuggerHook := nullE; backtraceS := dummySymbol; From eaccf1a7b43facc2c63e02cc018861a4b5e74cca Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Wed, 29 May 2024 20:33:23 -0400 Subject: [PATCH 2/6] Export changeDirectory --- M2/Macaulay2/m2/exports.m2 | 1 + 1 file changed, 1 insertion(+) diff --git a/M2/Macaulay2/m2/exports.m2 b/M2/Macaulay2/m2/exports.m2 index 7c9fa981280..a1116044ef6 100644 --- a/M2/Macaulay2/m2/exports.m2 +++ b/M2/Macaulay2/m2/exports.m2 @@ -535,6 +535,7 @@ export { "centerString", "chainComplex", "changeBase", + "changeDirectory", "char", "characters", "check", From 9e1c52fc487dc284288d5cfc087b96a492e4ca82 Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Wed, 29 May 2024 20:54:00 -0400 Subject: [PATCH 3/6] Document changeDirectory --- M2/Macaulay2/packages/Macaulay2Doc/system.m2 | 33 +++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/M2/Macaulay2/packages/Macaulay2Doc/system.m2 b/M2/Macaulay2/packages/Macaulay2Doc/system.m2 index ea04f4b58ae..a65c32096c8 100644 --- a/M2/Macaulay2/packages/Macaulay2Doc/system.m2 +++ b/M2/Macaulay2/packages/Macaulay2Doc/system.m2 @@ -644,8 +644,39 @@ document { ///, PARA { "If a component of the path to the current directory no longer exist, an error will be signalled." - } + }, + SeeAlso => {changeDirectory} } + +doc /// + Key + changeDirectory + Headline + change the current working directory + Usage + changeDirectory dir + Inputs + dir:String + Outputs + :String -- the new working directory + Description + Text + Change the current working directory to @VAR "dir"@. + Example + dir = temporaryFileName() + makeDirectory dir + changeDirectory dir + currentDirectory() + Text + If @VAR "dir"@ is omitted, then the current working directory + is changed to the user's home directory. + Example + changeDirectory() + currentDirectory() + SeeAlso + currentDirectory +/// + document { Key => exec, Headline => "execute another program", From cacb8264547c31ad4fe552907ea8194913fb5f2e Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Thu, 30 May 2024 00:06:46 -0400 Subject: [PATCH 4/6] Update makeDirectory to return name of newly made directory --- M2/Macaulay2/m2/files.m2 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/M2/Macaulay2/m2/files.m2 b/M2/Macaulay2/m2/files.m2 index c7a0158e41b..6c1f112dfbe 100644 --- a/M2/Macaulay2/m2/files.m2 +++ b/M2/Macaulay2/m2/files.m2 @@ -17,7 +17,8 @@ makeDirectory String := name -> ( -- make the whole path, too name = minimizeFilename name; parts := separate("/", name); if last parts === "" then parts = drop(parts,-1); - makeDir fold((a,b) -> ( makeDir a; a|"/"|b ), parts)) + makeDir fold((a,b) -> ( makeDir a; a|"/"|b ), parts); + name) copyFile = method(Options => new OptionTable from { Verbose => false, UpdateOnly => false }) copyFile(String,String) := opts -> (src,tar) -> ( From 12da9bc4d11803d6c27f71a40e5c04d19c164e61 Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Thu, 30 May 2024 00:09:43 -0400 Subject: [PATCH 5/6] Update makeDirectory docs w/ new return value --- M2/Macaulay2/packages/Macaulay2Doc/system.m2 | 1 + 1 file changed, 1 insertion(+) diff --git a/M2/Macaulay2/packages/Macaulay2Doc/system.m2 b/M2/Macaulay2/packages/Macaulay2Doc/system.m2 index a65c32096c8..1f36df6fc6f 100644 --- a/M2/Macaulay2/packages/Macaulay2Doc/system.m2 +++ b/M2/Macaulay2/packages/Macaulay2Doc/system.m2 @@ -1223,6 +1223,7 @@ document { Headline => "make a directory", Usage => "makeDirectory dir", Inputs => { "dir" => String => "a path to the desired directory" }, + Outputs => { String => "the name of the newly made directory" }, Consequences => { { "the directory is made, with as many new path components as needed" } }, EXAMPLE lines /// dir = temporaryFileName() From bbbd5e18527b08e44ce60943dd1766b831b14f8d Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Wed, 29 May 2024 20:43:55 -0400 Subject: [PATCH 6/6] Add unit tests for directory-related functions --- M2/Macaulay2/tests/normal/files.m2 | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 M2/Macaulay2/tests/normal/files.m2 diff --git a/M2/Macaulay2/tests/normal/files.m2 b/M2/Macaulay2/tests/normal/files.m2 new file mode 100644 index 00000000000..c83fa91e5a8 --- /dev/null +++ b/M2/Macaulay2/tests/normal/files.m2 @@ -0,0 +1,8 @@ +-- TODO: add more unit tests for file operations + +dir = temporaryFileName() +assert(changeDirectory makeDirectory dir == dir | "/") +assert(currentDirectory() == dir | "/") +assert(changeDirectory() == homeDirectory) +assert(currentDirectory() == homeDirectory) +removeDirectory dir