Anonymous avatar 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'.

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
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.