2019-05-30 23:32:55 +05:30
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import shlex
|
2015-10-09 11:21:58 +05:30
|
|
|
from optparse import Option
|
2019-05-20 23:24:57 +05:30
|
|
|
from queue import Queue
|
|
|
|
from threading import Thread
|
|
|
|
|
2016-01-05 23:47:55 +05:30
|
|
|
class bcolors:
|
2016-03-04 19:52:01 +05:30
|
|
|
"""
|
2019-09-19 07:13:43 +05:30
|
|
|
ASCII Colors (Blender code).
|
2016-03-04 19:52:01 +05:30
|
|
|
|
|
|
|
https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py
|
|
|
|
http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
|
|
|
|
"""
|
|
|
|
|
2019-05-28 01:30:26 +05:30
|
|
|
HEADER = '\033[95m'
|
|
|
|
OKBLUE = '\033[94m'
|
|
|
|
OKGREEN = '\033[92m'
|
|
|
|
WARNING = '\033[93m'
|
|
|
|
FAIL = '\033[91m'
|
|
|
|
ENDC = '\033[0m'
|
|
|
|
BOLD = '\033[1m'
|
|
|
|
DIM = '\033[2m'
|
2016-01-05 23:47:55 +05:30
|
|
|
UNDERLINE = '\033[4m'
|
2019-05-28 01:30:26 +05:30
|
|
|
CROSSOUT = '\033[9m'
|
2016-01-05 23:47:55 +05:30
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
self.HEADER = ''
|
|
|
|
self.OKBLUE = ''
|
|
|
|
self.OKGREEN = ''
|
|
|
|
self.WARNING = ''
|
|
|
|
self.FAIL = ''
|
|
|
|
self.ENDC = ''
|
|
|
|
self.BOLD = ''
|
|
|
|
self.UNDERLINE = ''
|
2019-05-28 01:30:26 +05:30
|
|
|
self.CROSSOUT = ''
|
2016-01-05 23:47:55 +05:30
|
|
|
|
|
|
|
|
2015-11-20 21:45:34 +05:30
|
|
|
# -----------------------------
|
2016-03-04 19:52:01 +05:30
|
|
|
def srepr(arg,glue = '\n'):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Joins arguments as individual lines."""
|
2016-03-04 19:52:01 +05:30
|
|
|
if (not hasattr(arg, "strip") and
|
2018-07-19 19:16:14 +05:30
|
|
|
(hasattr(arg, "__getitem__") or
|
|
|
|
hasattr(arg, "__iter__"))):
|
2017-08-08 20:55:38 +05:30
|
|
|
return glue.join(str(x) for x in arg)
|
2016-09-11 22:33:32 +05:30
|
|
|
return arg if isinstance(arg,str) else repr(arg)
|
2015-11-20 21:45:34 +05:30
|
|
|
|
2015-09-23 02:30:18 +05:30
|
|
|
# -----------------------------
|
2016-03-04 19:52:01 +05:30
|
|
|
def croak(what, newline = True):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Writes formated to stderr."""
|
2019-05-26 21:18:59 +05:30
|
|
|
if what is not None:
|
|
|
|
sys.stderr.write(srepr(what,glue = '\n') + ('\n' if newline else ''))
|
2015-10-06 23:31:31 +05:30
|
|
|
sys.stderr.flush()
|
2015-09-23 02:30:18 +05:30
|
|
|
|
|
|
|
# -----------------------------
|
2016-07-18 19:50:39 +05:30
|
|
|
def report(who = None,
|
|
|
|
what = None):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Reports script and file name."""
|
2019-05-26 23:58:19 +05:30
|
|
|
croak( (emph(who)+': ' if who is not None else '') + (what if what is not None else '') + '\n' )
|
2015-09-23 02:30:18 +05:30
|
|
|
|
2016-04-24 21:50:55 +05:30
|
|
|
|
2015-08-22 22:32:49 +05:30
|
|
|
# -----------------------------
|
|
|
|
def emph(what):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Formats string with emphasis."""
|
2016-01-05 23:47:55 +05:30
|
|
|
return bcolors.BOLD+srepr(what)+bcolors.ENDC
|
2015-08-22 22:32:49 +05:30
|
|
|
|
2016-08-25 21:29:04 +05:30
|
|
|
# -----------------------------
|
|
|
|
def deemph(what):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Formats string with deemphasis."""
|
2016-08-25 21:29:04 +05:30
|
|
|
return bcolors.DIM+srepr(what)+bcolors.ENDC
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
def delete(what):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Formats string as deleted."""
|
2016-08-25 21:29:04 +05:30
|
|
|
return bcolors.DIM+srepr(what)+bcolors.ENDC
|
|
|
|
|
2019-05-28 06:44:09 +05:30
|
|
|
# -----------------------------
|
|
|
|
def strikeout(what):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Formats string as strikeout."""
|
2019-05-28 06:44:09 +05:30
|
|
|
return bcolors.CROSSOUT+srepr(what)+bcolors.ENDC
|
|
|
|
|
2016-03-21 18:21:56 +05:30
|
|
|
# -----------------------------
|
|
|
|
def execute(cmd,
|
|
|
|
streamIn = None,
|
2019-06-07 21:21:27 +05:30
|
|
|
wd = './',
|
|
|
|
env = None):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Executes a command in given directory and returns stdout and stderr for optional stdin."""
|
2016-03-21 18:21:56 +05:30
|
|
|
initialPath = os.getcwd()
|
|
|
|
os.chdir(wd)
|
2019-06-07 21:21:27 +05:30
|
|
|
myEnv = os.environ if env is None else env
|
2016-03-21 18:21:56 +05:30
|
|
|
process = subprocess.Popen(shlex.split(cmd),
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.PIPE,
|
2019-06-07 21:21:27 +05:30
|
|
|
stdin = subprocess.PIPE,
|
|
|
|
env = myEnv)
|
2018-07-20 06:39:53 +05:30
|
|
|
out,error = [i for i in (process.communicate() if streamIn is None
|
|
|
|
else process.communicate(streamIn.read().encode('utf-8')))]
|
|
|
|
out = out.decode('utf-8').replace('\x08','')
|
|
|
|
error = error.decode('utf-8').replace('\x08','')
|
2016-03-21 18:21:56 +05:30
|
|
|
os.chdir(initialPath)
|
|
|
|
if process.returncode != 0: raise RuntimeError('{} failed with returncode {}'.format(cmd,process.returncode))
|
|
|
|
return out,error
|
|
|
|
|
2014-06-17 12:40:10 +05:30
|
|
|
# -----------------------------
|
|
|
|
class extendableOption(Option):
|
2016-03-04 19:52:01 +05:30
|
|
|
"""
|
2019-09-19 07:13:43 +05:30
|
|
|
Used for definition of new option parser action 'extend', which enables to take multiple option arguments.
|
2016-03-04 19:52:01 +05:30
|
|
|
|
2019-09-19 07:13:43 +05:30
|
|
|
Adopted from online tutorial http://docs.python.org/library/optparse.html
|
2016-03-04 19:52:01 +05:30
|
|
|
"""
|
|
|
|
|
2014-06-17 12:40:10 +05:30
|
|
|
ACTIONS = Option.ACTIONS + ("extend",)
|
|
|
|
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
|
|
|
|
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
|
|
|
|
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
|
|
|
|
|
|
|
|
def take_action(self, action, dest, opt, value, values, parser):
|
|
|
|
if action == "extend":
|
|
|
|
lvalue = value.split(",")
|
|
|
|
values.ensure_value(dest, []).extend(lvalue)
|
|
|
|
else:
|
|
|
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
|
|
|
|
2018-12-09 13:38:33 +05:30
|
|
|
# Print iterations progress
|
|
|
|
# from https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
|
2019-01-05 15:11:49 +05:30
|
|
|
def progressBar(iteration, total, prefix='', bar_length=50):
|
2018-12-09 17:32:07 +05:30
|
|
|
"""
|
2019-09-19 07:13:43 +05:30
|
|
|
Call in a loop to create terminal progress bar.
|
2018-12-09 17:32:07 +05:30
|
|
|
|
|
|
|
@params:
|
|
|
|
iteration - Required : current iteration (Int)
|
|
|
|
total - Required : total iterations (Int)
|
|
|
|
prefix - Optional : prefix string (Str)
|
|
|
|
bar_length - Optional : character length of bar (Int)
|
|
|
|
"""
|
2019-01-05 15:11:49 +05:30
|
|
|
fraction = iteration / float(total)
|
|
|
|
if not hasattr(progressBar, "last_fraction"): # first call to function
|
|
|
|
progressBar.start_time = time.time()
|
|
|
|
progressBar.last_fraction = -1.0
|
|
|
|
remaining_time = ' n/a'
|
|
|
|
else:
|
|
|
|
if fraction <= progressBar.last_fraction or iteration == 0: # reset: called within a new loop
|
|
|
|
progressBar.start_time = time.time()
|
|
|
|
progressBar.last_fraction = -1.0
|
|
|
|
remaining_time = ' n/a'
|
|
|
|
else:
|
|
|
|
progressBar.last_fraction = fraction
|
|
|
|
remainder = (total - iteration) * (time.time()-progressBar.start_time)/iteration
|
|
|
|
remaining_time = '{: 3d}:'.format(int( remainder//3600)) + \
|
|
|
|
'{:02d}:'.format(int((remainder//60)%60)) + \
|
|
|
|
'{:02d}' .format(int( remainder %60))
|
|
|
|
|
|
|
|
filled_length = int(round(bar_length * fraction))
|
|
|
|
bar = '█' * filled_length + '░' * (bar_length - filled_length)
|
|
|
|
|
|
|
|
sys.stderr.write('\r{} {} {}'.format(prefix, bar, remaining_time)),
|
2018-12-09 13:38:33 +05:30
|
|
|
|
2019-02-27 19:07:47 +05:30
|
|
|
if iteration == total: sys.stderr.write('\n')
|
2018-12-09 17:32:07 +05:30
|
|
|
sys.stderr.flush()
|
2019-05-28 12:32:29 +05:30
|
|
|
|
|
|
|
|
|
|
|
class return_message():
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Object with formatted return message."""
|
2019-05-28 12:32:29 +05:30
|
|
|
|
|
|
|
def __init__(self,message):
|
2019-09-20 01:02:15 +05:30
|
|
|
"""
|
|
|
|
Sets return message.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
message : str or list of str
|
|
|
|
message for output to screen
|
2019-09-20 01:21:16 +05:30
|
|
|
|
2019-09-20 01:02:15 +05:30
|
|
|
"""
|
2019-05-28 12:32:29 +05:30
|
|
|
self.message = message
|
|
|
|
|
|
|
|
def __repr__(self):
|
2019-09-19 07:13:43 +05:30
|
|
|
"""Return message suitable for interactive shells."""
|
2019-05-28 12:32:29 +05:30
|
|
|
return srepr(self.message)
|
2018-12-09 13:38:33 +05:30
|
|
|
|
2016-03-04 19:52:01 +05:30
|
|
|
|
2019-09-20 01:02:15 +05:30
|
|
|
class ThreadPool:
|
|
|
|
"""Pool of threads consuming tasks from a queue."""
|
|
|
|
|
|
|
|
class Worker(Thread):
|
2019-09-20 00:14:15 +05:30
|
|
|
"""Thread executing tasks from a given tasks queue."""
|
2019-05-20 23:24:57 +05:30
|
|
|
|
|
|
|
def __init__(self, tasks):
|
2019-09-20 01:21:16 +05:30
|
|
|
"""Worker for tasks."""
|
2019-09-20 01:02:15 +05:30
|
|
|
Thread.__init__(self)
|
|
|
|
self.tasks = tasks
|
|
|
|
self.daemon = True
|
|
|
|
self.start()
|
2019-05-20 23:24:57 +05:30
|
|
|
|
|
|
|
def run(self):
|
2019-09-20 01:02:15 +05:30
|
|
|
while True:
|
|
|
|
func, args, kargs = self.tasks.get()
|
|
|
|
try:
|
|
|
|
func(*args, **kargs)
|
|
|
|
except Exception as e:
|
|
|
|
# An exception happened in this thread
|
|
|
|
print(e)
|
|
|
|
finally:
|
|
|
|
# Mark this task as done, whether an exception happened or not
|
|
|
|
self.tasks.task_done()
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, num_threads):
|
|
|
|
"""
|
|
|
|
Thread pool.
|
2019-05-20 23:24:57 +05:30
|
|
|
|
2019-09-20 01:02:15 +05:30
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
num_threads : int
|
|
|
|
number of threads
|
2019-05-20 23:24:57 +05:30
|
|
|
|
2019-09-20 01:02:15 +05:30
|
|
|
"""
|
|
|
|
self.tasks = Queue(num_threads)
|
|
|
|
for _ in range(num_threads):
|
|
|
|
self.Worker(self.tasks)
|
|
|
|
|
|
|
|
def add_task(self, func, *args, **kargs):
|
|
|
|
"""Add a task to the queue."""
|
|
|
|
self.tasks.put((func, args, kargs))
|
|
|
|
|
|
|
|
def map(self, func, args_list):
|
|
|
|
"""Add a list of tasks to the queue."""
|
|
|
|
for args in args_list:
|
|
|
|
self.add_task(func, args)
|
|
|
|
|
|
|
|
def wait_completion(self):
|
|
|
|
"""Wait for completion of all the tasks in the queue."""
|
|
|
|
self.tasks.join()
|