Skip to content

Commit

Permalink
Add conditional imports for Nim V2 and add CatchableError to bare exc…
Browse files Browse the repository at this point in the history
…epts
  • Loading branch information
zqqw committed Jan 23, 2023
1 parent fb40c84 commit 72654de
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ NIM_OPTIONS = \
--putenv:'PROG_LOCALSTATEDIR'="${LOCALSTATEDIR}" \
--putenv:'PROG_SYSCONFDIR'="${SYSCONFDIR}" \
-d:'${NIM_TARGET}' \
# -d:nimPreviewSlimSystem \
--opt:'${NIM_OPTIMIZE}' \
--hint'[Conf]':off \
--hint'[Processing]':off \
Expand Down
3 changes: 2 additions & 1 deletion lib/install.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import
os, posix, sequtils, strutils, sugar
when not declared(system.stdout): import std/syncio

proc splitCommands(params: seq[string], index: int, res: seq[seq[string]]): seq[seq[string]] =
if index < params.len:
Expand Down Expand Up @@ -68,7 +69,7 @@ proc handleInstall*(params: seq[string]): int =
let dest = destination & "/" & name
copyFile(pkg.file, dest)
discard chown(cstring(dest), (Uid) uid, (Gid) gid)
except:
except CatchableError:
discard

let asexplicit = install.filter(p => p.mode == "explicit").map(p => p.name)
Expand Down
3 changes: 2 additions & 1 deletion src/args.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import
options, os, posix, sequtils, sets, strutils, sugar,
utils
when not declared(system.stdout): import std/syncio

type
ArgumentType* {.pure.} = enum
Expand All @@ -26,7 +27,7 @@ iterator readLines(): string =
try:
while true:
yield readLine(stdin)
except:
except CatchableError:
discard

iterator splitSingle(valueFull: string, optionsWithParameter: HashSet[OptionKey],
Expand Down
2 changes: 1 addition & 1 deletion src/aur.nim
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ proc getRpcPackageInfosInternal(pkgs: seq[string], repo: string, useTimeout: boo
(@[], some(getCurrentException().msg))
except JsonParsingError:
(@[], some(tr"failed to parse server response"))
except:
except CatchableError:
(@[], some(getCurrentException().msg))

proc getRpcPackageInfos*(pkgs: seq[string], repo: string, useTimeout: bool, configColor: bool):
Expand Down
5 changes: 3 additions & 2 deletions src/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import
options, os, osproc, posix, sequtils, sets, strutils, sugar, tables,
args, config, format, lists, package, pacman, utils,
"wrapper/alpm"
when not declared(system.stdout): import std/syncio

type
CacheKind* {.pure.} = enum
Expand Down Expand Up @@ -331,7 +332,7 @@ proc createDirRecursive(dir: string, chownUser: Option[User], mkTmp: bool): bool
if chownUser.isSome and (not exists or index == segments.len - 1):
discard chown(cstring(path), (Uid) chownUser.unsafeGet.uid, (Gid) chownUser.unsafeGet.gid)
createDirIndex(index + 1)
except:
except CatchableError:
false
else:
true
Expand Down Expand Up @@ -882,7 +883,7 @@ proc cloneAurReposWithPackageInfos*(config: Config, rpcInfos: seq[RpcPackageInfo
else:
let srcInfos = try:
readFile(repoPath & "/.SRCINFO")
except:
except CatchableError:
""

let addPkgInfos = parseSrcInfo(config.aurRepo, srcInfos, config.common.arch,
Expand Down
1 change: 1 addition & 0 deletions src/config.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import
std/[options, posix, re, sequtils, sets, strutils, sugar, tables],
utils
when not declared(system.stdout): import std/syncio

type
ColorMode* {.pure.} = enum
Expand Down
5 changes: 3 additions & 2 deletions src/feature/syncinstall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import
"../args", "../aur", "../config", "../common", "../format", "../lists", "../package",
"../pacman", "../utils",
"../wrapper/alpm"
when not declared(system.stdout): import std/syncio

type
Installed = tuple[
Expand Down Expand Up @@ -444,7 +445,7 @@ proc buildLoop(config: Config, pkgInfos: seq[PackageInfo], skipDeps: bool,
finally:
file.close()
true
except:
except CatchableError:
discard unlink(cstring(workConfFile))
false

Expand Down Expand Up @@ -625,7 +626,7 @@ proc installGroupFromSources(config: Config, commonArgs: seq[Argument],
if clear or not (pair.file in installFiles):
try:
removeFile(pair.file)
except:
except CatchableError:
discard

if not clear:
Expand Down
1 change: 1 addition & 0 deletions src/format.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import
std/[macros, options, posix, sequtils, strutils, sugar, times, unicode, terminal],
utils
when not declared(system.stdout): import std/syncio

type
PackageLineFormat* = tuple[
Expand Down
1 change: 1 addition & 0 deletions src/main.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import
options, os, posix, re, sequtils, strutils, sugar,
args, config, format, pacman, utils
when not declared(system.stdout): import std/syncio

import
"feature/localquery",
Expand Down
3 changes: 2 additions & 1 deletion src/package.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import
options, os, re, sequtils, sets, strutils, sugar, tables, utils,
"wrapper/alpm"
when not declared(system.stdout): import std/syncio

type
ConstraintOperation* {.pure.} = enum
Expand Down Expand Up @@ -130,7 +131,7 @@ static:
try:
if m2[x2] != x1:
raise newException(CatchableError, "")
except:
except CatchableError:
raise newException(CatchableError, "Invalid url <> bareName links")

testBareNamesAndUrls(bareNameToUrl, urlToBareName)
Expand Down
3 changes: 2 additions & 1 deletion src/pacman.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import
macros, options, posix, sequtils, sets, strutils, sugar, tables,
args, config, utils
when not declared(system.stdout): import std/syncio

type
OpGroup* {.pure.} = enum
Expand Down Expand Up @@ -438,7 +439,7 @@ proc obtainPacmanConfig*(args: seq[Argument]): PacmanConfig =
let line = file.readLine()
if line.len > 10 and line[0 .. 9] == "keyserver ":
pgpKeyserver = some(line[9 .. ^1].strip)
except:
except CatchableError:
discard
finally:
file.close()
Expand Down
15 changes: 9 additions & 6 deletions src/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,12 @@ let initialUser* = try:
else:
none(string)

let uid = uidString.get.parseInt
if uid == 0 or currentUser.uid != 0: none(User) else: some(getUser(uid))
except:
if uidString.isSome:
let uid = uidString.get.parseInt
if uid == 0 or currentUser.uid != 0: none(User) else: some(getUser(uid))
else:
none(User)
except CatchableError:
none(User)

proc canDropPrivileges*(): bool =
Expand Down Expand Up @@ -303,7 +306,7 @@ proc execRedirect*(args: varargs[string]): int =
argSeq.delete(0)
try:
p = startProcess(command = args[0], args = argSeq, env = envs, options = {poStdErrToStdOut, poUsePath})
except:
except CatchableError:
echo "Error: " & getCurrentExceptionMsg()
return -1
var outp = outputStream(p)
Expand Down Expand Up @@ -421,7 +424,7 @@ proc toString*[T](arr: array[T, char], length: Option[int]): string =
proc removeDirQuiet*(s: string) =
try:
removeDir(s)
except:
except CatchableError:
discard

proc removeTmpDirQuiet*(s: string) =
Expand All @@ -432,7 +435,7 @@ proc removeTmpDirQuiet*(s: string) =
path = s
try:
removeDir(path)
except:
except CatchableError:
discard

const bashSpecialCharacters = " \t\"'`()[]{}#&|;!\\*~<>?"
Expand Down

0 comments on commit 72654de

Please sign in to comment.