]> scripts.mit.edu Git - wizard.git/commitdiff
Refactor more boilerplate out.
authorEdward Z. Yang <ezyang@mit.edu>
Wed, 25 Nov 2009 07:37:50 +0000 (02:37 -0500)
committerEdward Z. Yang <ezyang@mit.edu>
Wed, 25 Nov 2009 07:37:50 +0000 (02:37 -0500)
Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
wizard/app/__init__.py
wizard/app/mediawiki.py
wizard/app/wordpress.py
wizard/deploy.py
wizard/util.py

index ab4d0080423538c7acdb71d932586c87bb97d3ba..483f7ee30c01e8310eded59a8fd9993552747e30 100644 (file)
@@ -246,13 +246,10 @@ class Application(object):
         this application.
         """
         raise NotImplementedError
-    def checkWeb(self, deployment, output=None):
+    def checkWeb(self, deployment):
         """
-        Checks if the autoinstall is viewable from the web.  To get
-        the HTML source that was retrieved, pass a variable containing
-        an empty list to ``output``; it will be mutated to have its
-        first element be the output.  Subclasses should provide an
-        implementation.
+        Checks if the autoinstall is viewable from the web.  Subclasses should
+        provide an implementation.
 
         .. note::
             Finding a reasonable heuristic that works across skinning
@@ -262,6 +259,13 @@ class Application(object):
             not to depend on pages that are not the main page.
         """
         raise NotImplementedError
+    def checkWebPage(self, deployment, page, output):
+        """
+        Checks if a given page of an autoinstall contains a particular string.
+        """
+        page = deployment.fetch(page)
+        logging.info("checkWebPage:\n\n" + page)
+        return page.find(output) != -1
     def checkConfig(self, deployment):
         """
         Checks whether or not an autoinstall has been configured/installed
index fc81613872913f3af87b44d6b3cda0cbd578344e..adfdd42205d764a611093a29411270a6894ffdc9 100644 (file)
@@ -4,6 +4,7 @@ import os
 import lxml.cssselect
 import lxml.etree
 import StringIO
+import logging
 
 from wizard import app, install, resolve, shell, util
 from wizard.app import php
@@ -35,19 +36,12 @@ class Application(app.Application):
         return os.path.isfile("LocalSettings.php")
     def detectVersion(self, deployment):
         return self.detectVersionFromFile("includes/DefaultSettings.php", php.re_var("wgVersion"))
-    def checkWeb(self, deployment, out=None):
-        page = deployment.fetch("/index.php?title=Main_Page")
-        if type(out) is list:
-            out.append(page)
-        return page.find("<!-- Served") != -1
+    def checkWeb(self, deployment):
+        return self.checkWebPage(deployment, "/index.php?title=Main_Page", "<!-- Served")
     def prepareMerge(self, deployment):
         resolve.fix_newlines("LocalSettings.php")
     def install(self, version, options):
-        try:
-            os.unlink("LocalSettings.php")
-        except OSError:
-            pass
-
+        util.soft_unlink("LocalSettings.php")
         os.chmod("config", 0777) # XXX: vaguely sketchy
 
         postdata = {
@@ -68,7 +62,7 @@ class Application(app.Application):
         result_etree = lxml.etree.parse(StringIO.StringIO(result), lxml.etree.HTMLParser())
         selector = lxml.cssselect.CSSSelector(".error")
         error_messages = [e.text for e in selector(result_etree)]
-        if options.verbose or options.debug: print result
+        logging.debug("Installation output:\n\n" + result)
         if result.find("Installation successful") == -1:
             if not error_messages:
                 raise app.InstallFailure()
index ab80825bbb627e0aeb75af5f4dbb3f34952c2839..742881b93a857d5846b690963a08eced1ca1ddd4 100644 (file)
@@ -35,28 +35,18 @@ class Application(app.Application):
         return "http://wordpress.org/wordpress-%s.tar.gz" % version
     def checkConfig(self, deployment):
         return os.path.isfile("wp-config.php")
-    def checkWeb(self, deployment, out=None):
-        page = deployment.fetch("")
-        if type(out) is list:
-            out.append(page)
-        return page.find("<html") != -1
+    def checkWeb(self, deployment):
+        # XXX: this sucks pretty hard
+        return self.checkWebPage(deployment, "", "<html")
     def detectVersion(self, deployment):
-        # XXX: Very duplicate code with MediaWiki; refactor
-        contents = deployment.read("wp-includes/version.php")
-        match = php.re_var("wp_version").search(contents)
-        if not match: return None
-        return distutils.version.LooseVersion(match.group(2)[1:-1])
+        return self.detectVersionFromFile("wp-includes/version.php", php.re_var("wp_version"))
     def prepareMerge(self, deployment):
         # This file shouldn't really be edited by users, but be careful: it's
         # stored as DOS and not as UNIX, so you'll get conflicts if you add this line:
         # resolve.fix_newlines("wp-config.php")
         pass
     def install(self, version, options):
-        # XXX: Hmm... we should figure out something about this
-        try:
-            os.unlink("wp-config.php")
-        except OSError:
-            pass
+        util.soft_unlink("wp-config.php")
 
         post_setup_config = {
                 'dbhost': options.mysql_host,
index 22750586511e61f2773ffd3b0c7bddd11197aba1..421ba360e2551cf511e34d005ef31d3b2c462411 100644 (file)
@@ -326,9 +326,8 @@ class ProductionCopy(Deployment):
         """
         Checks if the autoinstall is viewable from the web.
         """
-        out = []
-        if not self.application.checkWeb(self, out):
-            raise WebVerificationError(out[0])
+        if not self.application.checkWeb(self):
+            raise WebVerificationError
     def fetch(self, path, post=None):
         """
         Performs a HTTP request on the website.
@@ -530,19 +529,13 @@ version %s.""" % (self.real_version, self.git_version)
 
 class WebVerificationError(Error):
     """Could not access the application on the web"""
-    #: Contents of web page access
-    contents = None
-    def __init__(self, contents):
-        self.contents = contents
     def __str__(self):
         return """
 
 ERROR: We were not able to access the application on the
 web.  This may indicate that the website is behind
-authentication on the htaccess level.  The contents
-of the page were:
-
-%s""" % self.contents
+authentication on the htaccess level.  You can find
+the contents of the page from the debug backtraces."""
 
 class UnknownWebPath(Error):
     """Could not determine application's web path."""
index 0cd8fc88cb8447fbd960a0d70cc64bbe3c536964..b0ab342b428f8c9e6c4e38903665ec21d4649984 100644 (file)
@@ -304,6 +304,13 @@ def safe_unlink(file):
     os.rename(file, name)
     return name
 
+def soft_unlink(file):
+    """Unlink a file, but don't complain if it doesn't exist."""
+    try:
+        os.unlink(file)
+    except OSError:
+        pass
+
 def fetch(host, path, subpath, post=None):
     h = httplib.HTTPConnection(host)
     fullpath = path.rstrip("/") + "/" + subpath.lstrip("/") # to be lenient about input we accept