From 695154970d839add2fbbe9754e9e638511120729 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Fri, 3 Jul 2020 14:04:23 -0700 Subject: [PATCH] deprecate existsDir; use dirExists instead (#14884) --- changelog.md | 1 + compiler/docgen.nim | 2 +- lib/pure/os.nim | 28 ++++++++----------- lib/pure/ssl_certs.nim | 2 +- lib/system/nimscript.nim | 13 ++++----- testament/categories.nim | 2 +- tests/manyloc/keineschweine/keineschweine.nim | 2 +- .../keineschweine/lib/client_helpers.nim | 2 +- tests/manyloc/nake/nakefile.nim | 2 +- tests/newconfig/tfoo.nims | 2 +- tests/test_nimscript.nims | 1 - tools/nimgrep.nim | 2 +- tools/niminst/niminst.nim | 8 +++--- tools/nimweb.nim | 4 +-- 14 files changed, 32 insertions(+), 39 deletions(-) diff --git a/changelog.md b/changelog.md index 8446f9467..abe69a086 100644 --- a/changelog.md +++ b/changelog.md @@ -121,6 +121,7 @@ - add `macros.extractDocCommentsAndRunnables` helper - `strformat.fmt` and `strformat.&` support `= specifier`. `fmt"{expr=}"` now expands to `fmt"expr={expr}"`. +- deprecations: `os.existsDir` => `dirExists`, `os.existsFile` => `fileExists` - Add `jsre` module, [Regular Expressions for the JavaScript target.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index f501dcc8d..cb8c77e87 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -82,7 +82,7 @@ proc presentationPath*(conf: ConfigRef, file: AbsoluteFile, isTitle = false): Re # we're (currently) requiring `isAbsolute` to avoid confusion when passing # a relative path (would it be relative wrt $PWD or to projectfile) conf.globalAssert conf.docRoot.isAbsolute, arg=conf.docRoot - conf.globalAssert conf.docRoot.existsDir, arg=conf.docRoot + conf.globalAssert conf.docRoot.dirExists, arg=conf.docRoot # needed because `canonicalizePath` called on `file` result = file.relativeTo conf.docRoot.expandFilename.AbsoluteDir else: diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 5d73544ce..8bbceddeb 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1117,13 +1117,7 @@ proc fileExists*(filename: string): bool {.rtl, extern: "nos$1", var res: Stat return stat(filename, res) >= 0'i32 and S_ISREG(res.st_mode) -when not defined(nimscript): - when not defined(js): # `noNimJs` doesn't work with templates, this should improve. - template existsFile*(args: varargs[untyped]): untyped {.deprecated: "use fileExists".} = - fileExists(args) - # {.deprecated: [existsFile: fileExists].} # pending bug #14819; this would avoid above mentioned issue - -proc existsDir*(dir: string): bool {.rtl, extern: "nos$1", tags: [ReadDirEffect], +proc dirExists*(dir: string): bool {.rtl, extern: "nos$1", tags: [ReadDirEffect], noNimJs.} = ## Returns true if the directory `dir` exists. If `dir` is a file, false ## is returned. Follows symlinks. @@ -1150,7 +1144,7 @@ proc symlinkExists*(link: string): bool {.rtl, extern: "nos$1", ## ## See also: ## * `fileExists proc <#fileExists,string>`_ - ## * `existsDir proc <#existsDir,string>`_ + ## * `dirExists proc <#dirExists,string>`_ when defined(windows): when useWinUnicode: wrapUnary(a, getFileAttributesW, link) @@ -1163,13 +1157,13 @@ proc symlinkExists*(link: string): bool {.rtl, extern: "nos$1", return lstat(link, res) >= 0'i32 and S_ISLNK(res.st_mode) -proc dirExists*(dir: string): bool {.inline, noNimJs.} = - ## Alias for `existsDir proc <#existsDir,string>`_. - ## - ## See also: - ## * `fileExists proc <#fileExists,string>`_ - ## * `symlinkExists proc <#symlinkExists,string>`_ - existsDir(dir) +when not defined(nimscript): + when not defined(js): # `noNimJs` doesn't work with templates, this should improve. + template existsFile*(args: varargs[untyped]): untyped {.deprecated: "use fileExists".} = + fileExists(args) + template existsDir*(args: varargs[untyped]): untyped {.deprecated: "use dirExists".} = + dirExists(args) + # {.deprecated: [existsFile: fileExists].} # pending bug #14819; this would avoid above mentioned issue when not defined(windows) and not weirdTarget: proc checkSymlink(path: string): bool = @@ -2026,7 +2020,7 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", # way of retrieving the true filename for x in walkFiles(result): result = x - if not fileExists(result) and not existsDir(result): + if not fileExists(result) and not dirExists(result): # consider using: `raiseOSError(osLastError(), result)` raise newException(OSError, "file '" & result & "' does not exist") else: @@ -2323,7 +2317,7 @@ proc existsOrCreateDir*(dir: string): bool {.rtl, extern: "nos$1", result = not rawCreateDir(dir) if result: # path already exists - need to check that it is indeed a directory - if not existsDir(dir): + if not dirExists(dir): raise newException(IOError, "Failed to create '" & dir & "'") proc createDir*(dir: string) {.rtl, extern: "nos$1", diff --git a/lib/pure/ssl_certs.nim b/lib/pure/ssl_certs.nim index df845decc..d04244307 100644 --- a/lib/pure/ssl_certs.nim +++ b/lib/pure/ssl_certs.nim @@ -81,7 +81,7 @@ iterator scanSSLCertificates*(useEnvVars = false): string = if p.endsWith(".pem") or p.endsWith(".crt"): if fileExists(p): yield p - elif existsDir(p): + elif dirExists(p): for fn in joinPath(p, "*").walkFiles(): yield fn else: diff --git a/lib/system/nimscript.nim b/lib/system/nimscript.nim index 75a4b7a6e..bf298e0d4 100644 --- a/lib/system/nimscript.nim +++ b/lib/system/nimscript.nim @@ -131,18 +131,17 @@ proc fileExists*(filename: string): bool {.tags: [ReadIOEffect].} = ## Checks if the file exists. builtin -template existsFile*(args: varargs[untyped]): untyped {.deprecated: "use fileExists".} = - # xxx: warning won't be shown for nimsscript because of current logic handling - # `foreignPackageNotes` - fileExists(args) - proc dirExists*(dir: string): bool {. tags: [ReadIOEffect].} = ## Checks if the directory `dir` exists. builtin -proc existsDir*(dir: string): bool = - ## An alias for ``dirExists``. +template existsFile*(args: varargs[untyped]): untyped {.deprecated: "use fileExists".} = + # xxx: warning won't be shown for nimsscript because of current logic handling + # `foreignPackageNotes` + fileExists(args) + +template existsDir*(args: varargs[untyped]): untyped {.deprecated: "use dirExists".} = dirExists(dir) proc selfExe*(): string = diff --git a/testament/categories.nim b/testament/categories.nim index 6abb3faa9..760c3c5be 100644 --- a/testament/categories.nim +++ b/testament/categories.nim @@ -507,7 +507,7 @@ proc testNimblePackages(r: var TResults; cat: Category; packageFilter: string, p inc r.total var test = makeSupTest(url, "", cat) let buildPath = packagesDir / name - if not existsDir(buildPath): + if not dirExists(buildPath): if useHead: let (installCmdLine, installOutput, installStatus) = execCmdEx2("git", ["clone", url, buildPath]) if installStatus != QuitSuccess: diff --git a/tests/manyloc/keineschweine/keineschweine.nim b/tests/manyloc/keineschweine/keineschweine.nim index 6d8e48f24..cde31bd18 100644 --- a/tests/manyloc/keineschweine/keineschweine.nim +++ b/tests/manyloc/keineschweine/keineschweine.nim @@ -446,7 +446,7 @@ ingameClient.registerHandler(KeyF12, down, proc() = toggleSpec()) ingameClient.registerHandler(KeyF11, down, toggleShipSelect) ingameClient.registerHandler(MouseLeft, down, handleLClick) when defined(recordMode): - if not existsDir("data/snapshots"): + if not dirExists("data/snapshots"): createDir("data/snapshots") ingameClient.registerHandler(keynum9, down, proc() = if not isRecording: startRecording() diff --git a/tests/manyloc/keineschweine/lib/client_helpers.nim b/tests/manyloc/keineschweine/lib/client_helpers.nim index 8c45feb62..b535225dc 100644 --- a/tests/manyloc/keineschweine/lib/client_helpers.nim +++ b/tests/manyloc/keineschweine/lib/client_helpers.nim @@ -103,7 +103,7 @@ proc saveCurrentFile() = let path = expandPath(currentFileTransfer.assetType, currentFileTransfer.fileName) parent = parentDir(path) - if not existsDir(parent): + if not dirExists(parent): createDir(parent) echo("Created dir") writeFile path, currentFIleTransfer.data diff --git a/tests/manyloc/nake/nakefile.nim b/tests/manyloc/nake/nakefile.nim index 1da92b76b..95058c277 100644 --- a/tests/manyloc/nake/nakefile.nim +++ b/tests/manyloc/nake/nakefile.nim @@ -77,7 +77,7 @@ task "testskel", "create skeleton test dir for testing": task "clean", "cleanup generated files": var dirs = @["nimcache", "server"/"nimcache"] dirs.apply(proc(x: var string) = - if existsDir(x): removeDir(x)) + if dirExists(x): removeDir(x)) task "download", "download game assets": var diff --git a/tests/newconfig/tfoo.nims b/tests/newconfig/tfoo.nims index fbff38f01..a53e777d4 100644 --- a/tests/newconfig/tfoo.nims +++ b/tests/newconfig/tfoo.nims @@ -73,7 +73,7 @@ assert fileExists("tests/newconfig/tfoo.nims") == true assert dirExists("tests") == true assert fileExists("tests/newconfig/tfoo.nims") == true -assert existsDir("tests") == true +assert dirExists("tests") == true discard selfExe() diff --git a/tests/test_nimscript.nims b/tests/test_nimscript.nims index 1a6f26137..d9f477366 100644 --- a/tests/test_nimscript.nims +++ b/tests/test_nimscript.nims @@ -75,7 +75,6 @@ block: doAssert "./foo//./bar/".normalizedPath == "foo/bar".unixToNativePath block: # #14142 - discard existsDir("/usr") discard dirExists("/usr") discard fileExists("/usr/foo") discard findExe("nim") diff --git a/tools/nimgrep.nim b/tools/nimgrep.nim index e4f0e91aa..138f1680c 100644 --- a/tools/nimgrep.nim +++ b/tools/nimgrep.nim @@ -495,7 +495,7 @@ proc styleInsensitive(s: string): string = else: addx() proc walker(pattern; dir: string; counter: var int, errors: var int) = - if existsDir(dir): + if dirExists(dir): for kind, path in walkDir(dir): case kind of pcFile: diff --git a/tools/niminst/niminst.nim b/tools/niminst/niminst.nim index 88d16f48a..a5fc388cd 100644 --- a/tools/niminst/niminst.nim +++ b/tools/niminst/niminst.nim @@ -251,12 +251,12 @@ proc walkDirRecursively(s: var seq[string], root, explicit: string, proc addFiles(s: var seq[string], patterns: seq[string]) = for p in items(patterns): - if existsDir(p): + if dirExists(p): walkDirRecursively(s, p, p, false) else: var i = 0 for f in walkPattern(p): - if existsDir(f): + if dirExists(f): walkDirRecursively(s, f, p, false) elif not ignoreFile(f, p, false): add(s, unixToNativePath(f)) @@ -518,7 +518,7 @@ template gatherFiles(fun, libpath, outDir) = proc srcdist(c: var ConfigData) = let cCodeDir = getOutputDir(c) / "c_code" - if not existsDir(cCodeDir): createDir(cCodeDir) + if not dirExists(cCodeDir): createDir(cCodeDir) gatherFiles(copyFile, c.libpath, cCodeDir) var winIndex = -1 var intel32Index = -1 @@ -532,7 +532,7 @@ proc srcdist(c: var ConfigData) = if cpuname.cmpIgnoreStyle("i386") == 0: intel32Index = cpuA elif cpuname.cmpIgnoreStyle("amd64") == 0: intel64Index = cpuA var dir = getOutputDir(c) / buildDir(osA, cpuA) - if existsDir(dir): removeDir(dir) + if dirExists(dir): removeDir(dir) createDir(dir) var cmd = ("nim compile -f --symbolfiles:off --compileonly " & "--gen_mapping --cc:gcc --skipUserCfg" & diff --git a/tools/nimweb.nim b/tools/nimweb.nim index 0a853a54d..10319be87 100644 --- a/tools/nimweb.nim +++ b/tools/nimweb.nim @@ -201,7 +201,7 @@ proc addFiles(s: var seq[string], dir, ext: string, patterns: seq[string]) = for p in items(patterns): if fileExists(dir / addFileExt(p, ext)): s.add(dir / addFileExt(p, ext)) - if existsDir(dir / p): + if dirExists(dir / p): walkDirRecursively(s, dir / p, ext) proc parseIniFile(c: var TConfigData) = @@ -483,7 +483,7 @@ proc buildPage(c: var TConfigData, file, title, rss: string, assetDir = "") = quit("[Error] cannot open: " & temp) var f: File var outfile = c.webUploadOutput / "$#.html" % file - if not existsDir(outfile.splitFile.dir): + if not dirExists(outfile.splitFile.dir): createDir(outfile.splitFile.dir) if open(f, outfile, fmWrite): writeLine(f, generateHTMLPage(c, file, title, content, rss, assetDir))