forked from Davidyz/VectorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (91 loc) · 3.36 KB
/
Copy pathmain.py
File metadata and controls
114 lines (91 loc) · 3.36 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import asyncio
import logging
import os
import sys
import traceback
from vectorcode import __version__
from vectorcode.cli_utils import (
CliAction,
find_project_root,
get_project_config,
parse_cli_args,
)
async def async_main():
cli_args = await parse_cli_args()
if cli_args.no_stderr:
sys.stderr = open(os.devnull, "w")
if cli_args.project_root is None:
cwd = os.getcwd()
cli_args.project_root = (
find_project_root(cwd, ".vectorcode")
or find_project_root(cwd, ".git")
or cwd
)
try:
final_configs = await (
await get_project_config(cli_args.project_root)
).merge_from(cli_args)
except IOError as e:
traceback.print_exception(e, file=sys.stderr)
return 1
match cli_args.action:
case CliAction.check:
from vectorcode.subcommands import check
return await check(cli_args)
case CliAction.init:
from vectorcode.subcommands import init
return await init(cli_args)
case CliAction.version:
print(__version__)
return 0
case CliAction.prompts:
from vectorcode.subcommands import prompts
return prompts(cli_args)
case CliAction.chunks:
from vectorcode.subcommands import chunks
return_val = await chunks(final_configs)
from vectorcode.common import start_server, try_server
server_process = None
if not await try_server(final_configs.host, final_configs.port):
print(
f"Host at {final_configs.host}:{final_configs.port} is unavailable. VectorCode will start its own Chromadb at a random port.",
file=sys.stderr,
)
server_process = await start_server(final_configs)
if final_configs.pipe:
# NOTE: NNCF (intel GPU acceleration for sentence transformer) keeps showing logs.
# This disables logs below ERROR so that it doesn't hurt the `pipe` output.
logging.disable(logging.ERROR)
return_val = 0
try:
match final_configs.action:
case CliAction.query:
from vectorcode.subcommands import query
return_val = await query(final_configs)
case CliAction.vectorise:
from vectorcode.subcommands import vectorise
return_val = await vectorise(final_configs)
case CliAction.drop:
from vectorcode.subcommands import drop
return_val = await drop(final_configs)
case CliAction.ls:
from vectorcode.subcommands import ls
return_val = await ls(final_configs)
case CliAction.update:
from vectorcode.subcommands import update
return_val = await update(final_configs)
case CliAction.clean:
from vectorcode.subcommands import clean
return_val = await clean(final_configs)
except Exception as e:
return_val = 1
traceback.print_exception(e, file=sys.stderr)
finally:
if server_process is not None:
server_process.terminate()
await server_process.wait()
return return_val
def main(): # pragma: nocover
return asyncio.run(async_main())
if __name__ == "__main__": # pragma: nocover
sys.exit(main())