Skip to content

Commit

Permalink
refactor python file
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgameiroborges committed Sep 27, 2024
1 parent 30ba3ea commit 4d6df68
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
40 changes: 2 additions & 38 deletions .github/workflows/fetch_snaps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,49 +51,13 @@ jobs:
juju set-model-constraints arch='amd64'
sg 'lxd' -c "lxc image list"
juju deploy ./releases/latest/postgresql-bundle.yaml
sleep 1000
sleep 600
juju status
juju exec --all --output snaps.txt -- snap list
cat ./snaps.txt
cat ./snaps.txt | awk '! /^[0-9]/ && $1 != "Name" { print $1, $3, $4 }' | sort | uniq > output_table.txt
snap info canonical-livepatch | awk '/^channels:/ {found=1; next} found && $1 != "" {print $1, $4; exit}' > canonical_livepatch.txt
cat ./output_table.txt
cat ./canonical_livepatch.txt
- name: Parse yaml file
timeout-minutes: 5
shell: python
run: |
import yaml
with open("output_table.txt", "r") as table_file:
table_lines = table_file.readlines()
with open("canonical_livepatch.txt", "r") as livepatch_file:
livepatch_line = livepatch_file.read().strip()
livepatch_channel, livepatch_revision = livepatch_line.split(":")
livepatch_revision = livepatch_revision.strip(" ()")
packages = []
for line in table_lines:
parts = line.split()
package = {
"name": parts[0],
"revision": parts[1],
"push_channel": parts[2],
}
packages.append(package)
packages.append({
"name": "canonical-livepatch",
"revision": livepatch_revision,
"push_channel": livepatch_channel,
})
with open("snaps.yaml", "w") as yaml_file:
yaml.dump({"packages": packages}, yaml_file, default_flow_style=False)
- name: print resulting yaml file
timeout-minutes: 3
run: |
python ./releases/latest/generate_snaps_yaml.py ./output_table.txt ./canonical_livepatch.txt
cat ./snaps.yaml
47 changes: 47 additions & 0 deletions releases/latest/generate_snaps_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import yaml
import argparse

def convert_to_yaml(output_table_file, canonical_livepatch_file):
# Read the output_table.txt
with open(output_table_file, "r") as table_file:
table_lines = table_file.readlines()

# Read the canonical_livepatch.txt
with open(canonical_livepatch_file, "r") as livepatch_file:
livepatch_line = livepatch_file.read().strip()

# Extract livepatch info
livepatch_channel, livepatch_revision = livepatch_line.split(":")
livepatch_revision = livepatch_revision.strip(" ()")

# Prepare the data for YAML
packages = []

# Process table file lines
for line in table_lines:
parts = line.split()
package = {
"name": parts[0],
"revision": parts[1],
"push_channel": parts[2]
}
packages.append(package)

# Add canonical-livepatch info
packages.append({
"name": "canonical-livepatch",
"revision": livepatch_revision,
"push_channel": livepatch_channel
})

# Write to YAML file
with open("package-output.yaml", "w") as yaml_file:
yaml.dump({"packages": packages}, yaml_file, default_flow_style=False)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Convert files to YAML.')
parser.add_argument('output_table', type=str, help='Path to output_table.txt')
parser.add_argument('canonical_livepatch', type=str, help='Path to canonical_livepatch.txt')
args = parser.parse_args()

convert_to_yaml(args.output_table, args.canonical_livepatch)

0 comments on commit 4d6df68

Please sign in to comment.