Coretex
main.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from importlib.metadata import version as getLibraryVersion
19 
20 import click
21 
22 from .commands.login import login
23 from .commands.model import model
24 from .commands.node import node
25 from .commands.task import task
26 from .commands.project import project
27 
28 from .modules import ui, utils
29 from .modules.intercept import ClickExceptionInterceptor
30 from ..utils.process import CommandException
31 
32 
33 @click.command()
34 def version() -> None:
35  version = getLibraryVersion("coretex")
36  ui.stdEcho(f"Coretex {version}")
37 
38 
39 @click.command()
40 def update() -> None:
41  currentVersion = utils.fetchCurrentVersion()
42  latestVersion = utils.fetchLatestVersion()
43 
44  if currentVersion is None or latestVersion is None:
45  return
46 
47  if latestVersion > currentVersion:
48  try:
49  ui.progressEcho("Updating coretex...")
50  utils.updateLib()
51  ui.successEcho(
52  f"Coretex successfully updated from {utils.formatCliVersion(currentVersion)} "
53  f"to {utils.formatCliVersion(latestVersion)}!"
54  )
55  except CommandException:
56  ui.errorEcho("Failed to update coretex.")
57  else:
58  ui.stdEcho("Coretex version is up to date.")
59 
60 
61 @click.group(cls = ClickExceptionInterceptor)
62 @utils.onBeforeCommandExecute(utils.checkLibVersion, excludeSubcommands = ["update"])
63 @click.version_option(getLibraryVersion("coretex"), "--version", "-v", message = "Coretex %(version)s")
64 def cli() -> None:
65  pass
66 
67 cli.add_command(login)
68 cli.add_command(model)
69 cli.add_command(project)
70 cli.add_command(node)
71 cli.add_command(task)
72 cli.add_command(version)
73 cli.add_command(update)