atlas.nim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #
  2. # Atlas Package Cloner
  3. # (c) Copyright 2021 Andreas Rumpf
  4. #
  5. # See the file "copying.txt", included in this
  6. # distribution, for details about the copyright.
  7. #
  8. ## Simple tool to automate frequent workflows: Can "clone"
  9. ## a Nimble dependency and its dependencies recursively.
  10. import std/[parseopt, strutils, os, osproc, unicode, tables, sets, json, jsonutils]
  11. import parse_requires, osutils, packagesjson
  12. const
  13. Version = "0.2"
  14. Usage = "atlas - Nim Package Cloner Version " & Version & """
  15. (c) 2021 Andreas Rumpf
  16. Usage:
  17. atlas [options] [command] [arguments]
  18. Command:
  19. clone url|pkgname clone a package and all of its dependencies
  20. install proj.nimble use the .nimble file to setup the project's dependencies
  21. search keyw keywB... search for package that contains the given keywords
  22. extract file.nimble extract the requirements and custom commands from
  23. the given Nimble file
  24. Options:
  25. --keepCommits do not perform any `git checkouts`
  26. --cfgHere also create/maintain a nim.cfg in the current
  27. working directory
  28. --workspace=DIR use DIR as workspace
  29. --version show the version
  30. --help show this help
  31. """
  32. proc writeHelp() =
  33. stdout.write(Usage)
  34. stdout.flushFile()
  35. quit(0)
  36. proc writeVersion() =
  37. stdout.write(Version & "\n")
  38. stdout.flushFile()
  39. quit(0)
  40. const
  41. MockupRun = defined(atlasTests)
  42. TestsDir = "tools/atlas/tests"
  43. type
  44. PackageName = distinct string
  45. DepRelation = enum
  46. normal, strictlyLess, strictlyGreater
  47. Dependency = object
  48. name: PackageName
  49. url, commit: string
  50. rel: DepRelation # "requires x < 1.0" is silly, but Nimble allows it so we have too.
  51. AtlasContext = object
  52. projectDir, workspace: string
  53. hasPackageList: bool
  54. keepCommits: bool
  55. cfgHere: bool
  56. p: Table[string, string] # name -> url mapping
  57. processed: HashSet[string] # the key is (url / commit)
  58. errors: int
  59. when MockupRun:
  60. currentDir: string
  61. step: int
  62. mockupSuccess: bool
  63. const
  64. InvalidCommit = "<invalid commit>"
  65. ProduceTest = false
  66. type
  67. Command = enum
  68. GitDiff = "git diff",
  69. GitTags = "git show-ref --tags",
  70. GitRevParse = "git rev-parse",
  71. GitCheckout = "git checkout",
  72. GitPull = "git pull",
  73. GitCurrentCommit = "git log -n 1 --format=%H"
  74. GitMergeBase = "git merge-base"
  75. include testdata
  76. proc exec(c: var AtlasContext; cmd: Command; args: openArray[string]): (string, int) =
  77. when MockupRun:
  78. assert TestLog[c.step].cmd == cmd, $(TestLog[c.step].cmd, cmd)
  79. case cmd
  80. of GitDiff, GitTags, GitRevParse, GitPull, GitCurrentCommit:
  81. result = (TestLog[c.step].output, TestLog[c.step].exitCode)
  82. of GitCheckout:
  83. assert args[0] == TestLog[c.step].output
  84. of GitMergeBase:
  85. let tmp = TestLog[c.step].output.splitLines()
  86. assert tmp.len == 4, $tmp.len
  87. assert tmp[0] == args[0]
  88. assert tmp[1] == args[1]
  89. assert tmp[3] == ""
  90. result[0] = tmp[2]
  91. result[1] = TestLog[c.step].exitCode
  92. inc c.step
  93. else:
  94. var cmdLine = $cmd
  95. for i in 0..<args.len:
  96. cmdLine.add ' '
  97. cmdLine.add quoteShell(args[i])
  98. result = osproc.execCmdEx(cmdLine)
  99. when ProduceTest:
  100. echo "cmd ", cmd, " args ", args, " --> ", result
  101. proc cloneUrl(c: var AtlasContext; url, dest: string; cloneUsingHttps: bool): string =
  102. when MockupRun:
  103. result = ""
  104. else:
  105. result = osutils.cloneUrl(url, dest, cloneUsingHttps)
  106. when ProduceTest:
  107. echo "cloned ", url, " into ", dest
  108. template withDir*(c: var AtlasContext; dir: string; body: untyped) =
  109. when MockupRun:
  110. c.currentDir = dir
  111. body
  112. else:
  113. let oldDir = getCurrentDir()
  114. try:
  115. when ProduceTest:
  116. echo "Current directory is now ", dir
  117. setCurrentDir(dir)
  118. body
  119. finally:
  120. setCurrentDir(oldDir)
  121. proc extractRequiresInfo(c: var AtlasContext; nimbleFile: string): NimbleFileInfo =
  122. result = extractRequiresInfo(nimbleFile)
  123. when ProduceTest:
  124. echo "nimble ", nimbleFile, " info ", result
  125. proc toDepRelation(s: string): DepRelation =
  126. case s
  127. of "<": strictlyLess
  128. of ">": strictlyGreater
  129. else: normal
  130. proc isCleanGit(c: var AtlasContext; dir: string): string =
  131. result = ""
  132. let (outp, status) = exec(c, GitDiff, [])
  133. if outp.len != 0:
  134. result = "'git diff' not empty"
  135. elif status != 0:
  136. result = "'git diff' returned non-zero"
  137. proc message(c: var AtlasContext; category: string; p: PackageName; args: varargs[string]) =
  138. var msg = category & "(" & p.string & ")"
  139. for a in args:
  140. msg.add ' '
  141. msg.add a
  142. stdout.writeLine msg
  143. inc c.errors
  144. proc warn(c: var AtlasContext; p: PackageName; args: varargs[string]) =
  145. message(c, "[Warning] ", p, args)
  146. proc error(c: var AtlasContext; p: PackageName; args: varargs[string]) =
  147. message(c, "[Error] ", p, args)
  148. proc sameVersionAs(tag, ver: string): bool =
  149. const VersionChars = {'0'..'9', '.'}
  150. proc safeCharAt(s: string; i: int): char {.inline.} =
  151. if i >= 0 and i < s.len: s[i] else: '\0'
  152. let idx = find(tag, ver)
  153. if idx >= 0:
  154. # we found the version as a substring inside the `tag`. But we
  155. # need to watch out the the boundaries are not part of a
  156. # larger/different version number:
  157. result = safeCharAt(tag, idx-1) notin VersionChars and
  158. safeCharAt(tag, idx+ver.len) notin VersionChars
  159. proc versionToCommit(c: var AtlasContext; d: Dependency): string =
  160. let (outp, status) = exec(c, GitTags, [])
  161. if status == 0:
  162. var useNextOne = false
  163. for line in splitLines(outp):
  164. let commitsAndTags = strutils.splitWhitespace(line)
  165. if commitsAndTags.len == 2:
  166. case d.rel
  167. of normal:
  168. if commitsAndTags[1].sameVersionAs(d.commit):
  169. return commitsAndTags[0]
  170. of strictlyLess:
  171. if d.commit == InvalidCommit or not commitsAndTags[1].sameVersionAs(d.commit):
  172. return commitsAndTags[0]
  173. of strictlyGreater:
  174. if commitsAndTags[1].sameVersionAs(d.commit):
  175. useNextOne = true
  176. elif useNextOne:
  177. return commitsAndTags[0]
  178. return ""
  179. proc shortToCommit(c: var AtlasContext; short: string): string =
  180. let (cc, status) = exec(c, GitRevParse, [short])
  181. result = if status == 0: strutils.strip(cc) else: ""
  182. proc checkoutGitCommit(c: var AtlasContext; p: PackageName; commit: string) =
  183. let (_, status) = exec(c, GitCheckout, [commit])
  184. if status != 0:
  185. error(c, p, "could not checkout commit", commit)
  186. proc gitPull(c: var AtlasContext; p: PackageName) =
  187. let (_, status) = exec(c, GitPull, [])
  188. if status != 0:
  189. error(c, p, "could not 'git pull'")
  190. proc updatePackages(c: var AtlasContext) =
  191. if dirExists(c.workspace / PackagesDir):
  192. withDir(c, c.workspace / PackagesDir):
  193. gitPull(c, PackageName PackagesDir)
  194. else:
  195. withDir c, c.workspace:
  196. let err = cloneUrl(c, "https://github.com/nim-lang/packages", PackagesDir, false)
  197. if err != "":
  198. error c, PackageName(PackagesDir), err
  199. proc fillPackageLookupTable(c: var AtlasContext) =
  200. if not c.hasPackageList:
  201. c.hasPackageList = true
  202. when not MockupRun:
  203. updatePackages(c)
  204. let plist = getPackages(when MockupRun: TestsDir else: c.workspace)
  205. for entry in plist:
  206. c.p[unicode.toLower entry.name] = entry.url
  207. proc toUrl(c: var AtlasContext; p: string): string =
  208. if p.isUrl:
  209. result = p
  210. else:
  211. fillPackageLookupTable(c)
  212. result = c.p.getOrDefault(unicode.toLower p)
  213. if result.len == 0:
  214. inc c.errors
  215. proc toName(p: string): PackageName =
  216. if p.isUrl:
  217. result = PackageName splitFile(p).name
  218. else:
  219. result = PackageName p
  220. proc needsCommitLookup(commit: string): bool {.inline.} =
  221. '.' in commit or commit == InvalidCommit
  222. proc isShortCommitHash(commit: string): bool {.inline.} =
  223. commit.len >= 4 and commit.len < 40
  224. proc checkoutCommit(c: var AtlasContext; w: Dependency) =
  225. let dir = c.workspace / w.name.string
  226. withDir c, dir:
  227. if w.commit.len == 0 or cmpIgnoreCase(w.commit, "head") == 0:
  228. gitPull(c, w.name)
  229. else:
  230. let err = isCleanGit(c, dir)
  231. if err != "":
  232. warn c, w.name, err
  233. else:
  234. let requiredCommit =
  235. if needsCommitLookup(w.commit): versionToCommit(c, w)
  236. elif isShortCommitHash(w.commit): shortToCommit(c, w.commit)
  237. else: w.commit
  238. let (cc, status) = exec(c, GitCurrentCommit, [])
  239. let currentCommit = strutils.strip(cc)
  240. if requiredCommit == "" or status != 0:
  241. if requiredCommit == "" and w.commit == InvalidCommit:
  242. warn c, w.name, "package has no tagged releases"
  243. else:
  244. warn c, w.name, "cannot find specified version/commit", w.commit
  245. else:
  246. if currentCommit != requiredCommit:
  247. # checkout the later commit:
  248. # git merge-base --is-ancestor <commit> <commit>
  249. let (cc, status) = exec(c, GitMergeBase, [currentCommit, requiredCommit])
  250. let mergeBase = strutils.strip(cc)
  251. if status == 0 and (mergeBase == currentCommit or mergeBase == requiredCommit):
  252. # conflict resolution: pick the later commit:
  253. if mergeBase == currentCommit:
  254. checkoutGitCommit(c, w.name, requiredCommit)
  255. else:
  256. checkoutGitCommit(c, w.name, requiredCommit)
  257. when false:
  258. warn c, w.name, "do not know which commit is more recent:",
  259. currentCommit, "(current) or", w.commit, " =", requiredCommit, "(required)"
  260. proc findNimbleFile(c: AtlasContext; dep: Dependency): string =
  261. when MockupRun:
  262. result = TestsDir / dep.name.string & ".nimble"
  263. doAssert fileExists(result), "file does not exist " & result
  264. else:
  265. result = c.workspace / dep.name.string / (dep.name.string & ".nimble")
  266. if not fileExists(result):
  267. result = ""
  268. for x in walkFiles(c.workspace / dep.name.string / "*.nimble"):
  269. if result.len == 0:
  270. result = x
  271. else:
  272. # ambiguous .nimble file
  273. return ""
  274. proc addUniqueDep(c: var AtlasContext; work: var seq[Dependency];
  275. tokens: seq[string]) =
  276. let oldErrors = c.errors
  277. let url = toUrl(c, tokens[0])
  278. if oldErrors != c.errors:
  279. warn c, toName(tokens[0]), "cannot resolve package name"
  280. elif not c.processed.containsOrIncl(url / tokens[2]):
  281. work.add Dependency(name: toName(tokens[0]), url: url, commit: tokens[2],
  282. rel: toDepRelation(tokens[1]))
  283. template toDestDir(p: PackageName): string = p.string
  284. proc collectDeps(c: var AtlasContext; work: var seq[Dependency];
  285. dep: Dependency; nimbleFile: string): string =
  286. # If there is a .nimble file, return the dependency path & srcDir
  287. # else return "".
  288. assert nimbleFile != ""
  289. let nimbleInfo = extractRequiresInfo(c, nimbleFile)
  290. for r in nimbleInfo.requires:
  291. var tokens: seq[string] = @[]
  292. for token in tokenizeRequires(r):
  293. tokens.add token
  294. if tokens.len == 1:
  295. # nimx uses dependencies like 'requires "sdl2"'.
  296. # Via this hack we map them to the first tagged release.
  297. # (See the `isStrictlySmallerThan` logic.)
  298. tokens.add "<"
  299. tokens.add InvalidCommit
  300. elif tokens.len == 2 and tokens[1].startsWith("#"):
  301. # Dependencies can also look like 'requires "sdl2#head"
  302. var commit = tokens[1][1 .. ^1]
  303. tokens[1] = "=="
  304. tokens.add commit
  305. if tokens.len >= 3 and cmpIgnoreCase(tokens[0], "nim") != 0:
  306. c.addUniqueDep work, tokens
  307. result = toDestDir(dep.name) / nimbleInfo.srcDir
  308. proc collectNewDeps(c: var AtlasContext; work: var seq[Dependency];
  309. dep: Dependency; result: var seq[string];
  310. isMainProject: bool) =
  311. let nimbleFile = findNimbleFile(c, dep)
  312. if nimbleFile != "":
  313. let x = collectDeps(c, work, dep, nimbleFile)
  314. result.add x
  315. else:
  316. result.add toDestDir(dep.name)
  317. proc cloneLoop(c: var AtlasContext; work: var seq[Dependency]): seq[string] =
  318. result = @[]
  319. var i = 0
  320. while i < work.len:
  321. let w = work[i]
  322. let destDir = toDestDir(w.name)
  323. let oldErrors = c.errors
  324. if not dirExists(c.workspace / destDir):
  325. withDir c, c.workspace:
  326. let err = cloneUrl(c, w.url, destDir, false)
  327. if err != "":
  328. error c, w.name, err
  329. if oldErrors == c.errors:
  330. if not c.keepCommits: checkoutCommit(c, w)
  331. # even if the checkout fails, we can make use of the somewhat
  332. # outdated .nimble file to clone more of the most likely still relevant
  333. # dependencies:
  334. collectNewDeps(c, work, w, result, i == 0)
  335. inc i
  336. proc clone(c: var AtlasContext; start: string): seq[string] =
  337. # non-recursive clone.
  338. let url = toUrl(c, start)
  339. var work = @[Dependency(name: toName(start), url: url, commit: "")]
  340. if url == "":
  341. error c, toName(start), "cannot resolve package name"
  342. return
  343. c.projectDir = c.workspace / toDestDir(work[0].name)
  344. result = cloneLoop(c, work)
  345. const
  346. configPatternBegin = "############# begin Atlas config section ##########\n"
  347. configPatternEnd = "############# end Atlas config section ##########\n"
  348. proc patchNimCfg(c: var AtlasContext; deps: seq[string]; cfgPath: string) =
  349. var paths = "--noNimblePath\n"
  350. for d in deps:
  351. let pkgname = toDestDir d.PackageName
  352. let x = relativePath(c.workspace / pkgname, cfgPath, '/')
  353. paths.add "--path:\"" & x & "\"\n"
  354. var cfgContent = configPatternBegin & paths & configPatternEnd
  355. when MockupRun:
  356. assert readFile(TestsDir / "nim.cfg") == cfgContent
  357. c.mockupSuccess = true
  358. else:
  359. let cfg = cfgPath / "nim.cfg"
  360. if cfgPath.len > 0 and not dirExists(cfgPath):
  361. error(c, c.projectDir.PackageName, "could not write the nim.cfg")
  362. elif not fileExists(cfg):
  363. writeFile(cfg, cfgContent)
  364. else:
  365. let content = readFile(cfg)
  366. let start = content.find(configPatternBegin)
  367. if start >= 0:
  368. cfgContent = content.substr(0, start-1) & cfgContent
  369. let theEnd = content.find(configPatternEnd, start)
  370. if theEnd >= 0:
  371. cfgContent.add content.substr(theEnd+len(configPatternEnd))
  372. else:
  373. cfgContent = content & "\n" & cfgContent
  374. if cfgContent != content:
  375. # do not touch the file if nothing changed
  376. # (preserves the file date information):
  377. writeFile(cfg, cfgContent)
  378. proc error*(msg: string) =
  379. when defined(debug):
  380. writeStackTrace()
  381. quit "[Error] " & msg
  382. proc findSrcDir(c: var AtlasContext): string =
  383. for nimbleFile in walkPattern("*.nimble"):
  384. let nimbleInfo = extractRequiresInfo(c, nimbleFile)
  385. return nimbleInfo.srcDir
  386. return ""
  387. proc installDependencies(c: var AtlasContext; nimbleFile: string) =
  388. # 1. find .nimble file in CWD
  389. # 2. install deps from .nimble
  390. var work: seq[Dependency] = @[]
  391. let (path, pkgname, _) = splitFile(nimbleFile)
  392. let dep = Dependency(name: toName(pkgname), url: "", commit: "")
  393. discard collectDeps(c, work, dep, nimbleFile)
  394. let paths = cloneLoop(c, work)
  395. patchNimCfg(c, paths, if c.cfgHere: getCurrentDir() else: findSrcDir(c))
  396. proc main =
  397. var action = ""
  398. var args: seq[string] = @[]
  399. template singleArg() =
  400. if args.len != 1:
  401. error action & " command takes a single package name"
  402. template noArgs() =
  403. if args.len != 0:
  404. error action & " command takes no arguments"
  405. var c = AtlasContext(
  406. projectDir: getCurrentDir(),
  407. workspace: "")
  408. for kind, key, val in getopt():
  409. case kind
  410. of cmdArgument:
  411. if action.len == 0:
  412. action = key.normalize
  413. else:
  414. args.add key
  415. of cmdLongOption, cmdShortOption:
  416. case normalize(key)
  417. of "help", "h": writeHelp()
  418. of "version", "v": writeVersion()
  419. of "keepcommits": c.keepCommits = true
  420. of "workspace":
  421. if val.len > 0:
  422. c.workspace = val
  423. createDir(val)
  424. else:
  425. writeHelp()
  426. of "cfghere": c.cfgHere = true
  427. else: writeHelp()
  428. of cmdEnd: assert false, "cannot happen"
  429. if c.workspace.len > 0:
  430. if not dirExists(c.workspace): error "Workspace directory '" & c.workspace & "' not found."
  431. else:
  432. c.workspace = getCurrentDir()
  433. while c.workspace.len > 0 and dirExists(c.workspace / ".git"):
  434. c.workspace = c.workspace.parentDir()
  435. echo "Using workspace ", c.workspace
  436. case action
  437. of "":
  438. error "No action."
  439. of "clone":
  440. singleArg()
  441. let deps = clone(c, args[0])
  442. patchNimCfg c, deps, if c.cfgHere: getCurrentDir() else: findSrcDir(c)
  443. when MockupRun:
  444. if not c.mockupSuccess:
  445. error "There were problems."
  446. else:
  447. if c.errors > 0:
  448. error "There were problems."
  449. of "install":
  450. if args.len > 1:
  451. error "install command takes a single argument"
  452. var nimbleFile = ""
  453. if args.len == 1:
  454. nimbleFile = args[0]
  455. else:
  456. for x in walkPattern("*.nimble"):
  457. nimbleFile = x
  458. break
  459. if nimbleFile.len == 0:
  460. error "could not find a .nimble file"
  461. installDependencies(c, nimbleFile)
  462. of "refresh":
  463. noArgs()
  464. updatePackages(c)
  465. of "search", "list":
  466. updatePackages(c)
  467. search getPackages(c.workspace), args
  468. of "extract":
  469. singleArg()
  470. if fileExists(args[0]):
  471. echo toJson(extractRequiresInfo(args[0]))
  472. else:
  473. error "File does not exist: " & args[0]
  474. else:
  475. error "Invalid action: " & action
  476. when isMainModule:
  477. main()
  478. when false:
  479. # some testing code for the `patchNimCfg` logic:
  480. var c = AtlasContext(
  481. projectDir: getCurrentDir(),
  482. workspace: getCurrentDir().parentDir)
  483. patchNimCfg(c, @[PackageName"abc", PackageName"xyz"])
  484. when false:
  485. assert sameVersionAs("v0.2.0", "0.2.0")
  486. assert sameVersionAs("v1", "1")
  487. assert sameVersionAs("1.90", "1.90")
  488. assert sameVersionAs("v1.2.3-zuzu", "1.2.3")
  489. assert sameVersionAs("foo-1.2.3.4", "1.2.3.4")
  490. assert not sameVersionAs("foo-1.2.3.4", "1.2.3")
  491. assert not sameVersionAs("foo", "1.2.3")
  492. assert not sameVersionAs("", "1.2.3")