Module copr_prune_results
[hide private]
[frames] | no frames]

Source Code for Module copr_prune_results

  1  #!/usr/bin/python 
  2   
  3  from __future__ import print_function 
  4  from __future__ import unicode_literals 
  5  from __future__ import division 
  6  from __future__ import absolute_import 
  7   
  8  import os 
  9  import sys 
 10  import logging 
 11  from subprocess import Popen, PIPE 
 12   
 13  from copr.client.exceptions import CoprException, CoprRequestException 
 14   
 15  sys.path.append("/usr/share/copr/") 
 16   
 17  from backend.helpers import BackendConfigReader, get_auto_createrepo_status 
 18  from backend.createrepo import createrepo_unsafe 
 19   
 20  log = logging.getLogger(__name__) 
 21  log.addHandler(logging.NullHandler()) 
 22   
 23  DEF_DAYS = 14 
 24  DEF_PRUNE_SCRIPT = "/usr/bin/copr_prune_old_builds.sh" 
 25   
 26   
27 -def list_subdir(path):
28 dir_names = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))] 29 return dir_names, map(lambda x: os.path.join(path, x), dir_names)
30 31
32 -def prune_project(opts, path, username, projectname):
33 log.debug("Going to prune {}/{}".format(username, projectname)) 34 # get ACR 35 try: 36 if not get_auto_createrepo_status(opts.frontend_base_url, username, projectname): 37 log.debug("Skipped {}/{} since auto createrepo option is disabled" 38 .format(username, projectname)) 39 return 40 except (CoprException, CoprRequestException) as exception: 41 log.debug("Failed to get project details for {}/{} with error: {}".format( 42 username, projectname, exception)) 43 return 44 45 # run prune project sh 46 days = getattr(opts, "prune_days", DEF_DAYS) 47 48 cmd = map(str, [DEF_PRUNE_SCRIPT, path, days]) 49 50 handle = Popen(cmd, stdout=PIPE, stderr=PIPE) 51 stdout, stderr = handle.communicate() 52 53 if handle.returncode != 0: 54 print("Failed to prune old builds for copr {}/{}".format(username, projectname)) 55 print("STDOUT: \n{}".format(stdout.decode())) 56 print("STDERR: \n{}".format(stderr.decode())) 57 return 58 59 # run createrepo 60 log.debug("Prune done for {}/{}".format(username, projectname)) 61 try: 62 retcode, stdout, stderr = createrepo_unsafe(path) 63 if retcode != 0: 64 print("Createrepo for {}/{} failed".format(username, projectname)) 65 print("STDOUT: \n{}".format(stdout.decode())) 66 print("STDERR: \n{}".format(stderr.decode())) 67 except Exception as exception: 68 print("Createrepo for {}/{} failed with error: {}" 69 .format(username, projectname, exception))
70 71
72 -def main():
73 config_file = os.environ.get("BACKEND_CONFIG", "/etc/copr/copr-be.conf") 74 opts = BackendConfigReader(config_file).read() 75 76 results_dir = opts.destdir 77 log.info("Pruning results dir: {} ".format(results_dir)) 78 user_dir_names, user_dirs = list_subdir(results_dir) 79 80 print("Going to process total number: {} of user's directories".format(len(user_dir_names))) 81 log.info("Going to process user's directories: {}".format(user_dir_names)) 82 83 counter = 0 84 for username, subpath in zip(user_dir_names, user_dirs): 85 log.debug("For user `{}` exploring path: {}".format(username, subpath)) 86 for projectname, project_path in zip(*list_subdir(subpath)): 87 log.debug("Exploring project `{}` with path: {}".format(projectname, project_path)) 88 prune_project(opts, project_path, username, projectname) 89 90 counter += 1 91 print("Pruned {}. projects".format(counter)) 92 93 print("Pruning finished")
94 95 96 if __name__ == "__main__": 97 # logging.basicConfig( 98 # level=logging.DEBUG, 99 # format='[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s', 100 # datefmt='%H:%M:%S' 101 # ) 102 main() 103