@@ -7,11 +7,12 @@
import signal
import os
+import subprocess
import sys
import threading
import time
-tmpfile = '/tmp/.buildworker-janitor'+os.getcwd().replace('/', '-')
+tmpfile = '/tmp/.buildworker-janitor' + os.getcwd().replace('/', '-')
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'scripts'))
@@ -19,8 +20,6 @@ import utils
ourconfig = utils.loadconfig()
-
-
trashdir = utils.getconfig("TRASH_DIR", ourconfig)
mirrordir = utils.getconfig("REPO_STASH_DIR", ourconfig)
@@ -41,22 +40,21 @@ def trash_processor(trashdir):
return
while True:
try:
- files = os.listdir(trashdir)
- if files:
- for file in files:
- file_path = trashdir + "/" + file
- file_age = time.time() - os.path.getmtime(file_path)
+ entries = os.listdir(trashdir)
+ if entries:
+ for entry in entries:
+ entry_path = os.path.join(trashdir, entry)
+ file_age = time.time() - os.path.getmtime(entry_path)
if file_age >= 60:
- os.system("nice -n 10 ionice -c 3 rm %s -rf" % file_path)
+ subprocess.run(["nice", "-n", "10", "ionice", "-c", "3",
+ "rm", "-rf", entry_path])
else:
- print("Not removing '%s' - age is only %s seconds. There may be another process using it" % (file_path, str(int(file_age))))
+ print("Not removing '%s' - age is only %d seconds. There may be another process using it" % (entry_path, int(file_age)))
else:
- time.sleep(120*60) # 30 minutes
+ time.sleep(120 * 60) # 2 hours
except Exception as e:
print("Exception %s in trash cleaner" % str(e))
- pass
- time.sleep(60) # 1 minute timeout to prevent crazy looping (particularly in age is only)
- return
+ time.sleep(60) # 1 minute timeout to prevent crazy looping
def mirror_processor(mirrordir):
print("Updating mirrors in %s" % mirrordir)
@@ -65,18 +63,16 @@ def mirror_processor(mirrordir):
mirror = ourconfig["repo-defaults"][repo]["url"]
mirrorpath = os.path.join(mirrordir, repo)
mirrorpaths.append(mirrorpath)
- while not os.path.exists(mirrorpath + "/refs"):
- exit_code = os.system("git clone --bare --mirror %s %s" % (mirror, mirrorpath))
- if exit_code == 0:
+ while not os.path.exists(os.path.join(mirrorpath, "refs")):
+ result = subprocess.run(["git", "clone", "--bare", "--mirror", mirror, mirrorpath])
+ if result.returncode == 0:
break
print("Failed to clone %s. Retrying in 30 seconds..." % mirror)
time.sleep(30)
while True:
for path in mirrorpaths:
- os.chdir(path)
- os.system("git fetch --prune --all")
- time.sleep(30*60) # 30 minutes
- return
+ subprocess.run(["git", "fetch", "--prune", "--all"], cwd=path)
+ time.sleep(30 * 60) # 30 minutes
# Check to see if this is running already. If so, kill it and rerun
if os.path.exists(tmpfile) and os.path.isfile(tmpfile):
@@ -109,7 +105,7 @@ elif os.path.exists(tmpfile) and not os.path.isfile(tmpfile):
raise Exception("/tmp/.buildworker-janitor is a directory. remove it to continue.")
try:
os.unlink(tmpfile)
-except:
+except Exception:
pass
with open(tmpfile, 'w') as f:
print(os.getpid(), file=f)
Replaced bare exceptions, fixed variable shadowing, and removed unreachable code. Corrected time in comment. Normalized spacing and simplified string formatting. Signed-off-by: Michael Halstead <mhalstead@linuxfoundation.org> --- janitor/ab-janitor | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)