aboutsummaryrefslogtreecommitdiff
path: root/lib/bisect.nim
blob: 53d73f1a184fea59b98e60c3898de64244fc2407 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import future, os, osproc, re, strutils

{.passL: "-lalpm".}

proc vercmp(a: cstring, b: cstring): cint
  {.cdecl, importc: "alpm_pkg_vercmp".}

proc getSourceVersion(relativePath: string): seq[string] =
  let lines = execProcess("/bin/bash", ["-c",
    """source "$1/PKGBUILD" && echo "$epoch" && echo "$pkgver" && echo "$pkgrel"""",
    "bash", relativePath], options = {}).split("\n")
  if lines.len == 4:
    lines[0 .. 2]
  else:
    @[]

proc getSrcInfoVersion(relativePath: string): seq[string] =
  var file: File
  var epoch = ""
  var pkgver = ""
  var pkgrel = ""

  if file.open(relativePath & "/.SRCINFO"):
    try:
      var matches: array[2, string]
      while true:
        let line = file.readLine()
        if line.match(re"[\t\ ]*(\w+)\ =\ (.*)", matches):
          case matches[0]:
            of "epoch":
              epoch = matches[1]
            of "pkgver":
              pkgver = matches[1]
            of "pkgrel":
              pkgrel = matches[1]
            else:
              discard
    except:
      discard
    finally:
      file.close()

  @[epoch, pkgver, pkgrel]

let compareMethod = commandLineParams()[0]
let relativePath = commandLineParams()[1]
let compareVersion = commandLineParams()[2]

let (currentVersion, supported) = if compareMethod == "source":
    (getSourceVersion(relativePath), true)
  elif compareMethod == "srcinfo":
    (getSrcInfoVersion(relativePath), true)
  else:
    (@[], false)

if not supported:
  programResult = 255
elif currentVersion.len != 3:
  programResult = 125
else:
  let epoch = currentVersion[0].strip
  let pkgver = currentVersion[1].strip
  let pkgrel = currentVersion[2].strip

  if pkgver.len == 0 or pkgrel.len == 0:
    programResult = 125
  else:
    let version = if epoch.len > 0:
        epoch & ":" & pkgver & "-" & pkgrel
      else:
        pkgver & "-" & pkgrel

    echo(version)
    if vercmp(compareVersion, version) > 0:
      programResult = 0
    else:
      programResult = 1