1 # (C) Copyright David Abrahams 2001. Permission to copy, use, modify, sell and
2 # distribute this software is granted provided this copyright notice appears in
3 # all copies. This software is provided "as is" without express or implied
4 # warranty, and with no claim as to its suitability for any purpose.
6 """ Utility functions to add/remove/get grists.
7 Grists are string enclosed in angle brackets (<>) that are used as prefixes. See Jam for more information.
13 from b2.exceptions import *
15 __re_grist_and_value = re.compile (r'(<[^>]*>)(.*)')
16 __re_grist_content = re.compile ('^<(.*)>$')
17 __re_backslash = re.compile (r'\\')
20 """ If value is a sequence, returns it.
21 If it is a string, returns a sequence with value as its sole element.
26 if isinstance (value, str):
32 def replace_references_by_objects (manager, refs):
35 objs.append (manager.get_object (r))
38 def add_grist (features):
39 """ Transform a string by bracketing it with "<>". If already bracketed, does nothing.
40 features: one string or a sequence of strings
41 return: the gristed string, if features is a string, or a sequence of gristed strings, if features is a sequence
44 def grist_one (feature):
45 if feature [0] != '<' and feature [len (feature) - 1] != '>':
46 return '<' + feature + '>'
50 if isinstance (features, str):
51 return grist_one (features)
53 return [ grist_one (feature) for feature in features ]
55 def replace_grist (features, new_grist):
56 """ Replaces the grist of a string by a new one.
57 Returns the string with the new grist.
59 def replace_grist_one (name, new_grist):
60 split = __re_grist_and_value.match (name)
62 return new_grist + name
64 return new_grist + split.group (2)
66 if isinstance (features, str):
67 return replace_grist_one (features, new_grist)
69 return [ replace_grist_one (feature, new_grist) for feature in features ]
71 def get_value (property):
72 """ Gets the value of a property, that is, the part following the grist, if any.
74 return replace_grist (property, '')
76 def get_grist (value):
77 """ Returns the grist of a string.
78 If value is a sequence, does it for every value and returns the result as a sequence.
80 def get_grist_one (name):
81 split = __re_grist_and_value.match (name)
85 return split.group (1)
87 if isinstance (value, str):
88 return get_grist_one (value)
90 return [ get_grist_one (v) for v in value ]
93 """ Returns the value without grist.
94 If value is a sequence, does it for every value and returns the result as a sequence.
96 def ungrist_one (value):
97 stripped = __re_grist_content.match (value)
99 raise BaseException ("in ungrist: '%s' is not of the form <.*>" % value)
101 return stripped.group (1)
103 if isinstance (value, str):
104 return ungrist_one (value)
106 return [ ungrist_one (v) for v in value ]
108 def replace_suffix (name, new_suffix):
109 """ Replaces the suffix of name by new_suffix.
110 If no suffix exists, the new one is added.
112 split = os.path.splitext (name)
113 return split [0] + new_suffix
115 def forward_slashes (s):
116 """ Converts all backslashes to forward slashes.
118 return __re_backslash.sub ('/', s)
121 def split_action_id (id):
122 """ Splits an id in the toolset and specific rule parts. E.g.
123 'gcc.compile.c++' returns ('gcc', 'compile.c++')
125 split = id.split ('.', 1)
130 return (toolset, name)
133 result = bjam.variable("OS")
134 assert(len(result) == 1)
138 return bjam.variable("OSPLAT")
141 return bjam.variable("OSVER")
144 """ Returns true if running on windows, whether in cygwin or not.
146 if bjam.variable("NT"):
149 elif bjam.variable("UNIX"):
151 uname = bjam.variable("JAMUNAME")
152 if uname and uname[0].startswith("CYGWIN"):