Commit ba2603a5 authored by Seblu's avatar Seblu
Browse files

Refactor package update

- Move maintainer check in update. So, no more maintainer check if there is
no need to build the update
- Move build code in a subfunction
- Improve some messages
parent 17095ada
Loading
Loading
Loading
Loading
+46 −42
Original line number Original line Diff line number Diff line
@@ -280,8 +280,8 @@ class Aurbot(object):
		msg["Date"] = formatdate(localtime=True)
		msg["Date"] = formatdate(localtime=True)
		self.send_message(pkgconfig, msg)
		self.send_message(pkgconfig, msg)


	def update(self, pkgconfig, localpkg, aurpkg):
	def build(self, pkgconfig, localpkg, aurpkg):
		''' Update (build and commit) a package
		''' Build a package
		'''
		'''
		# register the build
		# register the build
		localpkg.lastbuild = time()
		localpkg.lastbuild = time()
@@ -295,8 +295,8 @@ class Aurbot(object):
		debug("Extracting aur tarball")
		debug("Extracting aur tarball")
		aurpkg.extract(build_dir.name)
		aurpkg.extract(build_dir.name)
		with open(fp, "w") as fd:
		with open(fp, "w") as fd:
			try:
			cwd = getcwd()
			cwd = getcwd()
			try:
				chdir("%s/%s" % (build_dir.name, aurpkg.name))
				chdir("%s/%s" % (build_dir.name, aurpkg.name))
				# build
				# build
				info("Starting build command")
				info("Starting build command")
@@ -327,18 +327,35 @@ class Aurbot(object):
					end_time = time()
					end_time = time()
					info("Commit duration: %.2fs" % (end_time - start_time))
					info("Commit duration: %.2fs" % (end_time - start_time))
					fd.write("Commit duration: %.2fs\n" % (end_time - start_time))
					fd.write("Commit duration: %.2fs\n" % (end_time - start_time))
				status = True
				chdir(cwd)
				# we have to register after chdir in the original directory
				localpkg.lastsuccess = aurpkg.lastmodified
				return True
			except Exception as exp:
			except Exception as exp:
				error("Update failure: %s" % exp)
				error("Update failure: %s" % exp)
				status = False
			finally:
				chdir(cwd)
				chdir(cwd)
				# we have to register after chdir in the original directory
				# we have to register after chdir in the original directory
		if status:
			localpkg.lastsuccess = aurpkg.lastmodified
		else:
				localpkg.lastfailed = aurpkg.lastmodified
				localpkg.lastfailed = aurpkg.lastmodified
		# notify
				return False

	def update(self, pkgconfig, localpkg, aurpkg):
		''' Update (build and commit) a package
		'''
		debug("Updating %s" % aurpkg.name)
		# for security, if the maintainer is incorrect we fail
		maintainer = pkgconfig.get("maintainer")
		debug("Configured maintainer: %s" % maintainer)
		debug("AUR maintainer: %s" % aurpkg.maintainer)
		debug("Last maintainer: %s" % localpkg.lastmaintainer)
		if maintainer != aurpkg.maintainer:
			# we notify by mail only once the maintainer is invalid
			if localpkg.lastmaintainer != aurpkg.maintainer:
				self.send_maintainer_report(pkgconfig, localpkg, aurpkg)
				localpkg.lastmaintainer = aurpkg.maintainer
			error("Invalid maintainer for package %s" % aurpkg.name)
			return
		localpkg.lastmaintainer = aurpkg.maintainer
		status = self.build(pkgconfig, localpkg, aurpkg)
		if "notify" in pkgconfig:
		if "notify" in pkgconfig:
			self.send_build_report(pkgconfig, localpkg, aurpkg, status, fp)
			self.send_build_report(pkgconfig, localpkg, aurpkg, status, fp)


@@ -357,10 +374,10 @@ class Aurbot(object):
					if "build_cmd" not in pkgconfig:
					if "build_cmd" not in pkgconfig:
						error("build_cmd is missing in config file")
						error("build_cmd is missing in config file")
						continue
						continue
					local = LocalPackage(pkgname)
					localpkg = LocalPackage(pkgname)
					check_interval = pkgconfig.getint("check_interval", DEFAULT_CHECK_INTERVAL)
					check_interval = pkgconfig.getint("check_interval", DEFAULT_CHECK_INTERVAL)
					debug("Check interval is %ss" % check_interval)
					debug("Check interval is %ss" % check_interval)
					check_delta = int(local.lastchecked - time() + check_interval)
					check_delta = int(localpkg.lastchecked - time() + check_interval)
					debug("Check delta is %ss" % check_delta)
					debug("Check delta is %ss" % check_delta)
					if check_delta > 0:
					if check_delta > 0:
						# next check is in the future
						# next check is in the future
@@ -370,31 +387,18 @@ class Aurbot(object):
					next_checks.add(check_interval)
					next_checks.add(check_interval)
					# get remote data
					# get remote data
					try:
					try:
						aur = AURPackage(pkgname, pkgconfig.getint("timeout"))
						aurpkg = AURPackage(pkgname, pkgconfig.getint("timeout"))
						local.lastchecked = int(time())
						localpkg.lastchecked = int(time())
					except Exception as exp:
					except Exception as exp:
						error("Unable to get AUR package info: %s" % exp)
						error("Unable to get AUR package info: %s" % exp)
						continue
						continue
					# For security, if the maintainer has changed we pass
					# few debug printing
					maintainer = pkgconfig.get("maintainer")
					debug("AUR last modified: %s" % aurpkg.lastmodified)
					if maintainer != aur.maintainer:
					debug("Local last success lastmodified: %s" % localpkg.lastbuild)
						debug("Configured maintainer: %s" % maintainer)
					debug("Local last failed lastmodified: %s" % localpkg.lastfailed)
						debug("AUR maintainer: %s" % aur.maintainer)
					debug("Local last build time: %s" % localpkg.lastbuild)
						debug("Last maintainer: %s" % local.lastmaintainer)
					# check if package need to be updated
						# we notify by mail if the aur has changed
					if localpkg.lastsuccess >= aurpkg.lastmodified :
						if local.lastmaintainer != aur.maintainer:
							self.send_maintainer_report(pkgconfig, local, aur)
						local.lastmaintainer = aur.maintainer
						error("Invalid maintainer for package %s" % pkgname)
						continue
					local.lastmaintainer = aur.maintainer
					# checks update
					debug("AUR last modified: %s" % aur.lastmodified)
					debug("Local last success lastmodified: %s" % local.lastbuild)
					debug("Local last failed lastmodified: %s" % local.lastfailed)
					debug("Local last build time: %s" % local.lastbuild)
					# build new aur version
					if local.lastsuccess >= aur.lastmodified :
						if "force" in pkgconfig:
						if "force" in pkgconfig:
							info("Up to date, but force value is present.")
							info("Up to date, but force value is present.")
							if pkgconfig["force"].isdigit() is False:
							if pkgconfig["force"].isdigit() is False:
@@ -405,20 +409,20 @@ class Aurbot(object):
							now = int(time())
							now = int(time())
							force = int(pkgconfig["force"])
							force = int(pkgconfig["force"])
							debug("Force is: %ss" % force)
							debug("Force is: %ss" % force)
							force_delta = local.lastbuild - now + force
							force_delta = localpkg.lastbuild - now + force
							debug("Force Delta is: %ss" % force_delta)
							debug("Force Delta is: %ss" % force_delta)
							if force_delta < 0:
							if force_delta < 0:
								info("Forced update")
								info("Forced update")
								self.update(pkgconfig, local, aur)
								self.update(pkgconfig, localpkg, aurpkg)
							else:
							else:
								info("Next forced update in %ss" % force_delta)
								info("Next forced update in %ss" % force_delta)
						else:
						else:
							info("Up to date, nothing to do.")
							info("Up to date, nothing to do.")
					elif local.lastfailed >= aur.lastmodified:
					elif localpkg.lastfailed >= aurpkg.lastmodified:
						info("Last build has failed. We skip.")
						warning("Last build has failed, skipping. Remove lastfailed file to retry.")
					else:
					else:
						info("New version available: %s" % aur.version)
						info("New version available: %s" % aurpkg.version)
						self.update(pkgconfig, local, aur)
						self.update(pkgconfig, localpkg, aurpkg)
				# sleep until next check
				# sleep until next check
				# len(next_checks) is 0 when there is no package configured
				# len(next_checks) is 0 when there is no package configured
				timeout = min(next_checks) if len(next_checks) > 0 else DEFAULT_CHECK_INTERVAL
				timeout = min(next_checks) if len(next_checks) > 0 else DEFAULT_CHECK_INTERVAL