Skip to content

Commit 8805a05

Browse files
tulinkryVladimir Kotal
authored andcommitted
checking return values for incoming check (#2846)
- refactoring the code to use the constants fixes #2843
1 parent eea6103 commit 8805a05

File tree

15 files changed

+117
-62
lines changed

15 files changed

+117
-62
lines changed

opengrok-tools/src/main/python/opengrok_tools/config_merge.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
from .utils.java import Java
2929
from .utils.log import fatal
3030
from .utils.parsers import get_javaparser
31-
31+
from .utils.exitvals import (
32+
FAILURE_EXITVAL,
33+
SUCCESS_EXITVAL
34+
)
3235
"""
3336
Wrapper for Java program merging OpenGrok configuration.
3437
"""
@@ -55,10 +58,10 @@ def main():
5558
logger=logger)
5659
cmd.execute()
5760
ret = cmd.getretcode()
58-
if ret is None or ret != 0:
61+
if ret is None or ret != SUCCESS_EXITVAL:
5962
logger.error(cmd.geterroutput())
6063
logger.error("command failed (return code {})".format(ret))
61-
sys.exit(1)
64+
sys.exit(FAILURE_EXITVAL)
6265
else:
6366
print(cmd.getoutputstr())
6467

opengrok-tools/src/main/python/opengrok_tools/groups.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
from .utils.java import Java
2828
from .utils.log import get_console_logger, get_class_basename, fatal
2929
from .utils.parsers import get_javaparser
30-
30+
from .utils.exitvals import (
31+
FAILURE_EXITVAL,
32+
SUCCESS_EXITVAL
33+
)
3134
"""
3235
Script for manipulating project groups
3336
"""
@@ -51,10 +54,10 @@ def main():
5154
logger=logger)
5255
cmd.execute()
5356
ret = cmd.getretcode()
54-
if ret is None or ret != 0:
57+
if ret is None or ret != SUCCESS_EXITVAL:
5558
logger.error(cmd.geterroutput())
5659
logger.error("command failed (return code {})".format(ret))
57-
sys.exit(1)
60+
sys.exit(FAILURE_EXITVAL)
5861
else:
5962
print(cmd.getoutputstr())
6063

opengrok-tools/src/main/python/opengrok_tools/indexer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
from .utils.indexer import FindCtags, Indexer
3030
from .utils.log import get_console_logger, get_class_basename, fatal
3131
from .utils.parsers import get_javaparser
32+
from .utils.exitvals import (
33+
FAILURE_EXITVAL,
34+
SUCCESS_EXITVAL
35+
)
3236

3337
"""
3438
opengrok.jar wrapper
@@ -62,10 +66,10 @@ def main():
6266
env_vars=args.environment, doprint=True)
6367
indexer.execute()
6468
ret = indexer.getretcode()
65-
if ret is None or ret != 0:
69+
if ret is None or ret != SUCCESS_EXITVAL:
6670
# The output is already printed thanks to 'doprint' above.
6771
logger.error("Indexer command failed (return code {})".format(ret))
68-
sys.exit(1)
72+
sys.exit(FAILURE_EXITVAL)
6973

7074

7175
if __name__ == '__main__':

opengrok-tools/src/main/python/opengrok_tools/java.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
from .utils.java import Java
2929
from .utils.log import get_console_logger, get_class_basename, fatal
3030
from .utils.parsers import get_javaparser
31+
from .utils.exitvals import (
32+
FAILURE_EXITVAL,
33+
SUCCESS_EXITVAL
34+
)
3135

3236

3337
def main():
@@ -49,10 +53,10 @@ def main():
4953
env_vars=args.environment)
5054
java.execute()
5155
ret = java.getretcode()
52-
if ret is None or ret != 0:
56+
if ret is None or ret != SUCCESS_EXITVAL:
5357
logger.error(java.getoutputstr())
5458
logger.error("java command failed (return code {})".format(ret))
55-
sys.exit(1)
59+
sys.exit(FAILURE_EXITVAL)
5660

5761

5862
if __name__ == '__main__':

opengrok-tools/src/main/python/opengrok_tools/mirror.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838

3939
from filelock import Timeout, FileLock
4040

41+
from .utils.exitvals import (
42+
FAILURE_EXITVAL,
43+
CONTINUE_EXITVAL,
44+
SUCCESS_EXITVAL
45+
)
4146
from .utils.log import get_console_logger, get_class_basename, \
4247
fatal, get_batch_logger
4348
from .utils.opengrok import get_config_value, list_indexed_projects
@@ -71,7 +76,7 @@ def worker(args):
7176

7277

7378
def main():
74-
ret = 0
79+
ret = SUCCESS_EXITVAL
7580

7681
parser = argparse.ArgumentParser(description='project mirroring',
7782
parents=[get_baseparser(
@@ -175,13 +180,15 @@ def main():
175180
try:
176181
project_results = pool.map(worker, worker_args, 1)
177182
except KeyboardInterrupt:
178-
sys.exit(1)
183+
sys.exit(FAILURE_EXITVAL)
179184
else:
180-
if any([True for x in project_results if x == 1]):
181-
ret = 1
185+
if any([x == FAILURE_EXITVAL for x in project_results]):
186+
ret = FAILURE_EXITVAL
187+
if all([x == CONTINUE_EXITVAL for x in project_results]):
188+
ret = CONTINUE_EXITVAL
182189
except Timeout:
183190
logger.warning("Already running, exiting.")
184-
sys.exit(1)
191+
sys.exit(FAILURE_EXITVAL)
185192

186193
logging.shutdown()
187194
sys.exit(ret)

opengrok-tools/src/main/python/opengrok_tools/projadm.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
add_project, delete_project, get_config_value
4444
from .utils.parsers import get_baseparser
4545
from .utils.utils import get_command, is_web_uri
46+
from .utils.exitvals import (
47+
FAILURE_EXITVAL,
48+
SUCCESS_EXITVAL
49+
)
4650

4751
MAJOR_VERSION = sys.version_info[0]
4852
if (MAJOR_VERSION < 3):
@@ -62,11 +66,12 @@ def exec_command(doit, logger, cmd, msg):
6266
logger.info(cmd)
6367
return
6468
cmd.execute()
65-
if cmd.getstate() is not Command.FINISHED or cmd.getretcode() != 0:
69+
if cmd.getstate() is not Command.FINISHED \
70+
or cmd.getretcode() != SUCCESS_EXITVAL:
6671
logger.error(msg)
6772
logger.error("Standard output: {}".format(cmd.getoutput()))
6873
logger.error("Error output: {}".format(cmd.geterroutput()))
69-
sys.exit(1)
74+
sys.exit(FAILURE_EXITVAL)
7075

7176
logger.debug(cmd.geterroutputstr())
7277

@@ -99,11 +104,11 @@ def install_config(doit, logger, src, dst):
99104
except PermissionError:
100105
logger.error('Failed to copy {} to {} (permissions)'.
101106
format(src, dst))
102-
sys.exit(1)
107+
sys.exit(FAILURE_EXITVAL)
103108
except OSError:
104109
logger.error('Failed to copy {} to {} (I/O)'.
105110
format(src, dst))
106-
sys.exit(1)
111+
sys.exit(FAILURE_EXITVAL)
107112

108113

109114
def config_refresh(doit, logger, basedir, uri, configmerge, jar_file,
@@ -120,12 +125,12 @@ def config_refresh(doit, logger, basedir, uri, configmerge, jar_file,
120125
main_config = get_config_file(basedir)
121126
if not path.isfile(main_config):
122127
logger.error("file {} does not exist".format(main_config))
123-
sys.exit(1)
128+
sys.exit(FAILURE_EXITVAL)
124129

125130
if doit:
126131
current_config = get_configuration(logger, uri)
127132
if not current_config:
128-
sys.exit(1)
133+
sys.exit(FAILURE_EXITVAL)
129134
else:
130135
current_config = None
131136

@@ -260,7 +265,7 @@ def main():
260265

261266
if args.nosourcedelete and not args.delete:
262267
logger.error("The no source delete option is only valid for delete")
263-
sys.exit(1)
268+
sys.exit(FAILURE_EXITVAL)
264269

265270
# Set the base directory
266271
if args.base:
@@ -271,7 +276,7 @@ def main():
271276
logger.error("Not a directory: {}\n"
272277
"Set the base directory with the --base option."
273278
.format(args.base))
274-
sys.exit(1)
279+
sys.exit(FAILURE_EXITVAL)
275280

276281
# If read-only configuration file is specified, this means read-only
277282
# configuration will need to be merged with active webapp configuration.
@@ -282,14 +287,14 @@ def main():
282287
logger.debug("Using {} as read-only config".format(args.roconfig))
283288
else:
284289
logger.error("File {} does not exist".format(args.roconfig))
285-
sys.exit(1)
290+
sys.exit(FAILURE_EXITVAL)
286291

287292
configmerge_file = get_command(logger, args.configmerge,
288293
"opengrok-config-merge")
289294
if configmerge_file is None:
290295
logger.error("Use the --configmerge option to specify the path to"
291296
"the config merge script")
292-
sys.exit(1)
297+
sys.exit(FAILURE_EXITVAL)
293298

294299
configmerge = [configmerge_file]
295300
if args.loglevel:
@@ -299,12 +304,12 @@ def main():
299304
if args.jar is None:
300305
logger.error('jar file needed for config merge tool, '
301306
'use --jar to specify one')
302-
sys.exit(1)
307+
sys.exit(FAILURE_EXITVAL)
303308

304309
uri = args.uri
305310
if not is_web_uri(uri):
306311
logger.error("Not a URI: {}".format(uri))
307-
sys.exit(1)
312+
sys.exit(FAILURE_EXITVAL)
308313
logger.debug("web application URI = {}".format(uri))
309314

310315
lock = FileLock(os.path.join(tempfile.gettempdir(),
@@ -348,7 +353,7 @@ def main():
348353
java=args.java)
349354
else:
350355
parser.print_help()
351-
sys.exit(1)
356+
sys.exit(FAILURE_EXITVAL)
352357

353358
if args.upload:
354359
main_config = get_config_file(basedir=args.base)
@@ -359,13 +364,13 @@ def main():
359364
config_data = config_file.read().encode("utf-8")
360365
if not set_configuration(logger,
361366
config_data, uri):
362-
sys.exit(1)
367+
sys.exit(FAILURE_EXITVAL)
363368
else:
364369
logger.error("file {} does not exist".format(main_config))
365-
sys.exit(1)
370+
sys.exit(FAILURE_EXITVAL)
366371
except Timeout:
367372
logger.warning("Already running, exiting.")
368-
sys.exit(1)
373+
sys.exit(FAILURE_EXITVAL)
369374

370375

371376
if __name__ == '__main__':

opengrok-tools/src/main/python/opengrok_tools/reindex_project.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
from .utils.log import get_console_logger, get_class_basename, fatal
3232
from .utils.opengrok import get_configuration
3333
from .utils.parsers import get_javaparser
34+
from .utils.exitvals import (
35+
FAILURE_EXITVAL,
36+
SUCCESS_EXITVAL
37+
)
3438

3539
"""
3640
OpenGrok reindexing script for single project. Makes sure it uses
@@ -117,11 +121,11 @@ def main():
117121
ret = indexer.getretcode()
118122
os.remove(conf_file)
119123
os.remove(logprop_file)
120-
if ret is None or ret != 0:
124+
if ret is None or ret != SUCCESS_EXITVAL:
121125
logger.error(indexer.getoutputstr())
122126
logger.error("Indexer command for project {} failed (return code {})".
123127
format(args.project, ret))
124-
sys.exit(1)
128+
sys.exit(FAILURE_EXITVAL)
125129

126130

127131
if __name__ == '__main__':

opengrok-tools/src/main/python/opengrok_tools/sync.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
from .utils.parsers import get_baseparser
4343
from .utils.readconfig import read_config
4444
from .utils.utils import is_web_uri
45+
from .utils.exitvals import (
46+
FAILURE_EXITVAL,
47+
)
4548

4649
major_version = sys.version_info[0]
4750
if (major_version < 3):
@@ -102,14 +105,14 @@ def main():
102105
uri = args.uri
103106
if not is_web_uri(uri):
104107
logger.error("Not a URI: {}".format(uri))
105-
sys.exit(1)
108+
sys.exit(FAILURE_EXITVAL)
106109
logger.debug("web application URI = {}".format(uri))
107110

108111
# First read and validate configuration file as it is mandatory argument.
109112
config = read_config(logger, args.config)
110113
if config is None:
111114
logger.error("Cannot read config file from {}".format(args.config))
112-
sys.exit(1)
115+
sys.exit(FAILURE_EXITVAL)
113116

114117
# Changing working directory to root will avoid problems when running
115118
# programs via sudo/su. Do this only after the config file was read
@@ -119,13 +122,13 @@ def main():
119122
except OSError:
120123
logger.error("cannot change working directory to /",
121124
exc_info=True)
122-
sys.exit(1)
125+
sys.exit(FAILURE_EXITVAL)
123126

124127
try:
125128
commands = config["commands"]
126129
except KeyError:
127130
logger.error("The config file has to contain key \"commands\"")
128-
sys.exit(1)
131+
sys.exit(FAILURE_EXITVAL)
129132

130133
directory = args.directory
131134
if not args.directory and not args.projects and not args.indexed:
@@ -134,7 +137,7 @@ def main():
134137
if not directory:
135138
logger.error("Neither -d or -P or -I specified and cannot get "
136139
"source root from the webapp")
137-
sys.exit(1)
140+
sys.exit(FAILURE_EXITVAL)
138141
else:
139142
logger.info("Assuming directory: {}".format(directory))
140143

@@ -166,7 +169,7 @@ def main():
166169
dirs_to_process.append(line.strip())
167170
else:
168171
logger.error("cannot get list of projects")
169-
sys.exit(1)
172+
sys.exit(FAILURE_EXITVAL)
170173
else:
171174
logger.debug("Processing directory {}".format(directory))
172175
for entry in os.listdir(directory):
@@ -187,7 +190,7 @@ def main():
187190
try:
188191
cmds_base_results = pool.map(worker, cmds_base, 1)
189192
except KeyboardInterrupt:
190-
sys.exit(1)
193+
sys.exit(FAILURE_EXITVAL)
191194
else:
192195
for cmds_base in cmds_base_results:
193196
logger.debug("Checking results of project {}".
@@ -198,7 +201,7 @@ def main():
198201
cmds.check(ignore_errors)
199202
except Timeout:
200203
logger.warning("Already running, exiting.")
201-
sys.exit(1)
204+
sys.exit(FAILURE_EXITVAL)
202205

203206

204207
if __name__ == '__main__':

opengrok-tools/src/main/python/opengrok_tools/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from . import readconfig
77
from . import utils
88
from . import webutil
9+
from . import exitvals
910

1011
__all__ = [
1112
'opengrok',
@@ -16,4 +17,5 @@
1617
'webutil',
1718
'readconfig',
1819
'parsers',
20+
'exitvals',
1921
]

0 commit comments

Comments
 (0)