Mercurial > my-enso-commands
view 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 |
line wrap: on
line source
# -*-Mode:python-*- import os import tempfile import sys import subprocess import webbrowser import urllib import threading import logging from StringIO import StringIO import simplejson as json def _do_service(ensoapi, serviceName, text=None): import AppKit if not text: text = ensoapi.get_selection().get("text", "").strip() if not text: ensoapi.display_message("No text selection!") return pb = AppKit.NSPasteboard.pasteboardWithName_("EnsoServiceContent") pb.declareTypes_owner_( [AppKit.NSStringPboardType], None ) try: if not pb.setString_forType_( text, AppKit.NSStringPboardType ): raise Exception( "Failed to set pasteboard data." ) if not AppKit.NSPerformService( serviceName, pb ): raise Exception( "Failed to perform service." ) finally: pass def cmd_define(ensoapi, word=None): "Look up the given word in the dictionary." _do_service(ensoapi, "Look Up in Dictionary", word) DEFAULT_COUNTRY_CODE = "1" DEFAULT_AREA_CODE = "415" def number_to_pstn(number, default_country_code=DEFAULT_COUNTRY_CODE, default_area_code=DEFAULT_AREA_CODE): """ Converts a colloquial phone number to a pstn. >>> number_to_pstn('123.456.7890', default_country_code='1') '+11234567890' >>> number_to_pstn('456.7890', default_country_code='1', ... default_area_code='614') '+16144567890' >>> number_to_pstn('5') is None True """ DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] chars = "".join([char for char in number if char in DIGITS]) if len(chars) == 11: pass if len(chars) == 10: chars = default_country_code + chars elif len(chars) == 7: chars = default_country_code + default_area_code + chars else: chars = None if chars: return "+%s" % chars return None def cmd_skype(ensoapi, number=None): "Call the given number with Skype." if not number: number = ensoapi.get_selection().get("text", "").strip() if not number: ensoapi.display_message("No text selection!") return number = number_to_pstn(number) if not number: ensoapi.display_message("That doesn't look like a phone number.") return _runAppleScript('tell application "Skype" ' 'to send command "CALL %s" ' 'script name "Enso"' % number) def cmd_speak(ensoapi, what=None): "Speak the given text out loud." _do_service(ensoapi, "Speech/Start Speaking Text", what) def cmd_summarize(ensoapi): "Summarize the selected text." _do_service(ensoapi, "Summarize") def cmd_spotlight(ensoapi, query=None): "Search your computer for the given query." _do_service(ensoapi, "Spotlight", query) def cmd_line_count(ensoapi): "Count the number of lines in your selection." seldict = ensoapi.get_selection() ensoapi.display_message( "Selection is %s lines." % len(seldict.get("text","").splitlines()) ) def _get_filelike_timestamp(): import time return time.strftime("%Y%m%d%H%M%S") def _get_current_app(): import AppKit app = AppKit.NSWorkspace.sharedWorkspace().activeApplication() return app.objectForKey_("NSApplicationBundleIdentifier") def maybe_export_photoshop_clipboard(func): def wrapper(*args, **kwargs): if _get_current_app() == 'com.adobe.Photoshop': _runAppleScript('tell application "Adobe Photoshop CS3" ' 'to copy merged') _runAppleScript('tell application "Finder" to activate') retval = func(*args, **kwargs) _runAppleScript('tell application "Adobe Photoshop CS3" ' 'to activate') return retval return func(*args, **kwargs) return wrapper @maybe_export_photoshop_clipboard def _get_clipboard_img_datafile(): import AppKit pb = AppKit.NSPasteboard.generalPasteboard() typestr = pb.availableTypeFromArray_([AppKit.NSTIFFPboardType, AppKit.NSPICTPboardType]) tiffbytes = None if typestr == AppKit.NSTIFFPboardType: tiffbytes = pb.dataForType_(AppKit.NSTIFFPboardType).bytes() elif typestr == AppKit.NSPICTPboardType: img = AppKit.NSImage.alloc().initWithPasteboard_(pb) tiffbytes = img.TIFFRepresentation().bytes() if tiffbytes: return StringIO(str(tiffbytes)) return None def image_command(func): def wrapper(ensoapi): import Image datafile = _get_clipboard_img_datafile() if datafile: img = Image.open(datafile) return func(ensoapi, img) else: files = ensoapi.get_selection().get("files", []) if files: if len(files) == 1: filename = files[0] img = None try: img = Image.open(filename) except IOError, e: ensoapi.display_message("An error occurred: %s." % e) if img: return func(ensoapi, img) else: ensoapi.display_message("More than one file selected.") else: ensoapi.display_message("No image in clipboard, and no " "file selected.") return wrapper @image_command def cmd_get_image_size(ensoapi, img): outputfile = StringIO() img.save(outputfile, "PNG") png_size = outputfile.tell() outputfile = StringIO() img.save(outputfile, "JPEG", quality=90) jpg_size = outputfile.tell() ensoapi.display_message("png size: %d bytes. jpg-90 size: %d bytes." % (png_size, jpg_size)) def cmd_mute(ensoapi): """mute the speaker.""" _runAppleScript('set volume output muted true') ensoapi.display_message("Speaker muted.") def cmd_unmute(ensoapi): """unmute the speaker.""" _runAppleScript('set volume output muted false') ensoapi.display_message("Speaker unmuted.") def cmd_unupload_image(ensoapi): url = ensoapi.get_selection().get("text", "") if url: if url.startswith(REMOTE_UPLOAD_URL): filename = url[len(REMOTE_UPLOAD_URL):] localfile = os.path.join(LOCAL_UPLOAD_DIR, filename) # It's just easier to upload a truncated file than it is # to remove the file remotely. open(localfile, "w").close() popen = subprocess.Popen( ["scp", localfile, "%s%s" % (REMOTE_UPLOAD_DIR, filename)] ) while popen.poll() is None: yield if popen.returncode == 0: os.remove(localfile) ensoapi.display_message("Image removed.") else: ensoapi.display_message("An error occurred.") else: ensoapi.display_message("URL is not an upload URL.") else: ensoapi.display_message("No selection!") LOCAL_UPLOAD_DIR = "/Users/varmaa/Archive/toolness-images/" REMOTE_UPLOAD_DIR = "toolness.com:/home/varmaa/toolness.com/images/" REMOTE_UPLOAD_URL = "http://www.toolness.com/images/" @image_command def cmd_upload_image(ensoapi, img): filename = "%s.jpg" % _get_filelike_timestamp() localfile = os.path.join(LOCAL_UPLOAD_DIR, filename) img.save(localfile, quality=90) ensoapi.display_message("Uploading image...") popen = subprocess.Popen( ["scp", localfile, "%s%s" % (REMOTE_UPLOAD_DIR, filename)] ) while popen.poll() is None: yield if popen.returncode == 0: webbrowser.open("%s%s" % (REMOTE_UPLOAD_URL, filename)) else: ensoapi.display_message("An error occurred.") class ThreadedFunc(threading.Thread): def __init__(self, target): self.__target = target threading.Thread.__init__(self) self.__success = False self.__retval = None self.start() def run(self): self.__retval = self.__target() self.__success = True def wasSuccessful(self): if self.isAlive(): raise Exception( "Thread not finished" ) return self.__success def getRetval(self): if self.isAlive(): raise Exception( "Thread not finished" ) return self.__retval def htmlifier(func): def wrapper(ensoapi): seldict = ensoapi.get_selection() text = seldict.get("text", "") html = seldict.get("html", text) if not text: ensoapi.display_message("No selection!") else: result = func(ensoapi, html) ensoapi.set_selection( {"html":result, "text":result} ) return wrapper @htmlifier def cmd_bold(ensoapi, text): return "<b>%s</b>" % text @htmlifier def cmd_italics(ensoapi, text): return "<i>%s</i>" % text @htmlifier def cmd_monospace(ensoapi, text): return "<tt>%s</tt>" % text def cmd_normalize(ensoapi): normal_template = "<span style=\"font-weight: normal;\">%s</span>" seldict = ensoapi.get_selection() text = seldict.get("text", "") if not text: ensoapi.display_message("No selection!") else: ensoapi.set_selection( {"html":normal_template % text, "text":text} ) def make_get_url_func(url): def get_url(): import urllib2 f = urllib2.urlopen(url) return f.read() return get_url def get_weather(xml_data): """ Shows the weather for the given place. """ import elementtree.ElementTree as ET page = ET.fromstring(xml_data) weather = page.find( "weather/current_conditions" ) return { 'f' : weather.find( "temp_f" ).get( "data" ), 'c' : weather.find( "temp_c" ).get( "data" ), 'humidity' : weather.find( "humidity" ).get( "data" ), 'wind' : weather.find( "wind_condition" ).get( "data" ) } def test_get_weather(): xml_data = """<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0"><forecast_information><city data="Chicago, IL"/><postal_code data="60657"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2008-04-07"/><current_date_time data="2008-04-07 06:38:00 +0000"/><unit_system data="US"/></forecast_information><current_conditions><condition data="Cloudy"/><temp_f data="57"/><temp_c data="14"/><humidity data="Humidity: 47%"/><icon data="/images/weather/cloudy.gif"/><wind_condition data="Wind: N at 0 mph"/></current_conditions><forecast_conditions><day_of_week data="Today"/><low data="40"/><high data="56"/><icon data="/images/weather/cloudy.gif"/><condition data="Cloudy"/></forecast_conditions><forecast_conditions><day_of_week data="Tue"/><low data="45"/><high data="54"/><icon data="/images/weather/thunderstorm.gif"/><condition data="Thunderstorm"/></forecast_conditions><forecast_conditions><day_of_week data="Wed"/><low data="40"/><high data="54"/><icon data="/images/weather/mostly_sunny.gif"/><condition data="Partly Sunny"/></forecast_conditions><forecast_conditions><day_of_week data="Thu"/><low data="38"/><high data="49"/><icon data="/images/weather/chance_of_rain.gif"/><condition data="Chance of Showers"/></forecast_conditions></weather></xml_api_reply>""" assert get_weather(xml_data) == {'c': '14', 'humidity': 'Humidity: 47%', 'wind': 'Wind: N at 0 mph', 'f': '57'} def cmd_weather(ensoapi, place="san francisco"): zipcode = cmd_weather.places[place] url = "http://www.google.com/ig/api?weather=%s" % zipcode thread = ThreadedFunc(make_get_url_func(url)) while thread.isAlive(): yield weather_xml = thread.getRetval() if not weather_xml: ensoapi.display_message("An error occurred when getting the weather.") else: wdict = get_weather(weather_xml) wdict["place"] = place ensoapi.display_message(u"In %(place)s it is " u"%(f)s\u00b0F/%(c)s\u00b0C, " u"%(humidity)s, %(wind)s." % wdict) cmd_weather.places = { "san francisco" : "94115", "chicago" : "60640", "mountain view" : "94043" } cmd_weather.valid_args = cmd_weather.places.keys() def cmd_what_is_my_ip(ensoapi): thread = ThreadedFunc(make_get_url_func("http://www.toolness.com/ip/")) while thread.isAlive(): yield ensoapi.display_message("%s." % thread.getRetval()) def cmd_character_count(ensoapi): text = ensoapi.get_selection().get("text", "").strip() if not text: ensoapi.display_message( "No selection." ) ensoapi.display_message( "%d characters." % len(text) ) def cmd_email_to_me(ensoapi): text = ensoapi.get_selection().get("text", "").strip() if not text: ensoapi.display_message( "No selection." ) else: server = "mail.toolness.com" port = 587 username = "no-reply@toolness.com" password = "4p5o3j45ot%$" mailfrom = "varmaa@gmail.com" rcpttos = ["varmaa@gmail.com"] text = text.encode("ascii", "xmlcharrefreplace") subject = text.splitlines()[0] text = ("This message was sent by Enso's " "'email to' command.\n\n" + text) data = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s" % \ (mailfrom, ", ".join(rcpttos), subject, text) def sendmail(): import smtplib try: svr = smtplib.SMTP( server, port ) svr.login( username, password ) svr.sendmail( mailfrom, rcpttos, data ) svr.quit() except: success[0] = False raise thread = ThreadedFunc(sendmail) ensoapi.display_message( "Sending message..." ) while thread.isAlive(): yield if thread.wasSuccessful(): ensoapi.display_message( "Message sent." ) else: ensoapi.display_message( "An error occurred when sending the message." ) def cmd_date(ensoapi): import time ensoapi.display_message( "%s" % time.asctime() ) def cmd_insert_date(ensoapi): import time ensoapi.set_selection( time.asctime() ) class WebSearchCmd(object): def __init__(self, url_template): self._url_template = url_template def __call__(self, ensoapi, query=None): if not query: query = ensoapi.get_selection().get("text", u"") query = query.strip() if not query: ensoapi.display_message( "No query." ) return query = urllib.quote( query.encode("utf-8") ) webbrowser.open( self._url_template % {"query" : query} ) cmd_wiki = WebSearchCmd("http://en.wikipedia.org/wiki/Special:Search?search=%(query)s") cmd_wowhead = WebSearchCmd("http://www.wowhead.com/?search=%(query)s") cmd_amaz = WebSearchCmd("http://www.amazon.com/exec/obidos/search-handle-url/index%%3Dblended%%26field-keywords%%3D%(query)s%%26store-name%%3Dall-product-search") cmd_imdb = WebSearchCmd("http://www.imdb.com/find?s=all&q=%(query)s&x=0&y=0") cmd_mdc = WebSearchCmd("http://www.google.com/search?hl=en&q=%(query)s+site%%3Adeveloper.mozilla.org&btnG=Google+Search") cmd_mxr = WebSearchCmd("http://mxr.mozilla.org/mozilla/search?string=%(query)s") cmd_phonebook = WebSearchCmd("https://ldap.mozilla.org/phonebook/#search/%(query)s") # For help on this one see http://www.squarefree.com/bugzilla/quicksearch-help.html cmd_bugzilla = WebSearchCmd("https://bugzilla.mozilla.org/buglist.cgi?quicksearch=%(query)s") def cmd_farewell(ensoapi): import AppKit app = AppKit.NSApplication.sharedApplication() app.terminate_(None) class Launcher(object): def __init__(self): import Foundation import AppKit self._targets = {} query = Foundation.NSMetadataQuery.alloc().init() queryString = ( "((kMDItemKind == \"Application\") " " and (kMDItemSupportFileType != \"MDSystemFile\"))" ) queryString += " or (kMDItemKind == \"Mac OS X Preference Pane\")" predicate = Foundation.NSPredicate.predicateWithFormat_( queryString ) query.setPredicate_( predicate ) if query.startQuery() != True: raise Exception( "startQuery() failed." ) self._query = query self._workspace = AppKit.NSWorkspace.sharedWorkspace() self._targets = {} def get_namespace(self): return self._targets.keys() def get_target(self, name): return self._targets[name] def update(self): query = self._query while query.isGathering(): yield # TODO: Modify this so we just get notified whenever the query # results change instead of constantly "polling" every time # the quasimode starts. resultList = [] targets = {} query.disableUpdates() numresults = query.resultCount() BATCH_SIZE = 10 CUSTOM_TARGETS = { 'computer': '/', 'home': '~', 'desktop': '~/Desktop', 'documents': '~/Documents', 'downloads': '~/Downloads', 'applications': '/Applications', } for i in range( numresults ): result = query.resultAtIndex_( i ) fsname = result.valueForAttribute_("kMDItemFSName") name = result.valueForAttribute_("kMDItemDisplayName") kind = result.valueForAttribute_("kMDItemKind") if name: name = name.lower() itempath = result.valueForAttribute_("kMDItemPath") if kind == "Mac OS X Preference Pane": name += " preferences" target = ShellOpenLaunchableTarget(itempath) else: target = AppLaunchableTarget(self._workspace, itempath) resultList.append(name) targets[name] = target if i / BATCH_SIZE == i / float(BATCH_SIZE): yield #print "total results: %s" % numresults query.enableUpdates() for name in CUSTOM_TARGETS: path = os.path.expanduser(CUSTOM_TARGETS[name]) targets[name] = ShellOpenLaunchableTarget(path) self._targets = targets class AppLaunchableTarget(object): def __init__(self, workspace, path): self._workspace = workspace self._path = path def launch(self, with_files=None): if with_files: for filename in with_files: self._workspace.openFile_withApplication_( filename, self._path ) else: self._workspace.launchApplication_( self._path ) def can_launch_with(self): return True class ShellOpenLaunchableTarget(object): def __init__(self, path): self._path = path def launch(self): subprocess.Popen( ["open", self._path] ) def can_launch_with(self): return False class OpenCommand(object): """ Opens an application, folder, or URL. """ def __init__(self, launcher): self.launcher = launcher self._isFetchingArgs = False def on_quasimode_start(self): if self._isFetchingArgs: return self._isFetchingArgs = True for _ in self.launcher.update(): yield self.valid_args = self.launcher.get_namespace() self._isFetchingArgs = False valid_args = [] def __call__(self, ensoapi, target=None): if not target: seldict = ensoapi.get_selection() if seldict.get("files"): for file in seldict["files"]: subprocess.Popen( ["open", file] ) elif seldict.get("text"): filename = seldict["text"].strip() if os.path.isabs(filename): subprocess.Popen( ["open", filename] ) else: webbrowser.open(filename) else: self.launcher.get_target(target).launch() class OpenWithCommand(object): """ Opens the selected file(s) with a particular application. """ def __init__(self, launcher): self.launcher = launcher def _get_valid_args(self): return self.launcher.get_namespace() valid_args = property(_get_valid_args) def __call__(self, ensoapi, target): files = ensoapi.get_selection().get("files", []) targ = self.launcher.get_target(target) if not files: ensoapi.display_message("No files selected!") elif not targ.can_launch_with(): ensoapi.display_message("Can't open files with %s." % target) else: targ.launch(files) cmd_go = OpenCommand(Launcher()) cmd_open = cmd_go cmd_bust_with = OpenWithCommand(cmd_open.launcher) def cmd_run_python(ensoapi): """ runs the selected text as python code. """ text = ensoapi.get_selection().get("text","") if not text: ensoapi.display_message("No text selection!") return output_fd, output_name = tempfile.mkstemp() # TODO: This is for OSX only; will have to figure out what to do # for other platforms. cmd = text.replace( "\r", "\n" ) try: compile( cmd, "<selected text>", "eval" ) is_eval = True except: is_eval = False if is_eval: cmd = "print %s," % cmd cmd = ("try:\n" " from autoimp import *\n" "except ImportError:\n" " pass\n\n" + cmd ) popen = subprocess.Popen( [ sys.executable, "-c", cmd ], stdout = output_fd, stderr = subprocess.STDOUT, ) while popen.poll() is None: yield os.close(output_fd) output_text = open(output_name, "r").read().strip() if output_text: seldict = {"text" : "\n".join([text, output_text])} ensoapi.set_selection(seldict) else: if popen.returncode == 0: ensoapi.display_message("Code run successfully.") else: ensoapi.display_message("An error occurred.") os.remove(output_name) def cmd_doctest(ensoapi): """ python doctests the selected section of plain text. """ text = ensoapi.get_selection().get("text","") if not text: ensoapi.display_message("No text selection!") return # TODO: This is for OSX only; will have to figure out what to do # for other platforms. new_text = text.replace( "\r", "\n" ) fd, source_name = tempfile.mkstemp() os.close(fd) open(source_name, "wb").write(new_text) output_fd, output_name = tempfile.mkstemp() cmd = ("import doctest; doctest.testfile(%s, " "module_relative=False)" % repr(source_name)) popen = subprocess.Popen( [ sys.executable, "-c", cmd ], stdout = output_fd, stderr = subprocess.STDOUT, ) while popen.poll() is None: yield os.close(output_fd) if popen.returncode == 0: output_text = open(output_name, "r").read() if output_text: seldict = {"text":text + output_text} ensoapi.set_selection(seldict) else: ensoapi.display_message("All doctests successful!") else: ensoapi.display_message("An error occurred.") os.remove(source_name) os.remove(output_name) def cmd_wow_firefox(ensoapi): popen = subprocess.Popen(["/Users/varmaa/bin/wow-firefox"]) def cmd_work_firefox(ensoapi): popen = subprocess.Popen(["/Users/varmaa/bin/work-firefox"]) def cmd_mozpics_firefox(ensoapi): popen = subprocess.Popen(["/Users/varmaa/bin/mozpics-firefox"]) def cmd_firefox(ensoapi): popen = subprocess.Popen(["/Users/varmaa/bin/personal-firefox"]) def cmd_nosetest(ensoapi): """ runs nosetests on the selected region of python code. """ text = ensoapi.get_selection().get("text","") if not text: ensoapi.display_message("No text selection!") return # TODO: This is for OSX only; will have to figure out what to do # for other platforms. new_text = text.replace( "\r", "\n" ) fd, source_name = tempfile.mkstemp(suffix=".py") os.close(fd) open(source_name, "wb").write(new_text) output_fd, output_name = tempfile.mkstemp() popen = subprocess.Popen( [ "/usr/local/bin/nosetests", source_name, "--with-doctest" ], stdout = output_fd, stderr = subprocess.STDOUT, ) while popen.poll() is None: yield os.close(output_fd) output_text = open(output_name, "r").read() if output_text: if popen.returncode == 0: lines = output_text.splitlines() ensoapi.display_message(lines[2]) else: seldict = {"text":text + output_text} ensoapi.set_selection(seldict) else: raise AssertionError( "nosetests didn't return anything!" ) os.remove(source_name) os.remove(output_name) def cmd_markdown_to_html(ensoapi): """ Converts the plaintext markdown selection to plaintext HTML source. """ import markdown2 text = ensoapi.get_selection().get("text", "") html = markdown2.markdown(text) ensoapi.set_selection({"text": html}) def cmd_unquote(ensoapi): """ Removes a '<' at the beginning of every line. """ text = ensoapi.get_selection().get("text", "") lines = ["%s\n" % line[2:] for line in text.splitlines() if line.startswith("> ")] ensoapi.set_selection({"text": ''.join(lines)}) def cmd_quote(ensoapi): """ Puts a '>' at the beginning of every line. """ text = ensoapi.get_selection().get("text", "") lines = ["> %s\n" % line for line in text.splitlines()] ensoapi.set_selection({"text": ''.join(lines)}) def cmd_markdown(ensoapi): """ Converts the plaintext markdown selection to rich text. """ import markdown2 text = ensoapi.get_selection().get("text", "") html = markdown2.markdown(text) ensoapi.set_selection({"html": html, "text": text}) def cmd_insert_html(ensoapi): ensoapi.set_selection( {"html":"<b>hi</b> <p>there</p>"} ) def _runAppleScript( script ): params = [ "osascript", "-e", script ] popen = subprocess.Popen( params ) if popen.wait(): raise Exception("Applescript failed: %s" % script) def cmd_screen_saver(ensoapi): """ Starts your screen saver. """ _runAppleScript( "tell application id \"com.apple.ScreenSaver.Engine\" " "to launch" ) def cmd_sleep(ensoapi): """ Puts your computer to sleep. """ _runAppleScript( "tell application \"Finder\" to sleep" ) def cmd_play(ensoapi): """ Plays the current iTunes song. """ _runAppleScript( "tell application \"iTunes\" to play" ) def cmd_pause(ensoapi): """ Pauses the current iTunes song. """ _runAppleScript( "tell application \"iTunes\" to pause" ) def cmd_next_track(ensoapi): """ Goes to the next track on iTunes. """ _runAppleScript( "tell application \"iTunes\" to next track" ) def cmd_previous_track(ensoapi): """ Goes to the previous track on iTunes. """ _runAppleScript( "tell application \"iTunes\" to back track" ) def cmd_html_template(ensoapi): ensoapi.set_selection({"text" : HTML_TEMPLATE}) HTML_TEMPLATE = """\ <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" media="all" href="" /> <title></title> </head> <body> </body> <script src=""></script> </html> """ def _gen_daily_edition(ensoapi, args=""): logfile = open('/tmp/daily-edition.log', 'w') popen = subprocess.Popen([ "ssh", "labs.toolness.com", "/home/varmaa/bin/publish-edition %s" % args ], stdout=logfile, stderr=subprocess.STDOUT) ensoapi.display_message("Please wait, this may take a while.") while popen.poll() is None: yield logfile.close() if popen.returncode: ensoapi.display_message("Failed to generate edition!") else: retval = subprocess.call([ "open", "-a", "/Applications/Safari.app", "http://labs.toolness.com/daily-edition/" ]) if retval: ensoapi.display_message("Opening web browser failed.") def cmd_publish_new_daily_edition(ensoapi): "Generates a new issue of The Daily Edition with new news." return _gen_daily_edition(ensoapi, "--refresh-feeds") def cmd_publish_old_daily_edition(ensoapi): "Generates a new issue of The Daily Edition with old news." return _gen_daily_edition(ensoapi) class SubprocessRunner(object): 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, env = self.env) while popen.poll() is None: yield os.close(output_fd) output_text = open(output_name, "r").read().strip() os.remove(output_name) 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." path = "~/Documents/MyPythonPackage/scripts/get_latest_feed_entry.py" url = "http://hg.mozilla.org/labs/jetpack-sdk/atom-log" cmdline = [sys.executable, os.path.expanduser(path), url] runner = SubprocessRunner(cmdline) ensoapi.display_message("Please wait.") for _ in runner.run(): yield info = json.loads(runner.output_text) text = "\n".join([" %s" % info['title'], " By %s" % info['author'], " %s" % info['link']]) html = '<a href="%s">%s</a>' % (info['link'], html_escape(info['title'])) ensoapi.set_selection({"text": text, "html": html}) # Taken from http://wiki.python.org/moin/EscapingHtml html_escape_table = { "&": "&", '"': """, "'": "'", ">": ">", "<": "<", } def html_escape(text): """Produce entities within text.""" return "".join(html_escape_table.get(c,c) for c in text)