Skip to content

Commit ae5b3cb

Browse files
Vladimir Kotalahornace
Vladimir Kotal
authored andcommitted
allow to override commands once again
fixes #3705 fixes #3697
1 parent 0104f19 commit ae5b3cb

File tree

11 files changed

+157
-84
lines changed

11 files changed

+157
-84
lines changed

tools/src/main/python/opengrok_tools/scm/cvs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
2323
#
2424

@@ -29,8 +29,8 @@
2929

3030

3131
class CVSRepository(Repository):
32-
def __init__(self, logger, path, project, command, env, hooks, timeout):
33-
super().__init__(logger, path, project, command, env, hooks, timeout)
32+
def __init__(self, name, logger, path, project, command, env, hooks, timeout):
33+
super().__init__(name, logger, path, project, command, env, hooks, timeout)
3434

3535
self.command = self._repository_command(command, default=lambda: which('cvs'))
3636

@@ -39,8 +39,8 @@ def __init__(self, logger, path, project, command, env, hooks, timeout):
3939

4040
def reposync(self):
4141
hg_command = [self.command, "update", "-dP"]
42-
cmd = self.getCommand(hg_command, work_dir=self.path,
43-
env_vars=self.env, logger=self.logger)
42+
cmd = self.get_command(hg_command, work_dir=self.path,
43+
env_vars=self.env, logger=self.logger)
4444
cmd.execute()
4545
self.logger.info("output of {}:".format(cmd))
4646
self.logger.info(cmd.getoutputstr())

tools/src/main/python/opengrok_tools/scm/git.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
2323
#
2424

@@ -29,25 +29,28 @@
2929

3030

3131
class GitRepository(Repository):
32-
def __init__(self, logger, path, project, command, env, hooks, timeout):
33-
super().__init__(logger, path, project, command, env, hooks, timeout)
32+
def __init__(self, name, logger, path, project, command, env, hooks, timeout):
33+
super().__init__(name, logger, path, project, command, env, hooks, timeout)
3434

3535
self.command = self._repository_command(command, default=lambda: which('git'))
3636

3737
if not self.command:
3838
raise RepositoryException("Cannot get git command")
3939

40+
def _configure_git_pull(self):
4041
# The incoming() check relies on empty output so configure
4142
# the repository first to avoid getting extra output.
4243
git_command = [self.command, "config", "--local", "pull.ff", "only"]
43-
cmd = self.getCommand(git_command, work_dir=self.path,
44-
env_vars=self.env, logger=self.logger)
44+
cmd = self.get_command(git_command, work_dir=self.path,
45+
env_vars=self.env, logger=self.logger)
4546
cmd.execute()
4647
if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED:
4748
cmd.log_error("failed to configure git pull.ff")
4849

4950
def reposync(self):
51+
self._configure_git_pull()
5052
return self._run_custom_sync_command([self.command, 'pull', '--ff-only'])
5153

5254
def incoming_check(self):
55+
self._configure_git_pull()
5356
return self._run_custom_incoming_command([self.command, 'pull', '--dry-run'])

tools/src/main/python/opengrok_tools/scm/mercurial.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
2323
#
2424

@@ -29,8 +29,8 @@
2929

3030

3131
class MercurialRepository(Repository):
32-
def __init__(self, logger, path, project, command, env, hooks, timeout):
33-
super().__init__(logger, path, project, command, env, hooks, timeout)
32+
def __init__(self, name, logger, path, project, command, env, hooks, timeout):
33+
super().__init__(name, logger, path, project, command, env, hooks, timeout)
3434

3535
self.command = self._repository_command(command, default=lambda: which('hg'))
3636

@@ -39,8 +39,8 @@ def __init__(self, logger, path, project, command, env, hooks, timeout):
3939

4040
def get_branch(self):
4141
hg_command = [self.command, "branch"]
42-
cmd = self.getCommand(hg_command, work_dir=self.path,
43-
env_vars=self.env, logger=self.logger)
42+
cmd = self.get_command(hg_command, work_dir=self.path,
43+
env_vars=self.env, logger=self.logger)
4444
cmd.execute()
4545
self.logger.info("output of {}:".format(cmd))
4646
self.logger.info(cmd.getoutputstr())
@@ -68,8 +68,8 @@ def reposync(self):
6868
if branch != "default":
6969
hg_command.append("-b")
7070
hg_command.append(branch)
71-
cmd = self.getCommand(hg_command, work_dir=self.path,
72-
env_vars=self.env, logger=self.logger)
71+
cmd = self.get_command(hg_command, work_dir=self.path,
72+
env_vars=self.env, logger=self.logger)
7373
cmd.execute()
7474
self.logger.info("output of {}:".format(cmd))
7575
self.logger.info(cmd.getoutputstr())
@@ -82,8 +82,8 @@ def reposync(self):
8282
# some servers do not support it.
8383
if branch == "default":
8484
hg_command.append("--check")
85-
cmd = self.getCommand(hg_command, work_dir=self.path,
86-
env_vars=self.env, logger=self.logger)
85+
cmd = self.get_command(hg_command, work_dir=self.path,
86+
env_vars=self.env, logger=self.logger)
8787
cmd.execute()
8888
self.logger.info("output of {}:".format(cmd))
8989
self.logger.info(cmd.getoutputstr())
@@ -104,8 +104,8 @@ def incoming_check(self):
104104
if branch != "default":
105105
hg_command.append("-b")
106106
hg_command.append(branch)
107-
cmd = self.getCommand(hg_command, work_dir=self.path,
108-
env_vars=self.env, logger=self.logger)
107+
cmd = self.get_command(hg_command, work_dir=self.path,
108+
env_vars=self.env, logger=self.logger)
109109
cmd.execute()
110110
self.logger.info("output of {}:".format(cmd))
111111
self.logger.info(cmd.getoutputstr())

tools/src/main/python/opengrok_tools/scm/perforce.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
# Portions Copyright 2020 Robert Williams
2323
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
2424
#
@@ -29,8 +29,8 @@
2929

3030

3131
class PerforceRepository(Repository):
32-
def __init__(self, logger, path, project, command, env, hooks, timeout):
33-
super().__init__(logger, path, project, command, env, hooks, timeout)
32+
def __init__(self, name, logger, path, project, command, env, hooks, timeout):
33+
super().__init__(name, logger, path, project, command, env, hooks, timeout)
3434

3535
self.command = self._repository_command(command, default=lambda: which('p4'))
3636

tools/src/main/python/opengrok_tools/scm/repo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
2323
#
2424

@@ -28,8 +28,8 @@
2828

2929

3030
class RepoRepository(Repository):
31-
def __init__(self, logger, path, project, command, env, hooks, timeout):
32-
super().__init__(logger, path, project, command, env, hooks, timeout)
31+
def __init__(self, name, logger, path, project, command, env, hooks, timeout):
32+
super().__init__(logger, name, path, project, command, env, hooks, timeout)
3333

3434
self.command = self._repository_command(command, default=lambda: which('repo'))
3535

tools/src/main/python/opengrok_tools/scm/repofactory.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,11 @@
3333
from .teamware import TeamwareRepository
3434

3535

36-
# Note: these have to correspond with get_repository().
37-
REPO_TYPES = ["mercurial", "hg", "teamware", "sccs", "cvs",
38-
"svn", "subversion", "git", "perforce", "repo"]
39-
40-
4136
def get_repository(path, repo_type, project,
4237
commands=None, env=None, hooks=None, timeout=None):
4338
"""
44-
:param path: full path
45-
:param repo_type: repository type
39+
:param path: full path for the working directory
40+
:param repo_type: repository type name
4641
:param project: project name
4742
:param commands: commands dictionary with paths to SCM utilities
4843
:param env: environment variables dictionary
@@ -56,37 +51,38 @@ def get_repository(path, repo_type, project,
5651

5752
repo_lower = repo_type.lower()
5853

59-
logger.debug("Constructing repo object for path {}".format(path))
54+
logger.debug("Constructing repository object of type '{}' for path '{}'".
55+
format(repo_type, path))
6056

6157
if not commands:
6258
commands = {}
6359

6460
if repo_lower in ["mercurial", "hg"]:
65-
return MercurialRepository(logger, path, project,
61+
return MercurialRepository(repo_type, logger, path, project,
6662
commands.get("hg"),
6763
env, hooks, timeout)
6864
elif repo_lower in ["teamware", "sccs"]:
69-
return TeamwareRepository(logger, path, project,
65+
return TeamwareRepository(repo_type, logger, path, project,
7066
commands.get("teamware"),
7167
env, hooks, timeout)
7268
elif repo_lower == "cvs":
73-
return CVSRepository(logger, path, project,
69+
return CVSRepository(repo_type, logger, path, project,
7470
commands.get("cvs"),
7571
env, hooks, timeout)
7672
elif repo_lower in ["svn", "subversion"]:
77-
return SubversionRepository(logger, path, project,
73+
return SubversionRepository(repo_type, logger, path, project,
7874
commands.get("svn"),
7975
env, hooks, timeout)
8076
elif repo_lower == "git":
81-
return GitRepository(logger, path, project,
77+
return GitRepository(repo_type, logger, path, project,
8278
commands.get("git"),
8379
env, hooks, timeout)
8480
elif repo_lower == "perforce":
85-
return PerforceRepository(logger, path, project,
81+
return PerforceRepository(repo_type, logger, path, project,
8682
commands.get("perforce"),
8783
env, hooks, timeout)
8884
elif repo_lower == "repo":
89-
return RepoRepository(logger, path, project,
85+
return RepoRepository(repo_type, logger, path, project,
9086
commands.get("repo"),
9187
env, hooks, timeout)
9288
else:

tools/src/main/python/opengrok_tools/scm/repository.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
2323
#
2424

2525
import abc
26+
import os
2627

2728
from ..utils.command import Command
2829

@@ -43,9 +44,11 @@ class Repository:
4344

4445
SYNC_COMMAND_SECTION = 'sync'
4546
INCOMING_COMMAND_SECTION = 'incoming'
47+
COMMAND_PROPERTY = 'command'
4648

47-
def __init__(self, logger, path, project, configured_commands, env, hooks,
48-
timeout):
49+
def __init__(self, name, logger, path, project, configured_commands, env, hooks, timeout):
50+
self.name = name
51+
self.command = None
4952
self.logger = logger
5053
self.path = path
5154
self.project = project
@@ -59,12 +62,16 @@ def __init__(self, logger, path, project, configured_commands, env, hooks,
5962
def __str__(self):
6063
return self.path
6164

62-
def getCommand(self, cmd, **kwargs):
65+
def get_command(self, cmd, **kwargs):
66+
"""
67+
:param cmd: command
68+
:param kwargs: dictionary of command attributes
69+
:return: Command object ready for execution.
70+
"""
6371
kwargs['timeout'] = self.timeout
6472
return Command(cmd, **kwargs)
6573

6674
def sync(self):
67-
# Eventually, there might be per-repository hooks added here.
6875
if self.is_command_overridden(self.configured_commands, self.SYNC_COMMAND_SECTION):
6976
return self._run_custom_sync_command(
7077
self.listify(self.configured_commands[self.SYNC_COMMAND_SECTION])
@@ -77,6 +84,8 @@ def reposync(self):
7784
Synchronize the repository by running sync command specific for
7885
given repository type.
7986
87+
This method definition has to be overriden by given repository class.
88+
8089
Return 1 on failure, 0 on success.
8190
"""
8291
raise NotImplementedError()
@@ -96,6 +105,8 @@ def incoming(self):
96105
def incoming_check(self):
97106
"""
98107
Check if there are any incoming changes.
108+
Normally this method definition is overriden, unless the repository
109+
type has no way how to check for incoming changes.
99110
100111
Return True if so, False otherwise.
101112
"""
@@ -137,8 +148,8 @@ def _run_command(self, command):
137148
- status: 0 on success execution, non-zero otherwise
138149
- output: command output as string
139150
"""
140-
cmd = self.getCommand(command, work_dir=self.path,
141-
env_vars=self.env, logger=self.logger)
151+
cmd = self.get_command(command, work_dir=self.path,
152+
env_vars=self.env, logger=self.logger)
142153
cmd.execute()
143154
if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED:
144155
cmd.log_error("failed to perform command")
@@ -164,7 +175,7 @@ def _repository_command(configured_commands, default=lambda: None):
164175
if isinstance(configured_commands, str):
165176
return configured_commands
166177
elif isinstance(configured_commands, dict) and \
167-
configured_commands.get('command'):
178+
configured_commands.get('command'): # COMMAND_PROPERTY
168179
return configured_commands['command']
169180

170181
return default()
@@ -185,3 +196,38 @@ def is_command_overridden(config, command):
185196
:return: true if overridden, false otherwise
186197
"""
187198
return isinstance(config, dict) and config.get(command) is not None
199+
200+
def _check_command(self):
201+
"""
202+
Could be overriden in given repository class to provide different check.
203+
:return: True if self.command is a file, False otherwise.
204+
"""
205+
if self.command and not os.path.isfile(self.command):
206+
self.logger.error("path for '{}' is not a file: {}".
207+
format(self.name, self.command))
208+
return False
209+
210+
return True
211+
212+
def check_command(self):
213+
"""
214+
Check the validity of the command. Does not check the command if
215+
the sync/incoming is overriden.
216+
:return: True if self.command is valid, False otherwise.
217+
"""
218+
219+
if isinstance(self.configured_commands, dict):
220+
for key in self.configured_commands.keys():
221+
if key not in [self.SYNC_COMMAND_SECTION,
222+
self.INCOMING_COMMAND_SECTION,
223+
self.COMMAND_PROPERTY]:
224+
self.logger.error("Unknown property '{}' for '{}'".
225+
format(key, self.name))
226+
return False
227+
228+
if self.command and not os.path.exists(self.command):
229+
self.logger.error("path for '{}' does not exist: {}".
230+
format(self.name, self.command))
231+
return False
232+
233+
return self._check_command()

tools/src/main/python/opengrok_tools/scm/svn.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2222
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
2323
#
2424

@@ -28,8 +28,8 @@
2828

2929

3030
class SubversionRepository(Repository):
31-
def __init__(self, logger, path, project, command, env, hooks, timeout):
32-
super().__init__(logger, path, project, command, env, hooks, timeout)
31+
def __init__(self, name, logger, path, project, command, env, hooks, timeout):
32+
super().__init__(name, logger, path, project, command, env, hooks, timeout)
3333

3434
self.command = self._repository_command(command, default=lambda: which('svn'))
3535

0 commit comments

Comments
 (0)