Skip to content

Commit

Permalink
additional ruff issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Chsudeepta authored and Chsudeepta committed Oct 24, 2024
1 parent 9ebea8b commit a514def
Show file tree
Hide file tree
Showing 24 changed files with 94 additions and 265 deletions.
20 changes: 5 additions & 15 deletions src/common_upgrades/add_to_base_iocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ def perform(self, file_access, logger):

modified_file_contents = self._add_ioc(ioc_file_contents, logger)
logger.info(
"Adding {what} ioc to autostart in {0}.".format(
IOC_FILENAME, what=self._ioc_to_add
)
"Adding {what} ioc to autostart in {0}.".format(IOC_FILENAME, what=self._ioc_to_add)
)

if not self._check_final_file_contains_one_of_added_ioc(
logger, modified_file_contents
):
if not self._check_final_file_contains_one_of_added_ioc(logger, modified_file_contents):
return -3

file_access.write_xml_file(IOC_FILENAME, modified_file_contents)
Expand Down Expand Up @@ -84,9 +80,7 @@ def _check_final_file_contains_one_of_added_ioc(self, logger, xml):
node_count = ioc_names.count(self._ioc_to_add)
if node_count != 1:
# I can not see how to generate this error but it is here because it is important
logger.error(
INCORRECT_ADDING.format(FILE_TO_CHECK_STR, node_count, self._ioc_to_add)
)
logger.error(INCORRECT_ADDING.format(FILE_TO_CHECK_STR, node_count, self._ioc_to_add))
return False
return True

Expand All @@ -109,9 +103,7 @@ def _check_prerequistes_for_file(self, xml, logger):
node_count = ioc_names.count(self._add_after_ioc)
if node_count != 1:
logger.error(
ADD_AFTER_MISSING.format(
FILE_TO_CHECK_STR, node_count, self._add_after_ioc
)
ADD_AFTER_MISSING.format(FILE_TO_CHECK_STR, node_count, self._add_after_ioc)
)
return False
return True
Expand All @@ -130,9 +122,7 @@ def _add_ioc(self, ioc_xml, logger):
new_ioc_node = minidom.parseString(self._xml_to_add).firstChild
ioc_xml.firstChild.insertBefore(new_ioc_node, ioc.nextSibling)
# add some formatting to make it look nice
ioc_xml.firstChild.insertBefore(
ioc_xml.createTextNode("\n "), new_ioc_node
)
ioc_xml.firstChild.insertBefore(ioc_xml.createTextNode("\n "), new_ioc_node)
return ioc_xml

logger.error(
Expand Down
4 changes: 1 addition & 3 deletions src/common_upgrades/change_macro_in_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ def _globals_filter_generator(self, ioc_to_change):
"""
for index, line in enumerate(self._loaded_file):
if line.startswith("{}_".format(ioc_to_change)):
self._logger.info(
"Found line '{}' in {}".format(line, GLOBALS_FILENAME)
)
self._logger.info("Found line '{}' in {}".format(line, GLOBALS_FILENAME))
yield index

def _determine_replacement_values(self, old_macro, new_macro):
Expand Down
4 changes: 1 addition & 3 deletions src/common_upgrades/change_macros_in_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ def change_ioc_name_in_synoptics(self, old_ioc_name, new_ioc_name):
"""
path = SYNOPTIC_FOLDER

for xml_path in [
c for c in self._file_access.listdir(path) if c.endswith(".xml")
]:
for xml_path in [c for c in self._file_access.listdir(path) if c.endswith(".xml")]:
try:
synoptic_xml = self._file_access.open_xml_file(xml_path)
except IOError:
Expand Down
8 changes: 2 additions & 6 deletions src/common_upgrades/change_pvs_in_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,8 @@ def get_number_of_instances_of_pv(self, pv_names):
num_of_instances = 0
for pv_name in pv_names:
for path, xml in self._file_access.get_config_files(BLOCK_FILE):
num_of_instances += len(
list(self.node_text_filter(pv_name, "read_pv", path, xml))
)
num_of_instances += len(list(self.node_text_filter(pv_name, "read_pv", path, xml)))
for path, xml in self._file_access.get_synoptic_files():
num_of_instances += len(
list(self.node_text_filter(pv_name, "address", path, xml))
)
num_of_instances += len(list(self.node_text_filter(pv_name, "address", path, xml)))

return num_of_instances
8 changes: 2 additions & 6 deletions src/common_upgrades/sql_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def get_session(logger):
root_pass = os.getenv("MYSQL_PASSWORD") or getpass(
"Please enter db root password: "
)
SqlConnection._connection = mysql.connector.connect(
user="root", password=root_pass
)
SqlConnection._connection = mysql.connector.connect(user="root", password=root_pass)
except Exception as e:
logger.error("Failed to connect to database: {}".format(e))
return SqlConnection._connection
Expand Down Expand Up @@ -118,9 +116,7 @@ def add_new_user(logger, user, password):
try:
run_sql(
logger,
"CREATE USER {} IDENTIFIED WITH mysql_native_password BY '{}';".format(
user, password
),
"CREATE USER {} IDENTIFIED WITH mysql_native_password BY '{}';".format(user, password),
)
run_sql(
logger,
Expand Down
4 changes: 1 addition & 3 deletions src/common_upgrades/utils/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def __init__(self, name, value=None):
self.__value = value

def __repr__(self):
return "<Macro object with name {} and value {}>".format(
self.__name, self.__value
)
return "<Macro object with name {} and value {}>".format(self.__name, self.__value)

@property
def name(self):
Expand Down
13 changes: 3 additions & 10 deletions src/file_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ def create_directories(self, path):
Returns:
"""
os.makedirs(
os.path.dirname(os.path.join(self.config_base, path)), exist_ok=True
)
os.makedirs(os.path.dirname(os.path.join(self.config_base, path)), exist_ok=True)

def line_exists(self, filename, string):
"""Check if string exists as a line in file"""
Expand Down Expand Up @@ -149,10 +147,7 @@ def listdir(self, dir):
Return:
List of file paths (strings)
"""
return [
os.path.join(dir, f)
for f in os.listdir(os.path.join(self.config_base, dir))
]
return [os.path.join(dir, f) for f in os.listdir(os.path.join(self.config_base, dir))]

def remove_file(self, filename):
"""Removes a file from the file system.
Expand Down Expand Up @@ -214,9 +209,7 @@ def get_synoptic_files(self):
Tuple: The path to the synoptic file and its xml representation.
"""
for synoptic_path in [
filename
for filename in self.listdir(SYNOPTIC_FOLDER)
if filename.endswith(".xml")
filename for filename in self.listdir(SYNOPTIC_FOLDER) if filename.endswith(".xml")
]:
yield synoptic_path, self._get_xml(synoptic_path)

Expand Down
4 changes: 1 addition & 3 deletions src/local_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def __init__(self, log_dir):

log_file = os.path.join(
log_dir,
"upgrade_{0}.txt".format(
datetime.datetime.now().strftime("%Y_%m_%d__%H_%M")
),
"upgrade_{0}.txt".format(datetime.datetime.now().strftime("%Y_%m_%d__%H_%M")),
)

self._log_file = log_file
Expand Down
24 changes: 6 additions & 18 deletions src/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def get_version_number(self):
return line.strip()
except IOError:
initial_version_number = self._upgrade_steps[0][0]
self._file_access.write_version_number(
initial_version_number, VERSION_FILENAME
)
self._file_access.write_version_number(initial_version_number, VERSION_FILENAME)
return initial_version_number

def upgrade(self):
Expand All @@ -63,9 +61,7 @@ def upgrade(self):
if version == current_version:
upgrade = True
if upgrade_step is None:
self._logger.info(
"Current config is on latest version, no upgrade needed"
)
self._logger.info("Current config is on latest version, no upgrade needed")

if upgrade:
final_upgrade_version = version
Expand All @@ -75,18 +71,12 @@ def upgrade(self):
result = upgrade_step.perform(self._file_access, self._logger)
if result != 0:
return result
self._file_access.write_version_number(
version, VERSION_FILENAME
)
self._file_access.write_version_number(version, VERSION_FILENAME)
self._commit_tag_and_push(version)

if upgrade:
self._file_access.write_version_number(
final_upgrade_version, VERSION_FILENAME
)
self._logger.info(
"Finished upgrade. Now on version {0}".format(final_upgrade_version)
)
self._file_access.write_version_number(final_upgrade_version, VERSION_FILENAME)
self._logger.info("Finished upgrade. Now on version {0}".format(final_upgrade_version))
self._commit_tag_and_push(final_upgrade_version, final=True)
return 0
else:
Expand All @@ -97,8 +87,6 @@ def _commit_tag_and_push(self, version, final=False):
self._git_repo.git.add(A=True)
commit_message = f"IBEX Upgrade {'from' if not final else 'to'} {version}"
self._git_repo.index.commit(commit_message)
tag_name = (
f"{self._git_repo.active_branch}_{version}{'_upgrade' if not final else ''}"
)
tag_name = f"{self._git_repo.active_branch}_{version}{'_upgrade' if not final else ''}"
self._git_repo.create_tag(tag_name, message=commit_message, force=True)
self._git_repo.remote(name="origin").push()
5 changes: 1 addition & 4 deletions src/upgrade_step_check_init_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ def search_files(self, files, root, file_access):
if file_name.startswith("init_"):
search_file = open(os.path.join(root, file_name))
search_file_contents = search_file.read()
if (
"precmd" in search_file_contents
or "postcmd" in search_file_contents
):
if "precmd" in search_file_contents or "postcmd" in search_file_contents:
return (
"Pre or post cmd methods found in {} these will now no longer be hooked into the command. Please ensure they are hooked using the new style of inserting these methods, "
"see https://github.com/ISISComputingGroup/ibex_user_manual/wiki/Pre-and-Post-Command-Hooks".format(
Expand Down
4 changes: 1 addition & 3 deletions src/upgrade_step_from_11p0p0.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ def perform(self, file_access, logger):
change_macros_in_xml = ChangeMacrosInXML(file_access, logger)
change_macros_in_xml.change_macros(ioc_name, self.rename_macros)
for macro in self.new_macros:
change_macros_in_xml.add_macro(
ioc_name, macro[0], macro[1], macro[2], macro[3]
)
change_macros_in_xml.add_macro(ioc_name, macro[0], macro[1], macro[2], macro[3])
change_pvs_in_xml = ChangePVsInXML(file_access, logger)
change_pvs_in_xml.change_pv_name("FULL_AUTO", "SPC")
return 0
Expand Down
4 changes: 1 addition & 3 deletions src/upgrade_step_from_12p0p2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ class UpgradeFrom12p0p2(UpgradeStep):
def perform(self, file_access, logger):
# add JMS2RDB Tables
try:
file = os.path.join(
EPICS_ROOT, "CSS", "master", "AlarmJMS2RDB", "MySQL-Log-DDL.sql"
)
file = os.path.join(EPICS_ROOT, "CSS", "master", "AlarmJMS2RDB", "MySQL-Log-DDL.sql")
logger.info("Updating JMS2RDB schema")
with SqlConnection():
return run_sql_file(logger, file)
Expand Down
12 changes: 3 additions & 9 deletions src/upgrade_step_from_15p0p0.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,16 @@ class UpgradeFrom15p0p0(UpgradeStep):

def perform(self, file_access: FileAccess, logger: logging.Logger) -> int:
try:
controller_dirs: list[str] = [
os.path.join(CONFIG_ROOT, c) for c in CONTROLLERS
]
existent_controller_dirs: list[str] = [
c for c in controller_dirs if os.path.exists(c)
]
controller_dirs: list[str] = [os.path.join(CONFIG_ROOT, c) for c in CONTROLLERS]
existent_controller_dirs: list[str] = [c for c in controller_dirs if os.path.exists(c)]

for directory in existent_controller_dirs:
logger.info(f"Checking for recent commits in {directory}")

repo: git.Repo = RepoFactory.get_repo(directory)
commit_timestamps: str = repo.git.log("--format=%ct", directory)

integer_commit_timestamps: list[int] = [
int(ts) for ts in commit_timestamps.split()
]
integer_commit_timestamps: list[int] = [int(ts) for ts in commit_timestamps.split()]

if any(ts > CUTOFF_TIMESTAMP for ts in integer_commit_timestamps):
motorext_path = os.path.join(
Expand Down
15 changes: 3 additions & 12 deletions src/upgrade_step_from_7p2p0.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ def perform(self, file_access, logger):

if not file_access.exists(self.file_name):
# In case there isn't a .gitignore file, write new one
print(
".gitignore not found at "
+ self.file_name
+ ", making a new .gitignore"
)
print(".gitignore not found at " + self.file_name + ", making a new .gitignore")
file_access.write_file(self.file_name, self.text_content, "w")
else:
# If existing .gitignore file is found, append to it
Expand Down Expand Up @@ -102,10 +98,7 @@ def perform(self, file_access, logger):
print("")

# CoordX:MTR is gone, hard to automatically replace so just raise as issue
if (
changer.get_number_of_instances_of_pv(["COORD0:MTR", "COORD1:MTR"])
> 0
):
if changer.get_number_of_instances_of_pv(["COORD0:MTR", "COORD1:MTR"]) > 0:
print(
"The PV COORDX:MTR has been found in a config/synoptic but no longer exists"
)
Expand Down Expand Up @@ -135,6 +128,4 @@ def perform(self, file_access, logger):
"""
changer = SynopticsAndDeviceScreens(file_access, logger)
return changer.update_opi_keys(
{self.REFL_OPI_TARGET_OLD: self.REFL_OPI_TARGET_NEW}
)
return changer.update_opi_keys({self.REFL_OPI_TARGET_OLD: self.REFL_OPI_TARGET_NEW})
1 change: 1 addition & 0 deletions test/mother.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Mother for test objects"""

from xml.dom import minidom

from src.file_access import FileAccess
from src.local_logger import LocalLogger

Expand Down
14 changes: 4 additions & 10 deletions test/test_add_to_base_iocs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from xml.parsers.expat import ExpatError

from hamcrest import assert_that, is_, has_item
from hamcrest import assert_that, has_item, is_
from mock import MagicMock as Mock

from src.common_upgrades.add_to_base_iocs import (
Expand Down Expand Up @@ -33,9 +33,7 @@ def test_GIVEN_no_ioc_xml_file_WHEN_adding_ioc_THEN_error(self):
result = adder.perform(self.file_access, self.logger)

assert_that(result, is_(-1), "result")
assert_that(
self.logger.log_err, has_item("Can not find file to modify in config.")
)
assert_that(self.logger.log_err, has_item("Can not find file to modify in config."))

def test_GIVEN_invalid_xml_WHEN_adding_ioc_THEN_error(self):
error_str = "No configs Exist"
Expand Down Expand Up @@ -149,9 +147,7 @@ def test_GIVEN_xml_containing_no_ioc_to_add_after_WHEN_add_ioc_THEN_error(self):
assert_that(
self.logger.log_err,
has_item(
"Could not find {0} ioc in file so no {1} ioc added.".format(
after_ioc, ioc_to_add
)
"Could not find {0} ioc in file so no {1} ioc added.".format(after_ioc, ioc_to_add)
),
)

Expand All @@ -168,9 +164,7 @@ def test_GIVEN_xml_containing_ioc_to_add_after_WHEN_add_ioc_THEN_ioc_added(self)

assert_that(len(iocs), is_(3), "ioc added")

assert_that(
iocs[1].getAttribute("name"), is_(ioc_to_add), "correctly named ioc added"
)
assert_that(iocs[1].getAttribute("name"), is_(ioc_to_add), "correctly named ioc added")
[
assert_that(iocs[1].hasAttribute(attr), "correct xml added")
for attr in ["restart", "autostart", "simlevel"]
Expand Down
Loading

0 comments on commit a514def

Please sign in to comment.