Skip to content

Commit 8e6d60e

Browse files
author
Vladimir Kotal
authored
merge command options smarter (#2362)
fixes #2361
1 parent 90c196c commit 8e6d60e

File tree

2 files changed

+96
-27
lines changed

2 files changed

+96
-27
lines changed

tools/src/main/python/indexer.py

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,63 @@ def __init__(self, command, logger=None, java=None, jar='opengrok.jar',
4747
java_opts=None, env_vars=None):
4848

4949
java_options = []
50-
java_options.extend(self.get_SCM_properties(logger))
5150
if java_opts:
5251
java_options.extend(java_opts)
52+
java_options = merge_properties(java_options,
53+
get_SCM_properties(logger))
5354
logger.debug("Java options: {}".format(java_options))
5455

5556
super().__init__(command, jar=jar, java=java, java_opts=java_options,
5657
logger=logger, env_vars=env_vars)
5758

58-
def get_SCM_properties(self, logger):
59-
"""
60-
Return list of Java System properties that contain valid paths to
61-
SCM commands.
62-
"""
63-
SCM_COMMANDS = {
64-
'bk': '-Dorg.opengrok.indexer.history.BitKeeper',
65-
'hg': '-Dorg.opengrok.indexer.history.Mercurial',
66-
'cvs': '-Dorg.opengrok.indexer.history.cvs',
67-
'svn': '-Dorg.opengrok.indexer.history.Subversion',
68-
'sccs': '-Dorg.opengrok.indexer.history.SCCS',
69-
'cleartool': '-Dorg.opengrok.indexer.history.ClearCase',
70-
'git': '-Dorg.opengrok.indexer.history.git',
71-
'p4': '-Dorg.opengrok.indexer.history.Perforce',
72-
'mtn': '-Dorg.opengrok.indexer.history.Monotone',
73-
'blame': '-Dorg.opengrok.indexer.history.RCS',
74-
'bzr': '-Dorg.opengrok.indexer.history.Bazaar'}
75-
76-
properties = []
77-
for cmd in SCM_COMMANDS.keys():
78-
executable = get_command(logger, None, cmd, level=logging.INFO)
79-
if executable:
80-
properties.append("{}={}".
81-
format(SCM_COMMANDS[cmd], executable))
82-
83-
return properties
59+
60+
def get_SCM_properties(logger):
61+
"""
62+
Return list of Java System properties that contain valid paths to
63+
SCM commands.
64+
"""
65+
SCM_COMMANDS = {
66+
'bk': '-Dorg.opengrok.indexer.history.BitKeeper',
67+
'hg': '-Dorg.opengrok.indexer.history.Mercurial',
68+
'cvs': '-Dorg.opengrok.indexer.history.cvs',
69+
'svn': '-Dorg.opengrok.indexer.history.Subversion',
70+
'sccs': '-Dorg.opengrok.indexer.history.SCCS',
71+
'cleartool': '-Dorg.opengrok.indexer.history.ClearCase',
72+
'git': '-Dorg.opengrok.indexer.history.git',
73+
'p4': '-Dorg.opengrok.indexer.history.Perforce',
74+
'mtn': '-Dorg.opengrok.indexer.history.Monotone',
75+
'blame': '-Dorg.opengrok.indexer.history.RCS',
76+
'bzr': '-Dorg.opengrok.indexer.history.Bazaar'}
77+
78+
properties = []
79+
for cmd in SCM_COMMANDS.keys():
80+
executable = get_command(logger, None, cmd, level=logging.INFO)
81+
if executable:
82+
properties.append("{}={}".
83+
format(SCM_COMMANDS[cmd], executable))
84+
85+
return properties
86+
87+
88+
def merge_properties(base, extra):
89+
"""
90+
Merge two lists of options (strings in the form of name=value).
91+
Take everything from base and add properties from extra
92+
(according to names) that are not present in the base.
93+
:param base: list of properties
94+
:param extra: list of properties
95+
:return: merged list
96+
"""
97+
98+
extra_prop_names = set(map(lambda x: x.split('=')[0], base))
99+
100+
ret = set(base)
101+
for item in extra:
102+
nv = item.split("=")
103+
if nv[0] not in extra_prop_names:
104+
ret.add(item)
105+
106+
return list(ret)
84107

85108

86109
def FindCtags(logger):

tools/src/test/python/test_indexer.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# CDDL HEADER START
5+
#
6+
# The contents of this file are subject to the terms of the
7+
# Common Development and Distribution License (the "License").
8+
# You may not use this file except in compliance with the License.
9+
#
10+
# See LICENSE.txt included in this distribution for the specific
11+
# language governing permissions and limitations under the License.
12+
#
13+
# When distributing Covered Code, include this CDDL HEADER in each
14+
# file and include the License file at LICENSE.txt.
15+
# If applicable, add the following below this CDDL HEADER, with the
16+
# fields enclosed by brackets "[]" replaced with your own identifying
17+
# information: Portions Copyright [yyyy] [name of copyright owner]
18+
#
19+
# CDDL HEADER END
20+
#
21+
22+
#
23+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
24+
#
25+
26+
import unittest
27+
import sys
28+
import os
29+
30+
31+
sys.path.insert(0, os.path.abspath(
32+
os.path.join(os.path.dirname(__file__), '..', '..',
33+
'main', 'python')))
34+
35+
from indexer import merge_properties
36+
37+
38+
class TestApp(unittest.TestCase):
39+
def test_merge_properties(self):
40+
merged = merge_properties(['foo', '-Dfoo=1'],
41+
['-Dfoo=2', '-Dbar=bar'])
42+
self.assertEqual(['-Dbar=bar', '-Dfoo=1', 'foo'], sorted(merged))
43+
44+
45+
if __name__ == '__main__':
46+
unittest.main()

0 commit comments

Comments
 (0)