[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r17134: {projects} General clean up and install of a functional cronjob. (projects/gettor)



Author: ioerror
Date: 2008-10-17 15:05:51 -0400 (Fri, 17 Oct 2008)
New Revision: 17134

Modified:
   projects/gettor/gettor.py
   projects/gettor/gettor_config.py
   projects/gettor/gettor_log.py
   projects/gettor/gettor_opt.py
   projects/gettor/gettor_packages.py
   projects/gettor/gettor_requests.py
   projects/gettor/gettor_responses.py
Log:
General clean up and install of a functional cronjob.


Modified: projects/gettor/gettor.py
===================================================================
--- projects/gettor/gettor.py	2008-10-17 18:52:06 UTC (rev 17133)
+++ projects/gettor/gettor.py	2008-10-17 19:05:51 UTC (rev 17134)
@@ -57,6 +57,7 @@
 
 import sys
 import os
+import subprocess
 import gettext
 import gettor_blacklist
 import gettor_requests
@@ -67,48 +68,29 @@
 import gettor_packages
 
 
-# Somewhat poor hack to get what we want: Use different languages for logging
-# and for reply mails
-# XXX: Change to something more elegant
+# Switch language to 'newlocale'. Return default if language is not supported.
+# XXX: There should be a more elegant way to switch languages during runtime.
 def switchLocale(newlocale):
-    trans = gettext.translation("gettor", "/usr/share/locale", [newlocale])
+    trans = gettext.translation("gettor", "/usr/share/locale", [newlocale], fallback=True)
     trans.install()
 
 def runTests():
     # XXX 
     return True
 
-def installCron(mirror, distdir):
-    # XXX: TODO REDO THIS FUNCTION TO USE `crontab -e`
-    # XXX: We might want to install a cronjob file to /etc/cron.daily, on
-    # system that support it. Also, we should use the mirror from the command
-    # line or config file, as well as the distdir from the config to build 
-    # the command string
-    #comment="\n# Sync Tor software\n"
-    #command="0 3 * * * rsync -a rsync://" + mirror + "/tor/dist/current/" 
-    #            + distdir + "\n"
-    #try:
-    #    f = open("/etc/crontab", "a")
-    #    f.write(comment + command)
-    #    f.close
-    #except:
-    #    print "Installation failed. Are you root?"
-    #    return False
-    #print "Cronjob installed: Running every night at three after midnight"
-    return True
+def installCron(rsync):
+    # XXX: Check if cron is installed and understands our syntax?
+    echoCmd = ['echo', '3 2 * * * ' + rsync]
+    cronCmd = ['crontab', '-']
+    echoProc = subprocess.Popen(echoCmd, stdout=subprocess.PIPE)
+    cronProc = subprocess.Popen(cronCmd, stdin=echoProc.stdout)
+    cronProc.communicate()[0]
+    return cronProc.returncode
 
 def processMail(conf, log, logLang, packageList):
-    srcEmail = conf.getSrcEmail()
-    # Check package list sanity
-    for key, val in packageList.items():
-        # Remove invalid packages
-        if not os.access(val, os.R_OK):
-            log.info(_("Warning: %s not accessable. Removing from list.") % val)
-            del packageList[key]
     if len(packageList) < 1:
-        log.info(_("Sorry, your package list is unusable."))
+        log.error(_("Sorry, your package list is unusable."))
         return False
-
     # Receive mail
     rmail = gettor_requests.requestMail(packageList)
     rawMessage = rmail.getRawMessage()
@@ -119,40 +101,41 @@
     if not parsedMessage:
         log.error(_("No parsed message. Dropping message."))
         return False
-    # XXX TODO: Ensure we have a proper replyTO or bail out
+    # XXX: We should add a blacklist check here so that for exmaple ReplyTo can't be our own 
+    #      address (DoS) (in case we have DKIM) 
     replyTo = rmail.getReplyTo()
     if not replyTo:
         log.error(_("No help dispatched. Invalid reply address for user."))
         return False
-    # Get desired reply language, if any
     replyLang = rmail.getLocale()
     if not replyLang:
         replyLang = logLang
     # Initialize response
-    respmail = gettor_responses.gettorResponse(replyLang, logLang)
+    srcEmail = conf.getSrcEmail()
+    resp = gettor_responses.gettorResponse(replyLang, logLang)
     signature = rmail.hasVerifiedSignature()
     log.info(_("Signature is: %s") % str(signature))
     if not signature:
         # Check to see if we've helped them to understand that they need DKIM
         # in the past
         previouslyHelped = gettor_blacklist.blackList(replyTo)
-    if not signature and previouslyHelped:
-        log.info(_("Unsigned messaged to gettor by blacklisted user dropped."))
-        return False
-    if not signature and not previouslyHelped:
-        # Reply with some help and bail out
-        gettor_blacklist.blackList(replyTo, True)
-        respmail.sendHelp(srcEmail, replyTo)
-        log.info(_("Unsigned messaged to gettor. We issued some help."))
-        return True
-    if signature:
+        if previouslyHelped:
+            log.info(_("Unsigned messaged to gettor by blacklisted user dropped."))
+            return False
+        else:
+            # Reply with some help and bail out
+            gettor_blacklist.blackList(replyTo, True)
+            resp.sendHelp(srcEmail, replyTo)
+            log.info(_("Unsigned messaged to gettor. We issued some help."))
+            return True
+    else:
         log.info(_("Signed messaged to gettor."))
         package = rmail.getPackage()
         if package != None:
             log.info(_("Package: %s selected.") % str(package))
-            respmail.sendPackage(srcEmail, replyTo, packageList[package])  
+            resp.sendPackage(srcEmail, replyTo, packageList[package])  
         else:
-            respmail.sendPackageHelp(packageList, srcEmail, replyTo)
+            resp.sendPackageHelp(packageList, srcEmail, replyTo)
             log.info(_("We issued some help about proper email formatting."))
 
     return True
@@ -171,7 +154,7 @@
         exit(1)
 
     packs = gettor_packages.gettorPackages(options.mirror, conf)
-
+    # Action
     if options.fetchpackages:
         if packs.syncWithMirror() != 0:
             log.error(_("Syncing Tor packages failed."))
@@ -194,7 +177,7 @@
             log.info(_("Tests passed."))
             exit(0)
     if options.installcron:
-        if not installCron(options.mirror, distDir):
+        if installCron(packs.getCommandToStr()) != 0:
             log.error(_("Installing cron failed"))
             exit(1)
         else:

Modified: projects/gettor/gettor_config.py
===================================================================
--- projects/gettor/gettor_config.py	2008-10-17 18:52:06 UTC (rev 17133)
+++ projects/gettor/gettor_config.py	2008-10-17 19:05:51 UTC (rev 17134)
@@ -58,6 +58,8 @@
 import sys
 import ConfigParser
 
+__all__ = ["gettorConf"]
+
 class gettorConf:
     '''
     Initialize gettor with default values if one or more values are missing 

Modified: projects/gettor/gettor_log.py
===================================================================
--- projects/gettor/gettor_log.py	2008-10-17 18:52:06 UTC (rev 17133)
+++ projects/gettor/gettor_log.py	2008-10-17 19:05:51 UTC (rev 17134)
@@ -25,13 +25,14 @@
 import gettor_config
 from logging import handlers
 
+__all__ = ["gettorLogger"]
+
 # Leave this to INFO for now
 loglevel = logging.INFO
 
 class gettorLogger:
-    '''
-    A configurable logging system for gettor.
-    '''
+    """A configurable logging system for gettor.
+    """
 
     format = '%(asctime)-15s (%(process)d) %(message)s'
 
@@ -61,7 +62,7 @@
 
         # If anything went wrong or the user doesn't want to log
         if self.logSubSystem == "nothing":
-            handler = logging.FileHandler("/dev/null")
+            handler = logging.FileHandler(os.devnull)
 
         formatter = logging.Formatter(fmt=self.format)
         handler.setFormatter(formatter)

Modified: projects/gettor/gettor_opt.py
===================================================================
--- projects/gettor/gettor_opt.py	2008-10-17 18:52:06 UTC (rev 17133)
+++ projects/gettor/gettor_opt.py	2008-10-17 19:05:51 UTC (rev 17134)
@@ -14,6 +14,8 @@
 
 import optparse
 
+__all__ = ["parseOpts"]
+
 def parseOpts():
     cmdParser = optparse.OptionParser()
     cmdParser.add_option("-c", "--config", dest="configfile",
@@ -36,3 +38,6 @@
                         help="run some tests")
 
     return cmdParser.parse_args()
+
+if __name__ == "__main__":
+    print >> sys.stderr "You shouldn't run this directly."

Modified: projects/gettor/gettor_packages.py
===================================================================
--- projects/gettor/gettor_packages.py	2008-10-17 18:52:06 UTC (rev 17133)
+++ projects/gettor/gettor_packages.py	2008-10-17 19:05:51 UTC (rev 17134)
@@ -18,6 +18,8 @@
 import gettor_config
 import re
 
+__all__ = ["gettorPackages"]
+
 class gettorPackages:
 
     packageRegex = { "windows-bundle": "vidalia-bundle-.*.exe$",
@@ -41,8 +43,15 @@
         self.rsync.append(self.distDir)
 
     def getPackageList(self):
+        # Build dict like 'name': 'name.z'
         for filename in os.listdir(self.packDir):
             self.packageList[filename[:-2]] = self.packDir + "/" + filename
+        # Check sanity
+        for key, val in self.packageList.items():
+            # Remove invalid packages
+            if not os.access(val, os.R_OK):
+                log.info(_("Warning: %s not accessable. Removing from list.") % val)
+                del self.packageList[key]
         return self.packageList
 
     def buildPackages(self):
@@ -70,6 +79,14 @@
         process.wait()
         return process.returncode
 
+    def getCommandToStr(self):
+        """This is useful for cronjob installations
+        """
+        rsyncstr = ""
+        for i,v in enumerate(self.rsync):
+            rsyncstr += self.rsync[i] + " "
+        return rsyncstr
+
 if __name__ == "__main__" :
     c = gettor_config.gettorConf()
     p = gettorPackages("rsync.torproject.org", c)

Modified: projects/gettor/gettor_requests.py
===================================================================
--- projects/gettor/gettor_requests.py	2008-10-17 18:52:06 UTC (rev 17133)
+++ projects/gettor/gettor_requests.py	2008-10-17 19:05:51 UTC (rev 17134)
@@ -16,6 +16,8 @@
 import dkim
 import re
 
+__all__ = ["requestMail"]
+
 class requestMail:
 
     defaultLang = "en"

Modified: projects/gettor/gettor_responses.py
===================================================================
--- projects/gettor/gettor_responses.py	2008-10-17 18:52:06 UTC (rev 17133)
+++ projects/gettor/gettor_responses.py	2008-10-17 19:05:51 UTC (rev 17134)
@@ -17,8 +17,9 @@
 import base64
 import gettext
 
+__all__ = ["gettorResponse"]
 
-class gettorResponse():
+class gettorResponse:
 
     def __init__(self, mailLang="en", logLang="en"):
         self.mailLang = mailLang