Refactor Env class and purge unused code

parent bb17aeac
...@@ -106,10 +106,7 @@ def _action_runner(modules_pool): ...@@ -106,10 +106,7 @@ def _action_runner(modules_pool):
top_mod = modules_pool.get_top_module() top_mod = modules_pool.get_top_module()
options = modules_pool.env.options options = modules_pool.env.options
if options.command == "check-env": if options.command == "manifest-help":
modules_pool.env.check_env(verbose=True)
quit()
elif options.command == "manifest-help":
ManifestParser().print_help() ManifestParser().print_help()
quit() quit()
elif options.command == "auto": elif options.command == "auto":
...@@ -166,11 +163,6 @@ def _get_parser(): ...@@ -166,11 +163,6 @@ def _get_parser():
description=description) description=description)
subparsers = parser.add_subparsers(title="commands", dest="command") subparsers = parser.add_subparsers(title="commands", dest="command")
check_env = subparsers.add_parser("check-env",
help="check environment for HDLMAKE-related settings",
description="Look for environmental variables specific for HDLMAKE.\n"
"Hdlmake will examine presence of supported synthesis and simulation"
"tools.\n")
manifest_help = subparsers.add_parser( manifest_help = subparsers.add_parser(
"manifest-help", "manifest-help",
help="print manifest file variables description") help="print manifest file variables description")
...@@ -307,7 +299,6 @@ def _get_options(sys, parser): ...@@ -307,7 +299,6 @@ def _get_options(sys, parser):
if sys.argv[1] == "_conditioncheck": if sys.argv[1] == "_conditioncheck":
options = condition_check.parse_args(sys.argv[2:]) options = condition_check.parse_args(sys.argv[2:])
env = Env(options) env = Env(options)
env.check_env()
CheckCondition(modules_pool=None, CheckCondition(modules_pool=None,
options=options, options=options,
env=env).run() env=env).run()
......
...@@ -73,7 +73,6 @@ class ActionSimulation(Action): ...@@ -73,7 +73,6 @@ class ActionSimulation(Action):
path_key = tool_info['id'] + '_path' path_key = tool_info['id'] + '_path'
name = tool_info['name'] name = tool_info['name']
self.env.check_tool(tool_object) self.env.check_tool(tool_object)
self.env.check_general()
if self.env[path_key] is None and self.env.options.force is not True: if self.env[path_key] is None and self.env.options.force is not True:
logging.error("Can't generate a " + name + " makefile. " + logging.error("Can't generate a " + name + " makefile. " +
bin_name + " not found.") bin_name + " not found.")
......
...@@ -86,7 +86,6 @@ class ActionSynthesis(Action): ...@@ -86,7 +86,6 @@ class ActionSynthesis(Action):
path_key = tool_info['id'] + '_path' path_key = tool_info['id'] + '_path'
name = tool_info['name'] name = tool_info['name']
env = self.env env = self.env
env.check_general()
env.check_tool(tool_object) env.check_tool(tool_object)
top_module = self.get_top_module() top_module = self.get_top_module()
if env[path_key]: if env[path_key]:
......
...@@ -21,154 +21,93 @@ ...@@ -21,154 +21,93 @@
# along with Hdlmake. If not, see <http://www.gnu.org/licenses/>. # along with Hdlmake. If not, see <http://www.gnu.org/licenses/>.
# #
"""Package providing the bridge with the Host O.S. environment"""
from __future__ import print_function from __future__ import print_function
import os import os
import sys import sys
import platform
from subprocess import Popen, PIPE
import os.path import os.path
import logging import logging
from .util import path from .util import path as path_mod
from .util.termcolor import colored
_plain_print = print
class _PrintClass(object):
def __init__(self):
self.verbose = None
def set_verbose(self, verbose):
self.verbose = verbose
def __call__(self, *args, **kwargs):
if self.verbose:
_plain_print(*args, **kwargs)
print = _PrintClass()
_64bit_architecture = sys.maxsize > 2 ** 32
def _green(text):
return colored(text, 'green')
def _red(text):
return colored(text, 'red')
class Env(dict): class Env(dict):
# def __init__(self, options, top_module=None): """The Env (Environment) is a dictionary containing the environmental
variables related with HDLMake for a proper use in the Python code"""
def __init__(self, options): def __init__(self, options):
dict.__init__(self) dict.__init__(self)
self.options = options self.options = options
# self.top_module = top_module
def check_env(self, verbose=True):
print.set_verbose(verbose)
# Check and determine general environment
self.check_general()
def _get(self, name): def check_tool(self, info_class):
assert not name.startswith("HDLMAKE_") """Check if the binary is available in the O.S. environment"""
assert isinstance(name, basestring) def _get_path(name):
name = name.upper() """Get the directory in which the tool binary is at Host"""
return os.environ.get("HDLMAKE_%s" % name)
def _get_path(self, name):
if platform.system() == 'Windows':
which_cmd = "where"
else:
which_cmd = "which"
location = os.popen( location = os.popen(
which_cmd + " %s" % path_mod.which_cmd() + " %s" %
name).read().split('\n', 1)[0].strip() name).read().split('\n', 1)[0].strip()
logging.debug("location for %s: %s" % (name, location)) logging.debug("location for %s: %s", name, location)
return os.path.dirname(location) return os.path.dirname(location)
def _is_in_path(name, path=None):
def _is_in_path(self, name, path=None): """Check if the directory is in the system path"""
if path is not None: if path is not None:
return os.path.exists(os.path.join(path, name)) return os.path.exists(os.path.join(path, name))
else: else:
assert isinstance(name, basestring) assert isinstance(name, basestring)
path = self._get_path(name) path = _get_path(name)
return len(path) > 0 return len(path) > 0
def _check_in_system_path(name):
def _check_in_system_path(self, name): """Check if if in the system path exists a file named (name)"""
path = self._get_path(name) path = _get_path(name)
if path: if path:
return True return True
else: else:
return False return False
def check_general(self):
self["architecture"] = 64 if _64bit_architecture else 32
self["platform"] = sys.platform
print("Architecture: %s" % self["architecture"])
print("Platform: %s" % self["platform"])
# general
print("### General variabless ###")
self._report_and_set_hdlmake_var("coredir")
if self["coredir"] is not None:
print(
"All modules will be fetched to %s" %
path.rel2abs(self["coredir"]))
else:
print(
"'fetchto' variables in the manifests will be respected when fetching.")
def check_tool(self, info_class):
tool_info = info_class.TOOL_INFO tool_info = info_class.TOOL_INFO
if sys.platform == 'cygwin': if sys.platform == 'cygwin':
bin_name = tool_info['windows_bin'] bin_name = tool_info['windows_bin']
else: else:
bin_name = tool_info['linux_bin'] bin_name = tool_info['linux_bin']
path_key = tool_info['id'] + '_path' path_key = tool_info['id'] + '_path'
name = tool_info['name'] name = tool_info['name']
logging.debug("Checking if " + name + " tool is available on PATH")
print("\n### " + name + " tool environment information ###")
self._report_and_set_hdlmake_var(path_key) self._report_and_set_hdlmake_var(path_key)
if self[path_key] is not None: if self[path_key] is not None:
if self._is_in_path(bin_name, self[path_key]): if _is_in_path(bin_name, self[path_key]):
print( logging.info("%s found under HDLMAKE_%s: %s",
name + " " + _green("found") + " under HDLMAKE_" + path_key.upper() + ": %s" % name, path_key.upper(), self[path_key])
self[path_key])
else: else:
print( logging.warning("%s NOT found under HDLMAKE_%s: %s",
name + " " + _red("NOT found") + " under HDLMAKE_" + path_key.upper() + ": %s" % name, path_key.upper(), self[path_key])
self[path_key])
else: else:
if self._check_in_system_path(bin_name): if _check_in_system_path(bin_name):
self[path_key] = self._get_path(bin_name) self[path_key] = _get_path(bin_name)
print( logging.info("%s found in system PATH: %s",
name + " " + _green("found") + " in system path: %s" % name, self[path_key])
self[path_key])
else: else:
print(name + " " + _red("cannnot") + " be found.") logging.warning("%s cannnot be found in system PATH", name)
def _report_and_set_hdlmake_var(self, name): def _report_and_set_hdlmake_var(self, name):
"""Create a new entry in the Env dictionary and initialize the value
to the obtained from the O.S. environmental variable if defined"""
def _get(name):
"""Ask the Host O.S. for the value of an HDLMAKE_(name)
environmental variable"""
assert not name.startswith("HDLMAKE_")
assert isinstance(name, basestring)
name = name.upper()
return os.environ.get("HDLMAKE_%s" % name)
name = name.upper() name = name.upper()
val = self._get(name) val = _get(name)
if val: if val:
print( logging.debug('Environmental variable HDLMAKE_%s is set: "%s".',
("Environmental variable HDLMAKE_%s " + _green("is set:") + ' "%s".') % name, val)
(name, val))
self[name.lower()] = val self[name.lower()] = val
return True return True
else: else:
print( logging.warning("Environmental variable HDLMAKE_%s is not set.",
("Environmental variable HDLMAKE_%s " + _red("is not set.")) %
name) name)
self[name.lower()] = None self[name.lower()] = None
return False return False
if __name__ == "__main__":
ec = Env({}, {})
ec.check()
...@@ -31,6 +31,7 @@ import logging ...@@ -31,6 +31,7 @@ import logging
import sys import sys
from .make_sim import ToolSim from .make_sim import ToolSim
from hdlmake.util import path as path_mod
from hdlmake.srcfile import VerilogFile, VHDLFile from hdlmake.srcfile import VerilogFile, VHDLFile
...@@ -80,7 +81,7 @@ class ToolISim(ToolSim): ...@@ -80,7 +81,7 @@ class ToolISim(ToolSim):
os_prefix = 'nt' os_prefix = 'nt'
else: else:
os_prefix = 'lin' os_prefix = 'lin'
if env["architecture"] == 32: if path_mod.architecture() == 32:
arch_sufix = '' arch_sufix = ''
else: else:
arch_sufix = '64' arch_sufix = '64'
......
...@@ -193,9 +193,22 @@ def mkdir_command(): ...@@ -193,9 +193,22 @@ def mkdir_command():
return "mkdir -p" return "mkdir -p"
def which_cmd():
"""Get a string with the O.S. specific which command"""
if check_windows():
return "where"
else:
return "which"
def slash_char(): def slash_char():
"""Get a string with the O.S. specific path separator""" """Get a string with the O.S. specific path separator"""
if check_windows(): if check_windows():
return "\\" return "\\"
else: else:
return "/" return "/"
def architecture():
"""Get a string with the O.S. bus width"""
return 64 if sys.maxsize > 2 ** 32 else 32
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment