/
terre_image_run_process.py
124 lines (112 loc) · 5.33 KB
/
terre_image_run_process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
"""
/***************************************************************************
TerreImage
-------------------
begin : 2015-10-28
copyright : (C) 2014 by CNES
email : alexia.mondot@c-s.fr
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os
from PyQt4.QtCore import QProcessEnvironment, QProcess
import logging
logging.basicConfig()
# create logger
logger = logging.getLogger('TerreImage_RunProcess')
logger.setLevel(logging.DEBUG)
def run_process(fused_command, read_output = False):
# print "run process", fused_command
# qprocess = QProcess()
# set_process_env(qprocess)
# code_de_retour = qprocess.execute(fused_command)
# print "code de retour", code_de_retour
# logger.info("command: ")
# logger.info(fused_command)
# logger.info("code de retour" + str(code_de_retour))
#
# # if not qprocess.waitForStarted():
# # # handle a failed command here
# # print "qprocess.waitForStarted()"
# # return
# #
# # if not qprocess.waitForReadyRead():
# # # handle a timeout or error here
# # print "qprocess.waitForReadyRead()"
# # return
# # #if not qprocess.waitForFinished(1):
# # # qprocess.kill()
# # # qprocess.waitForFinished(1)
#
# # if read_output:
#
# # logger.info("Erreur")
# code_d_erreur = qprocess.error()
# dic_err = { 0:"QProcess::FailedToStart", 1:"QProcess::Crashed", 2:"QProcess::TimedOut", 3:"QProcess::WriteError", 4:"QProcess::ReadError", 5:"QProcess::UnknownError" }
# logger.info("Code de retour: " + str(code_d_erreur))
# logger.info(dic_err[code_d_erreur])
#
# print "get output"
# output = str(qprocess.readAllStandardOutput())
# # print "output", output
# print 'end output'
process = QProcess()
process.start(fused_command)
if process.waitForStarted():
process.waitForFinished(-1)
exit_code = process.exitCode()
logger.info("Code de sortie : " + str(exit_code))
if exit_code < 0:
code_d_erreur = process.error().data
dic_err = { 0:"QProcess::FailedToStart", 1:"QProcess::Crashed", 2:"QProcess::TimedOut", 3:"QProcess::WriteError", 4:"QProcess::ReadError", 5:"QProcess::UnknownError" }
logger.info("Code erreur : " + str(code_d_erreur))
logger.info(dic_err[code_d_erreur])
result = process.readAllStandardOutput()
# print type(result), result
error = process.readAllStandardError().data()
# print repr(error)
if not error == "\n":
logger.info("error : " + "\'" + str(error) + "\'")
logger.info("output : " + result.data() + "fin output")
return result
else:
code_d_erreur = process.error()
dic_err = { 0:"QProcess::FailedToStart", 1:"QProcess::Crashed", 2:"QProcess::TimedOut", 3:"QProcess::WriteError", 4:"QProcess::ReadError", 5:"QProcess::UnknownError" }
logger.info("Code erreur : " + str(code_d_erreur))
logger.info(dic_err[code_d_erreur])
return None
def run_otb_app(app_name, arguments):
dirname = os.path.dirname(os.path.abspath(__file__))
launcher = os.path.join(dirname, "win32", "bin", "otbApplicationLauncherCommandLine.exe") + " " + app_name + " " + os.path.join(dirname, "win32", "plugin")
command = launcher + " " + arguments
output = run_process(command)
return output
def set_OTB_PATH():
dirname = os.path.dirname(os.path.abspath(__file__))
if not os.name == "posix":
if "PATH" in os.environ.keys():
os.environ["PATH"] = os.path.join(dirname, "win32", "bin") + ";" + os.environ["PATH"]
else:
os.environ["PATH"] = os.path.join(dirname, "win32", "bin")
if "ITK_AUTOLOAD_PATH" in os.environ.keys():
os.environ["ITK_AUTOLOAD_PATH"] = os.path.join(dirname, "win32", "plugin") + ";" + os.environ["ITK_AUTOLOAD_PATH"]
else:
os.environ["ITK_AUTOLOAD_PATH"] = os.path.join(dirname, "win32", "plugin")
# print os.environ["PATH"]
# print os.environ["ITK_AUTOLOAD_PATH"]
def set_process_env(process):
dirname = os.path.dirname(os.path.abspath(__file__))
env = QProcessEnvironment.systemEnvironment()
env.insert("ITK_AUTOLOAD_PATH", os.path.join(dirname, "win32", "plugin")) # Add an environment variable
env.insert("PATH", os.path.join(dirname, "win32", "bin") + ";" + env.value("Path"))
process.setProcessEnvironment(env)
# print "env ITK_AUTOLOAD_PATH", env.value("ITK_AUTOLOAD_PATH")
# print "env PATH", env.value("PATH")