Skip to content

Instantly share code, notes, and snippets.

@bburgin
Last active April 22, 2022 11:48
Show Gist options
  • Save bburgin/b42f3a26a2e5661d2366dcdd2283b715 to your computer and use it in GitHub Desktop.
Save bburgin/b42f3a26a2e5661d2366dcdd2283b715 to your computer and use it in GitHub Desktop.
FASTBuild brokerage path cleanup script
#!/bin/env python3
#
# Script to cleanup FASTBuild brokerage path
# Written by David Curtiss
#
# MIT License
# Copyright (c) 2019, National Instruments Corp.
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import print_function
import os
import sys
import time
ONE_MIN = 60
ONE_HOUR = 3600
NOW = time.time()
ROOT_DIR = "//servername/fastbuild"
MAX_AGE_NEW_VERSIONS = 5 * ONE_MIN
MAX_AGE_OLD_VERSIONS = ONE_HOUR
MAX_AGE_DENYLIST = 24 * ONE_HOUR
def _main():
exceptions = 0
for path in _get_files():
try:
if "denylist" in path:
max_age = MAX_AGE_DENYLIST
elif _get_version(path) >= 21:
max_age = MAX_AGE_NEW_VERSIONS
else:
max_age = MAX_AGE_OLD_VERSIONS
if _is_old(path, max_age):
print("Removing", path)
os.remove(path)
except Exception as ex:
print("{} on {}: {}".format(type(ex).__name__, path, ex))
exceptions += 1
print()
print("Done")
if exceptions:
print()
print("Failed on {} paths (see above)".format(exceptions))
return 2
def _get_files():
for root, dirs, files in os.walk(ROOT_DIR.replace("/", os.path.sep)):
for fname in files:
yield os.path.join(root, fname)
def _is_old(path, max_age=MAX_AGE_NEW_VERSIONS, now=NOW):
return now - _get_mtime(path) > max_age
def _get_mtime(path):
return os.stat(path).st_mtime
def _get_version(path):
"""Get the FASTBuild version from the given path.
>>> _get_version(".../dev/main/20.windows/mymachine")
20
>>> _get_version("...\\dev\\main\\21.linux\\mymachine")
21
"""
try:
after_main = path.split("main", 1)[1] # e.g. "/20.windows/..."
return int(after_main[1:].split(".", 1)[0])
except Exception:
return 0
if __name__ == "__main__":
sys.exit(_main(*sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment