Skip to content

Commit dd24724

Browse files
tulinkryVladimir Kotal
authored andcommitted
Python tools improved incoming check (#2847)
Improving --incoming check to check also the index presence for the particular project. After that, the name "incoming" could be a little misleading so I refactored the name to "check changes". fixes #2844
1 parent 8805a05 commit dd24724

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#
2222
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
23+
# Portions Copyright (c) 2019, Krystof Tulinger <[email protected]>
2324
#
2425

2526
"""
@@ -57,11 +58,11 @@
5758
if major_version < 3:
5859
fatal("Need Python 3, you are running {}".format(major_version))
5960

60-
__version__ = "0.8"
61+
__version__ = "0.9"
6162

6263

6364
def worker(args):
64-
project_name, logdir, loglevel, backupcount, config, incoming, uri, \
65+
project_name, logdir, loglevel, backupcount, config, check_changes, uri, \
6566
source_root, batch = args
6667

6768
if batch:
@@ -71,7 +72,7 @@ def worker(args):
7172
get_class_basename())
7273

7374
return mirror_project(config, project_name,
74-
incoming,
75+
check_changes,
7576
uri, source_root)
7677

7778

@@ -94,9 +95,11 @@ def main():
9495
help='batch mode - will log into a file')
9596
parser.add_argument('-B', '--backupcount', default=8,
9697
help='how many log files to keep around in batch mode')
97-
parser.add_argument('-I', '--incoming', action='store_true',
98-
help='Check for incoming changes, terminate the '
99-
'processing if not found.')
98+
parser.add_argument('-I', '--check-changes', action='store_true',
99+
help='Check for changes in the project or its'
100+
' repositories,'
101+
' terminate the processing'
102+
' if no change is found.')
100103
parser.add_argument('-w', '--workers', default=cpu_count(),
101104
help='Number of worker processes')
102105

@@ -174,7 +177,7 @@ def main():
174177
for x in projects:
175178
worker_args.append([x, logdir, args.loglevel,
176179
args.backupcount, config,
177-
args.incoming,
180+
args.check_changes,
178181
args.uri, source_root,
179182
args.batch])
180183
try:

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

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@
2626
import os
2727
import fnmatch
2828
import logging
29+
import urllib
30+
31+
from requests.exceptions import HTTPError
2932

3033
from .exitvals import (
3134
FAILURE_EXITVAL,
3235
CONTINUE_EXITVAL,
3336
SUCCESS_EXITVAL
3437
)
3538
from .utils import is_exe, check_create_dir, get_int
36-
from .opengrok import get_repos, get_repo_type
39+
from .webutil import get
40+
from .opengrok import get_repos, get_repo_type, get_uri
3741
from .hook import run_hook
3842

3943
from ..scm.repofactory import get_repository
@@ -196,13 +200,14 @@ def get_project_properties(project_config, project_name, hookdir):
196200
use_proxy, ignored_repos
197201

198202

199-
def mirror_project(config, project_name, check_incoming, uri,
203+
def mirror_project(config, project_name, check_changes, uri,
200204
source_root):
201205
"""
202206
Mirror the repositories of single project.
203207
:param config global configuration dictionary
204208
:param project_name: name of the project
205-
:param check_incoming:
209+
:param check_changes: check for changes in the project or its repositories
210+
and terminate if no change is found
206211
:param uri
207212
:param source_root
208213
:return exit code
@@ -244,22 +249,44 @@ def mirror_project(config, project_name, check_incoming, uri,
244249
format(project_name))
245250
return CONTINUE_EXITVAL
246251

247-
# Check if any of the repositories contains incoming changes.
248-
if check_incoming:
249-
got_incoming = False
250-
for repo in repos:
251-
try:
252-
if repo.incoming():
253-
logger.debug('Repository {} has incoming changes'.
254-
format(repo))
255-
got_incoming = True
256-
break
257-
except RepositoryException:
258-
logger.error('Cannot determine incoming changes for '
259-
'repository {}'.format(repo))
260-
return FAILURE_EXITVAL
252+
# Check if the project or any of its repositories have changed.
253+
if check_changes:
254+
changes_detected = False
255+
256+
# check if the project is a new project - full index is necessary
257+
try:
258+
r = get(logger, get_uri(uri, 'api', 'v1', 'projects',
259+
urllib.parse.quote_plus(project_name),
260+
'property', 'indexed'))
261+
r.raise_for_status()
262+
if not bool(r.json()):
263+
changes_detected = True
264+
logger.debug('Project {} has not been indexed yet'
265+
.format(project_name))
266+
except ValueError as e:
267+
logger.error('Unable to parse project \'{}\' indexed flag: {}'
268+
.format(project_name, e))
269+
return FAILURE_EXITVAL
270+
except HTTPError as e:
271+
logger.error('Unable to determine project \'{}\' indexed flag: {}'
272+
.format(project_name, e))
273+
return FAILURE_EXITVAL
261274

262-
if not got_incoming:
275+
# check if the project has any new changes in the SCM
276+
if not changes_detected:
277+
for repo in repos:
278+
try:
279+
if repo.incoming():
280+
logger.debug('Repository {} has incoming changes'.
281+
format(repo))
282+
changes_detected = True
283+
break
284+
except RepositoryException:
285+
logger.error('Cannot determine incoming changes for '
286+
'repository {}'.format(repo))
287+
return FAILURE_EXITVAL
288+
289+
if not changes_detected:
263290
logger.info('No incoming changes for repositories in '
264291
'project {}'.
265292
format(project_name))

0 commit comments

Comments
 (0)