18
18
#
19
19
20
20
#
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.
22
22
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected] >
23
23
#
24
24
25
25
import abc
26
+ import os
26
27
27
28
from ..utils .command import Command
28
29
@@ -43,9 +44,11 @@ class Repository:
43
44
44
45
SYNC_COMMAND_SECTION = 'sync'
45
46
INCOMING_COMMAND_SECTION = 'incoming'
47
+ COMMAND_PROPERTY = 'command'
46
48
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
49
52
self .logger = logger
50
53
self .path = path
51
54
self .project = project
@@ -59,12 +62,16 @@ def __init__(self, logger, path, project, configured_commands, env, hooks,
59
62
def __str__ (self ):
60
63
return self .path
61
64
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
+ """
63
71
kwargs ['timeout' ] = self .timeout
64
72
return Command (cmd , ** kwargs )
65
73
66
74
def sync (self ):
67
- # Eventually, there might be per-repository hooks added here.
68
75
if self .is_command_overridden (self .configured_commands , self .SYNC_COMMAND_SECTION ):
69
76
return self ._run_custom_sync_command (
70
77
self .listify (self .configured_commands [self .SYNC_COMMAND_SECTION ])
@@ -77,6 +84,8 @@ def reposync(self):
77
84
Synchronize the repository by running sync command specific for
78
85
given repository type.
79
86
87
+ This method definition has to be overriden by given repository class.
88
+
80
89
Return 1 on failure, 0 on success.
81
90
"""
82
91
raise NotImplementedError ()
@@ -96,6 +105,8 @@ def incoming(self):
96
105
def incoming_check (self ):
97
106
"""
98
107
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.
99
110
100
111
Return True if so, False otherwise.
101
112
"""
@@ -137,8 +148,8 @@ def _run_command(self, command):
137
148
- status: 0 on success execution, non-zero otherwise
138
149
- output: command output as string
139
150
"""
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 )
142
153
cmd .execute ()
143
154
if cmd .getretcode () != 0 or cmd .getstate () != Command .FINISHED :
144
155
cmd .log_error ("failed to perform command" )
@@ -164,7 +175,7 @@ def _repository_command(configured_commands, default=lambda: None):
164
175
if isinstance (configured_commands , str ):
165
176
return configured_commands
166
177
elif isinstance (configured_commands , dict ) and \
167
- configured_commands .get ('command' ):
178
+ configured_commands .get ('command' ): # COMMAND_PROPERTY
168
179
return configured_commands ['command' ]
169
180
170
181
return default ()
@@ -185,3 +196,38 @@ def is_command_overridden(config, command):
185
196
:return: true if overridden, false otherwise
186
197
"""
187
198
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