Skip to content

Commit e427d80

Browse files
committed
refactor LinuxDistro
1 parent cceb033 commit e427d80

File tree

1 file changed

+75
-83
lines changed

1 file changed

+75
-83
lines changed

src/Utility/LinuxDistro.vala

Lines changed: 75 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class LinuxDistro : GLib.Object{
3636
public string codename = "";
3737

3838
public LinuxDistro(){
39-
4039
dist_id = "";
4140
description = "";
4241
release = "";
@@ -62,101 +61,94 @@ public class LinuxDistro : GLib.Object{
6261
/* Returns information about the Linux distribution
6362
* installed at the given root path */
6463

64+
65+
/*
66+
try to read from /etc/lsb-release
67+
example content:
68+
69+
DISTRIB_ID=Ubuntu
70+
DISTRIB_RELEASE=13.04
71+
DISTRIB_CODENAME=raring
72+
DISTRIB_DESCRIPTION="Ubuntu 13.04"
73+
*/
74+
LinuxDistro? info = read_info_file(root_path + "/etc/lsb-release");
75+
if(info != null) {
76+
return info;
77+
}
78+
79+
/*
80+
fallback to /etc/os-release
81+
example content:
82+
83+
NAME="Ubuntu"
84+
VERSION="13.04, Raring Ringtail"
85+
ID=ubuntu
86+
ID_LIKE=debian
87+
PRETTY_NAME="Ubuntu 13.04"
88+
VERSION_ID="13.04"
89+
HOME_URL="http://www.ubuntu.com/"
90+
SUPPORT_URL="http://help.ubuntu.com/"
91+
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
92+
*/
93+
return read_info_file(root_path + "/etc/os-release");
94+
}
95+
96+
// read a generic info file like /etc/os-release or /etc/lsb-release
97+
private static LinuxDistro? read_info_file(string file_path) {
98+
string? dist_file_cont = file_read(file_path);
99+
if(dist_file_cont == null || dist_file_cont.length == 0) {
100+
return null;
101+
}
102+
65103
LinuxDistro info = new LinuxDistro();
104+
string[] lines = dist_file_cont.split("\n");
66105

67-
string dist_file = root_path + "/etc/lsb-release";
68-
var f = File.new_for_path(dist_file);
69-
if (f.query_exists()){
70-
71-
/*
72-
DISTRIB_ID=Ubuntu
73-
DISTRIB_RELEASE=13.04
74-
DISTRIB_CODENAME=raring
75-
DISTRIB_DESCRIPTION="Ubuntu 13.04"
76-
*/
77-
78-
foreach(string line in file_read(dist_file).split("\n")){
79-
80-
if (line.split("=").length != 2){ continue; }
81-
82-
string key = line.split("=")[0].strip();
83-
string val = line.split("=")[1].strip();
84-
85-
if (val.has_prefix("\"")){
86-
val = val[1:val.length];
87-
}
88-
89-
if (val.has_suffix("\"")){
90-
val = val[0:val.length-1];
91-
}
92-
93-
switch (key){
94-
case "DISTRIB_ID":
95-
info.dist_id = val;
96-
break;
97-
case "DISTRIB_RELEASE":
98-
info.release = val;
99-
break;
100-
case "DISTRIB_CODENAME":
101-
info.codename = val;
102-
break;
103-
case "DISTRIB_DESCRIPTION":
104-
info.description = val;
105-
break;
106-
}
106+
foreach(string line in lines){
107+
// split for 3 to detect if there are to many
108+
string[] linesplit = line.split("=", 3);
109+
110+
if (linesplit.length != 2){ continue; }
111+
112+
string key = linesplit[0].strip();
113+
string val = linesplit[1].strip();
114+
115+
// removeing leading "
116+
if (val.has_prefix("\"")) {
117+
val = val[1:val.length];
107118
}
108-
}
109-
else{
110119

111-
dist_file = root_path + "/etc/os-release";
112-
f = File.new_for_path(dist_file);
113-
if (f.query_exists()){
114-
115-
/*
116-
NAME="Ubuntu"
117-
VERSION="13.04, Raring Ringtail"
118-
ID=ubuntu
119-
ID_LIKE=debian
120-
PRETTY_NAME="Ubuntu 13.04"
121-
VERSION_ID="13.04"
122-
HOME_URL="http://www.ubuntu.com/"
123-
SUPPORT_URL="http://help.ubuntu.com/"
124-
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
125-
*/
126-
127-
foreach(string line in file_read(dist_file).split("\n")){
128-
129-
if (line.split("=").length != 2){ continue; }
130-
131-
string key = line.split("=")[0].strip();
132-
string val = line.split("=")[1].strip();
133-
134-
switch (key){
135-
case "ID":
136-
info.dist_id = val;
137-
break;
138-
case "VERSION_ID":
139-
info.release = val;
140-
break;
141-
//case "DISTRIB_CODENAME":
142-
//info.codename = val;
143-
//break;
144-
case "PRETTY_NAME":
145-
info.description = val;
146-
break;
147-
}
148-
}
120+
// remove trailing "
121+
if (val.has_suffix("\"")) {
122+
val = val[0:val.length-1];
149123
}
150-
}
151124

125+
switch (key) {
126+
case "ID":
127+
case "DISTRIB_ID":
128+
info.dist_id = val;
129+
break;
130+
case "VERSION_ID":
131+
case "DISTRIB_RELEASE":
132+
info.release = val;
133+
break;
134+
case "VERSION_CODENAME":
135+
case "DISTRIB_CODENAME":
136+
info.codename = val;
137+
break;
138+
case "PRETTY_NAME":
139+
case "DISTRIB_DESCRIPTION":
140+
info.description = val;
141+
break;
142+
}
143+
}
152144
return info;
153145
}
154146

155147
public string dist_type {
156148

157149
owned get{
158150

159-
if (dist_id in "fedora rhel rocky centos almalinux"){
151+
if (dist_id.down() in "fedora rhel rocky centos almalinux"){
160152
return "redhat";
161153
}
162154
else if (dist_id.down().contains("manjaro") || dist_id.down().contains("arch")){

0 commit comments

Comments
 (0)