Commits

Anonymous committed 94945b6

Support detecting whether we're running under emerge.

Optionally use 'psutil' Python module to traverse our process tree and
find out whether we're running under emerge. If we do, then we're
certainly being called as a set, and we should assume '--pretend'.

  • Participants
  • Parent commits d5ec567

Comments (0)

Files changed (3)

 sets.conf (/etc/portage/sets.conf) file. You may see the 'sets.conf.example'
 file for reference.
 
-Please remember than you need to explicitly specify '--pretend' ('-p') when
-being called from sets.conf. Otherwise, the program will execute second emerge
-instance.
+Please notice that if you don't have the 'psutil' Python module (required to
+traverse the process tree) you need to explicitly specify '--pretend' ('-p')
+when being called from sets.conf. Otherwise, the program will spawn second
+emerge instance.
 
 
 CONFIGURATION FILE

sets.conf.example

 # --alternatively--
 #command = smart-live-rebuild -pQj16
 
-# Please notice that '--pretend' argument is obligatory; else
-# smart-live-rebuild will call another emerge process.
+# Please notice that '--pretend' argument is obligatory unless psutil
+# Python module is available; else smart-live-rebuild will spawn another
+# emerge process.

smart-live-rebuild.py

 	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