| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | # Low level unix utility functions | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2016  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							| 
									
										
										
										
											2017-01-09 23:33:23 -05:00
										 |  |  | import sys, os, pty, fcntl, termios, signal, logging | 
					
						
							|  |  |  | import subprocess, traceback, shlex | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Return the SIGINT interrupt handler back to the OS default | 
					
						
							|  |  |  | def fix_sigint(): | 
					
						
							|  |  |  |     signal.signal(signal.SIGINT, signal.SIG_DFL) | 
					
						
							|  |  |  | fix_sigint() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Set a file-descriptor as non-blocking | 
					
						
							|  |  |  | def set_nonblock(fd): | 
					
						
							|  |  |  |     fcntl.fcntl(fd, fcntl.F_SETFL | 
					
						
							|  |  |  |                 , fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-09 19:04:30 -05:00
										 |  |  | # Clear HUPCL flag | 
					
						
							|  |  |  | def clear_hupcl(fd): | 
					
						
							|  |  |  |     attrs = termios.tcgetattr(fd) | 
					
						
							|  |  |  |     attrs[2] = attrs[2] & ~termios.HUPCL | 
					
						
							|  |  |  |     termios.tcsetattr(fd, termios.TCSADRAIN, attrs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | # Support for creating a pseudo-tty for emulating a serial port | 
					
						
							|  |  |  | def create_pty(ptyname): | 
					
						
							|  |  |  |     mfd, sfd = pty.openpty() | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         os.unlink(ptyname) | 
					
						
							|  |  |  |     except os.error: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  |     os.symlink(os.ttyname(sfd), ptyname) | 
					
						
							|  |  |  |     fcntl.fcntl(mfd, fcntl.F_SETFL | 
					
						
							|  |  |  |                 , fcntl.fcntl(mfd, fcntl.F_GETFL) | os.O_NONBLOCK) | 
					
						
							|  |  |  |     old = termios.tcgetattr(mfd) | 
					
						
							|  |  |  |     old[3] = old[3] & ~termios.ECHO | 
					
						
							|  |  |  |     termios.tcsetattr(mfd, termios.TCSADRAIN, old) | 
					
						
							|  |  |  |     return mfd | 
					
						
							| 
									
										
										
										
											2016-12-24 10:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-12 19:14:26 -05:00
										 |  |  | def get_cpu_info(): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         f = open('/proc/cpuinfo', 'rb') | 
					
						
							|  |  |  |         data = f.read() | 
					
						
							|  |  |  |         f.close() | 
					
						
							|  |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2017-09-27 11:43:14 -04:00
										 |  |  |         logging.debug("Exception on read /proc/cpuinfo: %s", | 
					
						
							|  |  |  |                       traceback.format_exc()) | 
					
						
							| 
									
										
										
										
											2017-02-12 19:14:26 -05:00
										 |  |  |         return "?" | 
					
						
							|  |  |  |     lines = [l.split(':', 1) for l in data.split('\n')] | 
					
						
							|  |  |  |     lines = [(l[0].strip(), l[1].strip()) for l in lines if len(l) == 2] | 
					
						
							|  |  |  |     core_count = [k for k, v in lines].count("processor") | 
					
						
							|  |  |  |     model_name = dict(lines).get("model name", "?") | 
					
						
							|  |  |  |     return "%d core %s" % (core_count, model_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-28 21:45:29 -05:00
										 |  |  | def get_git_version(): | 
					
						
							| 
									
										
										
										
											2016-12-24 10:07:02 -05:00
										 |  |  |     # Obtain version info from "git" program | 
					
						
							| 
									
										
										
										
											2017-09-07 10:39:21 -04:00
										 |  |  |     gitdir = os.path.join(sys.path[0], '..') | 
					
						
							| 
									
										
										
										
											2017-01-09 23:33:23 -05:00
										 |  |  |     if not os.path.exists(gitdir): | 
					
						
							| 
									
										
										
										
											2016-12-24 10:07:02 -05:00
										 |  |  |         logging.debug("No '.git' file/directory found") | 
					
						
							| 
									
										
										
										
											2016-12-28 21:45:29 -05:00
										 |  |  |         return "?" | 
					
						
							| 
									
										
										
										
											2017-09-07 10:39:21 -04:00
										 |  |  |     prog = "git -C %s describe --tags --long --dirty" % (gitdir,) | 
					
						
							| 
									
										
										
										
											2016-12-24 10:07:02 -05:00
										 |  |  |     try: | 
					
						
							|  |  |  |         process = subprocess.Popen(shlex.split(prog), stdout=subprocess.PIPE) | 
					
						
							|  |  |  |         output = process.communicate()[0] | 
					
						
							|  |  |  |         retcode = process.poll() | 
					
						
							|  |  |  |     except OSError: | 
					
						
							| 
									
										
										
										
											2017-09-27 11:43:14 -04:00
										 |  |  |         logging.debug("Exception on run: %s", traceback.format_exc()) | 
					
						
							| 
									
										
										
										
											2016-12-28 21:45:29 -05:00
										 |  |  |         return "?" | 
					
						
							|  |  |  |     return output.strip() |