<commit_before>#
#  This file is part of Bakefile (http://bakefile.sourceforge.net)
#
#  Copyright (C) 2003-2005 <NAME>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  This program 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 this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  $Id$
#
#  Misc utility functions for use in Bakefiles
#

import sys, os, os.path, string, glob
import mk, errors, config
import containers
import dependencies

def checkBakefileVersion(version):
    """Returns true iff current bakefile version is at least 'version'.
       'version' is string with three numbers separated with dots,
       e.g. '0.1.4'."""
    vcur = mk.vars['BAKEFILE_VERSION'].split('.')
    vreq = version.split('.')
    return vcur >= vreq

def isoption(name):
    return name in mk.options

def iscondvar(name):
    return name in mk.cond_vars

def isdefined(name):
    try:
        exec('__foo__ = %s' % name, mk.__curNamespace)
    except NameError:
        return isoption(name) or iscondvar(name)
    return 1

def isconst(expr):
    try:
        mk.evalExpr(expr, use_options=0)
        return True
    except NameError:
        return False

def ifthenelse(cond, iftrue, iffalse):
    if eval(str(cond)): return iftrue
    else: return iffalse


__refEval = 0
__refContexts = {}

def ref(var, target=None, context=None):
    if __refEval:
        if target != None and target not in mk.targets:
            if context != None:
                context = __refContexts[context]
            raise errors.Error("target '%s' cannot be used in ref() since it doesn't exist" % target,
                               context=context)
        if target==None or var not in mk.targets[target].vars:
            return mk.vars[var]
        else:
            return mk.targets[target].vars[var]
    else:
        if mk.__trackUsage: mk.__usageTracker.refs += 1
        if context == None:
            context = len(__refContexts)
            __refContexts[context] = errors.getCtx()
        if target == None:
            return "$(ref('%s',None,%i))" % (var, context)
        else:
            return "$(ref('%s','%s',%i))" % (var, target, context)

deadTargets = []
def isDeadTarget(target):
    return target in deadTargets
<commit_msg>better error message when ref() evaluation fails on undefined variable: show when was the value set<commit_after>#
#  This file is part of Bakefile (http://bakefile.sourceforge.net)
#
#  Copyright (C) 2003-2005 <NAME>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  This program 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 this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  $Id$
#
#  Misc utility functions for use in Bakefiles
#

import sys, os, os.path, string, glob
import mk, errors, config
import containers
import dependencies

def checkBakefileVersion(version):
    """Returns true iff current bakefile version is at least 'version'.
       'version' is string with three numbers separated with dots,
       e.g. '0.1.4'."""
    vcur = mk.vars['BAKEFILE_VERSION'].split('.')
    vreq = version.split('.')
    return vcur >= vreq

def isoption(name):
    return name in mk.options

def iscondvar(name):
    return name in mk.cond_vars

def isdefined(name):
    try:
        exec('__foo__ = %s' % name, mk.__curNamespace)
    except NameError:
        return isoption(name) or iscondvar(name)
    return 1

def isconst(expr):
    try:
        mk.evalExpr(expr, use_options=0)
        return True
    except NameError:
        return False

def ifthenelse(cond, iftrue, iffalse):
    if eval(str(cond)): return iftrue
    else: return iffalse


__refEval = 0
__refContexts = {}

def ref(var, target=None, context=None):
    if __refEval:
        if context != None:
            context = __refContexts[context]
        if target != None and target not in mk.targets:
            raise errors.Error("target '%s' cannot be used in ref() since it doesn't exist" % target,
                               context=context)
        try:
            if target == None or var not in mk.targets[target].vars:
                return mk.vars[var]
            else:
                return mk.targets[target].vars[var]
        except KeyError:
            if target == None:
                raise errors.Error("undefined variable '%s'" % var,
                                   context=context)
            else:
                raise errors.Error("undefined variable '%s' on target '%s'" % (var, target),
                                   context=context)
    else:
        if mk.__trackUsage: mk.__usageTracker.refs += 1
        if context == None:
            context = len(__refContexts)
            __refContexts[context] = errors.getCtx()
        if target == None:
            return "$(ref('%s',None,%i))" % (var, context)
        else:
            return "$(ref('%s','%s',%i))" % (var, target, context)

deadTargets = []
def isDeadTarget(target):
    return target in deadTargets

{'fraction_non_alphanumeric': 0.09837453437182526, 'fraction_numerical': 0.01015916017609211, 'mean_word_length': 2.610635696821516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5848205', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1680, 'n_words': 631}