from __future__ import print_function import os import subprocess import sys def short_path(name): abs_path = os.path.abspath(name) rel_path = os.path.relpath(name) if len(abs_path) < len(rel_path): return abs_path else: return rel_path def check_pyconf_file(pyconf_file): if not os.path.isfile(pyconf_file): help_str = ''' You should create a file named %s in this directory with the following definitions: - ... (to complete) Optional definitions: - dsk_dir: path to a local storage (typically in /dsk/l1/misc) to store generated data and images *** Stopping execution ''' % (short_path(pyconf_file)) print(help_str) sys.exit() def my_mkdir(abs_dirname): dirname = short_path(abs_dirname) try: print("mkdir %s" % (dirname)) os.mkdir(dirname) except OSError: print("*** Error: impossible to create directory %s" % (dirname), file = sys.stderr) sys.exit(1) def my_chdir(abs_dirname): dirname = short_path(abs_dirname) print("cd %s" % (dirname)); os.chdir(dirname) def my_cp(abs_src, abs_dst): src = short_path(abs_src) dst = short_path(abs_dst) print("cp %s %s" % (src, dst)) shutil.copy(src, dst) def get_random_img_file(density, x, y, granularity, seed): density_str = str(density) if density < 10: density_str = "0" + density_str if density < 100: density_str = "0" + density_str return "rand_" + str(x) + "_" + str(y) + "_" + density_str + "_G" + str(granularity) + ".pgm" def get_filename(dir, nthreads, config, ftrs, basename): if ftrs: ftrs_str = "_FEATURES_1" else: ftrs_str = "_FEATURES_0" config_keys = list(config.keys()) # duplicates list try: config_keys.remove('FEATURES') except: pass config_keys = sorted(config_keys) filename = os.path.join(dir, basename + "_" + str(nthreads) + "_" + "_".join(map(lambda x:'%s_%s' % (x, config[x]), config_keys)) + ftrs_str + ".txt") return filename def get_graph_filename(dir, nthreads, config, ext): config_keys = list(config.keys()) # duplicates list config_keys = sorted(config_keys) filename = os.path.join(dir, "random_" + str(nthreads) + "_" + "_".join(map(lambda x:'%s_%s' % (x, config[x]), config_keys)) + ext) return filename def print_and_call(cmd): print(subprocess.list2cmdline(cmd)) retval = subprocess.call(cmd) return retval def print_and_popen(cmd, abs_outfile): outfile = short_path(abs_outfile) print(subprocess.list2cmdline(cmd), end = "") print(" >", outfile) child = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) output = child.communicate()[0] retval = child.returncode # Write simulation results to log file file = open(outfile, 'w') file.write(output) file.close() if retval != 0: print("*** Error while executing command '%s'" % subprocess.list2cmdline(cmd)) sys.exit(1) return output