changeset 12:6de4d72474b9

Made logging better by distinguishing between public and private log messages, and added more error handling.
author Atul Varma <avarma@mozilla.com>
date Thu, 03 Jun 2010 09:33:55 -0700
parents 388528fb31c7
children 88da4618d578
files bzezpatch/app.py bzezpatch/hg.py static-files/app.js
diffstat 3 files changed, 41 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/bzezpatch/app.py	Thu Jun 03 07:16:00 2010 +0000
+++ b/bzezpatch/app.py	Thu Jun 03 09:33:55 2010 -0700
@@ -67,11 +67,16 @@
             tmplog = tempfile.TemporaryFile(prefix='hg-trypatch-log-')
             try:
                 patch = self.hg.trypatch(url, out=tmplog)
-                info = {'success': True, 'patch': patch}
+                tmplog.seek(0)
+                info = {'success': True, 'patch': patch,
+                        'log': tmplog.read()}
             except Exception, e:
                 print traceback.format_exc()
                 tmplog.seek(0)
-                info = {'success': False, 'log': tmplog.read()}
+                log = tmplog.read()
+                if not log:
+                    log = 'Internal server error when trying to apply patch.'
+                info = {'success': False, 'log': log}
 
             return ok_response(json.dumps(info), mimetype=self.JSON_TYPE)
         elif path.startswith('/') and method == 'GET':
--- a/bzezpatch/hg.py	Thu Jun 03 07:16:00 2010 +0000
+++ b/bzezpatch/hg.py	Thu Jun 03 09:33:55 2010 -0700
@@ -8,15 +8,32 @@
     def __init__(self, **kwargs):
         self.__dict__.update(kwargs)
 
-    def trypatch(self, pull_url, out=sys.stdout):
+    def trypatch(self, pull_url, out=sys.stdout, privout=sys.stdout):
         temp_dir = tempfile.mkdtemp(prefix='hg-trypatch-')
         temp_repo = os.path.join(temp_dir, 'repo')
+
+        def privlog(msg):
+            privout.flush()
+            privout.write('%s\n' % msg)
+            privout.flush()
+
+        def log(msg):
+            out.flush()
+            out.write('%s\n' % msg)
+            out.flush()
+
         try:
             # Step 1: Pull and update local canonical repo.
-            out.write('pulling and updating local canonical repo\n')
-            subprocess.check_call([self.hg, '-R', self.canonical_repo,
-                                   'pull', '-u'],
-                                  stdout=out, stderr=out)
+            privlog('pulling and updating local canonical repo')
+            rv = subprocess.call([self.hg, '-R', self.canonical_repo,
+                                  'pull', '-u'],
+                                 stdout=privout, stderr=privout)
+            if rv:
+                log('Warning: server was unable to pull the latest version '
+                    'of the canonical repository, which means that if '
+                    'the rest of this process fails, it might not be '
+                    'your fault. Alternatively, if this process succeeds, '
+                    'the generated patch may be stale.')
 
             # Step 2: Figure out tip changeset so we can diff against
             # it later.
@@ -26,16 +43,16 @@
             tip_revision, _ = popen.communicate()
             if popen.returncode:
                 raise Exception('failed to get tip changeset')
-            out.write('tip is %s\n' % tip_revision)
+            privlog('tip is %s' % tip_revision)
 
             # Step 3: Create a temporary clone to apply the patch to.
-            out.write('creating temporary clone at %s\n' % temp_repo)
+            privlog('creating temporary clone at %s' % temp_repo)
             subprocess.check_call([self.hg, 'clone', self.canonical_repo,
                                    temp_repo],
-                                  stdout=out, stderr=out)
+                                  stdout=privout, stderr=privout)
 
             # Step 4: Apply the patch by pulling from the foreign repo.
-            out.write('pulling from %s\n' % pull_url)
+            log('pulling from %s' % pull_url)
             subprocess.check_call([self.hg, '-R', temp_repo, 'pull', 
                                    '--rebase', pull_url],
                                   stdout=out, stderr=out)
@@ -55,5 +72,5 @@
                 raise Exception('failed to create patch')
             return patch
         finally:
-            out.write('removing temporary clone at %s\n' % temp_repo)
-            shutil.rmtree(temp_repo)
+            privlog('removing temporary dir at %s' % temp_dir)
+            shutil.rmtree(temp_dir)
--- a/static-files/app.js	Thu Jun 03 07:16:00 2010 +0000
+++ b/static-files/app.js	Thu Jun 03 09:33:55 2010 -0700
@@ -6,7 +6,12 @@
     data: JSON.stringify(data),
     dataType: "text/plain",
     success: function(response) {
-      cb(JSON.parse(response));
+      try {
+        response = JSON.parse(response);
+      } catch (e) {
+        response = {success: false, log: e.toString()};
+      }
+      cb(response);
     },
     error: function(xhr, textStatus, errorThrown) {
       var log = ("HTTP error: " + xhr.status + " " +