Download:
child 21:36132d921a97
parent 19:cee789a8154e
20:b83b3319ff6b draft
Anton Shestakov <av6@dwimlabs.net>, Wed, 21 Nov 2018 01:27:49 +0800
plugin: use subprocess.Popen instead of os.popen, handle stderr

1 файлов изменено, 29 вставок(+), 26 удалений(-) [+]
plugin.py file | annotate | diff | comparison | revisions
--- a/plugin.py Wed Nov 21 00:22:32 2018 +0800
+++ b/plugin.py Wed Nov 21 01:27:49 2018 +0800
@@ -32,6 +32,7 @@
from supybot.commands import *
import os
+import subprocess
import urllib
import logging
@@ -109,11 +110,12 @@
self.path = path
def run(self, args):
- fd = os.popen('HGPLAIN=1 %s %s' % (self.path, args))
- out = fd.read()
- fd.close()
-
- return out
+ with open(os.devnull) as null:
+ p = subprocess.Popen([self.path] + args, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, stdin=null,
+ env={'HGPLAIN': '1', 'HGENCODING': 'UTF-8'})
+ out, err = p.communicate()
+ return (out.decode('utf-8'), err.decode('utf-8'))
class Mercurial(callbacks.Plugin):
"""Add the help for "@plugin help Mercurial" here
@@ -142,24 +144,26 @@
elif len(out) > 1:
out[1] = ircutils.bold(out[1])
out = out[:10]
-
- if out and out[0].startswith('Mercurial Distributed SCM'):
- out = ['"%s": unknown command' % cmd]
return out
- out = self.hg.run('help ' + cmd)
- irc.replies(fmt(out))
+ out, err = self.hg.run(['help', cmd])
+ if err:
+ irc.reply(err.splitlines()[0])
+ else:
+ irc.replies(fmt(out))
hghelp = wrap(hghelp, ['text'])
- def changelog(self, repo, rev):
+ def changelog(self, irc, repo, rev):
tmpl = '{rev}:{node|short}\n{date|age}\n{author|person}\n{files}\n{desc}'
- cmd = "-R %s log -v --limit 1 --template '%s' -r %s" \
- % (repo, tmpl, rev)
- out = self.hg.run(cmd)
+ out, err = self.hg.run([
+ '-R', repo, 'log', '-r', rev, '-l', '1', '--template', tmpl, '-v'
+ ])
+ if err:
+ return irc.reply(err.splitlines()[0])
if not out:
- return ['No result found.']
+ return irc.reply('no changesets found')
lines = [x for x in out.strip().splitlines() if x]
node, date, user, files = lines[:4]
@@ -169,20 +173,18 @@
lines = ['%s %s %s' % (node, user, date)] + lines
- baseurl = self.hg.run("-R %s paths default" % repo)
+ baseurl, err = self.hg.run(['-R', repo, 'paths', 'default'])
url = "%s/rev/%s" % (baseurl.strip("\n/"), rev)
- return lines[:2] + [url]
+ irc.replies(lines[:2] + [url])
def crew(self, irc, msg, args, rev):
"gets the changelog message for the given revision of the crew repository"
- cl = self.changelog('/home/brendan/hg/mirror/mercurial/crew', rev)
- irc.replies(cl)
+ self.changelog(irc, '/home/brendan/hg/mirror/mercurial/crew', rev)
crew = wrap(crew, ['revision'])
def main(self, irc, msg, args, rev):
"gets the changelog message for the given revision of the crew repository"
- cl = self.changelog('/home/hg/repos/hg', rev)
- irc.replies(cl)
+ self.changelog(irc, '/home/hg/repos/hg', rev)
main = wrap(main, ['revision'])
def bts(self, irc, msg, args, issue):
@@ -208,11 +210,11 @@
def glossary(self, irc, msg, args, term):
"""usage: glossary <term>"""
- raw = self.hg.run('help glossary').splitlines()
+ raw, err = self.hg.run(['help', 'glossary'])
found = False
done = False
answer = []
- for line in raw:
+ for line in raw.splitlines():
if not found and line.lower().startswith(' ' + term.lower()):
found = True
elif found and not (line.startswith(' ') or not line):
@@ -222,9 +224,10 @@
if found and not done and line:
answer.append(line)
- if not answer:
- answer = ['', 'no match found']
- irc.reply(' '.join(answer[1:]))
+ if answer:
+ irc.replies(answer[1:], joiner=' ')
+ else:
+ irc.reply('no match found')
glossary = wrap(glossary, ['text'])
Class = Mercurial