|
51 | 51 |
|
52 | 52 | __version__ = "0.2"
|
53 | 53 |
|
| 54 | +# "constants" |
| 55 | +HOOK_TIMEOUT_PROPERTY = 'hook_timeout' |
| 56 | +CMD_TIMEOUT_PROPERTY = 'command_timeout' |
| 57 | +IGNORED_REPOS_PROPERTY = 'ignored_repos' |
| 58 | +PROXY_PROPERTY = 'proxy' |
| 59 | +COMMANDS_PROPERTY = 'commands' |
| 60 | +DISABLED_PROPERTY = 'disabled' |
| 61 | +HOOKDIR_PROPERTY = 'hookdir' |
| 62 | +HOOKS_PROPERTY = 'hooks' |
| 63 | +LOGDIR_PROPERTY = 'logdir' |
| 64 | +PROJECTS_PROPERTY = 'projects' |
| 65 | + |
| 66 | +# This is a special exit code that is recognized by sync.py to terminate |
| 67 | +# the processing of the command sequence. |
| 68 | +CONTINUE_EXITVAL = 2 |
| 69 | + |
| 70 | + |
| 71 | +def get_repos_for_project(logger, project, source_root, config, proxy, |
| 72 | + command_timeout, ignored_repos, uri): |
| 73 | + """ |
| 74 | + :param logger: logger |
| 75 | + :param project: project name |
| 76 | + :param source_root: source root path |
| 77 | + :param config: configuration dictionary |
| 78 | + :param proxy: proxy |
| 79 | + :param command_timeout: command timeout |
| 80 | + :param ignored_repos: list of ignored repositories |
| 81 | + :param uri: URI for the web application |
| 82 | + :return: tuple of lists of Repository objects and return value |
| 83 | + (1 on failure, 0 on success) |
| 84 | + """ |
| 85 | + repos = [] |
| 86 | + ret = 0 |
| 87 | + for repo_path in get_repos(logger, project, uri): |
| 88 | + logger.debug("Repository path = {}".format(repo_path)) |
| 89 | + |
| 90 | + if repo_path in ignored_repos: |
| 91 | + logger.info("repository {} ignored".format(repo_path)) |
| 92 | + continue |
| 93 | + |
| 94 | + repo_type = get_repo_type(logger, repo_path, uri) |
| 95 | + if not repo_type: |
| 96 | + logger.error("cannot determine type of {}". |
| 97 | + format(repo_path)) |
| 98 | + continue |
| 99 | + |
| 100 | + logger.debug("Repository type = {}".format(repo_type)) |
| 101 | + |
| 102 | + repo = get_repository(logger, |
| 103 | + source_root + repo_path, |
| 104 | + repo_type, |
| 105 | + project, |
| 106 | + config.get(COMMANDS_PROPERTY), |
| 107 | + proxy, |
| 108 | + None, |
| 109 | + command_timeout) |
| 110 | + if not repo: |
| 111 | + logger.error("Cannot get repository for {}". |
| 112 | + format(repo_path)) |
| 113 | + ret = 1 |
| 114 | + else: |
| 115 | + repos.add(repo) |
| 116 | + |
| 117 | + return repos, ret |
| 118 | + |
54 | 119 |
|
55 | 120 | def main():
|
56 | 121 | ret = 0
|
57 | 122 |
|
58 |
| - # "constants" |
59 |
| - HOOK_TIMEOUT_PROPERTY = 'hook_timeout' |
60 |
| - CMD_TIMEOUT_PROPERTY = 'command_timeout' |
61 |
| - IGNORED_REPOS_PROPERTY = 'ignored_repos' |
62 |
| - PROXY_PROPERTY = 'proxy' |
63 |
| - COMMANDS_PROPERTY = 'commands' |
64 |
| - DISABLED_PROPERTY = 'disabled' |
65 |
| - HOOKDIR_PROPERTY = 'hookdir' |
66 |
| - HOOKS_PROPERTY = 'hooks' |
67 |
| - LOGDIR_PROPERTY = 'logdir' |
68 |
| - PROJECTS_PROPERTY = 'projects' |
69 |
| - |
70 | 123 | parser = argparse.ArgumentParser(description='project mirroring')
|
71 | 124 |
|
72 | 125 | parser.add_argument('project')
|
@@ -282,67 +335,59 @@ def main():
|
282 | 335 | if project_config.get(DISABLED_PROPERTY):
|
283 | 336 | logger.info("Project {} disabled, exiting".
|
284 | 337 | format(args.project))
|
285 |
| - sys.exit(2) |
| 338 | + sys.exit(CONTINUE_EXITVAL) |
286 | 339 |
|
287 | 340 | lock = FileLock(os.path.join(tempfile.gettempdir(),
|
288 | 341 | args.project + "-mirror.lock"))
|
289 | 342 | try:
|
290 | 343 | with lock.acquire(timeout=0):
|
291 | 344 | proxy = config.get(PROXY_PROPERTY) if use_proxy else None
|
| 345 | + |
| 346 | + # |
| 347 | + # Cache the repositories first. This way it will be known that |
| 348 | + # something is not right, avoiding any needless pre-hook run. |
| 349 | + # |
| 350 | + repos, ret = get_repos_for_project(logger, args.project, |
| 351 | + source_root, config, proxy, |
| 352 | + command_timeout, ignored_repos, |
| 353 | + uri) |
| 354 | + if ret == 1: |
| 355 | + # The error was already logged in get_repos_for_project() |
| 356 | + sys.exit(1) |
| 357 | + if len(repos) == 0: |
| 358 | + logger.info("No repositories for project {}". |
| 359 | + format(args.project)) |
| 360 | + sys.exit(CONTINUE_EXITVAL) |
| 361 | + |
292 | 362 | if prehook:
|
293 | 363 | logger.info("Running pre hook")
|
294 | 364 | if run_hook(logger, prehook,
|
295 | 365 | os.path.join(source_root, args.project), proxy,
|
296 | 366 | hook_timeout) != 0:
|
297 |
| - logger.error("pre hook failed") |
| 367 | + logger.error("pre hook failed for project {}". |
| 368 | + format(args.project)) |
298 | 369 | logging.shutdown()
|
299 | 370 | sys.exit(1)
|
300 | 371 |
|
301 | 372 | #
|
302 | 373 | # If one of the repositories fails to sync, the whole project sync
|
303 | 374 | # is treated as failed, i.e. the program will return 1.
|
304 | 375 | #
|
305 |
| - for repo_path in get_repos(logger, args.project, uri): |
306 |
| - logger.debug("Repository path = {}".format(repo_path)) |
307 |
| - |
308 |
| - if repo_path in ignored_repos: |
309 |
| - logger.info("repository {} ignored".format(repo_path)) |
310 |
| - continue |
311 |
| - |
312 |
| - repo_type = get_repo_type(logger, repo_path, uri) |
313 |
| - if not repo_type: |
314 |
| - logger.error("cannot determine type of {}". |
315 |
| - format(repo_path)) |
316 |
| - continue |
317 |
| - |
318 |
| - logger.debug("Repository type = {}".format(repo_type)) |
319 |
| - |
320 |
| - repo = get_repository(logger, |
321 |
| - source_root + repo_path, |
322 |
| - repo_type, |
323 |
| - args.project, |
324 |
| - config.get(COMMANDS_PROPERTY), |
325 |
| - proxy, |
326 |
| - None, |
327 |
| - command_timeout) |
328 |
| - if not repo: |
329 |
| - logger.error("Cannot get repository for {}". |
330 |
| - format(repo_path)) |
331 |
| - ret = 1 |
332 |
| - else: |
| 376 | + for repo in repos: |
333 | 377 | logger.info("Synchronizing repository {}".
|
334 |
| - format(repo_path)) |
| 378 | + format(repo.path)) |
335 | 379 | if repo.sync() != 0:
|
336 | 380 | logger.error("failed to sync repository {}".
|
337 |
| - format(repo_path)) |
| 381 | + format(repo.path)) |
338 | 382 | ret = 1
|
339 | 383 |
|
340 | 384 | if posthook:
|
341 | 385 | logger.info("Running post hook")
|
342 | 386 | if run_hook(logger, posthook,
|
343 | 387 | os.path.join(source_root, args.project), proxy,
|
344 | 388 | hook_timeout) != 0:
|
345 |
| - logger.error("post hook failed") |
| 389 | + logger.error("post hook failed for project {}". |
| 390 | + format(args.project)) |
346 | 391 | logging.shutdown()
|
347 | 392 | sys.exit(1)
|
348 | 393 | except Timeout:
|
|
0 commit comments