forked from mozilla-conduit/git-hg-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
85 lines (68 loc) · 2.25 KB
/
__main__.py
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
import argparse
from pathlib import Path
import sentry_sdk
from kombu import Connection, Exchange, Queue
from mozlog import commandline
from git_hg_sync.application import Application
from git_hg_sync.config import Config, PulseConfig
from git_hg_sync.pulse_worker import PulseWorker
from git_hg_sync.repo_synchronizer import RepoSynchronizer
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config",
type=Path,
required=True,
help="Configuration file path.",
)
return parser
def get_connection(config: PulseConfig) -> Connection:
return Connection(
hostname=config.host,
port=config.port,
userid=config.userid,
password=config.password,
heartbeat=10,
ssl=config.ssl,
)
def get_queue(config: Config | PulseConfig) -> Queue:
exchange = Exchange(config.exchange, type="topic")
return Queue(
name=config.queue,
exchange=exchange,
routing_key=config.routing_key,
exclusive=False,
)
def start_app(
config: Config, logger: commandline.StructuredLogger, *, one_shot: bool = False
) -> None:
pulse_config = config.pulse
connection = get_connection(pulse_config)
queue = get_queue(pulse_config)
synchronizers = {
tracked_repo.url: RepoSynchronizer(
config.clones.directory / tracked_repo.name, tracked_repo.url
)
for tracked_repo in config.tracked_repositories
}
with connection as conn:
logger.info(f"connected to {conn.host}")
worker = PulseWorker(conn, queue, one_shot=one_shot)
app = Application(
worker, synchronizers, [*config.branch_mappings, *config.tag_mappings]
)
app.run()
def main() -> None:
parser = get_parser()
commandline.add_logging_group(parser)
args = parser.parse_args()
logger = commandline.setup_logging("service", args)
config = Config.from_file(args.config)
sentry_config = config.sentry
if sentry_config and sentry_config.sentry_url:
logger.info(f"sentry url: {sentry_config.sentry_url}")
sentry_sdk.init(sentry_config.sentry_url)
start_app(config, logger)
if __name__ == "__main__":
main()