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
129 lines (101 loc) · 3.81 KB
/
Copy pathmain.py
File metadata and controls
129 lines (101 loc) · 3.81 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import asyncio
import logging
import os
import sys
import traceback
from vectorcode import __version__
from vectorcode.cli_utils import (
CliAction,
config_logging,
find_project_root,
get_project_config,
parse_cli_args,
)
logger = logging.getLogger(name=__name__)
async def async_main():
cli_args = await parse_cli_args()
if cli_args.no_stderr:
sys.stderr = open(os.devnull, "w")
logger.info("Collected CLI arguments: %s", cli_args)
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
)
logger.info(f"Project root is set to {cli_args.project_root}")
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
logger.info("Final configuration has been built: %s", final_configs)
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 await chunks(final_configs)
case CliAction.hooks:
logger.warning(
"`vectorcode hooks` has been deprecated and will be removed in 0.7.0."
)
logger.warning("Please use `vectorcode init --hooks`.")
from vectorcode.subcommands import hooks
return await hooks(cli_args)
from vectorcode.common import start_server, try_server
server_process = None
if not await try_server(final_configs.db_url):
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:
return_val = 1
logger.error(traceback.format_exc())
finally:
if server_process is not None:
logger.info("Shutting down the bundled Chromadb instance.")
server_process.terminate()
await server_process.wait()
return return_val
def main(): # pragma: nocover
config_logging("vectorcode")
return asyncio.run(async_main())
if __name__ == "__main__": # pragma: nocover
sys.exit(main())