Skip to content

Commit

Permalink
Better handling temporary files for running at scale (#1026)
Browse files Browse the repository at this point in the history
* moved temporary files to default (os dependent) temp dir, added removal of temp files at termination when using .net.xml file path

* flake8 fix
  • Loading branch information
roireshef authored Dec 11, 2020
1 parent 183b0c5 commit a511c41
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions flow/core/kernel/network/traci.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Script containing the TraCI network kernel class."""
import tempfile

from flow.core.kernel.network import BaseKernelNetwork
from flow.core.util import makexml, printxml, ensure_dir
Expand Down Expand Up @@ -54,10 +55,8 @@ def __init__(self, master_kernel, sim_params):

# directories for the network-specific files that will be generated by
# the `generate_network` method
self.net_path = os.path.dirname(os.path.abspath(__file__)) \
+ '/debug/net/'
self.cfg_path = os.path.dirname(os.path.abspath(__file__)) \
+ '/debug/cfg/'
self.net_path = os.path.join(tempfile.gettempdir(), 'flow/debug/net/')
self.cfg_path = os.path.join(tempfile.gettempdir(), 'flow/debug/cfg/')

ensure_dir('%s' % self.net_path)
ensure_dir('%s' % self.cfg_path)
Expand Down Expand Up @@ -221,31 +220,28 @@ def close(self):
is to prevent them from building up in the debug folder. Note that in
the case of import .net.xml files we do not want to delete them.
"""
# Those files are being created even if self.network.net_params.template is a path to .net.xml file
files = [self.cfg_path + self.guifn,
self.cfg_path + self.addfn,
self.cfg_path + self.roufn,
self.cfg_path + self.sumfn]

if self.network.net_params.template is None:
files += [self.net_path + self.nodfn,
self.net_path + self.edgfn,
self.net_path + self.cfgfn,
self.net_path + self.confn,
self.net_path + self.typfn,
self.cfg_path + self.netfn]

for file in files:
try:
os.remove(self.net_path + self.nodfn)
os.remove(self.net_path + self.edgfn)
os.remove(self.net_path + self.cfgfn)
os.remove(self.cfg_path + self.addfn)
os.remove(self.cfg_path + self.guifn)
os.remove(self.cfg_path + self.netfn)
os.remove(self.cfg_path + self.roufn)
os.remove(self.cfg_path + self.sumfn)
except FileNotFoundError:
os.remove(file)
except (FileNotFoundError, OSError):
# the files were never created
pass

# the connection file is not always created
try:
os.remove(self.net_path + self.confn)
except OSError:
pass

# neither is the type file
try:
os.remove(self.net_path + self.typfn)
except OSError:
pass
# the connection file is not always created
# neither is the type file
continue

def get_edge(self, x):
"""See parent class."""
Expand Down

0 comments on commit a511c41

Please sign in to comment.