import repository from arizona
[raven.git] / apps / mprepo / repopubsub.py
1 #! /usr/bin/env python
2
3 """
4 <Program Name>
5
6 <Started>
7
8 <Author>
9
10 <Purpose>
11 """
12
13 #           [option, long option,      variable,       action,  data,     default,                           metavar, description]
14 """arizonaconfig
15    options=[["-C",   "--configfile",    "configfile",    "store",      "string", "/usr/local/mprepo/etc/mprepo.conf",               "FILE", "use a different config file (/usr/local/mprepo/etc/mprepo.conf is the default)"],
16              ["",    "--pubsubsync",    "pubsubsync",    "store_true",  None,           False,                                  None,           "Do not daemonize"]]
17
18    includes=[]
19 """
20
21 import os
22 import socket
23 import sys
24 import threading
25 import time
26
27 import arizonaconfig
28 import arizonageneral
29 import arizonareport
30 from ravenlib.pubsub.sender import PubSubSender, PubSubServer
31 import ravenlib.report
32 import repoconfig
33
34 REPO_PUBSUB_PORT = 4321
35
36 class Watcher(threading.Thread):
37     def __init__(self, filename, sender):
38         threading.Thread.__init__(self)
39         self.filename = filename
40         self.sender = sender
41         self.version = int(time.time())
42         self.start()
43
44     def get_mtime(self):
45       try:
46           return os.stat(self.filename).st_mtime
47       except OSError:
48           return 0
49
50     def run(self):
51         mtime = 0
52         while True:
53             if os.path.exists(self.filename):
54                  if mtime != self.get_mtime():
55                      mtime = self.get_mtime()
56                      print >> sys.stdout, "[repopubsub] Setting publish data for", self.filename
57                      contents = open(self.filename,"r").read()
58                      dict = {"filename": self.filename, "contents": contents, "version": self.version}
59                      self.sender.set_message(dict)
60             else:
61                 print >> sys.stdout, "[repopubsub] File", self.filename, "does not exist."
62             time.sleep(10)
63
64 def Main():
65     args = arizonaconfig.init_options("repopubsub.py", configfile_optvar="configfile", version="2.0")
66     repoconfig.init()
67
68     # tell ravenlib.report to use arizonareport
69     ravenlib.report.enable_arizonareport()
70
71     sync = arizonaconfig.get_option("pubsubsync")
72
73     if sync:
74         arizonareport.send_syslog(arizonareport.INFO, "[repopubsub] Starting")
75     else:
76         # run as a daemon
77         arizonageneral.make_daemon("repopubsub")
78
79     sender = PubSubSender()
80     server = PubSubServer(("", REPO_PUBSUB_PORT), sender)
81     watcher = Watcher(os.path.join(repoconfig.tarball_dir, "metafile"), sender)
82     while True:
83         time.sleep(1)
84
85 if __name__ == "__main__":
86     Main()