[rtems-tools commit] rtemstoolkit: Move host support access into a separate module.

Chris Johns chrisj at rtems.org
Mon Apr 24 06:42:38 UTC 2017


Module:    rtems-tools
Branch:    master
Commit:    7d3350d0bbcb7468fecfd9abffb3fe9f34c0c6c3
Changeset: http://git.rtems.org/rtems-tools/commit/?id=7d3350d0bbcb7468fecfd9abffb3fe9f34c0c6c3

Author:    Chris Johns <chrisj at rtems.org>
Date:      Tue Apr 25 00:31:44 2017 +1000

rtemstoolkit: Move host support access into a separate module.

Moving the host support into a module lets it get used where options
is not being used.

---

 rtemstoolkit/config.py  |   4 +-
 rtemstoolkit/darwin.py  |  17 +++++---
 rtemstoolkit/freebsd.py |  19 ++++++---
 rtemstoolkit/host.py    | 111 ++++++++++++++++++++++++++++++++++++++++++++++++
 rtemstoolkit/linux.py   |  18 +++++---
 rtemstoolkit/netbsd.py  |  19 ++++++---
 rtemstoolkit/options.py |  63 ++-------------------------
 rtemstoolkit/solaris.py |  19 ++++++---
 rtemstoolkit/windows.py |  21 +++++----
 tester/rt/stty.py       |   8 ++--
 10 files changed, 192 insertions(+), 107 deletions(-)

diff --git a/rtemstoolkit/config.py b/rtemstoolkit/config.py
index 697bcaf..a16261b 100644
--- a/rtemstoolkit/config.py
+++ b/rtemstoolkit/config.py
@@ -51,12 +51,14 @@ import sys
 try:
     from . import error
     from . import execute
+    from . import host
     from . import log
     from . import options
     from . import path
 except (ValueError, SystemError):
     import error
     import execute
+    import host
     import log
     import options
     import path
@@ -211,7 +213,7 @@ class file(object):
         if len(sl):
             e = execute.capture_execution()
             for s in sl:
-                if options.host_windows:
+                if host.is_windows:
                     cmd = '%s -c "%s"' % (self.macros.expand('%{__sh}'), s[2:-1])
                 else:
                     cmd = s[2:-1]
diff --git a/rtemstoolkit/darwin.py b/rtemstoolkit/darwin.py
index 57c8197..0400174 100644
--- a/rtemstoolkit/darwin.py
+++ b/rtemstoolkit/darwin.py
@@ -1,6 +1,6 @@
 #
 # RTEMS Tools Project (http://www.rtems.org/)
-# Copyright 2010-2016 Chris Johns (chrisj at rtems.org)
+# Copyright 2010-2017 Chris Johns (chrisj at rtems.org)
 # All rights reserved.
 #
 # This file is part of the RTEMS Tools package in 'rtems-tools'.
@@ -44,15 +44,19 @@ try:
 except (ValueError, SystemError):
     import execute
 
-def load():
-    uname = os.uname()
+def cpus():
     sysctl = '/usr/sbin/sysctl '
     e = execute.capture_execution()
     exit_code, proc, output = e.shell(sysctl + 'hw.ncpu')
     if exit_code == 0:
-        ncpus = output.split(' ')[1].strip()
+        ncpus = int(output.split(' ')[1].strip())
     else:
-        ncpus = '1'
+        ncpus = 1
+    return ncpus
+
+def overrides():
+    uname = os.uname()
+    ncpus = '%d' % (cores())
     defines = {
         '_ncpus':         ('none',    'none',     ncpus),
         '_os':            ('none',    'none',     'darwin'),
@@ -83,4 +87,5 @@ def load():
 
 if __name__ == '__main__':
     import pprint
-    pprint.pprint(load())
+    pprint.pprint(cpus())
+    pprint.pprint(overrides())
diff --git a/rtemstoolkit/freebsd.py b/rtemstoolkit/freebsd.py
index 499d7dd..2842394 100644
--- a/rtemstoolkit/freebsd.py
+++ b/rtemstoolkit/freebsd.py
@@ -1,6 +1,6 @@
 #
 # RTEMS Tools Project (http://www.rtems.org/)
-# Copyright 2010-2016 Chris Johns (chrisj at rtems.org)
+# Copyright 2010-2017 Chris Johns (chrisj at rtems.org)
 # All rights reserved.
 #
 # This file is part of the RTEMS Tools package in 'rtems-tools'.
@@ -33,7 +33,6 @@
 # RTEMS project's spec files.
 #
 
-import pprint
 import os
 
 #
@@ -47,15 +46,19 @@ except (ValueError, SystemError):
     import check
     import execute
 
-def load():
-    uname = os.uname()
+def cpus():
     sysctl = '/sbin/sysctl '
     e = execute.capture_execution()
     exit_code, proc, output = e.shell(sysctl + 'hw.ncpu')
     if exit_code == 0:
-        ncpus = output.split(' ')[1].strip()
+        ncpus = int(output.split(' ')[1].strip())
     else:
-        ncpus = '1'
+        ncpus = 1
+    return ncpus
+
+def overrides():
+    uname = os.uname()
+    ncpus = '%d' % (cpus())
     if uname[4] == 'amd64':
         cpu = 'x86_64'
     else:
@@ -104,4 +107,6 @@ def load():
     return defines
 
 if __name__ == '__main__':
-    pprint.pprint(load())
+    import pprint
+    pprint.pprint(cpus())
+    pprint.pprint(overrides())
diff --git a/rtemstoolkit/host.py b/rtemstoolkit/host.py
new file mode 100644
index 0000000..bae56b6
--- /dev/null
+++ b/rtemstoolkit/host.py
@@ -0,0 +1,111 @@
+#
+# RTEMS Tools Project (http://www.rtems.org/)
+# Copyright 2017 Chris Johns (chrisj at rtems.org)
+# All rights reserved.
+#
+# This file is part of the RTEMS Tools package in 'rtems-tools'.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+#
+# Host specifics.
+#
+
+from __future__ import print_function
+
+import os
+
+#
+# Support to handle use in a package and as a unit test.
+# If there is a better way to let us know.
+#
+try:
+    from . import error
+except (ValueError, SystemError):
+    import error
+
+is_windows = False
+platform = None
+name = None
+
+def _load():
+
+    global is_windows
+    global platform
+    global name
+
+    if os.name == 'nt':
+        name = 'windows'
+        is_windows = True
+    elif os.name == 'posix':
+        uname = os.uname()
+        if uname[0].startswith('CYGWIN_NT'):
+            name = 'windows'
+        elif uname[0] == 'Darwin':
+            name = darwin
+        elif uname[0] == 'FreeBSD':
+            name = 'freebsd'
+        elif uname[0] == 'NetBSD':
+            name = netbsd
+        elif uname[0] == 'Linux':
+            name = 'linux'
+        elif uname[0] == 'SunOS':
+            name = 'solaris'
+
+    if name is None:
+        raise error.general('unsupported host type; please add')
+
+    #try:
+    #    try:
+    #        platform = __import__(name, globals(), locals(), ['.'])
+    #    except:
+    #        platform = __import__(name, globals(), locals())
+    #except:
+    #    raise error.general('failed to load %s host support' % (name))
+
+    platform = __import__(name, globals(), locals(), ['.', ''])
+
+    if platform is None:
+        raise error.general('failed to load %s host support' % (name))
+
+def cpus():
+    _load()
+    return platform.cpus()
+
+def overrides():
+    _load()
+    return platform.overrides()
+
+if __name__ == '__main__':
+    import pprint
+    _load()
+    print('Name      : %s' % (name))
+    if is_windows:
+        status = 'Yes'
+    else:
+        status = 'No'
+    print('Windows   : %s' % (status))
+    print('CPUs      : %d' % (cpus()))
+    print('Overrides :')
+    pprint.pprint(overrides())
diff --git a/rtemstoolkit/linux.py b/rtemstoolkit/linux.py
index 7aea43e..1d7f577 100644
--- a/rtemstoolkit/linux.py
+++ b/rtemstoolkit/linux.py
@@ -1,6 +1,6 @@
 #
 # RTEMS Tools Project (http://www.rtems.org/)
-# Copyright 2010-2016 Chris Johns (chrisj at rtems.org)
+# Copyright 2010-2017 Chris Johns (chrisj at rtems.org)
 # All rights reserved.
 #
 # This file is part of the RTEMS Tools package in 'rtems-tools'.
@@ -33,7 +33,6 @@
 # RTEMS project's spec files.
 #
 
-import pprint
 import os
 import platform
 
@@ -48,9 +47,7 @@ except (ValueError, SystemError):
     import execute
     import path
 
-def load():
-    uname = os.uname()
-    smp_mflags = ''
+def cpus():
     processors = '/bin/grep processor /proc/cpuinfo'
     e = execute.capture_execution()
     exit_code, proc, output = e.shell(processors)
@@ -63,7 +60,12 @@ def load():
                     ncpus = int(count)
         except:
             pass
-    ncpus = str(ncpus + 1)
+    return ncpus + 1
+
+def overrides():
+    uname = os.uname()
+    smp_mflags = ''
+    ncpus = '%d' % cpus()
     if uname[4].startswith('arm'):
         cpu = 'arm'
     else:
@@ -153,4 +155,6 @@ def load():
     return defines
 
 if __name__ == '__main__':
-    pprint.pprint(load())
+    import pprint
+    pprint.pprint(cpus())
+    pprint.pprint(overrides())
diff --git a/rtemstoolkit/netbsd.py b/rtemstoolkit/netbsd.py
index 5883682..112fba1 100644
--- a/rtemstoolkit/netbsd.py
+++ b/rtemstoolkit/netbsd.py
@@ -1,6 +1,6 @@
 #
 # RTEMS Tools Project (http://www.rtems.org/)
-# Copyright 2010-2016 Chris Johns (chrisj at rtems.org)
+# Copyright 2010-2017 Chris Johns (chrisj at rtems.org)
 # All rights reserved.
 #
 # This file is part of the RTEMS Tools package in 'rtems-tools'.
@@ -24,7 +24,6 @@
 # RTEMS project's spec files.
 #
 
-import pprint
 import os
 
 try:
@@ -34,15 +33,19 @@ except (ValueError, SystemError):
     import check
     import execute
 
-def load():
-    uname = os.uname()
+def cpus():
     sysctl = '/sbin/sysctl '
     e = execute.capture_execution()
     exit_code, proc, output = e.shell(sysctl + 'hw.ncpu')
     if exit_code == 0:
-        ncpus = output.split(' ')[1].strip()
+        ncpus = int(output.split(' ')[1].strip())
     else:
-        ncpus = '1'
+        ncpus = 1
+    return ncpus
+
+def overrides():
+    uname = os.uname()
+    ncpus = '%d' % (cpus())
     if uname[4] == 'amd64':
         cpu = 'x86_64'
     else:
@@ -93,4 +96,6 @@ def load():
     return defines
 
 if __name__ == '__main__':
-    pprint.pprint(load())
+    import pprint
+    pprint.pprint(cpus())
+    pprint.pprint(overrides())
diff --git a/rtemstoolkit/options.py b/rtemstoolkit/options.py
index 8ff9bb2..70e9786 100644
--- a/rtemstoolkit/options.py
+++ b/rtemstoolkit/options.py
@@ -50,6 +50,7 @@ try:
     from . import error
     from . import execute
     from . import git
+    from . import host
     from . import log
     from . import macros
     from . import path
@@ -58,6 +59,7 @@ except (ValueError, SystemError):
     import error
     import execute
     import git
+    import host
     import log
     import macros
     import path
@@ -65,11 +67,6 @@ except (ValueError, SystemError):
 
 basepath = 'tb'
 
-#
-# Save the host state.
-#
-host_windows = False
-
 class command_line(object):
     """Process the command line in a common way for all Tool Builder commands."""
 
@@ -539,61 +536,7 @@ def load(opts):
     if not isinstance(opts, command_line):
         raise error.general('invalid options type passed to options loader')
 
-    global host_windows
-
-    overrides = None
-    if os.name == 'nt':
-        try:
-            import windows
-            overrides = windows.load()
-            host_windows = True
-        except:
-            raise error.general('failed to load Windows host support')
-    elif os.name == 'posix':
-        uname = os.uname()
-        try:
-            if uname[0].startswith('CYGWIN_NT'):
-                try:
-                    from . import windows
-                except:
-                    import windows
-                overrides = windows.load()
-            elif uname[0] == 'Darwin':
-                try:
-                    from . import darwin
-                except:
-                    import darwin
-                overrides = darwin.load()
-            elif uname[0] == 'FreeBSD':
-                try:
-                    from . import freebsd
-                except:
-                    import freebsd
-                overrides = freebsd.load()
-            elif uname[0] == 'NetBSD':
-                try:
-                    from . import netbsd
-                except:
-                    import netbsd
-                overrides = netbsd.load()
-            elif uname[0] == 'Linux':
-                try:
-                    from . import linux
-                except:
-                    import linux
-                overrides = linux.load()
-            elif uname[0] == 'SunOS':
-                try:
-                    from . import solaris
-                except:
-                    import solaris
-                overrides = solaris.load()
-        except:
-            raise error.general('failed to load %s host support' % (uname[0]))
-    else:
-        raise error.general('unsupported host type; please add')
-    if overrides is None:
-        raise error.general('no hosts defaults found; please add')
+    overrides = host.overrides()
     for k in overrides:
         opts.defaults[k] = overrides[k]
 
diff --git a/rtemstoolkit/solaris.py b/rtemstoolkit/solaris.py
index 397df68..dc3e490 100644
--- a/rtemstoolkit/solaris.py
+++ b/rtemstoolkit/solaris.py
@@ -1,6 +1,6 @@
 #
 # RTEMS Tools Project (http://www.rtems.org/)
-# Copyright 2010-2016 Chris Johns (chrisj at rtems.org)
+# Copyright 2010-2017 Chris Johns (chrisj at rtems.org)
 # All rights reserved.
 #
 # This file is part of the RTEMS Tools package in 'rtems-tools'.
@@ -22,7 +22,6 @@
 # RTEMS project's spec files.
 #
 
-import pprint
 import os
 
 try:
@@ -34,15 +33,19 @@ except (ValueError, SystemError):
     import error
     import execute
 
-def load():
-    uname = os.uname()
+def cpus():
     psrinfo = '/sbin/psrinfo|wc -l'
     e = execute.capture_execution()
     exit_code, proc, output = e.shell(psrinfo)
     if exit_code == 0:
-        ncpus = output
+        ncpus = int(output)
     else:
-        ncpus = '1'
+        ncpus = 1
+    return ncpus
+
+def overrides():
+    uname = os.uname()
+    ncpus = '%d' % (cpus())
     if uname[4] == 'i86pc':
         cpu = 'i386'
     else:
@@ -87,4 +90,6 @@ def load():
     return defines
 
 if __name__ == '__main__':
-    pprint.pprint(load())
+    import pprint
+    pprint.pprint(cpus())
+    pprint.pprint(overrides())
diff --git a/rtemstoolkit/windows.py b/rtemstoolkit/windows.py
index 2c08258..8f51069 100644
--- a/rtemstoolkit/windows.py
+++ b/rtemstoolkit/windows.py
@@ -1,6 +1,6 @@
 #
 # RTEMS Tools Project (http://www.rtems.org/)
-# Copyright 2010-2016 Chris Johns (chrisj at rtems.org)
+# Copyright 2010-2017 Chris Johns (chrisj at rtems.org)
 # All rights reserved.
 #
 # This file is part of the RTEMS Tools package in 'rtems-tools'.
@@ -32,7 +32,6 @@
 # Windows specific support and overrides.
 #
 
-import pprint
 import os
 
 #
@@ -46,7 +45,14 @@ except (ValueError, SystemError):
     import error
     import execute
 
-def load():
+def cpus():
+    if os.environ.has_key('NUMBER_OF_PROCESSORS'):
+        ncpus = int(os.environ['NUMBER_OF_PROCESSORS'])
+    else:
+        ncpus = 1
+    return ncpus
+
+def overrides():
     # Default to the native Windows Python.
     uname = 'win32'
     system = 'mingw32'
@@ -76,10 +82,7 @@ def load():
         except:
             pass
 
-    if os.environ.has_key('NUMBER_OF_PROCESSORS'):
-        ncpus = os.environ['NUMBER_OF_PROCESSORS']
-    else:
-        ncpus = '1'
+    ncpus = '%d' % (cpus())
 
     defines = {
         '_ncpus':         ('none',    'none',     ncpus),
@@ -140,4 +143,6 @@ def load():
     return defines
 
 if __name__ == '__main__':
-    pprint.pprint(load())
+    import pprint
+    pprint.pprint(cpus())
+    pprint.pprint(overrides())
diff --git a/tester/rt/stty.py b/tester/rt/stty.py
index b6f0204..55c4ed6 100644
--- a/tester/rt/stty.py
+++ b/tester/rt/stty.py
@@ -37,11 +37,11 @@ import sys
 import termios
 
 from rtemstoolkit import error
-from rtemstoolkit import options
+from rtemstoolkit import host
 from rtemstoolkit import path
 
 def save():
-    if not options.host_windows:
+    if not host.is_windows:
         try:
             sin = termios.tcgetattr(sys.stdin)
             sout = termios.tcgetattr(sys.stdout)
@@ -60,13 +60,13 @@ def restore(attributes):
 class tty:
 
     def __init__(self, dev):
-        if options.host_windows:
+        if host.is_windows:
             raise error.general('termios not support on host')
         self.dev = dev
         self.default_attr = None
         self.fd = None
         self.if_on = False
-        if options.host_windows:
+        if host.is_windows:
             raise error.general('TTY consoles not supported on Windows.')
         if not path.exists(dev):
             raise error.general('dev not found: %s' % (dev))




More information about the vc mailing list