This file is indexed.

/usr/bin/powertop2tuned is in tuned-utils 2.9.0-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/python -Es
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008-2013 Red Hat, Inc.
# Authors: Jan Kaluža <jkaluza@redhat.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

import os
import sys
import tempfile
import shutil
import argparse
import codecs
from subprocess import *
from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint

SCRIPT_SH = """#!/bin/sh

. /usr/lib/tuned/functions

start() {
%s
	return 0
}

stop() {
%s
	return 0
}

process $@
"""

TUNED_CONF_PROLOG = "# Automatically generated by powertop2tuned tool\n\n"
TUNED_CONF_INCLUDE = """[main]
%s\n
"""
TUNED_CONF_EPILOG="""\n[powertop_script]
type=script
replace=1
script=script.sh
"""


class PowertopHTMLParser(HTMLParser):
	def __init__(self, enable_tunings):
		HTMLParser.__init__(self)

		self.inProperTable = False
		self.inScript = False
		self.intd = False
		self.lastStartTag = ""
		self.tdCounter = 0
		self.lastDesc = ""
		self.data = ""
		self.currentScript = ""
		if enable_tunings:
			self.prefix = ""
		else:
			self.prefix = "#"

		self.plugins = {}

	def getParsedData(self):
		return self.data

	def getPlugins(self):
		return self.plugins

	def handle_starttag(self, tag, attrs):
		self.lastStartTag = tag
		if self.lastStartTag == "div" and dict(attrs).get("id")  == "tuning":
			self.inProperTable = True
		if self.inProperTable and tag == "td":
			self.tdCounter += 1
			self.intd = True

	def parse_command(self, command):
		prefix = ""
		command = command.strip()
		if command[0] == '#':
			prefix = "#"
			command = command[1:]

		if command.startswith("echo") and command.find("/proc/sys") != -1:
			splitted = command.split("'")
			value = splitted[1]
			path = splitted[3]
			path = path.replace("/proc/sys/", "").replace("/", ".")
			self.plugins.setdefault("sysctl", "[sysctl]\n")
			self.plugins["sysctl"] += "#%s\n%s%s=%s\n\n" % (self.lastDesc, prefix, path, value)
		# TODO: plugins/plugin_sysfs.py doesn't support this so far, it has to be implemented to 
		# let it work properly.
		elif command.startswith("echo") and (command.find("'/sys/") != -1 or command.find("\"/sys/") != -1):
			splitted = command.split("'")
			value = splitted[1]
			path = splitted[3]
			if path in ("/sys/module/snd_hda_intel/parameters/power_save", "/sys/module/snd_ac97_codec/parameters/power_save"):
				self.plugins.setdefault("audio", "[audio]\n")
				self.plugins["audio"] += "#%s\n%stimeout=1\n" % (self.lastDesc, prefix)
			else:
				self.plugins.setdefault("sysfs", "[sysfs]\n")
				self.plugins["sysfs"] += "#%s\n%s%s=%s\n\n" % (self.lastDesc, prefix, path, value)
		elif command.startswith("ethtool -s ") and command.endswith("wol d;"):
			self.plugins.setdefault("net", "[net]\n")
			self.plugins["net"] += "#%s\n%swake_on_lan=0\n" % (self.lastDesc, prefix)
		else:
			return False
		return True

	def handle_endtag(self, tag):
		if self.inProperTable and tag == "table":
			self.inProperTable = False
			self.intd = False
		if tag == "tr":
			self.tdCounter = 0
			self.intd = False
		if tag == "td":
			self.intd = False
		if self.inScript:
			#print self.currentScript
			self.inScript = False
			# Command is not handled, so just store it in the script
			if not self.parse_command(self.currentScript.split("\n")[-1]):
				self.data += self.currentScript + "\n\n"

	def handle_entityref(self, name):
		if self.inScript:
			self.currentScript += unichr(name2codepoint[name])

	def handle_data(self, data):
		prefix = self.prefix
		if self.inProperTable and self.intd and self.tdCounter == 1:
			self.lastDesc = data
			if self.lastDesc.lower().find("autosuspend") != -1 and (self.lastDesc.lower().find("keyboard") != -1 or self.lastDesc.lower().find("mouse") != -1):
					self.lastDesc += "\n# WARNING: For some devices, uncommenting this command can disable the device."
					prefix = "#"
		if self.intd and ((self.inProperTable and self.tdCounter == 2) or self.inScript):
			self.tdCounter = 0
			if not self.inScript:
				self.currentScript += "\t# " + self.lastDesc + "\n"
				self.currentScript += "\t" + prefix + data.strip()
				self.inScript = True
			else:
				self.currentScript += data.strip()

class PowertopProfile:
	BAD_PRIVS = 100
	PARSING_ERROR = 101
	BAD_SCRIPTSH = 102

	def __init__(self, output, profile_name, name = ""):
		self.profile_name = profile_name
		self.name = name
		self.output = output

	def currentActiveProfile(self):
		proc = Popen(["tuned-adm", "active"], stdout=PIPE)
		output = proc.communicate()[0]
		if output and output.find("Current active profile: ") == 0:
			return output[len("Current active profile: "):output.find("\n")]
		return None

	def checkPrivs(self):
		myuid = os.geteuid()
		if myuid != 0:
			print >> sys.stderr, 'Run this program as root'
			return False
		return True

	def generateHTML(self):
		print "Running PowerTOP, please wait..."
		environment = os.environ.copy()
		environment["LC_ALL"] = "C"
		try:
			proc = Popen(["/usr/sbin/powertop", "--html=/tmp/powertop", "--time=1"], stdout=PIPE, stderr=PIPE, env=environment)
			output = proc.communicate()[1]
		except (OSError, IOError):
			print >> sys.stderr, 'Unable to execute PowerTOP, is PowerTOP installed?'
			return -2

		if proc.returncode != 0:
			print >> sys.stderr, 'PowerTOP returned error code: %d' % proc.returncode
			return -2

		prefix = "PowerTOP outputing using base filename "
		if output.find(prefix) == -1:
			return -1

		name = output[output.find(prefix)+len(prefix):-1]
		#print "Parsed filename=", [name]
		return name

	def parseHTML(self, enable_tunings):
		f = None
		data = None
		parser = PowertopHTMLParser(enable_tunings)
		try:
			f = codecs.open(self.name, "r", "utf-8")
			data = f.read()
		except (OSError, IOError, UnicodeDecodeError):
			data = None

		if f is not None:
			f.close()

		if data is None:
			return "", ""

		parser.feed(data)
		return parser.getParsedData(), parser.getPlugins()

	def generateShellScript(self, data):
		print "Generating shell script", os.path.join(self.output, "script.sh")
		f = None
		try:
			f = codecs.open(os.path.join(self.output, "script.sh"), "w", "utf-8")
			f.write(SCRIPT_SH % (data, ""))
			os.fchmod(f.fileno(), 0755)
			f.close()
		except (OSError, IOError) as e:
			print >> sys.stderr, "Error writing shell script: %s" % e
			if f is not None:
				f.close()
			return False
		return True

	def generateTunedConf(self, profile, plugins):
		print "Generating Tuned config file", os.path.join(self.output, "tuned.conf")
		f = codecs.open(os.path.join(self.output, "tuned.conf"), "w", "utf-8")
		f.write(TUNED_CONF_PROLOG)
		if profile is not None:
			if self.profile_name == profile:
				print >> sys.stderr, 'New profile has same name as active profile, not including active profile (avoiding circular deps).'
			else:
				f.write(TUNED_CONF_INCLUDE % ("include=" + profile))

		for plugin in plugins.values():
			f.write(plugin + "\n")

		f.write(TUNED_CONF_EPILOG)
		f.close()

	def generate(self, new_profile, merge_profile, enable_tunings):
		generated_html = False
		if len(self.name) == 0:
			generated_html = True
			if not self.checkPrivs():
				return self.BAD_PRIVS

			name = self.generateHTML()
			if isinstance(name, int):
				return name
			self.name = name

		data, plugins = self.parseHTML(enable_tunings)

		if generated_html:
			os.unlink(self.name)

		if len(data) == 0 and len(plugins) == 0:
			print >> sys.stderr, 'Your Powertop version is incompatible (maybe too old) or the generated HTML output is malformed'
			return self.PARSING_ERROR

		if new_profile is False:
			if merge_profile is None:
				profile = self.currentActiveProfile()
			else:
				profile = merge_profile
		else:
			profile = None

		if not os.path.exists(self.output):
			os.makedirs(self.output)

		if not self.generateShellScript(data):
			return self.BAD_SCRIPTSH

		self.generateTunedConf(profile, plugins)

		return 0

if __name__ == "__main__":
	parser = argparse.ArgumentParser(description='Creates Tuned profile from Powertop HTML output.')
	parser.add_argument('profile', metavar='profile_name', type=unicode, nargs='?', help='Name for the profile to be written.')
	parser.add_argument('-i', '--input', metavar='input_html', type=unicode, help='Path to Powertop HTML report. If not given, it is generated automatically.')
	parser.add_argument('-o', '--output', metavar='output_directory', type=unicode, help='Directory where the profile will be written, default is /etc/tuned/profile_name directory.')
	parser.add_argument('-n', '--new-profile', action='store_true', help='Creates new profile, otherwise it merges (include) your current profile.')
	parser.add_argument('-m', '--merge-profile', action = 'store', help = 'Merges (includes) the specified profile (can be suppressed by -n option).')
	parser.add_argument('-f', '--force', action='store_true', help='Overwrites the output directory if it already exists.')
	parser.add_argument('-e', '--enable', action='store_true', help='Enable all tunings (not recommended). Even with this enabled tunings known to be harmful (like USB_AUTOSUSPEND) won''t be enabled.')
	args = parser.parse_args()
	args = vars(args)

	if not args['profile'] and not args['output']:
		print >> sys.stderr, 'You have to specify the profile_name or output directory using the --output argument.'
		parser.print_help()
		sys.exit(-1)

	if not args['output']:
		args['output'] = "/etc/tuned"

	if args['profile']:
		args['output'] = os.path.join(args['output'], args['profile'])

	if not args['input']:
		args['input'] = ''

	if os.path.exists(args['output']) and not args['force']:
		print >> sys.stderr, 'Output directory already exists, use --force to overwrite it.'
		sys.exit(-1)

	p = PowertopProfile(args['output'], args['profile'], args['input'])
	sys.exit(p.generate(args['new_profile'], args['merge_profile'], args['enable']))