Imported Upstream version 1.51.0
[platform/upstream/boost.git] / tools / build / src / util / utility.py
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.
5
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.
8 """
9
10 import re
11 import os
12 import bjam
13 from b2.exceptions import *
14
15 __re_grist_and_value = re.compile (r'(<[^>]*>)(.*)')
16 __re_grist_content = re.compile ('^<(.*)>$')
17 __re_backslash = re.compile (r'\\')
18
19 def to_seq (value):
20     """ If value is a sequence, returns it.
21         If it is a string, returns a sequence with value as its sole element.
22         """
23     if not value:
24         return []
25
26     if isinstance (value, str):
27         return [value]
28
29     else:
30         return value
31
32 def replace_references_by_objects (manager, refs):
33     objs = []
34     for r in refs:
35         objs.append (manager.get_object (r))
36     return objs
37
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
42     """
43
44     def grist_one (feature):
45         if feature [0] != '<' and feature [len (feature) - 1] != '>':
46             return '<' + feature + '>'
47         else:
48             return feature
49     
50     if isinstance (features, str):
51         return grist_one (features)
52     else:
53         return [ grist_one (feature) for feature in features ]
54
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.
58     """
59     def replace_grist_one (name, new_grist):
60         split = __re_grist_and_value.match (name)
61         if not split:
62             return new_grist + name
63         else:
64             return new_grist + split.group (2)
65
66     if isinstance (features, str):
67         return replace_grist_one (features, new_grist)
68     else:
69         return [ replace_grist_one (feature, new_grist) for feature in features ]
70
71 def get_value (property):
72     """ Gets the value of a property, that is, the part following the grist, if any.
73     """
74     return replace_grist (property, '')
75     
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.
79     """
80     def get_grist_one (name):
81         split = __re_grist_and_value.match (name)
82         if not split:
83             return ''
84         else:
85             return split.group (1)
86
87     if isinstance (value, str):
88         return get_grist_one (value)
89     else:
90         return [ get_grist_one (v) for v in value ]
91
92 def ungrist (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.
95     """
96     def ungrist_one (value):
97         stripped = __re_grist_content.match (value)
98         if not stripped:
99             raise BaseException ("in ungrist: '%s' is not of the form <.*>" % value)
100
101         return stripped.group (1)
102
103     if isinstance (value, str):
104         return ungrist_one (value)
105     else:
106         return [ ungrist_one (v) for v in value ]
107
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.
111     """
112     split = os.path.splitext (name)
113     return split [0] + new_suffix
114
115 def forward_slashes (s):
116     """ Converts all backslashes to forward slashes.
117     """
118     return __re_backslash.sub ('/', s)
119
120
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++')
124     """
125     split = id.split ('.', 1)
126     toolset = split [0]
127     name = ''
128     if len (split) > 1:
129         name = split [1]
130     return (toolset, name)
131
132 def os_name ():
133     result = bjam.variable("OS")
134     assert(len(result) == 1)
135     return result[0]
136
137 def platform ():
138     return bjam.variable("OSPLAT")
139     
140 def os_version ():
141     return bjam.variable("OSVER")
142
143 def on_windows ():
144     """ Returns true if running on windows, whether in cygwin or not.
145     """
146     if bjam.variable("NT"):
147         return True
148
149     elif bjam.variable("UNIX"):
150
151         uname = bjam.variable("JAMUNAME")
152         if uname and uname[0].startswith("CYGWIN"):
153             return True
154
155     return False