Commits

Michał Górny  committed ac0e2fa

Move action-specific code out of SmartLiveRebuild.core.

Make the SmartLiveRebuild() function simply return the updated package
list. Perform further actions in the smart-live-rebuild script. Move the
emerge-child-detect code there too.

  • Participants
  • Parent commits 06861f6

Comments (0)

Files changed (2)

File SmartLiveRebuild/core.py

 
 		return val
 
-def SmartLiveRebuild(opts, args):
+class SLRFailure(Exception):
+	pass
+
+def SmartLiveRebuild(opts):
 	if not opts.color:
 		out.monochromize()
 
-	if not opts.pretend:
-		try:
-			import psutil
-
-			def getproc(pid):
-				for ps in psutil.get_process_list():
-					if pid == ps.pid:
-						return ps
-				raise Exception()
-
-			def getscriptname(ps):
-				if os.path.basename(ps.cmdline[0]) != ps.name:
-					return ps.cmdline[0]
-				cmdline = ps.cmdline[1:]
-				while cmdline[0].startswith('-'): # omit options
-					cmdline.pop(0)
-				return os.path.basename(cmdline[0])
-
-			ps = getproc(os.getppid())
-			# traverse upstream to find the emerge process
-			while ps.pid > 1:
-				if getscriptname(ps) == 'emerge':
-					out.s1('Running under the emerge process, assuming --pretend.')
-					opts.pretend = True
-					break
-				ps = ps.parent
-		except Exception:
-			pass
-
 	if opts.setuid and 'userpriv' not in portage.settings.features:
 		out.err('setuid requested but FEATURES=userpriv not set, assuming --no-setuid.')
 		opts.setuid = False
 	if opts.local_rev and not opts.network:
 		out.err('The --local-rev and --no-network options can not be specified together.')
-		return 1
+		raise SLRFailure('')
 	if opts.jobs <= 0:
 		out.err('The argument to --jobs option must be a positive integer.')
-		return 1
+		raise SLRFailure('')
 	elif opts.jobs > 1 and not opts.network:
 		out.s1('Using parallel jobs with --no-network is inefficient, assuming no --jobs.')
 		opts.jobs = 1
 privileges. If you would like to force running the update using your current
 user account, please pass the --unprivileged-user option.
 ''')
-		return 1
+		raise SLRFailure('')
 
 	try:
 		if not childpid:
 				pdata = {'packages': packages, 'erraneous': erraneous}
 				pipe = os.fdopen(commpipe[1], 'wb')
 				pickle.dump(pdata, pipe, pickle.HIGHEST_PROTOCOL)
-				return 0
+				pipe.flush()
+				pipe.close()
+				os._exit(0)
 		else:
 			os.close(commpipe[1])
 			pipe = os.fdopen(commpipe[0], 'rb')
 			try:
 				pdata = pickle.load(pipe)
 			except EOFError: # child terminated early
-				return 1
+				raise SLRFailure('')
 			signal.signal(signal.SIGINT, sigint)
 			packages = pdata['packages']
 			erraneous = pdata['erraneous']
 			out.s2(' '.join(cmd))
 			subprocess.Popen(cmd, stdout=sys.stderr).wait()
 
+		if opts.offline:
+			if opts.erraneous_merge and len(erraneous) > 0:
+				out.s1('Merging update-failed packages, assuming --no-offline.')
+			else:
+				os.putenv('ESCM_OFFLINE', 'true')
+
 		if len(packages) < 1:
 			out.s1('No updates found')
-		elif opts.pretend:
-			out.s1('Printing a list of updated packages ...')
-			if opts.erraneous_merge and len(erraneous) > 0:
-				out.s2('(please notice that it contains the update-failed ones as well)')
-			for p in packages:
-				print('>=%s' % p)
 		else:
-			if opts.erraneous_merge and len(erraneous) > 0:
-				if opts.offline:
-					out.s1('Merging update-failed packages, assuming --no-offline.')
-					opts.offline = False
-
-			out.s1('Calling emerge to rebuild %s%d%s packages ...' % (out.white, len(packages), out.s1reset))
-			if opts.offline:
-				os.putenv('ESCM_OFFLINE', 'true')
-			cmd = ['emerge', '--oneshot']
-			cmd.extend(args)
-			cmd.extend(['>=%s' % x for x in packages])
-			out.s2(' '.join(cmd))
-			os.execv('/usr/bin/emerge', cmd)
+			out.s1('Found %s%d%s packages to rebuild.' % (out.white, len(packages), out.s1reset))
 	finally:
 		if childpid: # make sure that we leave no orphans
 			os.kill(childpid, signal.SIGTERM)
 
-	return 0
+	return packages

File smart-live-rebuild

 # (c) 2010 Michał Górny <gentoo@mgorny.alt.pl>
 # Released under the terms of the 3-clause BSD license or the GPL-2 license.
 
-import sys
+import os, sys
 
-from SmartLiveRebuild.core import Config, SmartLiveRebuild
+from SmartLiveRebuild.core import Config, SmartLiveRebuild, SLRFailure
 from SmartLiveRebuild.cli import parse_options
+from SmartLiveRebuild.output import out
 
 def main(argv):
 	# initialize config with defaults
 
 	# and now reapply the options to override config file defaults
 	c.apply_optparse(opts)
+	opts = c.get_options()
 
-	return SmartLiveRebuild(c.get_options(), args)
+	if not opts.pretend:
+		try:
+			import psutil
+
+			def getproc(pid):
+				for ps in psutil.get_process_list():
+					if pid == ps.pid:
+						return ps
+				raise Exception()
+
+			def getscriptname(ps):
+				if os.path.basename(ps.cmdline[0]) != ps.name:
+					return ps.cmdline[0]
+				cmdline = ps.cmdline[1:]
+				while cmdline[0].startswith('-'): # omit options
+					cmdline.pop(0)
+				return os.path.basename(cmdline[0])
+
+			ps = getproc(os.getppid())
+			# traverse upstream to find the emerge process
+			while ps.pid > 1:
+				if getscriptname(ps) == 'emerge':
+					out.s1('Running under the emerge process, assuming --pretend.')
+					opts.pretend = True
+					break
+				ps = ps.parent
+		except Exception:
+			pass
+
+	try:
+		packages = SmartLiveRebuild(opts)
+	except SLRFailure:
+		return 1
+
+	if opts.pretend:
+		for p in packages:
+			print('>=%s' % p)
+		return 0
+	else:
+		cmd = ['emerge', '--oneshot']
+		cmd.extend(args)
+		cmd.extend(['>=%s' % x for x in packages])
+		out.s2(' '.join(cmd))
+		os.execv('/usr/bin/emerge', cmd)
+		return 126
 
 if __name__ == '__main__':
 	sys.exit(main(sys.argv))