Created
February 18, 2015 00:08
-
-
Save jonatasleon/2e739cef80430a9ff6a1 to your computer and use it in GitHub Desktop.
This code snippet changes the mode that your Ubuntu is started
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env python3 | |
#-*- encoding:utf-8 -*- | |
import sys, os | |
CHANGED = 0 | |
NOT_CHANGED = 1 | |
ERROR = 2 | |
FILE_NAME = '/etc/default/grub' | |
LINES_TO_CHANGE = ['GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"', 'GRUB_CMDLINE_LINUX="', 'GRUB_TERMINAL=console'] | |
def resultForMode(mode, index): | |
if index == 0: | |
if mode: | |
return (LINES_TO_CHANGE[index]+'\n') | |
else: | |
return ("#"+LINES_TO_CHANGE[index]+'\n') | |
elif index == 1: | |
if mode: | |
return (LINES_TO_CHANGE[index]+'"\n') | |
else: | |
return (LINES_TO_CHANGE[index]+'text"\n') | |
elif index == 2: | |
if mode: | |
return ("#"+LINES_TO_CHANGE[index]+'\n') | |
else: | |
return (LINES_TO_CHANGE[index]+'\n') | |
def editLine(foundValue, mode): | |
for i, value in enumerate(LINES_TO_CHANGE): | |
if foundValue == value: | |
return resultForMode(mode, i) | |
def main(args): | |
argMode = args[1] | |
mode = 2 | |
newText = '' | |
if argMode == 'text': | |
mode = 0 | |
elif argMode == 'display': | |
mode = 1 | |
ret = NOT_CHANGED | |
f = open(FILE_NAME, 'r') | |
for line in f: | |
canWrite = True | |
for value in LINES_TO_CHANGE: | |
if value in line: | |
newText += editLine(value, mode) | |
canWrite = False | |
if canWrite: | |
newText += line | |
f.close | |
temp = open(FILE_NAME, 'w') | |
temp.write(newText) | |
temp.close | |
os.system('update-grub') | |
ret = CHANGED | |
return (ret) | |
args = sys.argv | |
ret = main(args) | |
sys.exit(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment