Antoine Musso | d9ac4c1 | 2014-07-24 14:44:02 +0200 | [diff] [blame] | 1 | #!/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 | """ |
| 20 | Zuul references cleaner. |
| 21 | |
| 22 | Clear up references under /refs/zuul/ by inspecting the age of the commit the |
| 23 | reference points to. If the commit date is older than a number of days |
| 24 | specificed by --until, the reference is deleted from the git repository. |
| 25 | |
| 26 | Use --dry-run --verbose to finely inspect the script behavior. |
| 27 | """ |
| 28 | |
| 29 | import argparse |
| 30 | import git |
| 31 | import logging |
| 32 | import time |
| 33 | import sys |
| 34 | |
| 35 | NOW = int(time.time()) |
| 36 | DEFAULT_DAYS = 360 |
| 37 | ZUUL_REF_PREFIX = 'refs/zuul/' |
| 38 | |
| 39 | parser = argparse.ArgumentParser( |
| 40 | description=__doc__, |
| 41 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 42 | ) |
| 43 | parser.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) |
| 46 | parser.add_argument('-n', '--dry-run', dest='dryrun', action='store_true', |
| 47 | help='do not delete references') |
| 48 | parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', |
| 49 | help='set log level from info to debug') |
| 50 | parser.add_argument('gitrepo', help='path to a Zuul git repository') |
| 51 | args = parser.parse_args() |
| 52 | |
| 53 | logging.basicConfig() |
| 54 | log = logging.getLogger('zuul-clear-refs') |
| 55 | if args.verbose: |
| 56 | log.setLevel(logging.DEBUG) |
| 57 | else: |
| 58 | log.setLevel(logging.INFO) |
| 59 | |
| 60 | try: |
| 61 | repo = git.Repo(args.gitrepo) |
| 62 | except git.exc.InvalidGitRepositoryError: |
| 63 | log.error("Invalid git repo: %s" % args.gitrepo) |
| 64 | sys.exit(1) |
| 65 | |
| 66 | for 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) |