Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get state_reader via function #90

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyaptly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def prepare(args):

with open(args.config, "rb") as f:
cfg = tomli.load(f)
state_reader.state.read()
state_reader.state_reader().read()
return cfg


Expand Down
8 changes: 4 additions & 4 deletions pyaptly/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def add_gpg_keys(mirror_config):
keys_urls[key] = None

for key in keys_urls.keys():
if key in state_reader.state.gpg_keys:
if key in state_reader.state_reader().gpg_keys:
continue
try:
key_command = [
Expand All @@ -59,7 +59,7 @@ def add_gpg_keys(mirror_config):
util.run_command(["bash", "-c", key_shell], check=True)
else:
raise
state_reader.state.read_gpg()
state_reader.state_reader().read_gpg()


def mirror(cfg, args):
Expand Down Expand Up @@ -102,7 +102,7 @@ def cmd_mirror_create(cfg, mirror_name, mirror_config):
:param mirror_config: Configuration of the snapshot from the toml file.
:type mirror_config: dict
"""
if mirror_name in state_reader.state.mirrors: # pragma: no cover
if mirror_name in state_reader.state_reader().mirrors: # pragma: no cover
return

add_gpg_keys(mirror_config)
Expand Down Expand Up @@ -142,7 +142,7 @@ def cmd_mirror_update(cfg, mirror_name, mirror_config):
:param mirror_config: Configuration of the snapshot from the toml file.
:type mirror_config: dict
"""
if mirror_name not in state_reader.state.mirrors: # pragma: no cover
if mirror_name not in state_reader.state_reader().mirrors: # pragma: no cover
raise Exception("Mirror not created yet")
add_gpg_keys(mirror_config)
aptly_cmd = ["aptly", "mirror", "update"]
Expand Down
19 changes: 12 additions & 7 deletions pyaptly/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def publish(cfg, args):
]

for cmd in command.Command.order_commands(
commands, state_reader.state.has_dependency
commands, state_reader.state_reader().has_dependency
):
cmd.execute()

Expand All @@ -49,7 +49,7 @@ def publish(cfg, args):
for publish_conf_entry in cfg["publish"][args.publish_name]
]
for cmd in command.Command.order_commands(
commands, state_reader.state.has_dependency
commands, state_reader.state_reader().has_dependency
):
cmd.execute()
else:
Expand Down Expand Up @@ -83,7 +83,7 @@ def publish_cmd_update(cfg, publish_name, publish_config, ignore_existing=False)
return command.Command(publish_cmd + options + args)

publish_fullname = "%s %s" % (publish_name, publish_config["distribution"])
current_snapshots = state_reader.state.publish_map[publish_fullname]
current_snapshots = state_reader.state_reader().publish_map[publish_fullname]
if "snapshots" in publish_config:
snapshots_config = publish_config["snapshots"]
new_snapshots = [
Expand All @@ -97,7 +97,7 @@ def publish_cmd_update(cfg, publish_name, publish_config, ignore_existing=False)
if publish["distribution"] == distribution:
snapshots_config.extend(publish["snapshots"])
break
new_snapshots = list(state_reader.state.publish_map[conf_value])
new_snapshots = list(state_reader.state_reader().publish_map[conf_value])
else: # pragma: no cover
raise ValueError(
"No snapshot references configured in publish %s" % publish_name
Expand All @@ -121,7 +121,9 @@ def publish_cmd_update(cfg, publish_name, publish_config, ignore_existing=False)
archive = archive.replace(
"%T", date_tools.format_timestamp(datetime.datetime.now())
)
if archive in state_reader.state.snapshots: # pragma: no cover
if (
archive in state_reader.state_reader().snapshots
): # pragma: no cover
continue
prefix_to_search = re.sub("%T$", "", snap["name"])

Expand Down Expand Up @@ -155,7 +157,10 @@ def publish_cmd_create(cfg, publish_name, publish_config, ignore_existing=False)
:type publish_config: dict
"""
publish_fullname = "%s %s" % (publish_name, publish_config["distribution"])
if publish_fullname in state_reader.state.publishes and not ignore_existing:
if (
publish_fullname in state_reader.state_reader().publishes
and not ignore_existing
):
# Nothing to do, publish already created
return

Expand Down Expand Up @@ -228,7 +233,7 @@ def publish_cmd_create(cfg, publish_name, publish_config, ignore_existing=False)
conf_value = " ".join(conf_value.split("/"))
source_args.append("snapshot")
try:
sources = state_reader.state.publish_map[conf_value]
sources = state_reader.state_reader().publish_map[conf_value]
except KeyError:
lg.critical(
(
Expand Down
6 changes: 3 additions & 3 deletions pyaptly/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def repo(cfg, args):
]

for cmd in command.Command.order_commands(
commands, state_reader.state.has_dependency
commands, state_reader.state_reader().has_dependency
):
cmd.execute()

else:
if args.repo_name in cfg["repo"]:
commands = [cmd_repo(cfg, args.repo_name, cfg["repo"][args.repo_name])]
for cmd in command.Command.order_commands(
commands, state_reader.state.has_dependency
commands, state_reader.state_reader().has_dependency
):
cmd.execute()
else:
Expand All @@ -57,7 +57,7 @@ def repo_cmd_create(cfg, repo_name, repo_config):
:param repo_config: Configuration of the repo from the toml file.
:type repo_config: dict
"""
if repo_name in state_reader.state.repos: # pragma: no cover
if repo_name in state_reader.state_reader().repos: # pragma: no cover
# Nothing to do, repo already created
return

Expand Down
21 changes: 13 additions & 8 deletions pyaptly/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def snapshot(cfg, args):

if len(commands) > 0:
for cmd in command.Command.order_commands(
commands, state_reader.state.has_dependency
commands, state_reader.state_reader().has_dependency
):
cmd.execute()

Expand All @@ -55,7 +55,7 @@ def snapshot(cfg, args):

if len(commands) > 0:
for cmd in command.Command.order_commands(
commands, state_reader.state.has_dependency
commands, state_reader.state_reader().has_dependency
):
cmd.execute()

Expand Down Expand Up @@ -115,7 +115,9 @@ def dependents_of_snapshot(snapshot_name):

:rtype: generator
"""
for dependent in state_reader.state.snapshot_map.get(snapshot_name, []):
for dependent in state_reader.state_reader().snapshot_map.get(
snapshot_name, []
):
yield dependent
# TODO I fixed a bug, but there is no test. We do not test recursive dependants
yield from dependents_of_snapshot(dependent)
Expand All @@ -140,7 +142,7 @@ def rotate_snapshot(cfg, snapshot_name):
# First, verify that our snapshot environment is in a sane state_reader.state.
# Fixing the environment is not currently our task.

if rotated_name in state_reader.state.snapshots: # pragma: no cover
if rotated_name in state_reader.state_reader().snapshots: # pragma: no cover
raise Exception(
"Cannot update snapshot %s - rotated name %s already exists"
% (snapshot_name, rotated_name)
Expand Down Expand Up @@ -185,7 +187,7 @@ def cmd_snapshot_update(

# The "intermediate" command causes the state reader to refresh. At the
# same time, it provides a collection point for dependency handling.
intermediate = command.FunctionCommand(state_reader.state.read)
intermediate = command.FunctionCommand(state_reader.state_reader().read)
intermediate.provide("virtual", "all-snapshots-rotated")

for cmd in rename_cmds:
Expand All @@ -200,7 +202,7 @@ def cmd_snapshot_update(

# Same as before - create a focal point to "collect" dependencies
# after the snapshots have been rebuilt. Also reload state once again
intermediate2 = command.FunctionCommand(state_reader.state.read)
intermediate2 = command.FunctionCommand(state_reader.state_reader().read)
intermediate2.provide("virtual", "all-snapshots-rebuilt")

create_cmds = []
Expand Down Expand Up @@ -245,7 +247,7 @@ def cmd_snapshot_update(
def is_publish_affected(name, publish_info):
if (
"%s %s" % (name, publish_info["distribution"])
in state_reader.state.publishes
in state_reader.state_reader().publishes
):
try:
for snap in publish_info["snapshots"]:
Expand Down Expand Up @@ -316,7 +318,10 @@ def cmd_snapshot_create(

snapshot_name = date_tools.expand_timestamped_name(snapshot_name, snapshot_config)

if snapshot_name in state_reader.state.snapshots and not ignore_existing:
if (
snapshot_name in state_reader.state_reader().snapshots
and not ignore_existing
):
return []

default_aptly_cmd = ["aptly", "snapshot", "create"]
Expand Down
9 changes: 8 additions & 1 deletion pyaptly/state_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,11 @@ def has_dependency(self, dependency):
raise ValueError("Unknown dependency to resolve: %s" % str(dependency))


state = SystemStateReader()
_state_reader: SystemStateReader | None = None


def state_reader():
global _state_reader
if not _state_reader:
_state_reader = SystemStateReader()
return _state_reader
1 change: 1 addition & 0 deletions tools/rebuild-rpm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def main():
"python3-tomli-w",
"python3-devel",
"python3-setuptools",
"python3-colorama",
],
check=True,
)
Expand Down