Skip to content

Instantly share code, notes, and snippets.

@gyadam
Last active November 20, 2020 04:00
Show Gist options
  • Save gyadam/866940153ff94ca68610c2bc870ec69d to your computer and use it in GitHub Desktop.
Save gyadam/866940153ff94ca68610c2bc870ec69d to your computer and use it in GitHub Desktop.
PowerTop HTML output parser
from bs4 import BeautifulSoup
import sys
import re
# Description:
# PowerTop is a Linux tool used to diagnose issues with power consumption and power management.
# This script can be used to parse shell script commands from the HTML output of PowerTop.
# More info: https://www.makeuseof.com/tag/powertop-will-maximize-your-linux-laptops-battery-life/
#
# Usage:
# sudo powertop --html
# python parse_commands.py powertop.html
filename = sys.argv[1]
with open(filename, 'r') as file:
html_doc = file.read().replace('\n', '')
soup = BeautifulSoup(html_doc, 'html.parser')
tune_h2 = soup.find_all(string=re.compile("Software Settings in Need of Tuning"))
tune_table = tune_h2[0].parent.findNext("table")
tune_rows = tune_table.contents[1:]
commands = ""
for row in tune_rows:
command = row.find_all("td")[1].text
commands += command.strip()
commands += '\n'
# set this to the name of your startup script (e.g. rc.local)
startup_script_name = "my_startup_script"
with open(startup_script_name, 'w') as file:
file.write('#!/bin/sh -e')
file.write('\n\n')
file.write(commands)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment