27 from __future__
import unicode_literals
43 if sys.version_info < (3,):
55 COPYRIGHT =
"""2011-12 University of Pennsylvania, 2013-14 Carnegie Mellon University, 2013-16 Andreas Schuh""" 57 LICENSE =
"""See https://cmake-basis.github.io/download.html#license or COPYING file.""" 59 CONTACT =
"""andreas.schuh.84@gmail.com""" 63 _MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
70 if isinstance(s, binary_type):
return s.decode()
71 elif isinstance(s, text_type):
return s.decode()
83 sys.stdout.write(
'Contact:\n ' + contact +
'\n')
102 def print_version(name, version=None, project=None, copyright=COPYRIGHT, license=LICENSE):
103 if not version:
raise Exception(
'print_version(): Missing version argument')
105 sys.stdout.write(name)
107 sys.stdout.write(
' (')
108 sys.stdout.write(project)
109 sys.stdout.write(
')')
110 sys.stdout.write(
' ')
111 sys.stdout.write(version)
112 sys.stdout.write(
'\n')
115 sys.stdout.write(
"Copyright (c) ");
116 sys.stdout.write(copyright)
117 sys.stdout.write(
". All rights reserved.\n")
120 sys.stdout.write(license)
121 sys.stdout.write(
'\n')
136 if not name:
return None 138 if name.startswith(
'.'):
return name
140 if prefix
is None or not targets:
return name
144 if separator.join([prefix, name])
in targets:
145 return separator.join([prefix, name])
146 parts = prefix.split(separator, 1)
147 if len(parts) == 1:
break 161 uid =
targetuid(name, prefix=prefix, targets=targets)
162 if not uid
or not targets:
return False 163 if uid.startswith(
'.'): uid = uid[1:]
164 return uid
in targets
184 def exepath(name=None, prefix=None, targets=None, base='.'):
187 path = os.path.realpath(sys.argv[0])
188 elif istarget(name, prefix=prefix, targets=targets):
189 uid =
targetuid(name, prefix=prefix, targets=targets)
190 if uid.startswith(
'.'): uid = uid[1:]
191 path = os.path.normpath(os.path.join(os.path.join(_MODULE_DIR, base), targets[uid]))
192 if '$<CONFIG>' in path:
193 for config
in [
'Release',
'Debug',
'RelWithDebInfo',
'MinSizeRel']:
194 tmppath = path.replace(
'$<CONFIG>', config)
195 if os.path.isfile(tmppath):
198 path = path.replace(
'$<CONFIG>',
'')
201 path = which.which(name)
216 def exename(name=None, prefix=None, targets=None, base='.'):
217 path =
exepath(name, prefix, targets, base)
218 if path
is None:
return None 219 name = os.path.basename(path)
220 if os.name ==
'nt' and (name.endswith(
'.exe')
or name.endswith(
'.com')):
234 def exedir(name=None, prefix=None, targets=None, base='.'):
235 path =
exepath(name, prefix, targets, base)
236 if path
is None:
return None 237 return os.path.dirname(path)
264 if type(args)
is list:
266 re_quote_or_not = re.compile(
r"'|\s|^$")
269 arg = arg.replace(
'"',
'\\"')
271 if re_quote_or_not.search(arg): qargs.append(
''.
join([
'"', arg,
'"']))
272 else: qargs.append(arg)
273 return ' '.
join(qargs)
274 elif type(args)
is binary_type:
276 elif type(args)
is text_type:
290 return shlex.split(args)
323 def execute(args, quiet=False, stdout=False, allow_fail=False, verbose=0, simulate=False,
324 prefix=None, targets=None, base='.'):
326 if type(args)
is list: args = [
tostring(i)
for i
in args]
328 if len(args) == 0:
raise Exception(
"execute(): No command specified for execution")
330 path =
exepath(args[0], prefix=prefix, targets=targets, base=base)
334 if verbose > 0
or simulate:
335 sys.stdout.write(
'$ ')
337 if simulate: sys.stdout.write(
' (simulated)')
338 sys.stdout.write(
'\n')
345 process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
347 if hasattr(sys.stdout,
'buffer'):
348 for line
in process.stdout:
350 output = b
''.
join([output, line])
352 sys.stdout.buffer.write(line)
355 for line
in process.stdout:
357 output = b
''.
join([output, line])
359 if type(line)
is text_type:
360 line = line.encode(sys.stdout.encoding)
361 elif type(line)
is not binary_type:
363 sys.stdout.write(line)
366 _, err = process.communicate()
368 if hasattr(sys.stderr,
'buffer'):
369 sys.stderr.buffer.write(err)
372 if type(line)
is text_type:
373 line = line.encode(sys.stderr.encoding)
374 elif type(line)
is not binary_type:
376 sys.stderr.write(line)
378 status = process.returncode
381 except Exception
as e:
382 msg =
"Exception while executing \"" + args[0] +
"\"!\n" 383 msg +=
"\tArguments: " +
tostring(args[1:]) +
'\n' 387 if status != 0
and not allow_fail:
390 if stdout:
return (status, output)
def __init__(self, msg)
Initialize exception, i.e., set message describing failure.
def exename(name=None, prefix=None, targets=None, base='.')
Get name of executable file.
std::string join(const std::string &base, const std::string &path)
Join two paths, e.g., base path and relative path.
def exedir(name=None, prefix=None, targets=None, base='.')
Get directory of executable file.
def qsplit(args)
Split quoted string of arguments.
def exepath(name=None, prefix=None, targets=None, base='.')
Get absolute path of executable file.
def print_contact(contact=CONTACT)
Print contact information.
def execute(args, quiet=False, stdout=False, allow_fail=False, verbose=0, simulate=False, prefix=None, targets=None, base='.')
Execute command as subprocess.
def targetuid(name, prefix=None, targets=None)
Get UID of build target.
Exception thrown when command execution failed.
def tostring(args)
Convert array of arguments to quoted string.
def istarget(name, prefix=None, targets=None)
Determine whether a given build target is known.
def print_version(name, version=None, project=None, copyright=COPYRIGHT, license=LICENSE)
Print version information including copyright and license notices.
def __str__(self)
Return string representation of exception message.