summaryrefslogtreecommitdiffstats
path: root/rtemstoolkit/options.py
diff options
context:
space:
mode:
Diffstat (limited to 'rtemstoolkit/options.py')
-rw-r--r--rtemstoolkit/options.py105
1 files changed, 73 insertions, 32 deletions
diff --git a/rtemstoolkit/options.py b/rtemstoolkit/options.py
index 657921d..d0e4c81 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
@@ -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'
@@ -273,12 +291,12 @@ class command_line(object):
return indent
def help(self):
- print '%s: [options] [args]' % (self.command_name)
- print 'RTEMS Tools Project (c) 2012-2014 Chris Johns'
- print 'Options and arguments:'
- opts = self.long_opts_help.keys()
+ print('%s: [options] [args]' % (self.command_name))
+ print('RTEMS Tools Project (c) 2012-2014 Chris Johns')
+ print('Options and arguments:')
+ opts = list(self.long_opts_help.keys())
if self.optargs:
- opts += self.optargs.keys()
+ opts += list(self.optargs.keys())
indent = self._help_indent()
for o in sorted(opts):
if o in self.long_opts_help:
@@ -287,7 +305,7 @@ class command_line(object):
h = self.optargs[o]
else:
raise error.general('invalid help data: %s' %(o))
- print '%-*s : %s' % (indent, o, h)
+ print('%-*s : %s' % (indent, o, h))
raise error.exit()
def process(self):
@@ -518,6 +536,9 @@ def load(opts):
command line.
"""
+ if not isinstance(opts, command_line):
+ raise error.general('invalid opt type')
+
global host_windows
overrides = None
@@ -532,20 +553,40 @@ 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
- overrides = linux.load()
+ try:
+ from . import linux
+ except:
+ import linux
+ 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:
@@ -580,13 +621,13 @@ def run(args):
log.notice(str(opts))
log.notice('Defaults:')
log.notice(str(opts.defaults))
- except error.general, gerr:
- print gerr
+ except error.general as gerr:
+ print(gerr)
sys.exit(1)
- except error.internal, ierr:
- print ierr
+ except error.internal as ierr:
+ print(ierr)
sys.exit(1)
- except error.exit, eerr:
+ except error.exit as eerr:
pass
except KeyboardInterrupt:
_notice(opts, 'abort: user terminated')