Skip to content

Commit

Permalink
refactor & add some comments
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Mitri <[email protected]>
  • Loading branch information
pet-mit committed Mar 25, 2024
1 parent 99defa8 commit 27bee49
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
27 changes: 10 additions & 17 deletions patch.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
from pathlib import Path
from typing import List
from dataclasses import dataclass

def replace_in_file(filepath, search, replace):
with open(filepath, 'r', encoding="utf8") as file:
data = file.read()
data = data.replace(search, replace)

with open(filepath, 'w', encoding="utf8") as file:
file.write(data)


@dataclass
class Addition:
filepath: Path
search: str
add: str
from patch_utils import *

full_patch: List[Addition] = []

# add the USE_SIRIUS configuration flag in CMakeLists.txt
full_patch.append(Addition(
Path.cwd()/'CMakeLists.txt',
'''option(USE_CPLEX "Use the CPLEX solver" OFF)
Expand All @@ -30,6 +16,8 @@ class Addition:
'''))

# add the USE_SIRIUS configuration flag in cpp.cmake

full_patch.append(
Addition(
Path.cwd()/'cmake'/'cpp.cmake',
Expand All @@ -48,6 +36,7 @@ class Addition:
endif()
'''))

# add the USE_SIRIUS configuration flag in deps.cmake
full_patch.append(Addition(
Path.cwd()/'cmake'/'deps.cmake',
'''
Expand All @@ -65,6 +54,7 @@ class Addition:
endif(USE_SIRIUS)
'''))

# add the USE_SIRIUS configuration flag in ortoolsConfig.cmake.in
full_patch.append(Addition(
Path.cwd()/'cmake'/'ortoolsConfig.cmake.in',
'''
Expand All @@ -85,7 +75,7 @@ class Addition:
endif()
'''))


# add SIRIUS execution in example files
full_patch.append(Addition(
Path.cwd()/'examples'/'cpp'/'linear_programming.cc',
' RunLinearProgrammingExample("XPRESS_LP");\n',
Expand All @@ -111,6 +101,7 @@ class Addition:
' RunLinearExampleCppStyleAPI("XPRESS_LP")\n',
' RunLinearExampleCppStyleAPI("SIRIUS_LP")\n'))

# add the USE_SIRIUS configuration flag in ortools/linear_solver/CMakeLists.txt
full_patch.append(Addition(
Path.cwd()/'ortools'/'linear_solver'/'CMakeLists.txt',
' $<$<BOOL:${USE_SCIP}>:libscip>\n',
Expand All @@ -129,6 +120,7 @@ class Addition:
endif()
'''))

# add the SIRIUS support in ortools/linear_solver/linear_solver.cc & .h
full_patch.append(Addition(
Path.cwd()/'ortools'/'linear_solver'/'linear_solver.cc',
'''extern MPSolverInterface* BuildXpressInterface(bool mip,
Expand Down Expand Up @@ -187,5 +179,6 @@ class Addition:
' friend class XpressInterface;\n',
' friend class SiriusInterface;\n'))

# run patch
for a in full_patch:
replace_in_file(a.filepath, a.search, a.search+a.add)
17 changes: 17 additions & 0 deletions patch_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pathlib import Path
from dataclasses import dataclass

def replace_in_file(filepath, search, replace):
with open(filepath, 'r', encoding="utf8") as file:
data = file.read()
data = data.replace(search, replace)

with open(filepath, 'w', encoding="utf8") as file:
file.write(data)


@dataclass
class Addition:
filepath: Path
search: str
add: str

0 comments on commit 27bee49

Please sign in to comment.