osutils.nim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ## OS utilities like 'withDir'.
  2. ## (c) 2021 Andreas Rumpf
  3. import os, strutils, osproc
  4. proc isUrl*(x: string): bool =
  5. x.startsWith("git://") or x.startsWith("https://") or x.startsWith("http://")
  6. proc cloneUrl*(url, dest: string; cloneUsingHttps: bool): string =
  7. ## Returns an error message on error or else "".
  8. result = ""
  9. var modUrl =
  10. if url.startsWith("git://") and cloneUsingHttps:
  11. "https://" & url[6 .. ^1]
  12. else: url
  13. # github + https + trailing url slash causes a
  14. # checkout/ls-remote to fail with Repository not found
  15. var isGithub = false
  16. if modUrl.contains("github.com") and modUrl.endsWith("/"):
  17. modUrl = modUrl[0 .. ^2]
  18. isGithub = true
  19. let (_, exitCode) = execCmdEx("git ls-remote --quiet --tags " & modUrl)
  20. var xcode = exitCode
  21. if isGithub and exitCode != QuitSuccess:
  22. # retry multiple times to avoid annoying github timeouts:
  23. for i in 0..4:
  24. os.sleep(4000)
  25. xcode = execCmdEx("git ls-remote --quiet --tags " & modUrl)[1]
  26. if xcode == QuitSuccess: break
  27. if xcode == QuitSuccess:
  28. # retry multiple times to avoid annoying github timeouts:
  29. let cmd = "git clone " & modUrl & " " & dest
  30. for i in 0..4:
  31. if execShellCmd(cmd) == 0: return ""
  32. os.sleep(4000)
  33. result = "exernal program failed: " & cmd
  34. elif not isGithub:
  35. let (_, exitCode) = execCmdEx("hg identify " & modUrl)
  36. if exitCode == QuitSuccess:
  37. let cmd = "hg clone " & modUrl & " " & dest
  38. for i in 0..4:
  39. if execShellCmd(cmd) == 0: return ""
  40. os.sleep(4000)
  41. result = "exernal program failed: " & cmd
  42. else:
  43. result = "Unable to identify url: " & modUrl
  44. else:
  45. result = "Unable to identify url: " & modUrl