gcmd.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. """
  2. @package gcmd
  3. @brief GRASS command interface
  4. Classes:
  5. - GException
  6. - GStdError
  7. - CmdError
  8. - SettingsError
  9. - DigitError
  10. - DBMError
  11. - NvizError
  12. - Popen (from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554)
  13. - Command
  14. - CommandThread
  15. (C) 2007-2008 by the GRASS Development Team
  16. This program is free software under the GNU General Public
  17. License (>=v2). Read the file COPYING that comes with GRASS
  18. for details.
  19. @author Jachym Cepicky
  20. Martin Landa <landa.martin gmail.com>
  21. @date 2007-2008
  22. """
  23. import os
  24. import sys
  25. import time
  26. import errno
  27. import signal
  28. import wx
  29. try:
  30. import subprocess
  31. except:
  32. compatPath = os.path.join(globalvar.ETCWXDIR, "compat")
  33. sys.path.append(compatPath)
  34. import subprocess
  35. if subprocess.mswindows:
  36. from win32file import ReadFile, WriteFile
  37. from win32pipe import PeekNamedPipe
  38. import msvcrt
  39. else:
  40. import select
  41. import fcntl
  42. from threading import Thread
  43. import globalvar
  44. grassPath = os.path.join(globalvar.ETCDIR, "python")
  45. sys.path.append(grassPath)
  46. import grass
  47. import utils
  48. from debug import Debug as Debug
  49. class GException(Exception):
  50. """Generic exception"""
  51. def __init__(self, message, title=_("Error"), parent=None):
  52. self.message = message
  53. self.parent = parent
  54. self.title = title
  55. def __str__(self):
  56. dlg = wx.MessageDialog(parent=self.parent,
  57. caption=self.title,
  58. message=self.message,
  59. style=wx.ICON_ERROR | wx.CENTRE)
  60. dlg.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass_error.ico'), wx.BITMAP_TYPE_ICO))
  61. if self.parent:
  62. dlg.CentreOnParent()
  63. else:
  64. dlg.CentreOnScreen()
  65. dlg.ShowModal()
  66. return ''
  67. class GStdError(GException):
  68. """Generic exception"""
  69. def __init__(self, message, title=_("Error"), parent=None):
  70. GException.__init__(self, message, title=title, parent=parent)
  71. class CmdError(GException):
  72. """Exception used for GRASS commands.
  73. See Command class (command exits with EXIT_FAILURE,
  74. G_fatal_error() is called)."""
  75. def __init__(self, cmd, message, parent=None):
  76. self.cmd = cmd
  77. GException.__init__(self, message,
  78. title=_("Error in command execution %s" % self.cmd[0]),
  79. parent=parent)
  80. class SettingsError(GException):
  81. """Exception used for GRASS settings, see
  82. gui_modules/preferences.py."""
  83. def __init__(self, message, parent=None):
  84. GException.__init__(self, message,
  85. title=_("Preferences error"),
  86. parent=parent)
  87. class DigitError(GException):
  88. """Exception raised during digitization session"""
  89. def __init__(self, message, parent=None):
  90. GException.__init__(self, message,
  91. title=_("Vector digitizer error"),
  92. parent=parent)
  93. class DBMError(GException):
  94. """Attribute Table Manager exception class"""
  95. def __init__(self, message, parent=None):
  96. GException.__init__(self, message,
  97. title=_("Attribute table manager error"),
  98. parent=parent)
  99. class NvizError(GException):
  100. """Nviz exception class"""
  101. def __init__(self, message, parent=None):
  102. GException.__init__(self, message,
  103. title=_("Nviz error"),
  104. parent=parent)
  105. class Popen(subprocess.Popen):
  106. """Subclass subprocess.Popen"""
  107. def recv(self, maxsize=None):
  108. return self._recv('stdout', maxsize)
  109. def recv_err(self, maxsize=None):
  110. return self._recv('stderr', maxsize)
  111. def send_recv(self, input='', maxsize=None):
  112. return self.send(input), self.recv(maxsize), self.recv_err(maxsize)
  113. def get_conn_maxsize(self, which, maxsize):
  114. if maxsize is None:
  115. maxsize = 1024
  116. elif maxsize < 1:
  117. maxsize = 1
  118. return getattr(self, which), maxsize
  119. def _close(self, which):
  120. getattr(self, which).close()
  121. setattr(self, which, None)
  122. def kill(self):
  123. """Try to kill running process"""
  124. if subprocess.mswindows:
  125. import win32api
  126. handle = win32api.OpenProcess(1, 0, self.pid)
  127. return (0 != win32api.TerminateProcess(handle, 0))
  128. else:
  129. try:
  130. os.kill(-self.pid, signal.SIGTERM) # kill whole group
  131. except OSError:
  132. pass
  133. if subprocess.mswindows:
  134. def send(self, input):
  135. if not self.stdin:
  136. return None
  137. try:
  138. x = msvcrt.get_osfhandle(self.stdin.fileno())
  139. (errCode, written) = WriteFile(x, input)
  140. except ValueError:
  141. return self._close('stdin')
  142. except (subprocess.pywintypes.error, Exception), why:
  143. if why[0] in (109, errno.ESHUTDOWN):
  144. return self._close('stdin')
  145. raise
  146. return written
  147. def _recv(self, which, maxsize):
  148. conn, maxsize = self.get_conn_maxsize(which, maxsize)
  149. if conn is None:
  150. return None
  151. try:
  152. x = msvcrt.get_osfhandle(conn.fileno())
  153. (read, nAvail, nMessage) = PeekNamedPipe(x, 0)
  154. if maxsize < nAvail:
  155. nAvail = maxsize
  156. if nAvail > 0:
  157. (errCode, read) = ReadFile(x, nAvail, None)
  158. except ValueError:
  159. return self._close(which)
  160. except (subprocess.pywintypes.error, Exception), why:
  161. if why[0] in (109, errno.ESHUTDOWN):
  162. return self._close(which)
  163. raise
  164. if self.universal_newlines:
  165. read = self._translate_newlines(read)
  166. return read
  167. else:
  168. def send(self, input):
  169. if not self.stdin:
  170. return None
  171. if not select.select([], [self.stdin], [], 0)[1]:
  172. return 0
  173. try:
  174. written = os.write(self.stdin.fileno(), input)
  175. except OSError, why:
  176. if why[0] == errno.EPIPE: #broken pipe
  177. return self._close('stdin')
  178. raise
  179. return written
  180. def _recv(self, which, maxsize):
  181. conn, maxsize = self.get_conn_maxsize(which, maxsize)
  182. if conn is None:
  183. return None
  184. flags = fcntl.fcntl(conn, fcntl.F_GETFL)
  185. if not conn.closed:
  186. fcntl.fcntl(conn, fcntl.F_SETFL, flags| os.O_NONBLOCK)
  187. try:
  188. if not select.select([conn], [], [], 0)[0]:
  189. return ''
  190. r = conn.read(maxsize)
  191. if not r:
  192. return self._close(which)
  193. if self.universal_newlines:
  194. r = self._translate_newlines(r)
  195. return r
  196. finally:
  197. if not conn.closed:
  198. fcntl.fcntl(conn, fcntl.F_SETFL, flags)
  199. message = "Other end disconnected!"
  200. def recv_some(p, t=.1, e=1, tr=5, stderr=0):
  201. if tr < 1:
  202. tr = 1
  203. x = time.time()+t
  204. y = []
  205. r = ''
  206. pr = p.recv
  207. if stderr:
  208. pr = p.recv_err
  209. while time.time() < x or r:
  210. r = pr()
  211. if r is None:
  212. if e:
  213. raise Exception(message)
  214. else:
  215. break
  216. elif r:
  217. y.append(r)
  218. else:
  219. time.sleep(max((x-time.time())/tr, 0))
  220. return ''.join(y)
  221. def send_all(p, data):
  222. while len(data):
  223. sent = p.send(data)
  224. if sent is None:
  225. raise Exception(message)
  226. data = buffer(data, sent)
  227. class Command:
  228. """
  229. Run GRASS command in separate thread
  230. If stdout/err is redirected, write() method is required for the
  231. given classes.
  232. @code
  233. cmd = Command(cmd=['d.rast', 'elevation.dem'], verbose=3, wait=True)
  234. if cmd.returncode == None:
  235. print 'RUNNING?'
  236. elif cmd.returncode == 0:
  237. print 'SUCCESS'
  238. else:
  239. print 'FAILURE (%d)' % cmd.returncode
  240. @endcode
  241. @param cmd command given as list
  242. @param stdin standard input stream
  243. @param verbose verbose level [0, 3] (--q, --v)
  244. @param wait wait for child execution terminated
  245. @param rerr error handling (when CmdError raised).
  246. True for redirection to stderr, False for GUI dialog,
  247. None for no operation (quiet mode)
  248. @param stdout redirect standard output or None
  249. @param stderr redirect standard error output or None
  250. """
  251. def __init__ (self, cmd, stdin=None,
  252. verbose=None, wait=True, rerr=False,
  253. stdout=None, stderr=None):
  254. self.cmd = cmd
  255. self.stderr = stderr
  256. # hack around platform-specific extension for binaries
  257. if self.cmd[0] in globalvar.grassCmd['script']:
  258. self.cmd[0] = self.cmd[0] + globalvar.EXT_SCT
  259. else:
  260. self.cmd[0] = self.cmd[0] + globalvar.EXT_BIN
  261. #
  262. # set verbosity level
  263. #
  264. verbose_orig = None
  265. if ('--q' not in self.cmd and '--quiet' not in self.cmd) and \
  266. ('--v' not in self.cmd and '--verbose' not in self.cmd):
  267. if verbose is not None:
  268. if verbose == 0:
  269. self.cmd.append('--quiet')
  270. elif verbose == 3:
  271. self.cmd.append('--verbose')
  272. else:
  273. verbose_orig = os.getenv("GRASS_VERBOSE")
  274. os.environ["GRASS_VERBOSE"] = str(verbose)
  275. #
  276. # create command thread
  277. #
  278. self.cmdThread = CommandThread(cmd, stdin,
  279. stdout, stderr)
  280. self.cmdThread.start()
  281. if wait:
  282. self.cmdThread.join()
  283. if self.cmdThread.module:
  284. self.cmdThread.module.wait()
  285. self.returncode = self.cmdThread.module.returncode
  286. else:
  287. self.returncode = 1
  288. else:
  289. self.cmdThread.join(0.5)
  290. self.returncode = None
  291. if self.returncode is not None:
  292. Debug.msg (3, "Command(): cmd='%s', wait=%s, returncode=%d, alive=%s" % \
  293. (' '.join(cmd), wait, self.returncode, self.cmdThread.isAlive()))
  294. if rerr is not None and self.returncode != 0:
  295. if rerr is False: # GUI dialog
  296. raise CmdError(cmd=self.cmd,
  297. message="%s '%s'%s%s%s %s%s" %
  298. (_("Execution failed:"),
  299. ' '.join(self.cmd),
  300. os.linesep, os.linesep,
  301. _("Details:"),
  302. os.linesep,
  303. _("Error: ") + self.GetError()))
  304. elif rerr == sys.stderr: # redirect message to sys
  305. stderr.write("Execution failed: '%s'" % (' '.join(self.cmd)))
  306. stderr.write("%sDetails:%s%s" % (os.linesep,
  307. _("Error: ") + self.GetError(),
  308. os.linesep))
  309. else:
  310. pass # nop
  311. else:
  312. Debug.msg (3, "Command(): cmd='%s', wait=%s, returncode=?, alive=%s" % \
  313. (' '.join(cmd), wait, self.cmdThread.isAlive()))
  314. if verbose_orig:
  315. os.environ["GRASS_VERBOSE"] = verbose_orig
  316. elif os.environ.has_key("GRASS_VERBOSE"):
  317. del os.environ["GRASS_VERBOSE"]
  318. def __ReadOutput(self, stream):
  319. """Read stream and return list of lines
  320. @param stream stream to be read
  321. """
  322. lineList = []
  323. if stream is None:
  324. return lineList
  325. while True:
  326. line = stream.readline()
  327. if not line:
  328. break
  329. line = line.replace('%s' % os.linesep, '').strip()
  330. lineList.append(line)
  331. return lineList
  332. def ReadStdOutput(self):
  333. """Read standard output and return list of lines"""
  334. if self.cmdThread.stdout:
  335. stream = self.cmdThread.stdout # use redirected stream instead
  336. stream.seek(0)
  337. else:
  338. stream = self.cmdThread.module.stdout
  339. return self.__ReadOutput(stream)
  340. def ReadErrOutput(self):
  341. """Read standard error output and return list of lines"""
  342. return self.__ReadOutput(self.cmdThread.module.stderr)
  343. def __ProcessStdErr(self):
  344. """
  345. Read messages/warnings/errors from stderr
  346. @return list of (type, message)
  347. """
  348. if self.stderr is None:
  349. lines = self.ReadErrOutput()
  350. else:
  351. lines = self.cmdThread.error.strip('%s' % os.linesep). \
  352. split('%s' % os.linesep)
  353. msg = []
  354. type = None
  355. content = ""
  356. for line in lines:
  357. if len(line) == 0:
  358. continue
  359. if 'GRASS_' in line: # error or warning
  360. if 'GRASS_INFO_WARNING' in line: # warning
  361. type = "WARNING"
  362. elif 'GRASS_INFO_ERROR' in line: # error
  363. type = "ERROR"
  364. elif 'GRASS_INFO_END': # end of message
  365. msg.append((type, content))
  366. type = None
  367. content = ""
  368. if type:
  369. content += line.split(':', 1)[1].strip()
  370. else: # stderr
  371. msg.append((None, line.strip()))
  372. return msg
  373. def GetError(self):
  374. """Get error message or ''"""
  375. if not self.cmdThread.module:
  376. return _("Unable to exectute command: '%s'") % ' '.join(self.cmd)
  377. for type, msg in self.__ProcessStdErr():
  378. if type == 'ERROR':
  379. return unicode(msg, "utf-8")
  380. return ''
  381. def PrintModuleOutput(self, error=True, warning=False, message=False):
  382. """Print module errors, warnings, messages to output
  383. @param error print errors
  384. @param warning print warnings
  385. @param message print messages
  386. @return string
  387. """
  388. msgString = ""
  389. for type, msg in self.__ProcessStdErr():
  390. if type:
  391. if (type == 'ERROR' and error) or \
  392. (type == 'WARNING' and warning) or \
  393. (type == 'MESSAGE' and message):
  394. msgString += " " + type + ": " + msg + "%s" % os.linesep
  395. else:
  396. msgString += " " + msg + "%s" % os.linesep
  397. return msgString
  398. class CommandThread(Thread):
  399. """Create separate thread for command
  400. @param cmd GRASS command (given as list)
  401. @param stdin standard input stream
  402. @param stdout redirect standard output or None
  403. @param stderr redirect standard error output or None
  404. """
  405. def __init__ (self, cmd, stdin=None,
  406. stdout=sys.stdout, stderr=sys.stderr):
  407. Thread.__init__(self)
  408. self.cmd = cmd
  409. self.stdin = stdin
  410. self.stdout = stdout
  411. self.stderr = stderr
  412. self.module = None
  413. self.error = ''
  414. self._want_abort = False
  415. self.aborted = False
  416. self.setDaemon(True)
  417. # set message formatting
  418. self.message_format = os.getenv("GRASS_MESSAGE_FORMAT")
  419. os.environ["GRASS_MESSAGE_FORMAT"] = "gui"
  420. def __del__(self):
  421. if self.message_format:
  422. os.environ["GRASS_MESSAGE_FORMAT"] = self.message_format
  423. else:
  424. del os.environ["GRASS_MESSAGE_FORMAT"]
  425. def run(self):
  426. if len(self.cmd) == 0:
  427. return
  428. self.startTime = time.time()
  429. try:
  430. self.module = Popen(self.cmd,
  431. stdin=subprocess.PIPE,
  432. stdout=subprocess.PIPE,
  433. stderr=subprocess.PIPE)
  434. except OSError, e:
  435. self.error = str(e)
  436. return 1
  437. if self.stdin: # read stdin if requested ...
  438. self.module.stdin.write(self.stdin)
  439. self.module.stdin.close()
  440. # redirect standard outputs...
  441. if self.stdout or self.stderr:
  442. self.__redirect_stream()
  443. def __redirect_stream(self):
  444. """Redirect stream"""
  445. if self.stdout:
  446. # make module stdout/stderr non-blocking
  447. out_fileno = self.module.stdout.fileno()
  448. if not subprocess.mswindows:
  449. flags = fcntl.fcntl(out_fileno, fcntl.F_GETFL)
  450. fcntl.fcntl(out_fileno, fcntl.F_SETFL, flags| os.O_NONBLOCK)
  451. if self.stderr:
  452. # make module stdout/stderr non-blocking
  453. out_fileno = self.module.stderr.fileno()
  454. if not subprocess.mswindows:
  455. flags = fcntl.fcntl(out_fileno, fcntl.F_GETFL)
  456. fcntl.fcntl(out_fileno, fcntl.F_SETFL, flags| os.O_NONBLOCK)
  457. # wait for the process to end, sucking in stuff until it does end
  458. while self.module.poll() is None:
  459. if self._want_abort: # abort running process
  460. self.module.kill()
  461. self.aborted = True
  462. return
  463. if self.stdout:
  464. line = recv_some(self.module, e=0, stderr=0)
  465. self.stdout.write(line)
  466. if self.stderr:
  467. line = recv_some(self.module, e=0, stderr=1)
  468. self.stderr.write(line)
  469. if len(line) > 0:
  470. self.error = line
  471. # get the last output
  472. if self.stdout:
  473. line = recv_some(self.module, e=0, stderr=0)
  474. self.stdout.write(line)
  475. if self.stderr:
  476. line = recv_some(self.module, e=0, stderr=1)
  477. self.stderr.write(line)
  478. if len(line) > 0:
  479. self.error = line
  480. def abort(self):
  481. """Abort running process, used by main thread to signal an abort"""
  482. self._want_abort = True