Explorar o código

WIP: added cache_valid_time

Sven Velt %!s(int64=8) %!d(string=hai) anos
pai
achega
12e2fb6485
Modificáronse 1 ficheiros con 39 adicións e 8 borrados
  1. 39 8
      xbps.py

+ 39 - 8
xbps.py

@@ -1,14 +1,41 @@
 #!/usr/bin/python2.7 -tt
 # -*- coding: utf-8 -*-
 
-def update_package_db(module, paths):
-	cmd = "%s -S" % paths['install']
+import glob
+import os
+import re
+import time
+
+
+def update_package_db(module, paths, cache_valid_time):
+	cmd = "%s -L" % paths['query']
 	rc, sout, serr = module.run_command(cmd, check_rc=False)
 
-	if rc == 0:
-		return True
+	# all repo indexes not older than cache_valid_time seconds?
+	cache_valid = False
+	if cache_valid_time:
+		cache_valid = True
+		matcher = re.compile('(http[^ ]+)')
+		now = time.time()
+		for line in sout:
+			repo = matcher.search(line)
+			for repofile in glob.glob('/var/db/xbps/%s/*-repodata' % repo):
+				ctime = os.stat(repofile).st_ctime
+
+				if ctime:
+					if ctime + cache_valid_time < now:
+						cache_valid = False
+
+	if not cache_valid:
+		cmd = "%s -S" % paths['install']
+		rc, sout, serr = module.run_command(cmd, check_rc=False)
+
+		if rc == 0:
+			return True
+		else:
+			module.fail_json(msg="could not update package db", rc=rc, stdout=sout, stderr=serr)
 	else:
-		module.fail_json(msg="could not update package db", rc=rc, stdout=sout, stderr=serr)
+		return False
 
 
 
@@ -156,7 +183,8 @@ def main():
 			force        = dict(default=False, type='bool'),
 			recurse      = dict(default=False, type='bool'),
 			upgrade      = dict(default=False, type='bool'),
-			update_cache = dict(default=False, aliases=['update-cache'], type='bool')
+			update_cache = dict(default=False, aliases=['update-cache'], type='bool'),
+			cache_valid_time = dict(type='int'),
 		),
 		required_one_of = [['name', 'update_cache', 'upgrade']],
 		supports_check_mode = True)
@@ -181,9 +209,12 @@ def main():
 
 	# sync repo index
 	if p["update_cache"] and not module.check_mode:
-		update_package_db(module, paths) # FIXME
+		updated = update_package_db(module, paths, p.get('cache_valid_time'))
 		if not (p['name'] or p['upgrade']):
-			module.exit_json(changed=True, msg='Updated the package master lists')
+			if updated:
+				module.exit_json(changed=True, msg='Updated the package master lists')
+			else:
+				module.exit_json(changed=False, msg='Package master lists uptodate')
 
 	if p['update_cache'] and module.check_mode and not (p['name'] or p['upgrade']):
 		module.exit_json(changed=True, msg='Would have updated the package cache')