changeset 19:854065d98c3d default tip

Added features to SubprocessRunner, and 'try jetpack bugzilla patch' command
author Atul Varma <varmaa@toolness.com>
date Sun, 04 Apr 2010 19:48:18 -0700
parents 400a9edd79ad
children
files my-enso-commands.py
diffstat 1 files changed, 85 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/my-enso-commands.py	Wed Mar 31 16:53:38 2010 -0700
+++ b/my-enso-commands.py	Sun Apr 04 19:48:18 2010 -0700
@@ -967,16 +967,19 @@
     return _gen_daily_edition(ensoapi)
 
 class SubprocessRunner(object):
-    def __init__(self, cmdline):
+    def __init__(self, cmdline, env=None, raise_on_error=True):
         self.cmdline = cmdline
         self.output_text = None
+        self.env = env
+        self.raise_on_error = raise_on_error
 
     def run(self):
         output_fd, output_name = tempfile.mkstemp()
 
         popen = subprocess.Popen(self.cmdline,
                                  stdout = output_fd,
-                                 stderr = subprocess.STDOUT)
+                                 stderr = subprocess.STDOUT,
+                                 env = self.env)
 
         while popen.poll() is None:
             yield
@@ -985,12 +988,91 @@
         output_text = open(output_name, "r").read().strip()
         os.remove(output_name)
 
-        if popen.returncode:
+        self.returncode = popen.returncode
+
+        if popen.returncode and self.raise_on_error:
             raise Exception("Process failed with output %s" %
                             repr(output_text))
 
         self.output_text = output_text
 
+def infer_patch_from_url(url):
+    """
+    >>> infer_patch_from_url("blah")
+    (None, None)
+
+    >>> url = "https://bugzilla.mozilla.org/show_bug.cgi?id=556075"
+    >>> infer_patch_from_url(url)
+    ('556075', None)
+    """
+
+    import re
+
+    BUG_RE = r"https:\/\/bugzilla.mozilla.org\/show_bug.cgi\?id=([0-9]+)"
+    bug_re = re.compile(BUG_RE)
+
+    match = bug_re.match(url)
+    if match:
+        return (match.group(1), None)
+    return (None, None)
+
+def cmd_try_jetpack_bugzilla_patch(ensoapi):
+    """Fetches the patch for the currently selected bug or attachment URL
+    and creates a clone of the Jetpack repository that has it applied."""
+
+    url = ensoapi.get_selection().get("text", "").strip()
+    if not url:
+        ensoapi.display_message("Please select a bug or attachment URL.")
+        return
+
+    bug_id, attach_id = infer_patch_from_url(url)
+    if not bug_id:
+        ensoapi.display_message("That does not appear to be a bug or "
+                                "attachment URL.")
+        return
+
+    mypkg = os.path.expanduser("~/Documents/MyPythonPackage")
+    hgzilla = mypkg + "/scripts/hgzilla.py"
+    root_repo_path = os.path.expanduser("~/Documents/jetpack-sdk")
+    clone_name = "jetpack-sdk-bug-%s" % bug_id
+    clone_path = os.path.expanduser("~/Documents/" + clone_name)
+
+    if os.path.exists(clone_path):
+        runner = SubprocessRunner(["/bin/hrm", clone_path])
+        for _ in runner.run():
+            yield
+
+    env = {}
+    env.update(os.environ)
+    env['PYTHONPATH'] = mypkg
+
+    cmdline = [sys.executable, hgzilla,
+               "fork",
+               "--hg-path", "/usr/local/bin/hg",
+               "--bug", bug_id,
+               "--clone-dir", clone_path,
+               "--root-repo-dir", root_repo_path]
+
+    if attach_id:
+        cmdline.extend(["--attachment", attach_id])
+
+    runner = SubprocessRunner(cmdline, env=env,
+                              raise_on_error=False)
+
+    ensoapi.display_message("Please wait.")
+
+    for _ in runner.run():
+        yield
+
+    if runner.returncode:
+        ensoapi.display_message(runner.output_text)
+    else:
+        _runAppleScript('tell application "Terminal" to activate')
+        _runAppleScript('tell application "Terminal" '
+                        'to do script "cd %s;source bin/activate"' %
+                        clone_path)
+        ensoapi.display_message("Repository created at %s." % clone_path)
+
 def cmd_latest_jetpack_rev(ensoapi):
     "Dumps info about the latest Jetpack revision."