Made GunPG dependency explicit
[0publish.git] / edit.py
blobeea51259beaee6a3834748f6e74db7e38e862393
1 import tempfile, os
2 from logging import info, debug
4 path_dirs = os.environ.get('PATH', '/bin:/usr/bin').split(':')
6 def available_in_path(command):
7 info("Checking for %s in $PATH", command)
8 for x in path_dirs:
9 debug("Checking for %s/%s", x, command)
10 if os.path.isfile(os.path.join(x, command)):
11 return True
12 return False
14 def edit(data):
15 fd, tmp = tempfile.mkstemp(prefix = '0publish-', suffix = '.xml')
16 try:
17 stream = os.fdopen(fd, 'w')
18 stream.write(data)
19 stream.close()
20 editor = os.environ.get('EDITOR', None)
21 if editor is None:
22 info("$EDITOR not set. Trying fallbacks...")
23 for editor in ['sensible-editor', 'nano', 'vi']: # ,'ed'] ;-)
24 if available_in_path(editor):
25 break
26 else:
27 raise Exception("No editor found. Try setting $EDITOR first.")
28 info("Editing tmp file with '%s %s'..." % (editor, tmp))
29 if os.spawnlp(os.P_WAIT, editor, editor, tmp):
30 raise Exception('Editing with $EDITOR ("%s") failed' % editor)
31 new_data = file(tmp).read()
32 finally:
33 os.unlink(tmp)
34 return new_data