Michał Górny avatar Michał Górny committed e621c79

Move main loop iteration code into VCSSupport.

Comments (0)

Files changed (2)

SmartLiveRebuild/core.py

 				out.s1('Updating the repositories using %s%d%s parallel jobs...' % (out.white, opts.jobs, out.s1reset))
 			packages = []
 
-			processes = []
-			items = list(rebuilds.values())
-			while True:
-				try:
-					if len(processes) < opts.jobs and len(items) > 0:
-						vcs = items.pop(0)
+			processes = list(rebuilds.values())
+			try:
+				while processes:
+					needsleep = True
+					for i, vcs in reversed(list(enumerate(processes[:opts.jobs]))):
 						try:
-							vcs.startupdate()
-							if opts.jobs == 1:
-								ret = vcs.endupdate(True)
-							else:
-								processes.append(vcs)
+							ret = vcs((opts.jobs == 1))
+							if ret is not None:
+								needsleep = False
+								if ret:
+									packages.extend(vcs.cpv)
+								del processes[i]
 						except KeyboardInterrupt:
-							vcs.abortupdate()
 							raise
-					elif len(processes) == 0: # which is true if jobs == 1 too
-						break
-					else:
-						time.sleep(0.3)
+						except Exception as e:
+							out.err('Error updating %s: [%s] %s' % (vcs.cpv, e.__class__.__name__, e))
+							erraneous.extend(vcs.cpv)
+							del processes[i]
 
-					for vcs in processes:
-						ret = vcs.endupdate()
-						if ret is not None:
-							processes.remove(vcs)
-							break
-
-					if ret:
-						packages.extend(vcs.cpv)
-				except KeyboardInterrupt:
-					out.err('Updates interrupted, proceeding with already updated repos.')
-					for vcs in processes:
-						vcs.abortupdate()
-					break
-				except Exception as e:
-					out.err('Error updating %s: [%s] %s' % (vcs.cpv, e.__class__.__name__, e))
-					if opts.jobs != 1 and vcs in processes:
-						processes.remove(vcs)
-					erraneous.extend(vcs.cpv)
+					if needsleep:
+						time.sleep(0.3)
+			except KeyboardInterrupt:
+				out.err('Updates interrupted, proceeding with already updated repos.')
+				for vcs in processes:
+					vcs.abortupdate()
 
 			if childpid == 0:
 				pdata = {'packages': packages, 'erraneous': erraneous}

SmartLiveRebuild/vcs/__init__.py

 		self.env = bash(self.reqenv + self.optenv)
 		self._opts = opts
 		self._settings = settings
+		self._running = False
 
 		missingvars = [v for v in self.reqenv if self.env[v] == '']
 		if len(missingvars) > 0:
 			raise KeyError('Environment does not declare: %s' % missingvars)
 
+	def __call__(self, blocking = False):
+		if not self._running:
+			self.startupdate()
+			self._running = True
+			if blocking:
+				return self.endupdate(True)
+			return None
+		else:
+			return self.endupdate()
+
 	def getpath(self):
 		raise NotImplementedError('VCS class needs to override getpath()')
 
 			raise Exception('update command returned non-zero result')
 
 	def abortupdate(self):
-		if self.subprocess is not None:
+		if self._running and self.subprocess is not None:
 			self.subprocess.terminate()
 
 	def __str__(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.