git-importer/opensuse-monitor.py

60 lines
1.5 KiB
Python
Raw Normal View History

2024-05-17 11:40:19 +02:00
#!/usr/bin/python3
import json
from pathlib import Path
import pika
import random
import time
2024-05-17 11:40:19 +02:00
MY_TASKS_DIR = Path(__file__).parent / "tasks"
2024-07-10 10:34:20 +02:00
def listen_events():
2024-07-10 10:34:20 +02:00
connection = pika.BlockingConnection(
pika.URLParameters("amqps://opensuse:opensuse@rabbit.opensuse.org")
)
channel = connection.channel()
2024-05-17 11:40:19 +02:00
2024-07-10 10:34:20 +02:00
channel.exchange_declare(
exchange="pubsub", exchange_type="topic", passive=True, durable=False
)
2024-05-17 11:40:19 +02:00
result = channel.queue_declare("", exclusive=True)
queue_name = result.method.queue
2024-05-17 11:40:19 +02:00
2024-07-10 10:34:20 +02:00
channel.queue_bind(
exchange="pubsub", queue=queue_name, routing_key="opensuse.obs.package.commit"
)
2024-05-17 11:40:19 +02:00
2024-07-10 10:34:20 +02:00
print(" [*] Waiting for logs. To exit press CTRL+C")
2024-05-17 11:40:19 +02:00
def callback(ch, method, properties, body):
if method.routing_key not in ("opensuse.obs.package.commit",):
2024-05-17 11:40:19 +02:00
return
body = json.loads(body)
2024-07-10 10:34:20 +02:00
if (
"project" in body
and "package" in body
and body["project"] == "openSUSE:Factory"
):
if "/" in body["package"]:
return
2024-05-17 11:40:19 +02:00
2024-07-10 10:34:20 +02:00
(MY_TASKS_DIR / body["package"]).touch()
print(" [x] %r:%r" % (method.routing_key, body["package"]))
2024-05-17 11:40:19 +02:00
2024-07-10 10:34:20 +02:00
channel.basic_consume(queue_name, callback, auto_ack=True)
2024-05-17 11:40:19 +02:00
channel.start_consuming()
def main():
while True:
try:
2024-07-10 10:34:20 +02:00
listen_events()
except (pika.exceptions.ConnectionClosed, pika.exceptions.AMQPHeartbeatTimeout):
time.sleep(random.randint(10, 100))
2024-07-10 10:34:20 +02:00
if __name__ == "__main__":
main()