1 | |
---|
2 | from __future__ import print_function |
---|
3 | import os |
---|
4 | import subprocess |
---|
5 | import sys |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | def short_path(name): |
---|
11 | abs_path = os.path.abspath(name) |
---|
12 | rel_path = os.path.relpath(name) |
---|
13 | if len(abs_path) < len(rel_path): |
---|
14 | return abs_path |
---|
15 | else: |
---|
16 | return rel_path |
---|
17 | |
---|
18 | |
---|
19 | def check_pyconf_file(pyconf_file): |
---|
20 | if not os.path.isfile(pyconf_file): |
---|
21 | help_str = ''' |
---|
22 | You should create a file named %s in this directory with the following definitions: |
---|
23 | - ... (to complete) |
---|
24 | Optional definitions: |
---|
25 | - dsk_dir: path to a local storage (typically in /dsk/l1/misc) to store generated data and images |
---|
26 | *** Stopping execution |
---|
27 | ''' % (short_path(pyconf_file)) |
---|
28 | print(help_str) |
---|
29 | sys.exit() |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | def my_mkdir(abs_dirname): |
---|
34 | dirname = short_path(abs_dirname) |
---|
35 | try: |
---|
36 | print("mkdir %s" % (dirname)) |
---|
37 | os.mkdir(dirname) |
---|
38 | except OSError: |
---|
39 | print("*** Error: impossible to create directory %s" % (dirname), file = sys.stderr) |
---|
40 | sys.exit(1) |
---|
41 | |
---|
42 | def my_chdir(abs_dirname): |
---|
43 | dirname = short_path(abs_dirname) |
---|
44 | print("cd %s" % (dirname)); |
---|
45 | os.chdir(dirname) |
---|
46 | |
---|
47 | def my_cp(abs_src, abs_dst): |
---|
48 | src = short_path(abs_src) |
---|
49 | dst = short_path(abs_dst) |
---|
50 | print("cp %s %s" % (src, dst)) |
---|
51 | shutil.copy(src, dst) |
---|
52 | |
---|
53 | |
---|
54 | def get_random_img_file(density, x, y, granularity, seed): |
---|
55 | density_str = str(density) |
---|
56 | if density < 10: |
---|
57 | density_str = "0" + density_str |
---|
58 | if density < 100: |
---|
59 | density_str = "0" + density_str |
---|
60 | |
---|
61 | return "rand_" + str(x) + "_" + str(y) + "_" + density_str + "_G" + str(granularity) + ".pgm" |
---|
62 | |
---|
63 | |
---|
64 | def get_filename(dir, nthreads, config, ftrs, basename): |
---|
65 | if ftrs: |
---|
66 | ftrs_str = "_FEATURES_1" |
---|
67 | else: |
---|
68 | ftrs_str = "_FEATURES_0" |
---|
69 | |
---|
70 | config_keys = list(config.keys()) # duplicates list |
---|
71 | try: |
---|
72 | config_keys.remove('FEATURES') |
---|
73 | except: |
---|
74 | pass |
---|
75 | config_keys = sorted(config_keys) |
---|
76 | filename = os.path.join(dir, basename + "_" + str(nthreads) + "_" + "_".join(map(lambda x:'%s_%s' % (x, config[x]), config_keys)) + ftrs_str + ".txt") |
---|
77 | |
---|
78 | return filename |
---|
79 | |
---|
80 | |
---|
81 | def get_graph_filename(dir, nthreads, config, ext): |
---|
82 | config_keys = list(config.keys()) # duplicates list |
---|
83 | config_keys = sorted(config_keys) |
---|
84 | filename = os.path.join(dir, "random_" + str(nthreads) + "_" + "_".join(map(lambda x:'%s_%s' % (x, config[x]), config_keys)) + ext) |
---|
85 | |
---|
86 | return filename |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | def print_and_call(cmd): |
---|
91 | print(subprocess.list2cmdline(cmd)) |
---|
92 | retval = subprocess.call(cmd) |
---|
93 | return retval |
---|
94 | |
---|
95 | def print_and_popen(cmd, abs_outfile): |
---|
96 | outfile = short_path(abs_outfile) |
---|
97 | print(subprocess.list2cmdline(cmd), end = "") |
---|
98 | print(" >", outfile) |
---|
99 | child = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) |
---|
100 | output = child.communicate()[0] |
---|
101 | retval = child.returncode |
---|
102 | |
---|
103 | # Write simulation results to log file |
---|
104 | file = open(outfile, 'w') |
---|
105 | file.write(output) |
---|
106 | file.close() |
---|
107 | |
---|
108 | if retval != 0: |
---|
109 | print("*** Error while executing command '%s'" % subprocess.list2cmdline(cmd)) |
---|
110 | sys.exit(1) |
---|
111 | |
---|
112 | return output |
---|
113 | |
---|
114 | |
---|