comparison my-enso-commands.py @ 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
comparison
equal deleted inserted replaced
18:400a9edd79ad 19:854065d98c3d
965 "Generates a new issue of The Daily Edition with old news." 965 "Generates a new issue of The Daily Edition with old news."
966 966
967 return _gen_daily_edition(ensoapi) 967 return _gen_daily_edition(ensoapi)
968 968
969 class SubprocessRunner(object): 969 class SubprocessRunner(object):
970 def __init__(self, cmdline): 970 def __init__(self, cmdline, env=None, raise_on_error=True):
971 self.cmdline = cmdline 971 self.cmdline = cmdline
972 self.output_text = None 972 self.output_text = None
973 self.env = env
974 self.raise_on_error = raise_on_error
973 975
974 def run(self): 976 def run(self):
975 output_fd, output_name = tempfile.mkstemp() 977 output_fd, output_name = tempfile.mkstemp()
976 978
977 popen = subprocess.Popen(self.cmdline, 979 popen = subprocess.Popen(self.cmdline,
978 stdout = output_fd, 980 stdout = output_fd,
979 stderr = subprocess.STDOUT) 981 stderr = subprocess.STDOUT,
982 env = self.env)
980 983
981 while popen.poll() is None: 984 while popen.poll() is None:
982 yield 985 yield
983 986
984 os.close(output_fd) 987 os.close(output_fd)
985 output_text = open(output_name, "r").read().strip() 988 output_text = open(output_name, "r").read().strip()
986 os.remove(output_name) 989 os.remove(output_name)
987 990
988 if popen.returncode: 991 self.returncode = popen.returncode
992
993 if popen.returncode and self.raise_on_error:
989 raise Exception("Process failed with output %s" % 994 raise Exception("Process failed with output %s" %
990 repr(output_text)) 995 repr(output_text))
991 996
992 self.output_text = output_text 997 self.output_text = output_text
998
999 def infer_patch_from_url(url):
1000 """
1001 >>> infer_patch_from_url("blah")
1002 (None, None)
1003
1004 >>> url = "https://bugzilla.mozilla.org/show_bug.cgi?id=556075"
1005 >>> infer_patch_from_url(url)
1006 ('556075', None)
1007 """
1008
1009 import re
1010
1011 BUG_RE = r"https:\/\/bugzilla.mozilla.org\/show_bug.cgi\?id=([0-9]+)"
1012 bug_re = re.compile(BUG_RE)
1013
1014 match = bug_re.match(url)
1015 if match:
1016 return (match.group(1), None)
1017 return (None, None)
1018
1019 def cmd_try_jetpack_bugzilla_patch(ensoapi):
1020 """Fetches the patch for the currently selected bug or attachment URL
1021 and creates a clone of the Jetpack repository that has it applied."""
1022
1023 url = ensoapi.get_selection().get("text", "").strip()
1024 if not url:
1025 ensoapi.display_message("Please select a bug or attachment URL.")
1026 return
1027
1028 bug_id, attach_id = infer_patch_from_url(url)
1029 if not bug_id:
1030 ensoapi.display_message("That does not appear to be a bug or "
1031 "attachment URL.")
1032 return
1033
1034 mypkg = os.path.expanduser("~/Documents/MyPythonPackage")
1035 hgzilla = mypkg + "/scripts/hgzilla.py"
1036 root_repo_path = os.path.expanduser("~/Documents/jetpack-sdk")
1037 clone_name = "jetpack-sdk-bug-%s" % bug_id
1038 clone_path = os.path.expanduser("~/Documents/" + clone_name)
1039
1040 if os.path.exists(clone_path):
1041 runner = SubprocessRunner(["/bin/hrm", clone_path])
1042 for _ in runner.run():
1043 yield
1044
1045 env = {}
1046 env.update(os.environ)
1047 env['PYTHONPATH'] = mypkg
1048
1049 cmdline = [sys.executable, hgzilla,
1050 "fork",
1051 "--hg-path", "/usr/local/bin/hg",
1052 "--bug", bug_id,
1053 "--clone-dir", clone_path,
1054 "--root-repo-dir", root_repo_path]
1055
1056 if attach_id:
1057 cmdline.extend(["--attachment", attach_id])
1058
1059 runner = SubprocessRunner(cmdline, env=env,
1060 raise_on_error=False)
1061
1062 ensoapi.display_message("Please wait.")
1063
1064 for _ in runner.run():
1065 yield
1066
1067 if runner.returncode:
1068 ensoapi.display_message(runner.output_text)
1069 else:
1070 _runAppleScript('tell application "Terminal" to activate')
1071 _runAppleScript('tell application "Terminal" '
1072 'to do script "cd %s;source bin/activate"' %
1073 clone_path)
1074 ensoapi.display_message("Repository created at %s." % clone_path)
993 1075
994 def cmd_latest_jetpack_rev(ensoapi): 1076 def cmd_latest_jetpack_rev(ensoapi):
995 "Dumps info about the latest Jetpack revision." 1077 "Dumps info about the latest Jetpack revision."
996 1078
997 path = "~/Documents/MyPythonPackage/scripts/get_latest_feed_entry.py" 1079 path = "~/Documents/MyPythonPackage/scripts/get_latest_feed_entry.py"