2011-09-10 11:13:51 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
2018-01-01 20:53:49 +01:00
|
|
|
# Thomas Nagy, 2005-2018 (ita)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Utilities and platform-specific fixes
|
|
|
|
|
|
|
|
The portability fixes try to provide a consistent behavior of the Waf API
|
2016-02-25 20:03:07 +01:00
|
|
|
through Python versions 2.5 to 3.X and across different platforms (win32, linux, etc)
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
|
2017-09-12 19:36:43 +02:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2017-09-02 23:34:58 +02:00
|
|
|
import atexit, os, sys, errno, inspect, re, datetime, platform, base64, signal, functools, time
|
2016-11-26 11:00:01 +01:00
|
|
|
|
2016-02-25 20:03:07 +01:00
|
|
|
try:
|
|
|
|
import cPickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle as cPickle
|
2016-02-10 23:46:55 +01:00
|
|
|
|
|
|
|
# leave this
|
|
|
|
if os.name == 'posix' and sys.version_info[0] < 3:
|
|
|
|
try:
|
|
|
|
import subprocess32 as subprocess
|
|
|
|
except ImportError:
|
|
|
|
import subprocess
|
|
|
|
else:
|
|
|
|
import subprocess
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2016-08-25 07:48:14 +02:00
|
|
|
try:
|
|
|
|
TimeoutExpired = subprocess.TimeoutExpired
|
|
|
|
except AttributeError:
|
2017-04-22 22:12:11 +02:00
|
|
|
class TimeoutExpired(Exception):
|
2016-08-25 07:48:14 +02:00
|
|
|
pass
|
|
|
|
|
2014-10-04 13:49:28 +02:00
|
|
|
from collections import deque, defaultdict
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
|
|
|
import _winreg as winreg
|
2012-02-11 13:43:47 +01:00
|
|
|
except ImportError:
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
|
|
|
import winreg
|
2012-02-11 13:43:47 +01:00
|
|
|
except ImportError:
|
2011-09-10 11:13:51 +02:00
|
|
|
winreg = None
|
|
|
|
|
|
|
|
from waflib import Errors
|
|
|
|
|
|
|
|
try:
|
|
|
|
from hashlib import md5
|
2012-02-11 13:43:47 +01:00
|
|
|
except ImportError:
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
|
|
|
from md5 import md5
|
2012-02-11 13:43:47 +01:00
|
|
|
except ImportError:
|
2011-09-10 11:13:51 +02:00
|
|
|
# never fail to enable fixes from another module
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
import threading
|
2012-02-11 13:43:47 +01:00
|
|
|
except ImportError:
|
2014-01-05 10:51:24 +01:00
|
|
|
if not 'JOBS' in os.environ:
|
|
|
|
# no threading :-(
|
|
|
|
os.environ['JOBS'] = '1'
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
class threading(object):
|
|
|
|
"""
|
2016-05-14 00:16:29 +02:00
|
|
|
A fake threading class for platforms lacking the threading module.
|
|
|
|
Use ``waf -j1`` on those platforms
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
class Lock(object):
|
|
|
|
"""Fake Lock class"""
|
|
|
|
def acquire(self):
|
|
|
|
pass
|
|
|
|
def release(self):
|
|
|
|
pass
|
|
|
|
threading.Lock = threading.Thread = Lock
|
|
|
|
|
2016-02-14 11:15:52 +01:00
|
|
|
SIG_NIL = 'SIG_NIL_SIG_NIL_'.encode()
|
|
|
|
"""Arbitrary null value for hashes. Modify this value according to the hash function in use"""
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
O644 = 420
|
|
|
|
"""Constant representing the permissions for regular files (0644 raises a syntax error on python 3)"""
|
|
|
|
|
|
|
|
O755 = 493
|
|
|
|
"""Constant representing the permissions for executable files (0755 raises a syntax error on python 3)"""
|
|
|
|
|
|
|
|
rot_chr = ['\\', '|', '/', '-']
|
|
|
|
"List of characters to use when displaying the throbber (progress bar)"
|
|
|
|
|
|
|
|
rot_idx = 0
|
|
|
|
"Index of the current throbber character (progress bar)"
|
|
|
|
|
2016-04-22 19:36:35 +02:00
|
|
|
class ordered_iter_dict(dict):
|
2016-06-25 16:23:06 +02:00
|
|
|
"""Ordered dictionary that provides iteration from the most recently inserted keys first"""
|
2016-04-22 19:36:35 +02:00
|
|
|
def __init__(self, *k, **kw):
|
|
|
|
self.lst = deque()
|
|
|
|
dict.__init__(self, *k, **kw)
|
|
|
|
def clear(self):
|
|
|
|
dict.clear(self)
|
|
|
|
self.lst = deque()
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
if key in dict.keys(self):
|
|
|
|
self.lst.remove(key)
|
|
|
|
dict.__setitem__(self, key, value)
|
|
|
|
self.lst.append(key)
|
|
|
|
def __delitem__(self, key):
|
|
|
|
dict.__delitem__(self, key)
|
|
|
|
try:
|
|
|
|
self.lst.remove(key)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
def __iter__(self):
|
|
|
|
return reversed(self.lst)
|
|
|
|
def keys(self):
|
|
|
|
return reversed(self.lst)
|
|
|
|
|
2016-04-22 19:43:18 +02:00
|
|
|
class lru_node(object):
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Used by :py:class:`waflib.Utils.lru_cache`
|
|
|
|
"""
|
2016-04-22 19:43:18 +02:00
|
|
|
__slots__ = ('next', 'prev', 'key', 'val')
|
|
|
|
def __init__(self):
|
|
|
|
self.next = self
|
|
|
|
self.prev = self
|
|
|
|
self.key = None
|
|
|
|
self.val = None
|
|
|
|
|
|
|
|
class lru_cache(object):
|
2016-11-26 18:14:22 +01:00
|
|
|
"""
|
|
|
|
A simple least-recently used cache with lazy allocation
|
|
|
|
"""
|
2016-05-14 11:29:06 +02:00
|
|
|
__slots__ = ('maxlen', 'table', 'head')
|
2016-02-15 00:35:06 +01:00
|
|
|
def __init__(self, maxlen=100):
|
|
|
|
self.maxlen = maxlen
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Maximum amount of elements in the cache
|
|
|
|
"""
|
2016-04-22 19:43:18 +02:00
|
|
|
self.table = {}
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Mapping key-value
|
|
|
|
"""
|
2016-04-22 19:43:18 +02:00
|
|
|
self.head = lru_node()
|
2016-11-26 18:14:22 +01:00
|
|
|
self.head.next = self.head
|
|
|
|
self.head.prev = self.head
|
2016-04-22 19:43:18 +02:00
|
|
|
|
2016-02-15 21:13:48 +01:00
|
|
|
def __getitem__(self, key):
|
2016-04-22 19:43:18 +02:00
|
|
|
node = self.table[key]
|
|
|
|
# assert(key==node.key)
|
|
|
|
if node is self.head:
|
|
|
|
return node.val
|
|
|
|
|
|
|
|
# detach the node found
|
|
|
|
node.prev.next = node.next
|
|
|
|
node.next.prev = node.prev
|
|
|
|
|
|
|
|
# replace the head
|
|
|
|
node.next = self.head.next
|
|
|
|
node.prev = self.head
|
2016-11-26 18:14:22 +01:00
|
|
|
self.head = node.next.prev = node.prev.next = node
|
2016-04-22 19:43:18 +02:00
|
|
|
|
|
|
|
return node.val
|
|
|
|
|
|
|
|
def __setitem__(self, key, val):
|
2016-11-26 18:14:22 +01:00
|
|
|
if key in self.table:
|
|
|
|
# update the value for an existing key
|
|
|
|
node = self.table[key]
|
|
|
|
node.val = val
|
|
|
|
self.__getitem__(key)
|
|
|
|
else:
|
|
|
|
if len(self.table) < self.maxlen:
|
|
|
|
# the very first item is unused until the maximum is reached
|
|
|
|
node = lru_node()
|
|
|
|
node.prev = self.head
|
|
|
|
node.next = self.head.next
|
|
|
|
node.prev.next = node.next.prev = node
|
|
|
|
else:
|
|
|
|
node = self.head = self.head.next
|
|
|
|
try:
|
|
|
|
# that's another key
|
|
|
|
del self.table[node.key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
node.key = key
|
|
|
|
node.val = val
|
|
|
|
self.table[key] = node
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2017-12-23 14:56:54 +01:00
|
|
|
class lazy_generator(object):
|
|
|
|
def __init__(self, fun, params):
|
|
|
|
self.fun = fun
|
|
|
|
self.params = params
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
try:
|
|
|
|
it = self.it
|
|
|
|
except AttributeError:
|
|
|
|
it = self.it = self.fun(*self.params)
|
|
|
|
return next(it)
|
|
|
|
|
2018-03-11 22:42:21 +01:00
|
|
|
next = __next__
|
|
|
|
|
2015-03-08 18:30:57 +01:00
|
|
|
is_win32 = os.sep == '\\' or sys.platform == 'win32' # msys2
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Whether this system is a Windows series
|
|
|
|
"""
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2017-01-21 13:28:06 +01:00
|
|
|
def readf(fname, m='r', encoding='latin-1'):
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Reads an entire file into a string. See also :py:meth:`waflib.Node.Node.readf`::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def build(ctx):
|
|
|
|
from waflib import Utils
|
|
|
|
txt = Utils.readf(self.path.find_node('wscript').abspath())
|
|
|
|
txt = ctx.path.find_node('wscript').read()
|
|
|
|
|
|
|
|
:type fname: string
|
|
|
|
:param fname: Path to file
|
|
|
|
:type m: string
|
|
|
|
:param m: Open mode
|
2012-06-05 04:31:31 +02:00
|
|
|
:type encoding: string
|
|
|
|
:param encoding: encoding value, only used for python 3
|
2011-09-10 11:13:51 +02:00
|
|
|
:rtype: string
|
|
|
|
:return: Content of the file
|
|
|
|
"""
|
2012-06-05 04:31:31 +02:00
|
|
|
|
|
|
|
if sys.hexversion > 0x3000000 and not 'b' in m:
|
|
|
|
m += 'b'
|
2017-09-12 19:36:43 +02:00
|
|
|
with open(fname, m) as f:
|
2012-06-05 04:31:31 +02:00
|
|
|
txt = f.read()
|
2014-09-28 01:30:00 +02:00
|
|
|
if encoding:
|
|
|
|
txt = txt.decode(encoding)
|
|
|
|
else:
|
|
|
|
txt = txt.decode()
|
2012-06-05 04:31:31 +02:00
|
|
|
else:
|
2017-09-12 19:36:43 +02:00
|
|
|
with open(fname, m) as f:
|
2012-06-05 04:31:31 +02:00
|
|
|
txt = f.read()
|
2011-09-10 11:13:51 +02:00
|
|
|
return txt
|
|
|
|
|
2017-01-21 13:28:06 +01:00
|
|
|
def writef(fname, data, m='w', encoding='latin-1'):
|
2012-02-12 15:27:38 +01:00
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Writes an entire file from a string.
|
|
|
|
See also :py:meth:`waflib.Node.Node.writef`::
|
2012-02-12 15:27:38 +01:00
|
|
|
|
|
|
|
def build(ctx):
|
|
|
|
from waflib import Utils
|
|
|
|
txt = Utils.writef(self.path.make_node('i_like_kittens').abspath(), 'some data')
|
|
|
|
self.path.make_node('i_like_kittens').write('some data')
|
|
|
|
|
|
|
|
:type fname: string
|
|
|
|
:param fname: Path to file
|
|
|
|
:type data: string
|
|
|
|
:param data: The contents to write to the file
|
|
|
|
:type m: string
|
|
|
|
:param m: Open mode
|
2012-06-05 04:31:31 +02:00
|
|
|
:type encoding: string
|
|
|
|
:param encoding: encoding value, only used for python 3
|
2012-02-12 15:27:38 +01:00
|
|
|
"""
|
2012-06-05 04:31:31 +02:00
|
|
|
if sys.hexversion > 0x3000000 and not 'b' in m:
|
|
|
|
data = data.encode(encoding)
|
|
|
|
m += 'b'
|
2017-09-12 19:36:43 +02:00
|
|
|
with open(fname, m) as f:
|
2012-05-17 13:49:09 +02:00
|
|
|
f.write(data)
|
2012-02-12 15:27:38 +01:00
|
|
|
|
2012-05-19 10:29:44 +02:00
|
|
|
def h_file(fname):
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
2016-06-25 12:50:04 +02:00
|
|
|
Computes a hash value for a file by using md5. Use the md5_tstamp
|
2016-05-21 12:01:48 +02:00
|
|
|
extension to get faster build hashes if necessary.
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2012-05-19 10:29:44 +02:00
|
|
|
:type fname: string
|
|
|
|
:param fname: path to the file to hash
|
2011-09-10 11:13:51 +02:00
|
|
|
:return: hash of the file contents
|
2016-06-25 16:23:06 +02:00
|
|
|
:rtype: string or bytes
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
m = md5()
|
2017-09-12 19:36:43 +02:00
|
|
|
with open(fname, 'rb') as f:
|
2012-05-19 10:29:44 +02:00
|
|
|
while fname:
|
|
|
|
fname = f.read(200000)
|
|
|
|
m.update(fname)
|
2011-09-10 11:13:51 +02:00
|
|
|
return m.digest()
|
|
|
|
|
2017-01-21 13:28:06 +01:00
|
|
|
def readf_win32(f, m='r', encoding='latin-1'):
|
2014-04-09 22:27:17 +02:00
|
|
|
flags = os.O_NOINHERIT | os.O_RDONLY
|
|
|
|
if 'b' in m:
|
|
|
|
flags |= os.O_BINARY
|
|
|
|
if '+' in m:
|
|
|
|
flags |= os.O_RDWR
|
|
|
|
try:
|
|
|
|
fd = os.open(f, flags)
|
|
|
|
except OSError:
|
|
|
|
raise IOError('Cannot read from %r' % f)
|
2012-05-19 10:29:44 +02:00
|
|
|
|
2014-04-09 22:27:17 +02:00
|
|
|
if sys.hexversion > 0x3000000 and not 'b' in m:
|
|
|
|
m += 'b'
|
2017-09-12 19:36:43 +02:00
|
|
|
with os.fdopen(fd, m) as f:
|
2014-04-09 22:27:17 +02:00
|
|
|
txt = f.read()
|
2014-09-28 01:30:00 +02:00
|
|
|
if encoding:
|
|
|
|
txt = txt.decode(encoding)
|
|
|
|
else:
|
|
|
|
txt = txt.decode()
|
2014-04-09 22:27:17 +02:00
|
|
|
else:
|
2017-09-12 19:36:43 +02:00
|
|
|
with os.fdopen(fd, m) as f:
|
2014-04-09 22:27:17 +02:00
|
|
|
txt = f.read()
|
|
|
|
return txt
|
|
|
|
|
2017-01-21 13:28:06 +01:00
|
|
|
def writef_win32(f, data, m='w', encoding='latin-1'):
|
2014-04-09 22:27:17 +02:00
|
|
|
if sys.hexversion > 0x3000000 and not 'b' in m:
|
|
|
|
data = data.encode(encoding)
|
|
|
|
m += 'b'
|
|
|
|
flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | os.O_NOINHERIT
|
|
|
|
if 'b' in m:
|
|
|
|
flags |= os.O_BINARY
|
|
|
|
if '+' in m:
|
|
|
|
flags |= os.O_RDWR
|
|
|
|
try:
|
|
|
|
fd = os.open(f, flags)
|
|
|
|
except OSError:
|
2016-02-28 14:23:54 +01:00
|
|
|
raise OSError('Cannot write to %r' % f)
|
2017-09-12 19:36:43 +02:00
|
|
|
with os.fdopen(fd, m) as f:
|
2014-04-09 22:27:17 +02:00
|
|
|
f.write(data)
|
2012-05-19 10:29:44 +02:00
|
|
|
|
2014-04-09 22:27:17 +02:00
|
|
|
def h_file_win32(fname):
|
|
|
|
try:
|
|
|
|
fd = os.open(fname, os.O_BINARY | os.O_RDONLY | os.O_NOINHERIT)
|
|
|
|
except OSError:
|
2016-02-28 14:23:54 +01:00
|
|
|
raise OSError('Cannot read from %r' % fname)
|
2014-04-09 22:27:17 +02:00
|
|
|
m = md5()
|
2017-09-12 19:36:43 +02:00
|
|
|
with os.fdopen(fd, 'rb') as f:
|
2014-04-09 22:27:17 +02:00
|
|
|
while fname:
|
|
|
|
fname = f.read(200000)
|
|
|
|
m.update(fname)
|
|
|
|
return m.digest()
|
|
|
|
|
|
|
|
# always save these
|
|
|
|
readf_unix = readf
|
|
|
|
writef_unix = writef
|
|
|
|
h_file_unix = h_file
|
|
|
|
if hasattr(os, 'O_NOINHERIT') and sys.hexversion < 0x3040000:
|
2012-05-19 10:29:44 +02:00
|
|
|
# replace the default functions
|
|
|
|
readf = readf_win32
|
|
|
|
writef = writef_win32
|
|
|
|
h_file = h_file_win32
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
|
|
|
x = ''.encode('hex')
|
2012-02-11 13:43:47 +01:00
|
|
|
except LookupError:
|
2011-09-10 11:13:51 +02:00
|
|
|
import binascii
|
|
|
|
def to_hex(s):
|
|
|
|
ret = binascii.hexlify(s)
|
|
|
|
if not isinstance(ret, str):
|
|
|
|
ret = ret.decode('utf-8')
|
|
|
|
return ret
|
|
|
|
else:
|
|
|
|
def to_hex(s):
|
|
|
|
return s.encode('hex')
|
|
|
|
|
|
|
|
to_hex.__doc__ = """
|
|
|
|
Return the hexadecimal representation of a string
|
|
|
|
|
|
|
|
:param s: string to convert
|
|
|
|
:type s: string
|
|
|
|
"""
|
|
|
|
|
2014-04-09 22:27:17 +02:00
|
|
|
def listdir_win32(s):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Lists the contents of a folder in a portable manner.
|
|
|
|
On Win32, returns the list of drive letters: ['C:', 'X:', 'Z:'] when an empty string is given.
|
2014-04-09 22:27:17 +02:00
|
|
|
|
|
|
|
:type s: string
|
|
|
|
:param s: a string, which can be empty on Windows
|
|
|
|
"""
|
|
|
|
if not s:
|
|
|
|
try:
|
|
|
|
import ctypes
|
|
|
|
except ImportError:
|
|
|
|
# there is nothing much we can do
|
2016-03-18 18:26:25 +01:00
|
|
|
return [x + ':\\' for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ']
|
2014-04-09 22:27:17 +02:00
|
|
|
else:
|
|
|
|
dlen = 4 # length of "?:\\x00"
|
|
|
|
maxdrives = 26
|
|
|
|
buf = ctypes.create_string_buffer(maxdrives * dlen)
|
|
|
|
ndrives = ctypes.windll.kernel32.GetLogicalDriveStringsA(maxdrives*dlen, ctypes.byref(buf))
|
|
|
|
return [ str(buf.raw[4*i:4*i+2].decode('ascii')) for i in range(int(ndrives/dlen)) ]
|
|
|
|
|
|
|
|
if len(s) == 2 and s[1] == ":":
|
|
|
|
s += os.sep
|
|
|
|
|
|
|
|
if not os.path.isdir(s):
|
|
|
|
e = OSError('%s is not a directory' % s)
|
|
|
|
e.errno = errno.ENOENT
|
|
|
|
raise e
|
|
|
|
return os.listdir(s)
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
listdir = os.listdir
|
|
|
|
if is_win32:
|
|
|
|
listdir = listdir_win32
|
|
|
|
|
|
|
|
def num2ver(ver):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Converts a string, tuple or version number into an integer. The number is supposed to have at most 4 digits::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
from waflib.Utils import num2ver
|
|
|
|
num2ver('1.3.2') == num2ver((1,3,2)) == num2ver((1,3,2,0))
|
|
|
|
|
|
|
|
:type ver: string or tuple of numbers
|
|
|
|
:param ver: a version number
|
|
|
|
"""
|
|
|
|
if isinstance(ver, str):
|
|
|
|
ver = tuple(ver.split('.'))
|
|
|
|
if isinstance(ver, tuple):
|
|
|
|
ret = 0
|
|
|
|
for i in range(4):
|
|
|
|
if i < len(ver):
|
|
|
|
ret += 256**(3 - i) * int(ver[i])
|
|
|
|
return ret
|
|
|
|
return ver
|
|
|
|
|
2016-06-25 16:23:06 +02:00
|
|
|
def to_list(val):
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Converts a string argument to a list by splitting it by spaces.
|
|
|
|
Returns the object if not a string::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
from waflib.Utils import to_list
|
2016-06-25 16:23:06 +02:00
|
|
|
lst = to_list('a b c d')
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2016-06-25 16:23:06 +02:00
|
|
|
:param val: list of string or space-separated string
|
2011-09-10 11:13:51 +02:00
|
|
|
:rtype: list
|
|
|
|
:return: Argument converted to list
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
if isinstance(val, str):
|
|
|
|
return val.split()
|
2011-09-10 11:13:51 +02:00
|
|
|
else:
|
2016-06-25 16:23:06 +02:00
|
|
|
return val
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2018-05-19 11:23:46 +02:00
|
|
|
def console_encoding():
|
|
|
|
try:
|
|
|
|
import ctypes
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
codepage = ctypes.windll.kernel32.GetConsoleCP()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if codepage:
|
|
|
|
return 'cp%d' % codepage
|
|
|
|
return sys.stdout.encoding or ('cp1252' if is_win32 else 'latin-1')
|
|
|
|
|
2014-04-09 22:27:17 +02:00
|
|
|
def split_path_unix(path):
|
2011-09-10 11:13:51 +02:00
|
|
|
return path.split('/')
|
|
|
|
|
|
|
|
def split_path_cygwin(path):
|
|
|
|
if path.startswith('//'):
|
|
|
|
ret = path.split('/')[2:]
|
|
|
|
ret[0] = '/' + ret[0]
|
|
|
|
return ret
|
|
|
|
return path.split('/')
|
|
|
|
|
2016-03-27 21:41:21 +02:00
|
|
|
re_sp = re.compile('[/\\\\]+')
|
2011-09-10 11:13:51 +02:00
|
|
|
def split_path_win32(path):
|
|
|
|
if path.startswith('\\\\'):
|
2017-03-02 20:45:01 +01:00
|
|
|
ret = re_sp.split(path)[1:]
|
|
|
|
ret[0] = '\\\\' + ret[0]
|
|
|
|
if ret[0] == '\\\\?':
|
|
|
|
return ret[1:]
|
2011-09-10 11:13:51 +02:00
|
|
|
return ret
|
2016-03-27 21:41:21 +02:00
|
|
|
return re_sp.split(path)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2015-10-16 22:45:27 +02:00
|
|
|
msysroot = None
|
|
|
|
def split_path_msys(path):
|
2017-03-02 20:45:01 +01:00
|
|
|
if path.startswith(('/', '\\')) and not path.startswith(('//', '\\\\')):
|
2015-10-16 22:45:27 +02:00
|
|
|
# msys paths can be in the form /usr/bin
|
|
|
|
global msysroot
|
|
|
|
if not msysroot:
|
|
|
|
# msys has python 2.7 or 3, so we can use this
|
2017-01-21 13:28:06 +01:00
|
|
|
msysroot = subprocess.check_output(['cygpath', '-w', '/']).decode(sys.stdout.encoding or 'latin-1')
|
2015-10-16 22:45:27 +02:00
|
|
|
msysroot = msysroot.strip()
|
|
|
|
path = os.path.normpath(msysroot + os.sep + path)
|
|
|
|
return split_path_win32(path)
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
if sys.platform == 'cygwin':
|
|
|
|
split_path = split_path_cygwin
|
|
|
|
elif is_win32:
|
2016-04-19 22:00:21 +02:00
|
|
|
if os.environ.get('MSYSTEM'):
|
2015-10-16 22:45:27 +02:00
|
|
|
split_path = split_path_msys
|
|
|
|
else:
|
|
|
|
split_path = split_path_win32
|
2014-04-09 22:27:17 +02:00
|
|
|
else:
|
|
|
|
split_path = split_path_unix
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
split_path.__doc__ = """
|
2016-06-25 16:23:06 +02:00
|
|
|
Splits a path by / or \\; do not confuse this function with with ``os.path.split``
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
:type path: string
|
|
|
|
:param path: path to split
|
2016-06-25 16:23:06 +02:00
|
|
|
:return: list of string
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def check_dir(path):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Ensures that a directory exists (similar to ``mkdir -p``).
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2013-08-02 18:39:21 +02:00
|
|
|
:type path: string
|
|
|
|
:param path: Path to directory
|
2016-06-25 16:23:06 +02:00
|
|
|
:raises: :py:class:`waflib.Errors.WafError` if the folder cannot be added.
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
try:
|
|
|
|
os.makedirs(path)
|
|
|
|
except OSError as e:
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
raise Errors.WafError('Cannot create the folder %r' % path, ex=e)
|
|
|
|
|
2014-04-07 22:10:56 +02:00
|
|
|
def check_exe(name, env=None):
|
2013-08-02 18:39:21 +02:00
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Ensures that a program exists
|
2015-10-03 09:12:08 +02:00
|
|
|
|
2013-08-02 18:39:21 +02:00
|
|
|
:type name: string
|
2016-06-25 16:23:06 +02:00
|
|
|
:param name: path to the program
|
|
|
|
:param env: configuration object
|
|
|
|
:type env: :py:class:`waflib.ConfigSet.ConfigSet`
|
2013-08-02 18:39:21 +02:00
|
|
|
:return: path of the program or None
|
2016-06-25 16:23:06 +02:00
|
|
|
:raises: :py:class:`waflib.Errors.WafError` if the folder cannot be added.
|
2013-08-02 18:39:21 +02:00
|
|
|
"""
|
2013-08-17 18:19:10 +02:00
|
|
|
if not name:
|
|
|
|
raise ValueError('Cannot execute an empty string!')
|
2013-08-02 18:39:21 +02:00
|
|
|
def is_exe(fpath):
|
|
|
|
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
|
|
|
|
|
|
|
|
fpath, fname = os.path.split(name)
|
|
|
|
if fpath and is_exe(name):
|
2014-01-08 07:58:03 +01:00
|
|
|
return os.path.abspath(name)
|
2013-08-02 18:39:21 +02:00
|
|
|
else:
|
2014-04-07 22:10:56 +02:00
|
|
|
env = env or os.environ
|
2016-06-25 16:23:06 +02:00
|
|
|
for path in env['PATH'].split(os.pathsep):
|
2013-08-02 18:39:21 +02:00
|
|
|
path = path.strip('"')
|
|
|
|
exe_file = os.path.join(path, name)
|
|
|
|
if is_exe(exe_file):
|
2014-01-08 07:58:03 +01:00
|
|
|
return os.path.abspath(exe_file)
|
2013-08-02 18:39:21 +02:00
|
|
|
return None
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def def_attrs(cls, **kw):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Sets default attributes on a class instance
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
:type cls: class
|
|
|
|
:param cls: the class to update the given attributes in.
|
|
|
|
:type kw: dict
|
|
|
|
:param kw: dictionary of attributes names and values.
|
|
|
|
"""
|
|
|
|
for k, v in kw.items():
|
|
|
|
if not hasattr(cls, k):
|
|
|
|
setattr(cls, k, v)
|
|
|
|
|
|
|
|
def quote_define_name(s):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Converts a string into an identifier suitable for C defines.
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
:type s: string
|
|
|
|
:param s: String to convert
|
|
|
|
:rtype: string
|
|
|
|
:return: Identifier suitable for C defines
|
|
|
|
"""
|
2014-02-22 10:47:19 +01:00
|
|
|
fu = re.sub('[^a-zA-Z0-9]', '_', s)
|
|
|
|
fu = re.sub('_+', '_', fu)
|
2011-09-10 11:13:51 +02:00
|
|
|
fu = fu.upper()
|
|
|
|
return fu
|
|
|
|
|
2017-03-04 08:20:27 +01:00
|
|
|
re_sh = re.compile('\\s|\'|"')
|
|
|
|
"""
|
|
|
|
Regexp used for shell_escape below
|
|
|
|
"""
|
|
|
|
|
|
|
|
def shell_escape(cmd):
|
|
|
|
"""
|
|
|
|
Escapes a command:
|
|
|
|
['ls', '-l', 'arg space'] -> ls -l 'arg space'
|
|
|
|
"""
|
|
|
|
if isinstance(cmd, str):
|
|
|
|
return cmd
|
|
|
|
return ' '.join(repr(x) if re_sh.search(x) else x for x in cmd)
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def h_list(lst):
|
|
|
|
"""
|
2017-03-04 08:20:27 +01:00
|
|
|
Hashes lists of ordered data.
|
|
|
|
|
|
|
|
Using hash(tup) for tuples would be much more efficient,
|
|
|
|
but Python now enforces hash randomization
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
:param lst: list to hash
|
|
|
|
:type lst: list of strings
|
|
|
|
:return: hash of the list
|
|
|
|
"""
|
2016-02-29 18:40:44 +01:00
|
|
|
return md5(repr(lst).encode()).digest()
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def h_fun(fun):
|
|
|
|
"""
|
|
|
|
Hash functions
|
|
|
|
|
|
|
|
:param fun: function to hash
|
|
|
|
:type fun: function
|
|
|
|
:return: hash of the function
|
2016-06-25 16:23:06 +02:00
|
|
|
:rtype: string or bytes
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return fun.code
|
|
|
|
except AttributeError:
|
2016-11-26 11:51:20 +01:00
|
|
|
if isinstance(fun, functools.partial):
|
|
|
|
code = list(fun.args)
|
2016-11-26 14:22:30 +01:00
|
|
|
# The method items() provides a sequence of tuples where the first element
|
|
|
|
# represents an optional argument of the partial function application
|
|
|
|
#
|
|
|
|
# The sorting result outcome will be consistent because:
|
|
|
|
# 1. tuples are compared in order of their elements
|
|
|
|
# 2. optional argument namess are unique
|
|
|
|
code.extend(sorted(fun.keywords.items()))
|
2016-11-26 11:51:20 +01:00
|
|
|
code.append(h_fun(fun.func))
|
|
|
|
fun.code = h_list(code)
|
|
|
|
return fun.code
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
|
|
|
h = inspect.getsource(fun)
|
2016-06-04 09:33:13 +02:00
|
|
|
except EnvironmentError:
|
2016-06-25 16:23:06 +02:00
|
|
|
h = 'nocode'
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
|
|
|
fun.code = h
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return h
|
|
|
|
|
2015-10-03 09:12:08 +02:00
|
|
|
def h_cmd(ins):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Hashes objects recursively
|
|
|
|
|
|
|
|
:param ins: input object
|
|
|
|
:type ins: string or list or tuple or function
|
|
|
|
:rtype: string or bytes
|
2015-10-03 09:12:08 +02:00
|
|
|
"""
|
|
|
|
# this function is not meant to be particularly fast
|
|
|
|
if isinstance(ins, str):
|
|
|
|
# a command is either a string
|
|
|
|
ret = ins
|
|
|
|
elif isinstance(ins, list) or isinstance(ins, tuple):
|
|
|
|
# or a list of functions/strings
|
|
|
|
ret = str([h_cmd(x) for x in ins])
|
|
|
|
else:
|
|
|
|
# or just a python function
|
|
|
|
ret = str(h_fun(ins))
|
|
|
|
if sys.hexversion > 0x3000000:
|
2017-01-21 13:28:06 +01:00
|
|
|
ret = ret.encode('latin-1', 'xmlcharrefreplace')
|
2015-10-03 09:12:08 +02:00
|
|
|
return ret
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
reg_subst = re.compile(r"(\\\\)|(\$\$)|\$\{([^}]+)\}")
|
|
|
|
def subst_vars(expr, params):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Replaces ${VAR} with the value of VAR taken from a dict or a config set::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
from waflib import Utils
|
|
|
|
s = Utils.subst_vars('${PREFIX}/bin', env)
|
|
|
|
|
|
|
|
:type expr: string
|
|
|
|
:param expr: String to perform substitution on
|
|
|
|
:param params: Dictionary or config set to look up variable values.
|
|
|
|
"""
|
|
|
|
def repl_var(m):
|
|
|
|
if m.group(1):
|
|
|
|
return '\\'
|
|
|
|
if m.group(2):
|
|
|
|
return '$'
|
|
|
|
try:
|
|
|
|
# ConfigSet instances may contain lists
|
|
|
|
return params.get_flat(m.group(3))
|
|
|
|
except AttributeError:
|
|
|
|
return params[m.group(3)]
|
2015-10-15 20:07:05 +02:00
|
|
|
# if you get a TypeError, it means that 'expr' is not a string...
|
|
|
|
# Utils.subst_vars(None, env) will not work
|
2011-09-10 11:13:51 +02:00
|
|
|
return reg_subst.sub(repl_var, expr)
|
|
|
|
|
|
|
|
def destos_to_binfmt(key):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Returns the binary format based on the unversioned platform name,
|
|
|
|
and defaults to ``elf`` if nothing is found.
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
:param key: platform name
|
|
|
|
:type key: string
|
|
|
|
:return: string representing the binary format
|
|
|
|
"""
|
|
|
|
if key == 'darwin':
|
|
|
|
return 'mac-o'
|
|
|
|
elif key in ('win32', 'cygwin', 'uwin', 'msys'):
|
|
|
|
return 'pe'
|
|
|
|
return 'elf'
|
|
|
|
|
|
|
|
def unversioned_sys_platform():
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Returns the unversioned platform name.
|
2011-09-10 11:13:51 +02:00
|
|
|
Some Python platform names contain versions, that depend on
|
|
|
|
the build environment, e.g. linux2, freebsd6, etc.
|
|
|
|
This returns the name without the version number. Exceptions are
|
|
|
|
os2 and win32, which are returned verbatim.
|
|
|
|
|
|
|
|
:rtype: string
|
|
|
|
:return: Unversioned platform name
|
|
|
|
"""
|
|
|
|
s = sys.platform
|
2015-03-06 22:02:10 +01:00
|
|
|
if s.startswith('java'):
|
2011-09-10 11:13:51 +02:00
|
|
|
# The real OS is hidden under the JVM.
|
|
|
|
from java.lang import System
|
|
|
|
s = System.getProperty('os.name')
|
|
|
|
# see http://lopica.sourceforge.net/os.html for a list of possible values
|
|
|
|
if s == 'Mac OS X':
|
|
|
|
return 'darwin'
|
|
|
|
elif s.startswith('Windows '):
|
|
|
|
return 'win32'
|
|
|
|
elif s == 'OS/2':
|
|
|
|
return 'os2'
|
|
|
|
elif s == 'HP-UX':
|
2015-03-14 03:02:01 +01:00
|
|
|
return 'hp-ux'
|
2011-09-10 11:13:51 +02:00
|
|
|
elif s in ('SunOS', 'Solaris'):
|
|
|
|
return 'sunos'
|
|
|
|
else: s = s.lower()
|
2014-01-05 10:51:24 +01:00
|
|
|
|
2011-12-03 21:44:38 +01:00
|
|
|
# powerpc == darwin for our purposes
|
|
|
|
if s == 'powerpc':
|
|
|
|
return 'darwin'
|
2014-08-09 18:08:39 +02:00
|
|
|
if s == 'win32' or s == 'os2':
|
|
|
|
return s
|
2015-10-07 00:13:09 +02:00
|
|
|
if s == 'cli' and os.name == 'nt':
|
|
|
|
# ironpython is only on windows as far as we know
|
|
|
|
return 'win32'
|
2011-09-10 11:13:51 +02:00
|
|
|
return re.split('\d+$', s)[0]
|
|
|
|
|
|
|
|
def nada(*k, **kw):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Does nothing
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Timer(object):
|
|
|
|
"""
|
|
|
|
Simple object for timing the execution of commands.
|
2017-01-03 15:48:15 +01:00
|
|
|
Its string representation is the duration::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
from waflib.Utils import Timer
|
|
|
|
timer = Timer()
|
|
|
|
a_few_operations()
|
|
|
|
s = str(timer)
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
2017-01-03 15:48:15 +01:00
|
|
|
self.start_time = self.now()
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-03 15:48:15 +01:00
|
|
|
delta = self.now() - self.start_time
|
|
|
|
if not isinstance(delta, datetime.timedelta):
|
|
|
|
delta = datetime.timedelta(seconds=delta)
|
2013-08-30 07:09:59 +02:00
|
|
|
days = delta.days
|
|
|
|
hours, rem = divmod(delta.seconds, 3600)
|
|
|
|
minutes, seconds = divmod(rem, 60)
|
|
|
|
seconds += delta.microseconds * 1e-6
|
2011-09-10 11:13:51 +02:00
|
|
|
result = ''
|
|
|
|
if days:
|
|
|
|
result += '%dd' % days
|
|
|
|
if days or hours:
|
|
|
|
result += '%dh' % hours
|
|
|
|
if days or hours or minutes:
|
|
|
|
result += '%dm' % minutes
|
|
|
|
return '%s%.3fs' % (result, seconds)
|
|
|
|
|
2017-01-03 15:48:15 +01:00
|
|
|
def now(self):
|
|
|
|
return datetime.datetime.utcnow()
|
|
|
|
|
|
|
|
if hasattr(time, 'perf_counter'):
|
|
|
|
def now(self):
|
|
|
|
return time.perf_counter()
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def read_la_file(path):
|
|
|
|
"""
|
2016-06-25 16:23:06 +02:00
|
|
|
Reads property files, used by msvc.py
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
:param path: file to read
|
|
|
|
:type path: string
|
|
|
|
"""
|
|
|
|
sp = re.compile(r'^([^=]+)=\'(.*)\'$')
|
|
|
|
dc = {}
|
|
|
|
for line in readf(path).splitlines():
|
|
|
|
try:
|
|
|
|
_, left, right, _ = sp.split(line.strip())
|
|
|
|
dc[left] = right
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return dc
|
|
|
|
|
|
|
|
def run_once(fun):
|
|
|
|
"""
|
|
|
|
Decorator: let a function cache its results, use like this::
|
|
|
|
|
|
|
|
@run_once
|
|
|
|
def foo(k):
|
|
|
|
return 345*2343
|
|
|
|
|
2016-06-25 16:23:06 +02:00
|
|
|
.. note:: in practice this can cause memory leaks, prefer a :py:class:`waflib.Utils.lru_cache`
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
:param fun: function to execute
|
|
|
|
:type fun: function
|
|
|
|
:return: the return value of the function executed
|
|
|
|
"""
|
|
|
|
cache = {}
|
2015-12-22 18:27:04 +01:00
|
|
|
def wrap(*k):
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
|
|
|
return cache[k]
|
|
|
|
except KeyError:
|
2015-12-22 18:27:04 +01:00
|
|
|
ret = fun(*k)
|
2011-09-10 11:13:51 +02:00
|
|
|
cache[k] = ret
|
|
|
|
return ret
|
|
|
|
wrap.__cache__ = cache
|
2015-11-05 02:01:32 +01:00
|
|
|
wrap.__name__ = fun.__name__
|
2011-09-10 11:13:51 +02:00
|
|
|
return wrap
|
|
|
|
|
|
|
|
def get_registry_app_path(key, filename):
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Returns the value of a registry key for an executable
|
|
|
|
|
|
|
|
:type key: string
|
|
|
|
:type filename: list of string
|
|
|
|
"""
|
2011-09-10 11:13:51 +02:00
|
|
|
if not winreg:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
result = winreg.QueryValue(key, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s.exe" % filename[0])
|
2017-06-23 16:51:54 +02:00
|
|
|
except OSError:
|
2011-09-10 11:13:51 +02:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if os.path.isfile(result):
|
|
|
|
return result
|
|
|
|
|
2015-02-19 13:46:18 +01:00
|
|
|
def lib64():
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Guess the default ``/usr/lib`` extension for 64-bit applications
|
|
|
|
|
|
|
|
:return: '64' or ''
|
|
|
|
:rtype: string
|
|
|
|
"""
|
2015-02-19 13:46:18 +01:00
|
|
|
# default settings for /usr/lib
|
|
|
|
if os.sep == '/':
|
2015-04-03 21:24:46 +02:00
|
|
|
if platform.architecture()[0] == '64bit':
|
|
|
|
if os.path.exists('/usr/lib64') and not os.path.exists('/usr/lib32'):
|
|
|
|
return '64'
|
2015-02-19 13:46:18 +01:00
|
|
|
return ''
|
|
|
|
|
2015-10-10 17:05:13 +02:00
|
|
|
def sane_path(p):
|
|
|
|
# private function for the time being!
|
|
|
|
return os.path.abspath(os.path.expanduser(p))
|
|
|
|
|
2016-02-26 21:09:50 +01:00
|
|
|
process_pool = []
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
List of processes started to execute sub-process commands
|
|
|
|
"""
|
|
|
|
|
2016-02-25 20:03:07 +01:00
|
|
|
def get_process():
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Returns a process object that can execute commands as sub-processes
|
|
|
|
|
|
|
|
:rtype: subprocess.Popen
|
|
|
|
"""
|
2016-02-25 21:53:17 +01:00
|
|
|
try:
|
2016-02-28 00:12:50 +01:00
|
|
|
return process_pool.pop()
|
2016-02-26 21:09:50 +01:00
|
|
|
except IndexError:
|
|
|
|
filepath = os.path.dirname(os.path.abspath(__file__)) + os.sep + 'processor.py'
|
2016-04-27 18:35:02 +02:00
|
|
|
cmd = [sys.executable, '-c', readf(filepath)]
|
2016-02-28 00:12:50 +01:00
|
|
|
return subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, bufsize=0)
|
2016-02-26 20:11:58 +01:00
|
|
|
|
|
|
|
def run_prefork_process(cmd, kwargs, cargs):
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Delegates process execution to a pre-forked process instance.
|
|
|
|
"""
|
2016-07-31 15:55:47 +02:00
|
|
|
if not 'env' in kwargs:
|
2016-07-31 18:48:36 +02:00
|
|
|
kwargs['env'] = dict(os.environ)
|
2016-06-27 20:53:25 +02:00
|
|
|
try:
|
|
|
|
obj = base64.b64encode(cPickle.dumps([cmd, kwargs, cargs]))
|
2017-12-22 14:58:20 +01:00
|
|
|
except (TypeError, AttributeError):
|
2016-06-27 20:53:25 +02:00
|
|
|
return run_regular_process(cmd, kwargs, cargs)
|
2016-02-26 20:11:58 +01:00
|
|
|
|
2016-02-26 21:09:50 +01:00
|
|
|
proc = get_process()
|
2016-02-28 00:12:50 +01:00
|
|
|
if not proc:
|
|
|
|
return run_regular_process(cmd, kwargs, cargs)
|
|
|
|
|
2016-02-26 20:11:58 +01:00
|
|
|
proc.stdin.write(obj)
|
|
|
|
proc.stdin.write('\n'.encode())
|
|
|
|
proc.stdin.flush()
|
|
|
|
obj = proc.stdout.readline()
|
|
|
|
if not obj:
|
2016-02-26 21:23:49 +01:00
|
|
|
raise OSError('Preforked sub-process %r died' % proc.pid)
|
2016-02-26 20:11:58 +01:00
|
|
|
|
|
|
|
process_pool.append(proc)
|
2017-07-01 13:15:17 +02:00
|
|
|
lst = cPickle.loads(base64.b64decode(obj))
|
|
|
|
# Jython wrapper failures (bash/execvp)
|
|
|
|
assert len(lst) == 5
|
|
|
|
ret, out, err, ex, trace = lst
|
2016-02-26 20:11:58 +01:00
|
|
|
if ex:
|
|
|
|
if ex == 'OSError':
|
|
|
|
raise OSError(trace)
|
|
|
|
elif ex == 'ValueError':
|
|
|
|
raise ValueError(trace)
|
2016-09-03 22:36:41 +02:00
|
|
|
elif ex == 'TimeoutExpired':
|
|
|
|
exc = TimeoutExpired(cmd, timeout=cargs['timeout'], output=out)
|
|
|
|
exc.stderr = err
|
|
|
|
raise exc
|
2016-02-26 20:11:58 +01:00
|
|
|
else:
|
|
|
|
raise Exception(trace)
|
|
|
|
return ret, out, err
|
|
|
|
|
2016-10-08 22:35:05 +02:00
|
|
|
def lchown(path, user=-1, group=-1):
|
|
|
|
"""
|
|
|
|
Change the owner/group of a path, raises an OSError if the
|
|
|
|
ownership change fails.
|
|
|
|
|
|
|
|
:param user: user to change
|
|
|
|
:type user: int or str
|
|
|
|
:param group: group to change
|
|
|
|
:type group: int or str
|
|
|
|
"""
|
|
|
|
if isinstance(user, str):
|
|
|
|
import pwd
|
|
|
|
entry = pwd.getpwnam(user)
|
|
|
|
if not entry:
|
|
|
|
raise OSError('Unknown user %r' % user)
|
|
|
|
user = entry[2]
|
|
|
|
if isinstance(group, str):
|
|
|
|
import grp
|
|
|
|
entry = grp.getgrnam(group)
|
|
|
|
if not entry:
|
|
|
|
raise OSError('Unknown group %r' % group)
|
|
|
|
group = entry[2]
|
|
|
|
return os.lchown(path, user, group)
|
|
|
|
|
2016-02-28 00:12:50 +01:00
|
|
|
def run_regular_process(cmd, kwargs, cargs={}):
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Executes a subprocess command by using subprocess.Popen
|
|
|
|
"""
|
2016-02-28 00:12:50 +01:00
|
|
|
proc = subprocess.Popen(cmd, **kwargs)
|
2016-04-19 22:00:21 +02:00
|
|
|
if kwargs.get('stdout') or kwargs.get('stderr'):
|
2016-08-25 07:48:14 +02:00
|
|
|
try:
|
|
|
|
out, err = proc.communicate(**cargs)
|
|
|
|
except TimeoutExpired:
|
2016-09-03 20:22:43 +02:00
|
|
|
if kwargs.get('start_new_session') and hasattr(os, 'killpg'):
|
2016-09-03 18:13:38 +02:00
|
|
|
os.killpg(proc.pid, signal.SIGKILL)
|
2016-09-03 20:22:43 +02:00
|
|
|
else:
|
2016-09-03 18:13:38 +02:00
|
|
|
proc.kill()
|
2016-08-28 11:03:04 +02:00
|
|
|
out, err = proc.communicate()
|
2016-09-03 22:24:29 +02:00
|
|
|
exc = TimeoutExpired(proc.args, timeout=cargs['timeout'], output=out)
|
|
|
|
exc.stderr = err
|
|
|
|
raise exc
|
2016-02-28 00:12:50 +01:00
|
|
|
status = proc.returncode
|
|
|
|
else:
|
|
|
|
out, err = (None, None)
|
2016-08-25 07:48:14 +02:00
|
|
|
try:
|
|
|
|
status = proc.wait(**cargs)
|
2016-08-28 11:03:04 +02:00
|
|
|
except TimeoutExpired as e:
|
2016-09-03 20:22:43 +02:00
|
|
|
if kwargs.get('start_new_session') and hasattr(os, 'killpg'):
|
2016-09-03 18:13:38 +02:00
|
|
|
os.killpg(proc.pid, signal.SIGKILL)
|
2016-09-03 20:22:43 +02:00
|
|
|
else:
|
2016-09-03 18:13:38 +02:00
|
|
|
proc.kill()
|
2016-08-28 11:03:04 +02:00
|
|
|
proc.wait()
|
|
|
|
raise e
|
2016-02-28 00:12:50 +01:00
|
|
|
return status, out, err
|
|
|
|
|
2016-02-26 14:25:54 +01:00
|
|
|
def run_process(cmd, kwargs, cargs={}):
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Executes a subprocess by using a pre-forked process when possible
|
|
|
|
or falling back to subprocess.Popen. See :py:func:`waflib.Utils.run_prefork_process`
|
|
|
|
and :py:func:`waflib.Utils.run_regular_process`
|
|
|
|
"""
|
2016-04-19 22:00:21 +02:00
|
|
|
if kwargs.get('stdout') and kwargs.get('stderr'):
|
2016-02-28 00:12:50 +01:00
|
|
|
return run_prefork_process(cmd, kwargs, cargs)
|
|
|
|
else:
|
2016-02-26 20:11:58 +01:00
|
|
|
return run_regular_process(cmd, kwargs, cargs)
|
2016-02-28 00:12:50 +01:00
|
|
|
|
|
|
|
def alloc_process_pool(n, force=False):
|
2016-06-25 16:23:06 +02:00
|
|
|
"""
|
|
|
|
Allocates an amount of processes to the default pool so its size is at least *n*.
|
|
|
|
It is useful to call this function early so that the pre-forked
|
|
|
|
processes use as little memory as possible.
|
|
|
|
|
|
|
|
:param n: pool size
|
|
|
|
:type n: integer
|
|
|
|
:param force: if True then *n* more processes are added to the existing pool
|
|
|
|
:type force: bool
|
|
|
|
"""
|
2016-02-28 00:12:50 +01:00
|
|
|
# mandatory on python2, unnecessary on python >= 3.2
|
|
|
|
global run_process, get_process, alloc_process_pool
|
|
|
|
if not force:
|
|
|
|
n = max(n - len(process_pool), 0)
|
|
|
|
try:
|
|
|
|
lst = [get_process() for x in range(n)]
|
|
|
|
except OSError:
|
|
|
|
run_process = run_regular_process
|
|
|
|
get_process = alloc_process_pool = nada
|
2016-02-25 20:03:07 +01:00
|
|
|
else:
|
2016-02-28 00:12:50 +01:00
|
|
|
for x in lst:
|
|
|
|
process_pool.append(x)
|
|
|
|
|
2016-12-08 22:43:58 +01:00
|
|
|
def atexit_pool():
|
|
|
|
for k in process_pool:
|
|
|
|
try:
|
|
|
|
os.kill(k.pid, 9)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
k.wait()
|
2017-03-02 20:10:28 +01:00
|
|
|
# see #1889
|
|
|
|
if (sys.hexversion<0x207000f and not is_win32) or sys.hexversion>=0x306000f:
|
2016-12-08 22:43:58 +01:00
|
|
|
atexit.register(atexit_pool)
|
|
|
|
|
2017-12-22 15:40:06 +01:00
|
|
|
if os.environ.get('WAF_NO_PREFORK') or sys.platform == 'cli' or not sys.executable:
|
2016-02-28 00:12:50 +01:00
|
|
|
run_process = run_regular_process
|
|
|
|
get_process = alloc_process_pool = nada
|
2016-02-25 20:03:07 +01:00
|
|
|
|