v0.10.0
convert.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import sys
5 import argparse
6 import subprocess
7 import os
8 from os import path
9 import multiprocessing as mp
10 from multiprocessing import Value
11 try:
12  # Python 3
13  from itertools import filterfalse
14 except ImportError:
15  # Python 2
16  from itertools import ifilterfalse
17 
18 def print_progress(iteration, total, decimals=1, bar_length=50):
19  str_format = "{0:." + str(decimals) + "f}"
20  percents = str_format.format(100 * (iteration / float(total)))
21  filled_length = int(round(bar_length * iteration / float(total)))
22  bar = '█' * filled_length + '-' * (bar_length - filled_length)
23 
24  sys.stdout.write('\r |%s| %s%s (%s of %s)' %
25  (bar, percents, '%', str(iteration), str(total)))
26  sys.stdout.flush()
27 
28 def is_not_h5m(file):
29  return not file.endswith('h5m')
30 
32  file_vtk = path.splitext(file)[0] + ".vtk"
33  if path.exists(file_vtk):
34  return path.getmtime(file) < path.getmtime(file_vtk)
35  return False
36 
37 def mb_convert(file):
38  file = path.splitext(file)[0]
39  p = subprocess.Popen(["mbconvert", file + ".h5m", file + ".vtk"],
40  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
41  (out, err) = p.communicate()
42  lock.acquire()
43  if p.returncode:
44  print('\n' + err.decode())
45  n.value += 1
46  print_progress(n.value, N.value)
47  lock.release()
48 
49 def init(l):
50  global lock
51  lock = l
52 
53 def filterfalse_comp(fun, file_list):
54  try:
55  # Python 3
56  return filterfalse(fun, file_list)
57  except NameError:
58  # Python 2
59  return ifilterfalse(fun, file_list)
60 
61 if __name__ == '__main__':
62  parser = argparse.ArgumentParser(
63  description="Convert multiple h5m files to vtk using mbconvert and multiprocessing")
64  parser.add_argument(
65  "file", help="list of h5m files or a regexp mask", nargs='+')
66  parser.add_argument("-np", help="number of processes", type=int, default=1)
67  args = parser.parse_args()
68 
69  file_list = list(filterfalse_comp(is_not_h5m, args.file))
70  if not len(file_list):
71  print("No h5m files found with the given name/mask")
72  exit()
73 
74  file_list = list(filterfalse_comp(is_older_than_vtk, file_list))
75  if not len(file_list):
76  print("All found h5m files are older than corresponding vtk files")
77  exit()
78 
79  N = Value('i', len(file_list))
80  n = Value('i', 0)
81 
82  l = mp.Lock()
83  pool = mp.Pool(processes=args.np, initializer=init, initargs=(l,))
84 
85  print_progress(n.value, N.value)
86  pool.map(mb_convert, file_list)
87  pool.close()
88  pool.join()
89 
90  print('\n')
91 
92  exit()
def filterfalse_comp(fun, file_list)
Definition: convert.py:53
def init(l)
Definition: convert.py:49
def mb_convert(file)
Definition: convert.py:37
def is_older_than_vtk(file)
Definition: convert.py:31
def is_not_h5m(file)
Definition: convert.py:28
def print_progress(iteration, total, decimals=1, bar_length=50)
Definition: convert.py:18