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
2525import abc
26+ import os
2627
2728from ..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 ()
0 commit comments