Skip to content

Commit 174a72a

Browse files
Save switch version in the output directory for improved archiving, especially during active software development.
1 parent ee0b58b commit 174a72a

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

switch_model/upgrade/manager.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import argparse
88
import os
99
import shutil
10-
from distutils.version import StrictVersion
10+
from pkg_resources import parse_version
1111

1212
import switch_model
1313

@@ -33,7 +33,7 @@
3333
# Not every code revision requires an update; this is the last revision that did.
3434
last_required_update = upgrade_plugins[-1][-1]
3535

36-
code_version = StrictVersion(switch_model.__version__)
36+
code_version = parse_version(switch_model.__version__)
3737
version_file = 'switch_inputs_version.txt'
3838
#verbose = False
3939
verbose = True
@@ -91,7 +91,7 @@ def do_inputs_need_upgrade(inputs_dir):
9191
# Not every code revision requires an update, so just hard-code the last
9292
# revision that required an update.
9393
inputs_version = get_input_version(inputs_dir)
94-
return StrictVersion(inputs_version) < StrictVersion(last_required_update)
94+
return parse_version(inputs_version) < parse_version(last_required_update)
9595

9696

9797
def _backup(inputs_dir):
@@ -120,15 +120,15 @@ def upgrade_inputs(inputs_dir, backup=True, assign_current_version=False):
120120
_backup(inputs_dir)
121121
# Successively apply the upgrade scripts as needed.
122122
for (upgrader, v_from, v_to) in upgrade_plugins:
123-
inputs_v = StrictVersion(get_input_version(inputs_dir))
123+
inputs_v = parse_version(get_input_version(inputs_dir))
124124
# note: the next line catches datasets created by/for versions of Switch that
125125
# didn't require input directory upgrades
126-
if StrictVersion(v_from) <= inputs_v < StrictVersion(v_to):
126+
if parse_version(v_from) <= inputs_v < parse_version(v_to):
127127
print_verbose('\tUpgrading from ' + v_from + ' to ' + v_to)
128128
upgrader.upgrade_input_dir(inputs_dir)
129129
upgraded = True
130130

131-
if (StrictVersion(last_required_update) < StrictVersion(switch_model.__version__)
131+
if (parse_version(last_required_update) < parse_version(switch_model.__version__)
132132
and assign_current_version):
133133
# user requested writing of current version number, even if no upgrade is needed
134134
# (useful for updating examples to track with new release of Switch)

switch_model/utilities.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from pyomo.environ import *
1313
import pyomo.opt
1414

15+
import switch_model
16+
1517
# Define string_types (same as six.string_types). This is useful for
1618
# distinguishing between strings and other iterables.
1719
try:
@@ -256,6 +258,12 @@ def post_solve(instance, outputs_dir=None):
256258
if hasattr(module, 'post_solve'):
257259
module.post_solve(instance, outputs_dir)
258260

261+
# Save the precise version used to solve this problem.
262+
version_path = os.path.join(outputs_dir, 'software_version.txt')
263+
with open(version_path, 'w') as f:
264+
f.write("This problem was solved with switch version {}.{}".format(
265+
switch_model.__version__, os.linesep))
266+
259267

260268
def min_data_check(model, *mandatory_model_components):
261269
"""

0 commit comments

Comments
 (0)