-
Notifications
You must be signed in to change notification settings - Fork 1
/
hostname_tool.py
executable file
·96 lines (71 loc) · 3.22 KB
/
hostname_tool.py
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
##!/usr/bin/python
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class HostnameToolWindow(Gtk.Window):
"""Hostname Tool Window class, with all GUI and program logic"""
def __init__(self):
"""Fires up the GUI"""
super(HostnameToolWindow, self).__init__()
self.set_title ("Hostname Tool")
self.set_size_request (320, 150)
self.current_hostname_label = Gtk.Label(label="Current hostname: ")
self.desired_hostname_label = Gtk.Label(label="New hostname: ")
self.current_hostname_textbox = Gtk.Entry()
self.current_hostname_textbox.set_text(self.get_hostname())
self.desired_hostname_textbox = Gtk.Entry ()
self.change_hostname_button = Gtk.Button(label="Change hostname")
self.change_hostname_button.connect("clicked", self.change_hostname, None)
self.change_hostname_button.connect_object("clicked", Gtk.Widget.destroy, self)
self.change_hostname_button.set_size_request(295, -1)
fix = Gtk.Fixed ()
fix.put(self.current_hostname_label, 10, 20)
fix.put(self.current_hostname_textbox, 140, 15)
fix.put(self.desired_hostname_label, 10, 60)
fix.put(self.desired_hostname_textbox, 140, 55)
fix.put(self.change_hostname_button, 10, 100)
self.add(fix)
self.connect("destroy", Gtk.main_quit)
self.show_all()
def change_hostname(self, widget, data=None):
"""Changes the name of the computer"""
current_hostname = self.get_hostname()
new_hostname = self.desired_hostname_textbox.get_text()
if new_hostname:
try:
with open("/etc/hostname", "w") as f:
f.writeline(new_hostname)
with open("/etc/hosts", "r") as f:
data = f.read()
assert current_hostname in data,"Current hostname invalid"
with open("/etc/hosts", "w") as f:
f.write(data.replace(current_hostname, new_hostname))
except PermissionError as e:
if e.errno == 13:
self.dia = Gtk.Dialog(title='ERROR', parent=self, modal=True, destroy_with_parent=True)
self.dia.vbox.pack_start(Gtk.Label(label='You must relaunch the app with root privileges !'), True, True, 0)
self.button = Gtk.Button(label="Ok")
self.button.connect("clicked", self.close_dialog, None)
self.dia.vbox.add(self.button)
self.dia.show_all()
result = self.dia.run()
self.dia.hide()
else:
print("I/O error({0}): {1}".format(e.errno, e.strerror))
else:
print("Empty hostname!")
def get_hostname(self):
"""Returns the current computer name"""
with open("/etc/hostname", "r") as f:
hostname = f.read().strip()
return hostname
def close_dialog(self,*args): # we won't use *args
if self.dia:
self.dia.destroy()
def main():
"""Starts the Hostname tool"""
hostname_tool_window = HostnameToolWindow()
Gtk.main()
if __name__ == "__main__":
main ()