/usr/lib/python3/dist-packages/pygccxml/utils/utils.py is in python3-pygccxml 1.8.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | # Copyright 2014-2016 Insight Software Consortium.
# Copyright 2004-2008 Roman Yakovenko.
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt
"""Logger classes and a few convenience methods."""
import os
import sys
import platform
import logging
import tempfile
import subprocess
import warnings
def is_str(string):
"""
Python 2 and 3 compatible string checker.
Args:
string (str | basestring): the string to check
Returns:
bool: True or False
"""
if sys.version_info >= (3, 0):
return isinstance(string, str)
else:
return isinstance(string, basestring)
def find_xml_generator(name=None):
"""
Try to find a c++ parser. Returns path and name.
:param name: name of the c++ parser: castxml or gccxml
:type name: str
If no name is given the function first looks for castxml,
then for gccxml. If no c++ parser is found the function
raises an exception.
"""
if platform.system() == "Windows":
command = "where"
else:
command = "which"
if name is None:
name = "castxml"
p = subprocess.Popen([command, name], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
path = p.stdout.read().decode("utf-8")
p.stdout.close()
p.stderr.close()
if path == "":
name = "gccxml"
p = subprocess.Popen([command, name], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
path = p.stdout.read().decode("utf-8")
p.stdout.close()
p.stderr.close()
else:
p = subprocess.Popen([command, name], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
path = p.stdout.read().decode("utf-8")
p.stdout.close()
p.stderr.close()
if path == "":
raise(Exception(
"No c++ parser found. Please install castxml or gccxml."))
else:
return path.rstrip(), name
def _create_logger_(name):
"""Implementation detail, creates a logger."""
logger = logging.getLogger(name)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(levelname)s %(message)s'))
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
class loggers(object):
"""Class-namespace, defines a few loggers classes, used in the project."""
cxx_parser = _create_logger_('pygccxml.cxx_parser')
"""
Logger for C++ parser functionality
If you set this logger level to DEBUG, you will be able to see the exact
command line, used to invoke GCC-XML and errors that occures during XML
parsing
"""
pdb_reader = _create_logger_('pygccxml.pdb_reader')
"""
Logger for MS .pdb file reader functionality
"""
queries_engine = _create_logger_('pygccxml.queries_engine')
"""
Logger for query engine functionality.
If you set this logger level to DEBUG, you will be able to see what queries
you do against declarations tree, measure performance and may be even to
improve it.
Query engine reports queries and whether they are optimized or not.
"""
declarations_cache = _create_logger_('pygccxml.declarations_cache')
"""
Logger for declarations tree cache functionality
If you set this logger level to DEBUG, you will be able to see what is
exactly happens, when you read the declarations from cache file. You will
be able to decide, whether it worse for you to use this or that cache
strategy.
"""
root = logging.getLogger('pygccxml')
"""
Root logger exists for your convenience only.
"""
all_loggers = [
root, cxx_parser, queries_engine, declarations_cache, pdb_reader]
"""
Contains all logger classes, defined by the class.
"""
@staticmethod
def set_level(level):
"""Set the same logging level for all the loggers at once."""
for logger in loggers.all_loggers:
logger.setLevel(level)
def remove_file_no_raise(file_name, config):
"""Removes file from disk if exception is raised."""
# The removal can be disabled by the config for debugging purposes.
if config.keep_xml:
return True
try:
if os.path.exists(file_name):
os.remove(file_name)
except Exception as error:
loggers.root.error(
"Error occurred while removing temporary created file('%s'): %s" %
(file_name, str(error)))
def create_temp_file_name(suffix, prefix=None, dir=None):
"""
Small convenience function that creates temporary files.
This function is a wrapper around the Python built-in
function tempfile.mkstemp.
"""
if not prefix:
prefix = tempfile.gettempprefix()
fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir)
file_obj = os.fdopen(fd)
file_obj.close()
return name
def normalize_path(some_path):
"""Return os.path.normpath(os.path.normcase(some_path))."""
return os.path.normpath(os.path.normcase(some_path))
def contains_parent_dir(fpath, dirs):
"""
Returns true if paths in dirs start with fpath.
Precondition: dirs and fpath should be normalizeed before calling
this function.
"""
# Note: this function is used nowhere in pygccxml but is used
# at least by pypluplus; so it should stay here.
return bool([x for x in dirs if _f(fpath, x)])
def _f(fpath, dir_):
"""Helper function for contains_parent_dir function."""
return fpath.startswith(dir_)
def get_architecture():
"""
Returns computer architecture: 32 or 64.
The guess is based on maxint.
"""
if sys.maxsize == 2147483647:
return 32
elif sys.maxsize == 9223372036854775807:
return 64
else:
raise RuntimeError("Unknown architecture")
class cached(property):
"""Convert a method into a cached attribute."""
# The following code is cut-and-paste from this post:
# http://groups.google.com/group/comp.lang.python/browse_thread/
# thread/5b71896c06bd0f76/
# Thanks to Michele Simionato
def __init__(self, method):
private = '_' + method.__name__
def fget(s):
try:
return getattr(s, private)
except AttributeError:
value = method(s)
setattr(s, private, value)
return value
def fdel(s):
del s.__dict__[private]
super(cached, self).__init__(fget, fdel=fdel)
@staticmethod
def reset(self):
cls = self.__class__
for name in dir(cls):
attr = getattr(cls, name)
if isinstance(attr, cached):
delattr(self, name)
class enum(object):
"""
A small enum object.
Deprecated since 1.8.0. Will be removed in 1.9.0.
Usage example:
.. code-block:: python
class fruits(enum):
apple = 0
orange = 1
fruits.has_value( 1 )
fruits.name_of( 1 )
"""
def __init__(self):
super(enum, self).__init__()
warnings.warn("enum is deprecated.", DeprecationWarning)
@classmethod
def has_value(cls, enum_numeric_value):
"""
Check if the enum contains the value.
Deprecated since 1.8.0. Will be removed in 1.9.0.
Args:
enum_numeric_value (int): the value to search for
Returns:
bool: True or False
"""
warnings.warn("enum.has_value is deprecated.", DeprecationWarning)
for name, value in cls.__dict__.items():
if enum_numeric_value == value:
return True
else:
return False
@classmethod
def name_of(cls, enum_numeric_value):
"""
Name of the value.
Deprecated since 1.8.0. Will be removed in 1.9.0.
Args:
enum_numeric_value (int): the value to search for
Returns:
str: the name of the value
"""
warnings.warn("enum.name_of is deprecated.", DeprecationWarning)
for name, value in cls.__dict__.items():
if enum_numeric_value == value:
return name
else:
raise RuntimeError(
'Unable to find name for value(%d) in enumeration "%s"' %
(enum_numeric_value, cls.__name__))
class native_compiler(object):
"""Get the compiler used to build the Python executable."""
@staticmethod
def get_version():
"""
Get the version of the compiler used to build the Python executable.
Deprecated since 1.8.0. Will be removed in 1.9.0.
Returns:
tuple:
* name of compiler (str): mscv
* compiler version (str): msvccompiler.get_build_version()
"""
warnings.warn(
"native_compiler.get_version is deprecated.\n",
DeprecationWarning)
if 'nt' != os.name:
return None # not implemented yet
else:
from distutils import msvccompiler
return 'msvc', str(msvccompiler.get_build_version())
@staticmethod
def get_gccxml_compiler():
"""Get a modified version string of the compiler.
Deprecated since 1.8.0. Will be removed in 1.9.0.
Returns:
str: mscvXX, where XX is a version number. Works only on windows.
"""
warnings.warn(
"native_compiler.get_gccxml_compiler is deprecated.\n",
DeprecationWarning)
compiler = native_compiler.get_version()
if not compiler:
return None
else:
n = compiler[1].replace('.', '')
if n.endswith('0'):
n = n[:-1]
return compiler[0] + n.replace('.', '')
def get_tr1(name):
"""In libstd++ the tr1 namespace needs special care.
Return either an empty string or tr1::, useful for
appending to search patterns.
Args:
name (str): the name of the declaration
Returns:
str: an empty string or "tr1::"
"""
tr1 = ""
if "tr1" in name:
tr1 = "tr1::"
return tr1
class cxx_standard(object):
"""Helper class for parsing the C++ standard version.
This class holds the C++ standard version the XML generator has been
configured with, and provides helpers functions for querying C++ standard
version related information.
"""
__STD_CXX = {
'-std=c++98': 199711,
'-std=gnu++98': 199711,
'-std=c++03': 199711,
'-std=gnu++03': 199711,
'-std=c++0x': 201103,
'-std=gnu++0x': 201103,
'-std=c++11': 201103,
'-std=gnu++11': 201103,
'-std=c++1y': 201402,
'-std=gnu++1y': 201402,
'-std=c++14': 201402,
'-std=gnu++14': 201402,
'-std=c++1z': float('inf'),
'-std=gnu++1z': float('inf'),
}
def __init__(self, cflags):
"""Class constructor that parses the XML generator's command line
Args:
cflags (str): cflags command line arguments passed to the XML
generator
"""
super(cxx_standard, self).__init__()
self._stdcxx = None
self._is_implicit = False
for key in cxx_standard.__STD_CXX:
if key in cflags:
self._stdcxx = key
self._cplusplus = cxx_standard.__STD_CXX[key]
if not self._stdcxx:
if '-std=' in cflags:
raise RuntimeError('Unknown -std=c++xx flag used')
# Assume c++03 by default
self._stdcxx = '-std=c++03'
self._cplusplus = cxx_standard.__STD_CXX['-std=c++03']
self._is_implicit = True
@property
def stdcxx(self):
"""Returns the -std=c++xx option passed to the constructor"""
return self._stdcxx
@property
def is_implicit(self):
"""Indicates whether a -std=c++xx was specified"""
return self._is_implicit
@property
def is_cxx03(self):
"""Returns true if -std=c++03 is being used"""
return self._cplusplus == cxx_standard.__STD_CXX['-std=c++03']
@property
def is_cxx11(self):
"""Returns true if -std=c++11 is being used"""
return self._cplusplus == cxx_standard.__STD_CXX['-std=c++11']
@property
def is_cxx11_or_greater(self):
"""Returns true if -std=c++11 or a newer standard is being used"""
return self._cplusplus >= cxx_standard.__STD_CXX['-std=c++11']
@property
def is_cxx14(self):
"""Returns true if -std=c++14 is being used"""
return self._cplusplus == cxx_standard.__STD_CXX['-std=c++14']
@property
def is_cxx14_or_greater(self):
"""Returns true if -std=c++14 or a newer standard is being used"""
return self._cplusplus >= cxx_standard.__STD_CXX['-std=c++14']
@property
def is_cxx1z(self):
"""Returns true if -std=c++1z is being used"""
return self._cplusplus == cxx_standard.__STD_CXX['-std=c++1z']
class DeprecationWrapper(object):
"""
A small wrapper class useful when deprecation classes.
This class is not part of the public API.
"""
def __init__(self, new_target, old_name, new_name, version):
self.new_target = new_target
self.old_name = old_name
self.new_name = new_name
self.version = version
def _warn(self):
warnings.warn(
self.old_name + " is deprecated. Please use " + self.new_name +
" instead. This will be removed in version " + self.version,
DeprecationWarning)
def __call__(self, *args, **kwargs):
self._warn()
return self.new_target(*args, **kwargs)
def __getattr__(self, attr):
self._warn()
return getattr(self.new_target, attr)
|