+use_env = {}
+for key,val in os.environ.iteritems():
+ if key in use_vars or key.startswith("M5"):
+ use_env[key] = val
+
+main = Environment(ENV=use_env)
+main.root = Dir(".") # The current directory (where this file lives).
+main.srcdir = Dir("src") # The source directory
+
+# add useful python code PYTHONPATH so it can be used by subprocesses
+# as well
+main.AppendENVPath('PYTHONPATH', extra_python_paths)
+
+########################################################################
+#
+# Mercurial Stuff.
+#
+# If the M5 directory is a mercurial repository, we should do some
+# extra things.
+#
+########################################################################
+
+hgdir = main.root.Dir(".hg")
+
+mercurial_style_message = """
+You're missing the M5 style hook.
+Please install the hook so we can ensure that all code fits a common style.
+
+All you'd need to do is add the following lines to your repository .hg/hgrc
+or your personal .hgrc
+----------------
+
+[extensions]
+style = %s/util/style.py
+
+[hooks]
+pretxncommit.style = python:style.check_whitespace
+""" % (main.root)
+
+mercurial_bin_not_found = """
+Mercurial binary cannot be found, unfortunately this means that we
+cannot easily determine the version of M5 that you are running and
+this makes error messages more difficult to collect. Please consider
+installing mercurial if you choose to post an error message
+"""
+
+mercurial_lib_not_found = """
+Mercurial libraries cannot be found, ignoring style hook
+If you are actually a M5 developer, please fix this and
+run the style hook. It is important.
+"""
+
+hg_info = "Unknown"
+if hgdir.exists():
+ # 1) Grab repository revision if we know it.
+ cmd = "hg id -n -i -t -b"
+ try:
+ hg_info = readCommand(cmd, cwd=main.root.abspath).strip()
+ except OSError:
+ print mercurial_bin_not_found
+
+ # 2) Ensure that the style hook is in place.
+ try:
+ ui = None
+ if ARGUMENTS.get('IGNORE_STYLE') != 'True':
+ from mercurial import ui
+ ui = ui.ui()
+ except ImportError:
+ print mercurial_lib_not_found
+
+ if ui is not None:
+ ui.readconfig(hgdir.File('hgrc').abspath)
+ style_hook = ui.config('hooks', 'pretxncommit.style', None)
+
+ if not style_hook:
+ print mercurial_style_message
+ sys.exit(1)
+else:
+ print ".hg directory not found"
+
+main['HG_INFO'] = hg_info