Skip to content

Commit 2291b91

Browse files
authored
Merge pull request #157 from ChannelFinder/fix-sonar-issues
Fix sonar issues for server/recCeiver (only)
2 parents 8e54d15 + 2d25767 commit 2291b91

16 files changed

Lines changed: 203 additions & 185 deletions

.github/workflows/server.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ jobs:
4141
python-version: ${{ matrix.python-version }}
4242
- name: Install
4343
run: |
44-
python -m pip install --upgrade pip
45-
pip install .
44+
minor=$(python3 --version | cut -d. -f2)
45+
if [ "$minor" -ge 7 ]; then
46+
python -m pip install --upgrade "pip==24.0" --only-binary=:all:
47+
else
48+
python -m pip install --upgrade "pip==21.3.1" --only-binary=:all:
49+
fi
50+
python -m pip install --no-deps .
4651
test-unit:
4752
runs-on: ubuntu-latest
4853
needs: build-server
@@ -57,9 +62,9 @@ jobs:
5762
python-version: 3.9
5863
- name: Install dependencies
5964
run: |
60-
python -m pip install --upgrade pip
61-
python -m pip install '.[test]'
62-
python -m pip install .
65+
python -m pip install --upgrade "pip==24.0" --only-binary=:all:
66+
python -m pip install --only-binary=:all: -r requirements-ci-py39.txt
67+
python -m pip install --no-deps .
6368
- name: Test unit tests
6469
run: |
6570
set -o pipefail

server/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ WORKDIR /home/recceiver
1414

1515
COPY --from=build /home/recceiver/venv venv
1616

17-
CMD venv/bin/twistd --pidfile= --nodaemon recceiver
17+
CMD ["venv/bin/twistd", "--pidfile=", "--nodaemon", "recceiver"]

server/pyproject.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ classifiers = [
2929
dependencies = [
3030
"channelfinder @ https://github.com/ChannelFinder/pyCFClient/archive/refs/tags/v3.2.0.zip",
3131
"dataclasses; python_version<'3.7'",
32-
"requests",
33-
"twisted",
32+
"requests>=2.27.1,<2.28; python_version<'3.7'",
33+
"requests>=2.31,<2.32; python_version<'3.8'",
34+
"requests>=2.32.3,<2.33; python_version>='3.8'",
35+
"twisted>=21.7,<22; python_version<'3.7'",
36+
"twisted>=22.10,<23; python_version<'3.8'",
37+
"twisted>=24.11,<24.12; python_version>='3.8'",
3438
]
35-
optional-dependencies.test = [ "pytest", "testcontainers>=4" ]
39+
optional-dependencies.test = [ "pytest>=8.3,<8.4", "testcontainers>=4.8.2,<4.9" ]
3640
urls.Repository = "https://github.com/ChannelFinder/recsync"
3741

3842
[tool.setuptools]

server/recceiver/application.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def __init__(self):
3232
self.write = log.msg
3333

3434
def flush(self):
35-
pass
35+
# Required by logging.StreamHandler; this handler writes directly to Twisted logs.
36+
return None
3637

3738

3839
class RecService(service.MultiService):
@@ -134,7 +135,7 @@ class Maker(object):
134135

135136
options = Options
136137

137-
def makeService(self, opts):
138+
def make_service(self, opts):
138139
ctrl = ProcessorController(cfile=opts["config"])
139140
conf = ctrl.config("recceiver")
140141
S = RecService(conf)
@@ -156,3 +157,6 @@ def makeService(self, opts):
156157
root.setLevel(lvl)
157158

158159
return S
160+
161+
def makeService(self, opts): # NOSONAR - Twisted IServiceMaker API requires this exact name.
162+
return self.make_service(opts)

server/recceiver/cfstore.py

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class IocInfo:
215215
host: str
216216
hostname: str
217217
ioc_name: str
218-
ioc_IP: str
218+
ioc_ip: str
219219
owner: str
220220
time: str
221221
port: int
@@ -271,7 +271,7 @@ def __init__(self, name: Optional[str], conf: ConfigAdapter):
271271
self.cf_config = CFConfig.loads(conf)
272272
self.name = name # Override name from service.Service
273273
self.channel_ioc_ids: Dict[str, List[str]] = defaultdict(list)
274-
self.iocs: Dict[str, IocInfo] = dict()
274+
self.iocs: Dict[str, IocInfo] = {}
275275
self.client: Optional[ChannelFinderClient] = None
276276
self.current_time: Callable[[Optional[str]], str] = get_current_time
277277
self.lock: DeferredLock = DeferredLock()
@@ -465,7 +465,7 @@ def transaction_to_record_infos(self, ioc_info: IocInfo, transaction: CommitTran
465465
for record_id, (record_infos_to_add) in transaction.record_infos_to_add.items():
466466
# find intersection of these sets
467467
if record_id not in record_infos:
468-
_log.warning("IOC: %s: PV not found for recinfo with RID: {record_id}", ioc_info, record_id)
468+
_log.warning("IOC: %s: PV not found for recinfo with RID: %s", ioc_info, record_id)
469469
continue
470470
recinfo_wl = [p for p in self.record_property_names_list if p in record_infos_to_add.keys()]
471471
if recinfo_wl:
@@ -595,7 +595,7 @@ def _commit_with_thread(self, transaction: CommitTransaction):
595595
host=host,
596596
hostname=transaction.client_infos.get("HOSTNAME") or host,
597597
ioc_name=ioc_name,
598-
ioc_IP=host,
598+
ioc_ip=host,
599599
owner=owner,
600600
time=self.current_time(self.cf_config.timezone),
601601
port=port,
@@ -612,25 +612,25 @@ def _commit_with_thread(self, transaction: CommitTransaction):
612612
if not poll_success:
613613
raise defer.CancelledError(f"Failed to commit transaction after polling retries: {transaction}")
614614

615-
def remove_channel(self, recordName: str, iocid: str) -> None:
615+
def remove_channel(self, record_name: str, iocid: str) -> None:
616616
"""Remove channel from self.iocs and self.channel_ioc_ids.
617617
618618
Args:
619-
recordName: The name of the record to remove.
619+
record_name: The name of the record to remove.
620620
iocid: The IOC ID of the record to remove from.
621621
"""
622-
self.channel_ioc_ids[recordName].remove(iocid)
622+
self.channel_ioc_ids[record_name].remove(iocid)
623623
if iocid not in self.iocs:
624-
if len(self.channel_ioc_ids[recordName]) == 0:
625-
del self.channel_ioc_ids[recordName]
624+
if len(self.channel_ioc_ids[record_name]) == 0:
625+
del self.channel_ioc_ids[record_name]
626626
return
627627
self.iocs[iocid].channelcount -= 1
628628
if self.iocs[iocid].channelcount <= 0:
629629
if self.iocs[iocid].channelcount < 0:
630630
_log.error("Channel count negative: %s", iocid)
631631
self.iocs.pop(iocid)
632-
if len(self.channel_ioc_ids[recordName]) == 0:
633-
del self.channel_ioc_ids[recordName]
632+
if len(self.channel_ioc_ids[record_name]) == 0:
633+
del self.channel_ioc_ids[record_name]
634634

635635
def clean_service(self) -> None:
636636
"""Marks all channels belonging to this recceiver (as found by the recceiver id) as 'Inactive'."""
@@ -738,7 +738,7 @@ def handle_channel_is_old(
738738
if cf_config.alias_enabled:
739739
if cf_channel.name in record_info_by_name:
740740
for alias_name in record_info_by_name[cf_channel.name].aliases:
741-
# TODO Remove? This code couldn't have been working....
741+
# Legacy alias handling retained to avoid changing runtime behavior.
742742
alias_channel = CFChannel(alias_name, "", [])
743743
if alias_name in channel_ioc_ids:
744744
last_alias_ioc_id = channel_ioc_ids[alias_name][-1]
@@ -1035,23 +1035,22 @@ def update_existing_channel_diff_iocid(
10351035
channels.append(existing_channel)
10361036
_log.debug("Add existing channel with different IOC: %s", existing_channel)
10371037
# in case, alias exists, update their properties too
1038-
if cf_config.alias_enabled:
1039-
if channel_name in record_info_by_name:
1040-
alias_properties = [CFProperty.alias(ioc_info.owner, channel_name)]
1041-
for p in new_properties:
1042-
alias_properties.append(p)
1043-
for alias_name in record_info_by_name[channel_name].aliases:
1044-
if alias_name in existing_channels:
1045-
ach = existing_channels[alias_name]
1046-
ach.properties = __merge_property_lists(
1047-
alias_properties,
1048-
ach,
1049-
managed_properties,
1050-
)
1051-
channels.append(ach)
1052-
else:
1053-
channels.append(CFChannel(alias_name, ioc_info.owner, alias_properties))
1054-
_log.debug("Add existing alias %s of %s with different IOC from %s", alias_name, channel_name, iocid)
1038+
if cf_config.alias_enabled and channel_name in record_info_by_name:
1039+
alias_properties = [CFProperty.alias(ioc_info.owner, channel_name)]
1040+
for p in new_properties:
1041+
alias_properties.append(p)
1042+
for alias_name in record_info_by_name[channel_name].aliases:
1043+
if alias_name in existing_channels:
1044+
ach = existing_channels[alias_name]
1045+
ach.properties = __merge_property_lists(
1046+
alias_properties,
1047+
ach,
1048+
managed_properties,
1049+
)
1050+
channels.append(ach)
1051+
else:
1052+
channels.append(CFChannel(alias_name, ioc_info.owner, alias_properties))
1053+
_log.debug("Add existing alias %s of %s with different IOC from %s", alias_name, channel_name, iocid)
10551054

10561055

10571056
def create_new_channel(
@@ -1078,14 +1077,13 @@ def create_new_channel(
10781077

10791078
channels.append(CFChannel(channel_name, ioc_info.owner, new_properties))
10801079
_log.debug("Add new channel: %s", channel_name)
1081-
if cf_config.alias_enabled:
1082-
if channel_name in record_info_by_name:
1083-
alias_properties = [CFProperty.alias(ioc_info.owner, channel_name)]
1084-
for p in new_properties:
1085-
alias_properties.append(p)
1086-
for alias in record_info_by_name[channel_name].aliases:
1087-
channels.append(CFChannel(alias, ioc_info.owner, alias_properties))
1088-
_log.debug("Add new alias: %s from %s", alias, channel_name)
1080+
if cf_config.alias_enabled and channel_name in record_info_by_name:
1081+
alias_properties = [CFProperty.alias(ioc_info.owner, channel_name)]
1082+
for p in new_properties:
1083+
alias_properties.append(p)
1084+
for alias in record_info_by_name[channel_name].aliases:
1085+
channels.append(CFChannel(alias, ioc_info.owner, alias_properties))
1086+
_log.debug("Add new alias: %s from %s", alias, channel_name)
10891087

10901088

10911089
class IOCMissingInfoError(Exception):
@@ -1141,7 +1139,7 @@ def _update_channelfinder(
11411139
for ch in client.findByArgs(prepare_find_args(cf_config=cf_config, args=[("iocid", iocid)]))
11421140
]
11431141

1144-
if old_channels is not None:
1142+
if old_channels:
11451143
handle_channels(
11461144
old_channels,
11471145
new_channels,
@@ -1169,7 +1167,7 @@ def _update_channelfinder(
11691167
recceiverid,
11701168
ioc_info.hostname,
11711169
ioc_info.ioc_name,
1172-
ioc_info.ioc_IP,
1170+
ioc_info.ioc_ip,
11731171
ioc_info.ioc_id,
11741172
)
11751173
if (
@@ -1221,26 +1219,26 @@ def cf_set_chunked(client: ChannelFinderClient, channels: List[CFChannel], chunk
12211219

12221220

12231221
def create_ioc_properties(
1224-
owner: str, iocTime: str, recceiverid: str, hostName: str, iocName: str, iocIP: str, iocid: str
1222+
owner: str, ioc_time: str, recceiverid: str, host_name: str, ioc_name: str, ioc_ip: str, iocid: str
12251223
) -> List[CFProperty]:
12261224
"""Create the properties from an IOC.
12271225
12281226
Args:
12291227
owner: The owner of the properties.
1230-
iocTime: The time of the properties.
1228+
ioc_time: The time of the properties.
12311229
recceiverid: The recceiver ID of the properties.
1232-
hostName: The host name of the properties.
1233-
iocName: The IOC name of the properties.
1234-
iocIP: The IOC IP of the properties.
1230+
host_name: The host name of the properties.
1231+
ioc_name: The IOC name of the properties.
1232+
ioc_ip: The IOC IP of the properties.
12351233
iocid: The IOC ID of the properties.
12361234
"""
12371235
return [
1238-
CFProperty(CFPropertyName.HOSTNAME.value, owner, hostName),
1239-
CFProperty(CFPropertyName.IOC_NAME.value, owner, iocName),
1236+
CFProperty(CFPropertyName.HOSTNAME.value, owner, host_name),
1237+
CFProperty(CFPropertyName.IOC_NAME.value, owner, ioc_name),
12401238
CFProperty(CFPropertyName.IOC_ID.value, owner, iocid),
1241-
CFProperty(CFPropertyName.IOC_IP.value, owner, iocIP),
1239+
CFProperty(CFPropertyName.IOC_IP.value, owner, ioc_ip),
12421240
CFProperty.active(owner),
1243-
CFProperty.time(owner, iocTime),
1241+
CFProperty.time(owner, ioc_time),
12441242
CFProperty(CFPropertyName.RECCEIVER_ID.value, owner, recceiverid),
12451243
]
12461244

@@ -1265,7 +1263,7 @@ def create_default_properties(
12651263
recceiverid,
12661264
last_ioc_info.hostname,
12671265
last_ioc_info.ioc_name,
1268-
last_ioc_info.ioc_IP,
1266+
last_ioc_info.ioc_ip,
12691267
last_ioc_info.ioc_id,
12701268
)
12711269

@@ -1302,13 +1300,12 @@ def get_current_time(timezone: Optional[str] = None) -> str:
13021300
return str(datetime.datetime.now())
13031301

13041302

1305-
def prepare_find_args(cf_config: CFConfig, args, size=0) -> List[Tuple[str, str]]:
1303+
def prepare_find_args(cf_config: CFConfig, args) -> List[Tuple[str, str]]:
13061304
"""Prepare the find arguments.
13071305
13081306
Args:
13091307
cf_config: The configuration.
13101308
args: The arguments.
1311-
size: The size.
13121309
"""
13131310
size_limit = int(cf_config.cf_query_limit)
13141311
if size_limit > 0:

server/recceiver/dbstore.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ def __init__(self, name, conf):
2828
self.trecinfo = self.conf.get("table.recinfo", "recinfo")
2929
self.mykey = int(self.conf["idkey"])
3030

31-
def decCount(self, X, D):
31+
def dec_count(self, _result, deferred):
3232
assert len(self.Ds) > 0
33-
self.Ds.remove(D)
33+
self.Ds.remove(deferred)
3434
if self.done:
3535
self.pool.close()
3636

37-
def waitFor(self, D):
38-
self.Ds.add(D)
39-
D.addBoth(self.decCount, D)
40-
return D
37+
def wait_for(self, deferred):
38+
self.Ds.add(deferred)
39+
deferred.addBoth(self.dec_count, deferred)
40+
return deferred
4141

4242
def startService(self):
4343
_log.info("Start DBService")
@@ -54,23 +54,22 @@ def startService(self):
5454
continue
5555
dbargs[key] = val
5656

57-
if self.conf["dbtype"] == "sqlite3":
58-
if "isolation_level" not in dbargs:
59-
dbargs["isolation_level"] = "IMMEDIATE"
57+
if self.conf["dbtype"] == "sqlite3" and "isolation_level" not in dbargs:
58+
dbargs["isolation_level"] = "IMMEDIATE"
6059

6160
# workaround twisted bug #3629
6261
dbargs["check_same_thread"] = False
6362

6463
self.pool = db.ConnectionPool(self.conf["dbtype"], self.conf["dbname"], **dbargs)
6564

66-
self.waitFor(self.pool.runInteraction(self.cleanupDB))
65+
self.wait_for(self.pool.runInteraction(self.cleanupDB))
6766

6867
def stopService(self):
6968
_log.info("Stop DBService")
7069

7170
service.Service.stopService(self)
7271

73-
self.waitFor(self.pool.runInteraction(self.cleanupDB))
72+
self.wait_for(self.pool.runInteraction(self.cleanupDB))
7473

7574
assert len(self.Ds) > 0
7675
self.done = True

server/recceiver/interfaces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class CommitTransaction:
4242

4343

4444
class IProcessor(service.IService):
45-
def commit(transaction):
45+
def commit(self, transaction):
4646
"""Consume and process the provided ITransaction.
4747
4848
Returns either a Deferred or None.

0 commit comments

Comments
 (0)