blob: 60ce74422f2ef7cf5eb84b25e481a9ff72bb481b [file] [log] [blame]
Antoine Mussod9ac4c12014-07-24 14:44:02 +02001#!/usr/bin/env python
2# Copyright 2014-2015 Antoine "hashar" Musso
3# Copyright 2014-2015 Wikimedia Foundation Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17# pylint: disable=locally-disabled, invalid-name
18
19"""
20Zuul references cleaner.
21
22Clear up references under /refs/zuul/ by inspecting the age of the commit the
23reference points to. If the commit date is older than a number of days
24specificed by --until, the reference is deleted from the git repository.
25
26Use --dry-run --verbose to finely inspect the script behavior.
27"""
28
29import argparse
30import git
31import logging
32import time
33import sys
34
35NOW = int(time.time())
36DEFAULT_DAYS = 360
37ZUUL_REF_PREFIX = 'refs/zuul/'
38
39parser = argparse.ArgumentParser(
40 description=__doc__,
41 formatter_class=argparse.RawDescriptionHelpFormatter,
42)
43parser.add_argument('--until', dest='days_ago', default=DEFAULT_DAYS, type=int,
44 help='references older than this number of day will '
45 'be deleted. Default: %s' % DEFAULT_DAYS)
46parser.add_argument('-n', '--dry-run', dest='dryrun', action='store_true',
47 help='do not delete references')
48parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
49 help='set log level from info to debug')
50parser.add_argument('gitrepo', help='path to a Zuul git repository')
51args = parser.parse_args()
52
53logging.basicConfig()
54log = logging.getLogger('zuul-clear-refs')
55if args.verbose:
56 log.setLevel(logging.DEBUG)
57else:
58 log.setLevel(logging.INFO)
59
60try:
61 repo = git.Repo(args.gitrepo)
62except git.exc.InvalidGitRepositoryError:
63 log.error("Invalid git repo: %s" % args.gitrepo)
64 sys.exit(1)
65
66for ref in repo.references:
67
68 if not ref.path.startswith(ZUUL_REF_PREFIX):
69 continue
70 if type(ref) is not git.refs.reference.Reference:
71 # Paranoia: ignore heads/tags/remotes ..
72 continue
73
74 try:
75 commit_ts = ref.commit.committed_date
76 except LookupError:
77 # GitPython does not properly handle PGP signed tags
78 log.exception("Error in commit: %s, ref: %s. Type: %s",
79 ref.commit, ref.path, type(ref))
80 continue
81
82 commit_age = int((NOW - commit_ts) / 86400) # days
83 log.debug(
84 "%s at %s is %3s days old",
85 ref.commit,
86 ref.path,
87 commit_age,
88 )
89 if commit_age > args.days_ago:
90 if args.dryrun:
91 log.info("Would delete old ref: %s (%s)", ref.path, ref.commit)
92 else:
93 log.info("Deleting old ref: %s (%s)", ref.path, ref.commit)
94 ref.delete(repo, ref.path)