|
2 | 2 | import importlib
|
3 | 3 | import logging
|
4 | 4 | import os
|
| 5 | +import tempfile |
5 | 6 | import time
|
6 | 7 | import re
|
7 | 8 |
|
|
15 | 16 | logger = logging.getLogger(__name__)
|
16 | 17 |
|
17 | 18 |
|
| 19 | +class RIDCounter: |
| 20 | + def __init__(self, cache_filename="last_rid.pyon", results_dir="results"): |
| 21 | + self.cache_filename = cache_filename |
| 22 | + self.results_dir = results_dir |
| 23 | + self._next_rid = self._last_rid() + 1 |
| 24 | + logger.debug("Next RID is %d", self._next_rid) |
| 25 | + |
| 26 | + def get(self): |
| 27 | + rid = self._next_rid |
| 28 | + self._next_rid += 1 |
| 29 | + self._update_cache(rid) |
| 30 | + return rid |
| 31 | + |
| 32 | + def _last_rid(self): |
| 33 | + try: |
| 34 | + rid = self._last_rid_from_cache() |
| 35 | + except FileNotFoundError: |
| 36 | + logger.debug("Last RID cache not found, scanning results") |
| 37 | + rid = self._last_rid_from_results() |
| 38 | + self._update_cache(rid) |
| 39 | + return rid |
| 40 | + else: |
| 41 | + logger.debug("Using last RID from cache") |
| 42 | + return rid |
| 43 | + |
| 44 | + def _update_cache(self, rid): |
| 45 | + contents = str(rid) + "\n" |
| 46 | + directory = os.path.abspath(os.path.dirname(self.cache_filename)) |
| 47 | + with tempfile.NamedTemporaryFile("w", dir=directory, delete=False) as f: |
| 48 | + f.write(contents) |
| 49 | + tmpname = f.name |
| 50 | + os.replace(tmpname, self.cache_filename) |
| 51 | + |
| 52 | + def _last_rid_from_cache(self): |
| 53 | + with open(self.cache_filename, "r") as f: |
| 54 | + return int(f.read()) |
| 55 | + |
| 56 | + def _last_rid_from_results(self): |
| 57 | + r = -1 |
| 58 | + try: |
| 59 | + day_folders = os.listdir(self.results_dir) |
| 60 | + except: |
| 61 | + return r |
| 62 | + day_folders = filter(lambda x: re.fullmatch('\d\d\d\d-\d\d-\d\d', x), |
| 63 | + day_folders) |
| 64 | + for df in day_folders: |
| 65 | + day_path = os.path.join(self.results_dir, df) |
| 66 | + try: |
| 67 | + minute_folders = os.listdir(day_path) |
| 68 | + except: |
| 69 | + continue |
| 70 | + minute_folders = filter(lambda x: re.fullmatch('\d\d-\d\d', x), |
| 71 | + minute_folders) |
| 72 | + for mf in minute_folders: |
| 73 | + minute_path = os.path.join(day_path, mf) |
| 74 | + try: |
| 75 | + h5files = os.listdir(minute_path) |
| 76 | + except: |
| 77 | + continue |
| 78 | + for x in h5files: |
| 79 | + m = re.fullmatch('(\d\d\d\d\d\d\d\d\d)-.*\.h5', x) |
| 80 | + if m is None: |
| 81 | + continue |
| 82 | + rid = int(m.group(1)) |
| 83 | + if rid > r: |
| 84 | + r = rid |
| 85 | + return r |
| 86 | + |
| 87 | + |
18 | 88 | def _create_device(desc, device_mgr):
|
19 | 89 | ty = desc["type"]
|
20 | 90 | if ty == "local":
|
@@ -87,38 +157,6 @@ def get_hdf5_output(start_time, rid, name):
|
87 | 157 | return h5py.File(os.path.join(dirname, filename), "w")
|
88 | 158 |
|
89 | 159 |
|
90 |
| -def get_last_rid(): |
91 |
| - r = -1 |
92 |
| - try: |
93 |
| - day_folders = os.listdir("results") |
94 |
| - except: |
95 |
| - return r |
96 |
| - day_folders = filter(lambda x: re.fullmatch('\d\d\d\d-\d\d-\d\d', x), |
97 |
| - day_folders) |
98 |
| - for df in day_folders: |
99 |
| - day_path = os.path.join("results", df) |
100 |
| - try: |
101 |
| - minute_folders = os.listdir(day_path) |
102 |
| - except: |
103 |
| - continue |
104 |
| - minute_folders = filter(lambda x: re.fullmatch('\d\d-\d\d', x), |
105 |
| - minute_folders) |
106 |
| - for mf in minute_folders: |
107 |
| - minute_path = os.path.join(day_path, mf) |
108 |
| - try: |
109 |
| - h5files = os.listdir(minute_path) |
110 |
| - except: |
111 |
| - continue |
112 |
| - for x in h5files: |
113 |
| - m = re.fullmatch('(\d\d\d\d\d\d\d\d\d)-.*\.h5', x) |
114 |
| - if m is None: |
115 |
| - continue |
116 |
| - rid = int(m.group(1)) |
117 |
| - if rid > r: |
118 |
| - r = rid |
119 |
| - return r |
120 |
| - |
121 |
| - |
122 | 160 | _type_to_hdf5 = {
|
123 | 161 | int: h5py.h5t.STD_I64BE,
|
124 | 162 | float: h5py.h5t.IEEE_F64BE,
|
|
0 commit comments