Skip to content

Commit 7968581

Browse files
author
Vladimir Kotal
committed
if no input is specified, assume directory
fixes #2438
1 parent 2ffbb0c commit 7968581

File tree

1 file changed

+31
-17
lines changed
  • opengrok-tools/src/main/python/opengrok_tools

1 file changed

+31
-17
lines changed

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141

4242
from .utils.commands import Commands, CommandsBase
4343
from .utils.filelock import Timeout, FileLock
44-
from .utils.opengrok import list_indexed_projects
44+
from .utils.opengrok import list_indexed_projects, get_config_value
4545
from .utils.readconfig import read_config
4646

4747
major_version = sys.version_info[0]
4848
if (major_version < 3):
4949
print("Need Python 3, you are running {}".format(major_version))
5050
sys.exit(1)
5151

52-
__version__ = "0.4"
52+
__version__ = "0.5"
5353

5454

5555
def worker(base):
@@ -73,10 +73,12 @@ def main():
7373

7474
# There can be only one way how to supply list of projects to process.
7575
group1 = parser.add_mutually_exclusive_group()
76-
group1.add_argument('-d', '--directory', default="/var/opengrok/src",
76+
group1.add_argument('-d', '--directory',
7777
help='Directory to process')
7878
group1.add_argument('-P', '--projects', nargs='*',
7979
help='List of projects to process')
80+
parser.add_argument('-I', '--indexed', action='store_true',
81+
help='Sync indexed projects only')
8082

8183
group2 = parser.add_mutually_exclusive_group()
8284
group2.add_argument('-D', '--debug', action='store_true',
@@ -88,8 +90,6 @@ def main():
8890
help='ignore errors from these projects')
8991
parser.add_argument('-c', '--config', required=True,
9092
help='config file in JSON format')
91-
parser.add_argument('-I', '--indexed', action='store_true',
92-
help='Sync indexed projects only')
9393
parser.add_argument('-U', '--uri', default='http://localhost:8080/source',
9494
help='URI of the webapp with context path')
9595
args = parser.parse_args()
@@ -105,12 +105,18 @@ def main():
105105
logger = logging.getLogger(os.path.basename(sys.argv[0]))
106106

107107
uri = args.uri
108-
if not uri:
109-
logger.error("uri of the webapp not specified")
110-
sys.exit(1)
108+
logger.debug("web application URI = {}".format(uri))
111109

112-
logger.debug("Uri = {}".format(uri))
110+
# Changing working directory to root will avoid problems when running
111+
# via sudo/su.
112+
try:
113+
os.chdir("/")
114+
except OSError:
115+
logger.error("cannot change working directory to /",
116+
exc_info=True)
117+
sys.exit(1)
113118

119+
# First read and validate configuration file as it is mandatory argument.
114120
config = read_config(logger, args.config)
115121
if config is None:
116122
logger.error("Cannot read config file from {}".format(args.config))
@@ -122,6 +128,17 @@ def main():
122128
logger.error("The config file has to contain key \"commands\"")
123129
sys.exit(1)
124130

131+
directory = args.directory
132+
if not args.directory and not args.projects and not args.indexed:
133+
# Assume directory, get the source root value from the webapp.
134+
directory = get_config_value(logger, 'sourceRoot', uri)
135+
if not directory:
136+
logger.error("Neither -d or -P or -I specified and cannot get "
137+
"source root from the webapp")
138+
sys.exit(1)
139+
else:
140+
logger.info("Assuming directory: {}".format(directory))
141+
125142
ignore_errors = []
126143
if args.ignore_errors:
127144
ignore_errors = args.ignore_errors
@@ -132,13 +149,6 @@ def main():
132149
pass
133150
logger.debug("Ignored projects: {}".format(ignore_errors))
134151

135-
try:
136-
os.chdir("/")
137-
except OSError:
138-
logger.error("cannot change working directory to /",
139-
exc_info=True)
140-
sys.exit(1)
141-
142152
lock = FileLock(os.path.join(tempfile.gettempdir(),
143153
"opengrok-sync.lock"))
144154
try:
@@ -147,8 +157,12 @@ def main():
147157

148158
if args.projects:
149159
dirs_to_process = args.projects
160+
logger.debug("Processing directories: {}".
161+
format(dirs_to_process))
150162
elif args.indexed:
151163
indexed_projects = list_indexed_projects(logger, uri)
164+
logger.debug("Processing indexed projects: {}".
165+
format(indexed_projects))
152166

153167
if indexed_projects:
154168
for line in indexed_projects:
@@ -157,7 +171,7 @@ def main():
157171
logger.error("cannot get list of projects")
158172
sys.exit(1)
159173
else:
160-
directory = args.directory
174+
logger.debug("Processing directory {}".format(directory))
161175
for entry in os.listdir(directory):
162176
if path.isdir(path.join(directory, entry)):
163177
dirs_to_process.append(entry)

0 commit comments

Comments
 (0)