2016-05-25 18:37:40 +03: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-10 07:33:23 +03:00
|
|
|
import sys, os, pty, fcntl, termios, signal, logging
|
|
|
|
import subprocess, traceback, shlex
|
2016-05-25 18:37:40 +03: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-10 03:04:30 +03: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 18:37:40 +03: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
|
2018-05-16 20:25:21 +03:00
|
|
|
filename = os.ttyname(sfd)
|
|
|
|
os.chmod(filename, 0660)
|
|
|
|
os.symlink(filename, ptyname)
|
2018-02-02 23:35:35 +03:00
|
|
|
set_nonblock(mfd)
|
2016-05-25 18:37:40 +03:00
|
|
|
old = termios.tcgetattr(mfd)
|
|
|
|
old[3] = old[3] & ~termios.ECHO
|
|
|
|
termios.tcsetattr(mfd, termios.TCSADRAIN, old)
|
|
|
|
return mfd
|
2016-12-24 18:07:02 +03:00
|
|
|
|
2017-02-13 03:14:26 +03:00
|
|
|
def get_cpu_info():
|
|
|
|
try:
|
|
|
|
f = open('/proc/cpuinfo', 'rb')
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
2019-01-11 21:00:44 +03:00
|
|
|
except IOError, OSError:
|
2017-09-27 18:43:14 +03:00
|
|
|
logging.debug("Exception on read /proc/cpuinfo: %s",
|
|
|
|
traceback.format_exc())
|
2017-02-13 03:14:26 +03: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)
|
|
|
|
|
2018-10-27 17:44:38 +03:00
|
|
|
def get_version_from_file(klippy_src):
|
|
|
|
try:
|
|
|
|
with open(os.path.join(klippy_src, '.version')) as h:
|
|
|
|
return h.read().rstrip()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
return "?"
|
|
|
|
|
|
|
|
def get_git_version(from_file=True):
|
|
|
|
klippy_src = os.path.dirname(__file__)
|
|
|
|
|
2016-12-24 18:07:02 +03:00
|
|
|
# Obtain version info from "git" program
|
2018-10-27 17:44:38 +03:00
|
|
|
gitdir = os.path.join(klippy_src, '..')
|
2019-02-27 21:05:37 +03:00
|
|
|
prog = ('git', '-C', gitdir, 'describe', '--always',
|
|
|
|
'--tags', '--long', '--dirty')
|
2016-12-24 18:07:02 +03:00
|
|
|
try:
|
2019-02-27 21:05:37 +03:00
|
|
|
process = subprocess.Popen(prog, stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2018-10-27 17:44:38 +03:00
|
|
|
ver, err = process.communicate()
|
|
|
|
retcode = process.wait()
|
|
|
|
if retcode == 0:
|
|
|
|
return ver.strip()
|
|
|
|
else:
|
|
|
|
logging.debug("Error getting git version: %s", err)
|
2016-12-24 18:07:02 +03:00
|
|
|
except OSError:
|
2017-09-27 18:43:14 +03:00
|
|
|
logging.debug("Exception on run: %s", traceback.format_exc())
|
2018-10-27 17:44:38 +03:00
|
|
|
|
|
|
|
if from_file:
|
|
|
|
return get_version_from_file(klippy_src)
|
|
|
|
return "?"
|