Skip to content

Commit

Permalink
deprecate existsDir; use dirExists instead (#14884)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour authored Jul 3, 2020
1 parent 4f6acf2 commit 6951549
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 39 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 11 additions & 17 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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 =
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/ssl_certs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions lib/system/nimscript.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion testament/categories.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/manyloc/keineschweine/keineschweine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/manyloc/keineschweine/lib/client_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/manyloc/nake/nakefile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/newconfig/tfoo.nims
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 0 additions & 1 deletion tests/test_nimscript.nims
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion tools/nimgrep.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions tools/niminst/niminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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" &
Expand Down
4 changes: 2 additions & 2 deletions tools/nimweb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 6951549

Please sign in to comment.