diff options
author | Chris Johns <chrisj@rtems.org> | 2016-03-03 16:46:18 +1100 |
---|---|---|
committer | Chris Johns <chrisj@rtems.org> | 2016-03-03 16:53:39 +1100 |
commit | b0fa2ae9981b0ccf6a66cb8df2241caa5038eb36 (patch) | |
tree | d4f7f225cd35334ff18c3189bf1cdefa0d3335b4 /rtemstoolkit | |
parent | 0e5d89d9469fb755402cbabc09280557c7e01fcc (diff) |
Update rtems-tool to support Python 2 and 3.
Add solaris and netbsd.
Close #2619.
Diffstat (limited to '')
-rw-r--r-- | rtemstoolkit/__init__.py | 17 | ||||
-rw-r--r-- | rtemstoolkit/check.py | 33 | ||||
-rw-r--r-- | rtemstoolkit/config.py | 67 | ||||
-rw-r--r-- | rtemstoolkit/darwin.py | 11 | ||||
-rw-r--r-- | rtemstoolkit/error.py | 4 | ||||
-rwxr-xr-x | rtemstoolkit/execute.py | 59 | ||||
-rw-r--r-- | rtemstoolkit/freebsd.py | 14 | ||||
-rw-r--r-- | rtemstoolkit/git.py | 58 | ||||
-rw-r--r-- | rtemstoolkit/linux.py | 18 | ||||
-rwxr-xr-x | rtemstoolkit/log.py | 15 | ||||
-rw-r--r-- | rtemstoolkit/macros.py | 44 | ||||
-rw-r--r-- | rtemstoolkit/mailer.py | 19 | ||||
-rw-r--r-- | rtemstoolkit/netbsd.py | 96 | ||||
-rw-r--r-- | rtemstoolkit/options.py | 86 | ||||
-rw-r--r-- | rtemstoolkit/path.py | 16 | ||||
-rw-r--r-- | rtemstoolkit/solaris.py | 90 | ||||
-rw-r--r-- | rtemstoolkit/version.py | 19 | ||||
-rw-r--r-- | rtemstoolkit/windows.py | 14 |
18 files changed, 546 insertions, 134 deletions
diff --git a/rtemstoolkit/__init__.py b/rtemstoolkit/__init__.py index 33f49f0..f3fbbcd 100644 --- a/rtemstoolkit/__init__.py +++ b/rtemstoolkit/__init__.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -37,4 +37,17 @@ all = ['check', 'macros', 'mailer', 'options', - 'path'] + 'path', + 'version'] + +from . import check +from . import config +from . import error +from . import execute +from . import git +from . import log +from . import macros +from . import mailer +from . import options +from . import path +from . import version diff --git a/rtemstoolkit/check.py b/rtemstoolkit/check.py index 19d4dfa..cbec3a3 100644 --- a/rtemstoolkit/check.py +++ b/rtemstoolkit/check.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -32,14 +32,28 @@ # Check the defaults for a specific host. # +from __future__ import print_function + import os -import error -import execute -import log -import options -import path -import version +# +# 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 + from . import execute + from . import log + from . import options + from . import path + from . import version +except (ValueError, SystemError): + import error + import execute + import log + import options + import path + import version def _check_none(_opts, macro, value, constraint): return True @@ -117,7 +131,7 @@ def host_setup(opts): sane = True - for d in opts.defaults.keys(): + for d in list(opts.defaults.keys()): try: (test, constraint, value) = opts.defaults.get(d) except: @@ -152,7 +166,8 @@ def check_dir(label, path): def run(): import sys try: - _opts = options.load(args = sys.argv) + _opts = options.command_line(argv = sys.argv) + options.load(_opts) log.notice('RTEMS Source Builder - Check, v%s' % (version.str())) if host_setup(_opts): print('Environment is ok') diff --git a/rtemstoolkit/config.py b/rtemstoolkit/config.py index ee7cb1c..4b2e8e1 100644 --- a/rtemstoolkit/config.py +++ b/rtemstoolkit/config.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -36,29 +36,26 @@ # other software modules. # +from __future__ import print_function + import copy +import functools import os import re import sys -import error -import execute -import log -import options -import path - try: - import error - import execute - import log - import options - import path -except KeyboardInterrupt: - print('user terminated') - sys.exit(1) -except: - print('error: unknown application load error') - sys.exit(1) + from . import error + from . import execute + from . import log + from . import options + from . import path +except (ValueError, SystemError): + import error + import execute + import log + import options + import path def _check_bool(value): if value.isdigit(): @@ -96,6 +93,8 @@ class file(object): self.macros.define(label) self._includes = [] self.load_depth = 0 + self.lc = 0 + self.name = 'none' def __del__(self): pass @@ -104,7 +103,7 @@ class file(object): def _dict(dd): s = '' - ddl = dd.keys() + ddl = list(dd.keys()) ddl.sort() for d in ddl: s += ' ' + d + ': ' + dd[d] + '\n' @@ -438,13 +437,13 @@ class file(object): else: istrue = _check_bool(ifls[0]) if istrue == None: - self._error('invalid if bool value: ' + reduce(add, ls, '')) + self._error('invalid if bool value: ' + functools.reduce(add, ls, '')) istrue = False elif len(ifls) == 2: if ifls[0] == '!': istrue = _check_bool(ifls[1]) if istrue == None: - self._error('invalid if bool value: ' + reduce(add, ls, '')) + self._error('invalid if bool value: ' + functools.reduce(add, ls, '')) istrue = False else: istrue = not istrue @@ -460,7 +459,7 @@ class file(object): elif ifls[1] == '!=': istrue = True else: - self._error('invalid if bool operator: ' + reduce(add, ls, '')) + self._error('invalid if bool operator: ' + functools.reduce(add, ls, '')) elif len(ifls) == 3: if ifls[1] == '==': if ifls[0] == ifls[2]: @@ -493,9 +492,9 @@ class file(object): else: istrue = False else: - self._error('invalid %if operator: ' + reduce(add, ls, '')) + self._error('invalid %if operator: ' + functools.reduce(add, ls, '')) else: - self._error('malformed if: ' + reduce(add, ls, '')) + self._error('malformed if: ' + functools.reduce(add, ls, '')) if invert: istrue = not istrue log.trace('config: %s: _if: %s %s' % (self.init_name, ifls, str(istrue))) @@ -842,12 +841,20 @@ def run(): # # Run where defaults.mc is located # - opts = options.load(sys.argv, defaults = 'defaults.mc') - log.trace('config: count %d' % (len(opts.config_files()))) - for config_file in opts.config_files(): - s = file(config_file, opts) - print(s) - del s + long_opts = { + # key macro handler param defs init + '--file' : ('_file', 'path', True, None, False) + } + opts = options.command_line(base_path = '.', + argv = sys.argv, + long_opts = long_opts) + options.load(opts) + if '_file' not in opts.defaults: + raise error.general('no --file option provided') + s = file(opts.defaults['_file'], opts) + s.load(opts.defaults['_file']) + print(s) + del s except error.general as gerr: print(gerr) sys.exit(1) diff --git a/rtemstoolkit/darwin.py b/rtemstoolkit/darwin.py index 1d092cb..57c8197 100644 --- a/rtemstoolkit/darwin.py +++ b/rtemstoolkit/darwin.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -35,7 +35,14 @@ import os -import execute +# +# 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 execute +except (ValueError, SystemError): + import execute def load(): uname = os.uname() diff --git a/rtemstoolkit/error.py b/rtemstoolkit/error.py index ea6b71d..e3a43c1 100644 --- a/rtemstoolkit/error.py +++ b/rtemstoolkit/error.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -32,6 +32,8 @@ # Various errors we can raise. # +from __future__ import print_function + class error(Exception): """Base class for Builder exceptions.""" def set_output(self, msg): diff --git a/rtemstoolkit/execute.py b/rtemstoolkit/execute.py index 3f8e3e6..982e35f 100755 --- a/rtemstoolkit/execute.py +++ b/rtemstoolkit/execute.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -34,6 +34,9 @@ # Note, the subprocess module is only in Python 2.4 or higher. # +from __future__ import print_function + +import functools import os import re import sys @@ -41,8 +44,16 @@ import subprocess import threading import time -import error -import log +# +# 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 + from . import log +except (ValueError, SystemError): + import error + import log # Trace exceptions trace_threads = False @@ -96,7 +107,7 @@ def arg_subst(command, substs): def arg_subst_str(command, subst): cmd = arg_subst(command, subst) def add(x, y): return x + ' ' + str(y) - return reduce(add, cmd, '') + return functools.reduce(add, cmd, '') class execute(object): """Execute commands or scripts. The 'output' is a funtion that handles the @@ -118,7 +129,7 @@ class execute(object): self.timing_out = False self.proc = None - def _capture(self, command, proc, timeout = None): + def capture(self, proc, command = 'pipe', timeout = None): """Create 3 threads to read stdout and stderr and send to the output handler and call an input handler is provided. Based on the 'communicate' code in the subprocess module.""" @@ -128,11 +139,18 @@ class execute(object): is a timeout check.""" if trace_threads: print('executte:_writethread: start') + encoding = True + try: + tmp = bytes('temp', sys.stdin.encoding) + except: + encoding = False try: while True: - lines = input() - if type(lines) == str: + lines = eval(input()) + if type(lines) == str or type(lines) == bytes: try: + if encoding: + lines = bytes(lines, sys.stdin.encoding) fh.write(lines) except: break @@ -155,10 +173,9 @@ class execute(object): """Read from a file handle and write to the output handler until the file closes.""" def _output_line(line, exe, prefix, out, count): - #print 'LINE:%d: %s' % (count, line) - exe.lock.acquire() + #exe.lock.acquire() #exe.outputting = True - exe.lock.release() + #exe.lock.release() if out: out(prefix + line) else: @@ -175,7 +192,8 @@ class execute(object): data = fh.read(1) if len(data) == 0: break - #print '))))) %02x "%s"' % (ord(data), data) + if type(data) == bytes: + data = data.decode(sys.stdout.encoding) for c in data: line += c if c == '\n': @@ -305,11 +323,13 @@ class execute(object): s = command if type(command) is list: def add(x, y): return x + ' ' + str(y) - s = reduce(add, command, '')[1:] + s = functools.reduce(add, command, '')[1:] what = 'spawn' if shell: what = 'shell' log.output(what + ': ' + s) + if self.output is None: + raise error.general('capture needs an output handler') if shell and self.shell_exe: command = arg_list(command) command[:0] = self.shell_exe @@ -342,7 +362,7 @@ class execute(object): return (0, proc) if self.output is None: raise error.general('capture needs an output handler') - exit_code = self._capture(command, proc, timeout) + exit_code = self.capture(proc, command, timeout) if self.verbose: log.output('exit: ' + str(exit_code)) except OSError as ose: @@ -395,7 +415,7 @@ class execute(object): shell = shell or self.shell_commands, cwd = cwd, env = env, stdin = stdin, stdout = stdout, stderr = stderr, - itmeout = timeout) + timeout = timeout) def set_shell(self, execute): """Set the shell to execute when issuing a shell command.""" @@ -519,11 +539,18 @@ if __name__ == "__main__": if ec == 0: print('piping input into ' + commands['pipe'][0] + ': ' + \ commands['pipe'][2]) - proc.stdin.write(commands['pipe'][2]) + try: + out = bytes(commands['pipe'][2], sys.stdin.encoding) + except: + out = commands['pipe'][2] + proc.stdin.write(out) proc.stdin.close() e.capture(proc) del proc + def capture_output(text): + print(text, end = '') + cmd_shell_test = 'if "%OS%" == "Windows_NT" (echo It is WinNT) else echo Is is not WinNT' sh_shell_test = 'x="me"; if [ $x = "me" ]; then echo "It was me"; else "It was him"; fi' @@ -549,7 +576,7 @@ if __name__ == "__main__": print(arg_subst(['nothing', 'xx-%0-yyy', '%1', '%2-something'], ['subst0', 'subst1', 'subst2'])) - e = execute(error_prefix = 'ERR: ', verbose = True) + e = execute(error_prefix = 'ERR: ', output = capture_output, verbose = True) if sys.platform == "win32": run_tests(e, commands['windows'], False) if os.path.exists('c:\\msys\\1.0\\bin\\sh.exe'): diff --git a/rtemstoolkit/freebsd.py b/rtemstoolkit/freebsd.py index 11a827f..499d7dd 100644 --- a/rtemstoolkit/freebsd.py +++ b/rtemstoolkit/freebsd.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -36,8 +36,16 @@ import pprint import os -import check -import execute +# +# 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 check + from . import execute +except (ValueError, SystemError): + import check + import execute def load(): uname = os.uname() diff --git a/rtemstoolkit/git.py b/rtemstoolkit/git.py index 20a1aa6..5f3af58 100644 --- a/rtemstoolkit/git.py +++ b/rtemstoolkit/git.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2015 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -31,17 +31,28 @@ import os -import error -import execute -import log -import options -import path +# +# 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 + from . import execute + from . import log + from . import path +except (ValueError, SystemError): + import error + import execute + import log + import path class repo: """An object to manage a git repo.""" - def _git_exit_code(self, ec): + def _git_exit_code(self, ec, cmd, output): if ec: + log.notice('git: cmd: ' + ' '.join(cmd)) + log.notice('git: output: ' + output) raise error.general('git command failed (%s): %d' % (self.git, ec)) def _run(self, args, check = False): @@ -55,7 +66,7 @@ class repo: exit_code, proc, output = e.spawn(cmd, cwd = path.host(cwd)) log.trace(output) if check: - self._git_exit_code(exit_code) + self._git_exit_code(exit_code, cmd, output) return exit_code, output def __init__(self, _path, opts = None, macros = None): @@ -76,9 +87,11 @@ class repo: if len(gvs) < 3: raise error.general('invalid version string from git: %s' % (output)) vs = gvs[2].split('.') - if len(vs) != 4: - raise error.general('invalid version number from git: %s' % (gvs[2])) - return (int(vs[0]), int(vs[1]), int(vs[2]), int(vs[3])) + if len(vs) == 4: + return (int(vs[0]), int(vs[1]), int(vs[2]), int(vs[3])) + if len(vs) == 3: + return (int(vs[0]), int(vs[1]), int(vs[2])) + raise error.general('invalid version number from git: %s' % (gvs[2])) def clone(self, url, _path): ec, output = self._run(['clone', url, path.host(_path)], check = True) @@ -207,12 +220,19 @@ class repo: if __name__ == '__main__': import sys - opts = options.load(sys.argv) + import options + long_opts = { + # key macro handler param defs init + } + opts = options.command_line(base_path = '.', + argv = sys.argv, + long_opts = long_opts) + options.load(opts) g = repo('.', opts) - print(g.git_version()) - print(g.valid()) - print(g.status()) - print(g.clean()) - print(g.remotes()) - print(g.email()) - print(g.head()) + print('version:', g.git_version()) + print('valid:', g.valid()) + print('status:', g.status()) + print('dirty:', g.dirty()) + print('remotes:', g.remotes()) + print('email:', g.email()) + print('head:', g.head()) diff --git a/rtemstoolkit/linux.py b/rtemstoolkit/linux.py index 022bcd0..7aea43e 100644 --- a/rtemstoolkit/linux.py +++ b/rtemstoolkit/linux.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -35,10 +35,18 @@ import pprint import os - import platform -import execute -import path + +# +# 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 execute + from . import path +except (ValueError, SystemError): + import execute + import path def load(): uname = os.uname() @@ -130,7 +138,7 @@ def load(): '__chown': ('exe', 'required', '/usr/sbin/chown') }, } - if variations.has_key(distro): + if distro in variations: for v in variations[distro]: if path.exists(variations[distro][v][2]): defines[v] = variations[distro][v] diff --git a/rtemstoolkit/log.py b/rtemstoolkit/log.py index 1308203..61a3b02 100755 --- a/rtemstoolkit/log.py +++ b/rtemstoolkit/log.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-testing'. @@ -32,11 +32,20 @@ # Log output to stdout and/or a file. # +from __future__ import print_function + import os import sys import threading -import error +# +# 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 # # A global log. @@ -126,7 +135,7 @@ class log: self.fhs[1] = sys.stderr else: try: - self.fhs.append(file(s, 'w')) + self.fhs.append(open(s, 'w')) except IOError as ioe: raise error.general("creating log file '" + s + \ "': " + str(ioe)) diff --git a/rtemstoolkit/macros.py b/rtemstoolkit/macros.py index 632be87..c92a6c9 100644 --- a/rtemstoolkit/macros.py +++ b/rtemstoolkit/macros.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -32,14 +32,24 @@ # Macro tables. # +from __future__ import print_function + import copy import inspect import re import os import string -import error -import path +# +# 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 + from . import path +except (ValueError, SystemError): + import error + import path # # Macro tables @@ -54,7 +64,7 @@ class macros: def __iter__(self): return self - def next(self): + def __next__(self): if self.index < len(self.keys): key = self.keys[self.index] self.index += 1 @@ -64,6 +74,19 @@ class macros: def iterkeys(self): return self.keys + def _unicode_to_str(self, us): + try: + if type(us) == unicode: + return us.encode('ascii', 'replace') + except: + pass + try: + if type(us) == bytes: + return us.encode('ascii', 'replace') + except: + pass + return us + def __init__(self, name = None, original = None, rtdir = '.'): self.files = [] self.macro_filter = re.compile(r'%{[^}]+}') @@ -167,12 +190,17 @@ class macros: def __setitem__(self, key, value): if type(key) is not str: raise TypeError('bad key type (want str): %s' % (type(key))) + if type(value) is not tuple: + value = self._unicode_to_str(value) if type(value) is str: value = ('none', 'none', value) if type(value) is not tuple: raise TypeError('bad value type (want tuple): %s' % (type(value))) if len(value) != 3: raise TypeError('bad value tuple (len not 3): %d' % (len(value))) + value = (self._unicode_to_str(value[0]), + self._unicode_to_str(value[1]), + self._unicode_to_str(value[2])) if type(value[0]) is not str: raise TypeError('bad value tuple type field: %s' % (type(value[0]))) if type(value[1]) is not str: @@ -195,10 +223,10 @@ class macros: return self.has_key(key) def __len__(self): - return len(self.keys()) + return len(list(self.keys())) def keys(self): - keys = self.macros['global'].keys() + keys = list(self.macros['global'].keys()) for rm in self.get_read_maps(): for mk in self.macros[rm]: if self.macros[rm][mk][1] == 'undefine': @@ -211,7 +239,7 @@ class macros: def has_key(self, key): if type(key) is not str: raise TypeError('bad key type (want str): %s' % (type(key))) - if self.key_filter(key) not in self.keys(): + if self.key_filter(key) not in list(self.keys()): return False return True @@ -491,7 +519,7 @@ if __name__ == "__main__": import copy import sys print(inspect.getfile(macros)) - m = macros(name = 'defaults.mc') + m = macros() d = copy.copy(m) m['test1'] = 'something' if d.has_key('test1'): diff --git a/rtemstoolkit/mailer.py b/rtemstoolkit/mailer.py index e24ed12..7f71d10 100644 --- a/rtemstoolkit/mailer.py +++ b/rtemstoolkit/mailer.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2013-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2013-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -32,13 +32,24 @@ # Manage emailing results or reports. # +from __future__ import print_function + import os import smtplib import socket -import error -import options -import path +# +# 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 + from . import options + from . import path +except (ValueError, SystemError): + import error + import options + import path def append_options(opts): opts['--mail'] = 'Send email report or results.' diff --git a/rtemstoolkit/netbsd.py b/rtemstoolkit/netbsd.py new file mode 100644 index 0000000..5883682 --- /dev/null +++ b/rtemstoolkit/netbsd.py @@ -0,0 +1,96 @@ +# +# RTEMS Tools Project (http://www.rtems.org/) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) +# All rights reserved. +# +# This file is part of the RTEMS Tools package in 'rtems-tools'. +# +# RTEMS Tools is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# RTEMS Tools is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with RTEMS Tools. If not, see <http://www.gnu.org/licenses/>. +# + +# +# This code is based on what ever doco about spec files I could find and +# RTEMS project's spec files. +# + +import pprint +import os + +try: + from . import check + from . import execute +except (ValueError, SystemError): + import check + import execute + +def load(): + uname = os.uname() + 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() + else: + ncpus = '1' + if uname[4] == 'amd64': + cpu = 'x86_64' + else: + cpu = uname[4] + version = uname[2] + if version.find('-') > 0: + version = version.split('-')[0] + defines = { + '_ncpus': ('none', 'none', '1'), + '_os': ('none', 'none', 'netbsd'), + '_host': ('triplet', 'required', cpu + '-netbsd' + version), + '_host_vendor': ('none', 'none', 'pc'), + '_host_os': ('none', 'none', 'netbsd'), + '_host_os_version': ('none', 'none', version), + '_host_cpu': ('none', 'none', cpu), + '_host_alias': ('none', 'none', '%{nil}'), + '_host_arch': ('none', 'none', cpu), + '_usr': ('dir', 'required', '/usr'), + '_var': ('dir', 'optional', '/var'), + 'optincludes_build': ('none', 'none', '-I/usr/pkg/include -L/usr/pkg/lib'), + '__bash': ('exe', 'optional', '/usr/pkg/bin/bash'), + '__bison': ('exe', 'required', '/usr/pkg/bin/bison'), + '__git': ('exe', 'required', '/usr/pkg/bin/git'), + '__svn': ('exe', 'required', '/usr/pkg/bin/svn'), + '__xz': ('exe', 'optional', '/usr/pkg/bin/xz'), + '__make': ('exe', 'required', 'gmake'), + '__patch_opts': ('none', 'none', '-E') + } + + defines['_build'] = defines['_host'] + defines['_build_vendor'] = defines['_host_vendor'] + defines['_build_os'] = defines['_host_os'] + defines['_build_cpu'] = defines['_host_cpu'] + defines['_build_alias'] = defines['_host_alias'] + defines['_build_arch'] = defines['_host_arch'] + + for gv in ['47', '48', '49']: + gcc = '%s-portbld-netbsd%s-gcc%s' % (cpu, version, gv) + if check.check_exe(gcc, gcc): + defines['__cc'] = gcc + break + for gv in ['47', '48', '49']: + gxx = '%s-portbld-netbsd%s-g++%s' % (cpu, version, gv) + if check.check_exe(gxx, gxx): + defines['__cxx'] = gxx + break + + return defines + +if __name__ == '__main__': + pprint.pprint(load()) diff --git a/rtemstoolkit/options.py b/rtemstoolkit/options.py index 132445e..8ff9bb2 100644 --- a/rtemstoolkit/options.py +++ b/rtemstoolkit/options.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -32,22 +32,36 @@ # Determine the defaults and load the specific file. # +from __future__ import print_function + import copy import glob import pprint import re import os import string - -import error -import execute -import git -import log -import macros -import path import sys -import version +# +# 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 + from . import execute + from . import git + from . import log + from . import macros + from . import path + from . import version +except (ValueError, SystemError): + import error + import execute + import git + import log + import macros + import path + import version basepath = 'tb' @@ -61,7 +75,7 @@ class command_line(object): def __init__(self, base_path = None, argv = None, optargs = None, defaults = None, long_opts = None, long_opts_help = None, - command_path = None, log_default = None): + command_path = '', log_default = None): if argv is None: return @@ -69,7 +83,7 @@ class command_line(object): global basepath if long_opts == None: - raise error.general('No options provided') + long_opts = {} basepath = base_path @@ -77,6 +91,9 @@ class command_line(object): raise error.general('log default is a list') self.log_default = log_default + if defaults is None: + defaults = macros.macros() + self.long_opts = { # key macro handler param defs init '--jobs' : ('_jobs', self._lo_jobs, True, 'default', True), @@ -128,7 +145,7 @@ class command_line(object): elif long_opts[lo][1] == 'string': handler = self._lo_string elif long_opts[lo][1] == 'path': - hanlder = self._lo_path + handler = self._lo_path elif long_opts[lo][1] == 'jobs': handler = self._lo_jobs elif long_opts[lo][1] == 'bool': @@ -139,9 +156,10 @@ class command_line(object): raise error.general('invalid option handler: %s: %s' % (lo, long_opts[lo][1])) self.long_opts[lo] = (long_opts[lo][0], handler, long_opts[lo][2], long_opts[lo][3], long_opts[lo][4]) - if lo not in long_opts_help: - raise error.general('no help for option: %s' % (lo)) - self.long_opts_help[lo] = long_opts_help[lo] + if long_opts_help is not None: + if lo not in long_opts_help: + raise error.general('no help for option: %s' % (lo)) + self.long_opts_help[lo] = long_opts_help[lo] def __copy__(self): new = type(self)() @@ -159,7 +177,7 @@ class command_line(object): def __str__(self): def _dict(dd): s = '' - ddl = dd.keys() + ddl = list(dd.keys()) ddl.sort() for d in ddl: s += ' ' + d + ': ' + str(dd[d]) + '\n' @@ -276,7 +294,7 @@ class command_line(object): print('%s: [options] [args]' % (self.command_name)) print('RTEMS Tools Project (c) 2012-2015 Chris Johns') print('Options and arguments:') - opts = self.long_opts_help.keys() + opts = list(self.long_opts_help.keys()) if self.optargs: opts += self.optargs.keys() indent = self._help_indent() @@ -518,6 +536,9 @@ def load(opts): command line. """ + if not isinstance(opts, command_line): + raise error.general('invalid options type passed to options loader') + global host_windows overrides = None @@ -532,20 +553,41 @@ def load(opts): uname = os.uname() try: if uname[0].startswith('CYGWIN_NT'): - import windows + try: + from . import windows + except: + import windows overrides = windows.load() elif uname[0] == 'Darwin': - import darwin + try: + from . import darwin + except: + import darwin overrides = darwin.load() elif uname[0] == 'FreeBSD': - import freebsd + try: + from . import freebsd + except: + import freebsd overrides = freebsd.load() elif uname[0] == 'NetBSD': - import netbsd + try: + from . import netbsd + except: + import netbsd overrides = netbsd.load() elif uname[0] == 'Linux': - import 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: diff --git a/rtemstoolkit/path.py b/rtemstoolkit/path.py index df51005..15dad1b 100644 --- a/rtemstoolkit/path.py +++ b/rtemstoolkit/path.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -34,13 +34,23 @@ # level. This allows macro expansion to work. # +from __future__ import print_function + import glob import os import shutil import string -import error -import log +# +# 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 + from . import log +except (ValueError, SystemError): + import error + import log windows = os.name == 'nt' diff --git a/rtemstoolkit/solaris.py b/rtemstoolkit/solaris.py new file mode 100644 index 0000000..397df68 --- /dev/null +++ b/rtemstoolkit/solaris.py @@ -0,0 +1,90 @@ +# +# RTEMS Tools Project (http://www.rtems.org/) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) +# All rights reserved. +# +# This file is part of the RTEMS Tools package in 'rtems-tools'. +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +# +# This code is based on what ever doco about spec files I could find and +# RTEMS project's spec files. +# + +import pprint +import os + +try: + from . import check + from . import error + from . import execute +except (ValueError, SystemError): + import check + import error + import execute + +def load(): + uname = os.uname() + psrinfo = '/sbin/psrinfo|wc -l' + e = execute.capture_execution() + exit_code, proc, output = e.shell(psrinfo) + if exit_code == 0: + ncpus = output + else: + ncpus = '1' + if uname[4] == 'i86pc': + cpu = 'i386' + else: + cpu = uname[4] + version = uname[2] + if version.find('-') > 0: + version = version.split('-')[0] + defines = { + '_ncpus': ('none', 'none', ncpus), + '_os': ('none', 'none', 'solaris'), + '_host': ('triplet', 'required', cpu + '-pc-solaris2'), + '_host_vendor': ('none', 'none', 'pc'), + '_host_os': ('none', 'none', 'solaris'), + '_host_os_version': ('none', 'none', version), + '_host_cpu': ('none', 'none', cpu), + '_host_alias': ('none', 'none', '%{nil}'), + '_host_arch': ('none', 'none', cpu), + '_usr': ('dir', 'required', '/usr'), + '_var': ('dir', 'optional', '/var'), + '__bash': ('exe', 'optional', '/usr/bin/bash'), + '__bison': ('exe', 'required', '/usr/bin/bison'), + '__git': ('exe', 'required', '/usr/bin/git'), + '__svn': ('exe', 'required', '/usr/bin/svn'), + '__cvs': ('exe', 'required', '/usr/bin/cvs'), + '__xz': ('exe', 'optional', '/usr/bin/xz'), + '__make': ('exe', 'required', 'gmake'), + '__patch_opts': ('none', 'none', '-E'), + '__chown': ('exe', 'required', '/usr/bin/chown'), + '__install': ('exe', 'required', '/usr/bin/ginstall'), + '__cc': ('exe', 'required', '/usr/bin/gcc'), + '__cxx': ('exe', 'required', '/usr/bin/g++'), + 'with_iconv': ('none', 'none', '0') + } + + defines['_build'] = defines['_host'] + defines['_build_vendor'] = defines['_host_vendor'] + defines['_build_os'] = defines['_host_os'] + defines['_build_cpu'] = defines['_host_cpu'] + defines['_build_alias'] = defines['_host_alias'] + defines['_build_arch'] = defines['_host_arch'] + + return defines + +if __name__ == '__main__': + pprint.pprint(load()) diff --git a/rtemstoolkit/version.py b/rtemstoolkit/version.py index f8b8a7b..3af70a3 100644 --- a/rtemstoolkit/version.py +++ b/rtemstoolkit/version.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2015 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -33,10 +33,22 @@ # file to the top directory. # +from __future__ import print_function + import sys -import error -import path +# +# 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 + from . import git + from . import path +except (ValueError, SystemError): + import error + import git + import path # # Default to an internal string. @@ -67,7 +79,6 @@ def _load_released_version(): return _released def _load_git_version(): - import git global _git global _version_str repo = git.repo(_at()) diff --git a/rtemstoolkit/windows.py b/rtemstoolkit/windows.py index be4e1f6..2c08258 100644 --- a/rtemstoolkit/windows.py +++ b/rtemstoolkit/windows.py @@ -1,6 +1,6 @@ # # RTEMS Tools Project (http://www.rtems.org/) -# Copyright 2010-2014 Chris Johns (chrisj@rtems.org) +# Copyright 2010-2016 Chris Johns (chrisj@rtems.org) # All rights reserved. # # This file is part of the RTEMS Tools package in 'rtems-tools'. @@ -32,11 +32,19 @@ # Windows specific support and overrides. # -import error import pprint import os -import execute +# +# 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 + from . import execute +except (ValueError, SystemError): + import error + import execute def load(): # Default to the native Windows Python. |