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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
|
"""
# Copyright 1999-2005 Gentoo Foundation
# This source code is distributed under the terms of version 2 of the GNU
# General Public License as published by the Free Software Foundation, a copy
# of which can be found in the main directory of this project.
Gentoo Linux Installer
The GLIUtility module contians all utility functions used throughout GLI.
"""
import string, os, re, shutil, sys, random, commands, crypt, pty, select
from GLIException import *
##
# Check to see if a string is actually a string, and if it is not null. Returns bool.
# @param string_a string to be checked.
def is_realstring(string):
# Make sure it is a string
if not isinstance(string, (str, unicode)):
return False
return True
##
# Checks to see if x is a numeral by doing a type conversion.
# @param x value to be checked
def is_numeric(x):
try:
float(x)
except ValueError:
return False
else:
return True
##
# Check to see if a string is a valid ip. Returns bool.
# @param ip ip to be checked.
def is_ip(ip):
# Make sure it is a string
if not is_realstring(ip):
return False
# Compile the regular expression that validates an IP. It will also check for valid ranges.
expr = re.compile('(([0-9]|[01]?[0-9]{2}|2([0-4][0-9]|5[0-5]))\.){3}([0-9]|[01]?[0-9]{2}|2([0-4][0-9]|5[0-5]))$')
# Run the test.
res = expr.match(ip)
# Return True only if there are results.
return(res != None)
##
# Check to see if mac is a valid MAC address. Make sure use format_mac
# before using this function. Returns bool.
# @param mac mac address to be checked.
def is_mac(mac):
expr = re.compile('([0-9A-F]{2}:){5}[0-9A-F]{2}')
res = expr.match(mac)
return(res != None)
##
# Format's a mac address properly. Returns the correctly formatted MAC. (a string)
# @param mac mac address to be formatted
def format_mac(mac):
mac = string.replace(mac, '-', ':')
mac = string.upper(mac)
mac = string.split(mac, ':')
for i in range(0, len(mac)):
if len(mac[i]) < 2:
mac[i] = "0" + mac[i]
return string.join(mac, ":")
##
# Removes leading zero's from an IP address. For example
# trim_ip('192.168.01.002') => '192.168.1.2'
# @param ip IP address to be trimmed
def trim_ip(ip):
# Remove leading zero's on the first octet
ip = re.sub('^0{1,2}','',ip)
# Remove leading zero's from the other octets
res = re.sub('((?<=\.)(00|0)(?P<num>\d))','\g<num>',ip)
return(res)
##
# Check to see if the string passed is a valid device. Returns bool.
# @param device device to be checked
def is_device(device):
# Make sure it is a string
if not is_realstring(device):
return False
# Make sure the string starts with /dev/
if device[0:5] != '/dev/':
return False
# Check to make sure the device exists
return os.access(device, os.F_OK)
##
# Check to see if the string is a valid hostname. Returns bool.
# @param hostname host to be checked
def is_hostname(hostname):
# Make sure it is a string
if not is_realstring(hostname):
return False
expr = re.compile('^([a-zA-Z0-9-_\.])+\.[a-zA-Z]{2,4}$')
res = expr.match(hostname)
return(res != None)
##
# Check to see if the string is a valid path. Returns bool.
# @param path Path to be checked.
def is_path(path):
# Make sure it is a string
if not is_realstring(path):
return False
# Create a regular expression that matches all words and the symbols '-_./' _ is included in the \w
expr = re.compile('^[\w\.\-\/~]+$')
# Run the match
res = expr.match(path)
# Return True only if there are results
return(res != None)
##
# Check to see if the string is a valid file. Returns bool.
# @param file file to be checked for validity.
def is_file(file):
# Make sure it is a string
if not is_realstring(file):
return False
# Check to make sure the device exists
return os.access(file, os.F_OK)
##
# Parse a URI. Returns a tuple (protocol, username, password, host, port, path)
# Returns None if URI is invalid.
# @param uri URI to be parsed
def parse_uri(uri):
# Compile the regex
expr = re.compile('(\w+)://(?:([^:@]+)(?::([^@]+))?@)?(?:([a-zA-Z0-9.-]+)(?::(\d+))?)?(/.*)')
# Run it against the URI
res = expr.match(uri)
if not res:
# URI doesn't match regex and therefore is invalid
return None
# Get tuple of matches
# 0 - Protocol
# 1 - Username
# 2 - Password
# 3 - Host
# 4 - Port
# 5 - Path
uriparts = res.groups()
return uriparts
##
# Check to see if the string is a valid URI. Returns bool.
# @param uri URI to be validated
# @param checklocal=True Whether to look for a local uri.
def is_uri(uri, checklocal=True):
# Make sure it is a string
if not is_realstring(uri):
return False
# Set the valid uri types
valid_uri_types = ('ftp', 'rsync', 'http', 'file', 'https', 'scp')
# Parse the URI
uriparts = parse_uri(uri)
if not uriparts:
# Invalid URI
return False
# Check for valid uri type
if not uriparts[0] in valid_uri_types:
return False
# If checklocal and the URI is a local file, check to see if the file exists
if uriparts[0] == "file" and checklocal:
if not is_file(uriparts[5]):
return False
return True
##
# Converts a string to a boolean value. anything not "True" is deemed false.
# @param input must be a string so it can be converted to boolean.
def strtobool(input):
if type(input) != str:
raise GLIException("GLIUtilityError", 'fatal','strtobool',"The input must be a string!")
if string.lower(input) == 'true':
return True
else:
return False
##
# Check to see if device is a valid ethernet device. Returns bool.
# @param device device to be checked
def is_eth_device(device):
# Make sure it is a string
if not is_realstring(device):
return False
# Old way w/ reg ex here:
# Create a regular expression to test the specified device.
#expr = re.compile('^(eth|wlan|ppp)([0-9]{1,2})(:[0-9]{1,2})?$')
# Run the match
#res = expr.match(device)
# Return True only if there are results
#return(res != None)
status, output = spawn("/sbin/ifconfig -a | grep -e '^[A-Za-z]'| cut -d ' ' -f 1 | grep '"+ device + "'", return_output=True)
if output:
return True
return False
##
# Will return a list of devices found in ifconfig.
def get_eth_devices():
status, output = spawn("/sbin/ifconfig -a | grep -e '^[A-Za-z]'| cut -d ' ' -f 1", return_output=True)
return output.split()
##
# Checks to see if device is a valid NFS device
# @param device device to be checked
def is_nfs(device):
if not is_realstring(device):
return False
colon_location = device.find(':')
if colon_location == -1:
return False
host = device[:colon_location]
path = device[colon_location+1:]
return((is_ip(host) or is_hostname(host)) and is_path(path))
##
# Sets the network ip (used for the livecd environment)
# @param dev device to be configured
# @param ip ip address of device
# @param broadcast broadcast address of device
# @param netmask netmask address of device
def set_ip(dev, ip, broadcast, netmask):
if not is_ip(ip) or not is_ip(netmask) or not is_ip(broadcast):
raise GLIException("GLIUtilityError", 'fatal','set_ip', ip + ", " + netmask + "and, " + broadcast + "must be a valid IP's!")
if not is_eth_device(dev):
raise GLIException("GLIUtilityError", 'fatal','set_ip', dev + "is not a valid ethernet device!")
options = "%s inet %s broadcast %s netmask %s" % (dev, ip, broadcast, netmask)
status = spawn("ifconfig " + options)
if not exitsuccess(status):
return False
return True
##
# Sets the default route (used for the livecd environment)
# @param route ip addresss of gateway.
def set_default_route(route):
if not is_ip(route):
raise GLIException("GLIUtilityError", 'fatal', 'set_default_route', route + " is not an ip address!")
status = spawn("route add default gw " + route)
if not exitsuccess(status):
return False
return True
##
# Will run a command with various flags for the style of output and logging.
#
# @param cmd The command to be run
# @param quiet=False Whether or not to filter output to /dev/null
# @param logfile=None if provied will log output to the given filename
# @param display_on_tty8=False will output to tty8 instead of the screen.
# @param chroot=None will run the command inside the new chroot env.
# @param append_log=False whether to start over on the logfile or append.
# @param return_output=False Returns the output along with the exit status
def spawn(cmd, quiet=False, logfile=None, display_on_tty8=False, chroot=None, append_log=False, return_output=False, linecount=0, match=None, cc=None, status_message=None):
# This is a hack since spawn() can't access _logger...set to True for verbose output on console
debug = False
if chroot:
wrapper = open(chroot+"/var/tmp/spawn.sh", "w")
wrapper.write("#!/bin/bash -l\n" + cmd + "\nexit $?\n")
wrapper.close()
cmd = "chmod a+x " + chroot + "/var/tmp/spawn.sh && chroot " + chroot + " /var/tmp/spawn.sh 2>&1"
else:
cmd += " 2>&1 "
if debug:
print "Command: " + cmd
output = ""
if logfile:
if append_log:
fd_logfile = open(logfile,'a')
else:
fd_logfile = open(logfile,'w')
if display_on_tty8:
fd_tty = open('/dev/tty8','w')
# Set initial sub-progress display
if cc:
cc.addNotification("progress", (0, status_message))
# open a read only pipe
ro_pipe = os.popen(cmd, 'r')
# read a line from the pipe and loop until
# pipe is empty
# data = ro_pipe.readline()
seenlines = 0
last_percent = 0
while 1:
# data = ro_pipe.read(16384)
data = os.read(ro_pipe.fileno(), 16384)
if debug:
print "DEBUG: read some data...length " + str(len(data))
if not data:
if linecount and cc:
if debug:
print "DEBUG: end of stream...progress is 1"
cc.addNotification("progress", (1, status_message))
break
# print "DEBUG: spawn(): data is " + str(len(data)) + " bytes long"
if logfile:
fd_logfile.write(data)
# fd_logfile.flush()
if display_on_tty8:
fd_tty.write(data)
fd_tty.flush()
if return_output:
output += data
if linecount and cc:
lastpos = -1
while 1:
lastpos = data.find("\n", lastpos + 1)
if lastpos == -1: break
# if match:
# if not re.match(match, uri):
# continue
seenlines += 1
if debug:
print "DEBUG: seenlines=" + str(seenlines)
percent = float(seenlines) / linecount
if debug:
print "DEBUG: percent=" + str(percent)
# print "DEBUG: spawn(): seenlines=" + str(seenlines) + ", linecount=" + str(linecount) + ", percent=" + str(percent)
if int(percent * 100) >= (last_percent + 5):
last_percent = int(percent * 100)
if debug:
print "DEBUG: setting next progress point...last_percent=" + str(last_percent)
cc.addNotification("progress", (percent, status_message))
# print "DEBUG: spawn(): send notification " + str((percent, status_message))
# data = ro_pipe.readline()
# close the file descriptors
if logfile: fd_logfile.close()
if display_on_tty8: fd_tty.close()
# close the pipe and save return value
ret = ro_pipe.close() or 0
if return_output:
return ret, output
else:
return ret
##
# Will check the status of a spawn result to see if it did indeed return successfully.
# @param status Parameter description
def exitsuccess(status):
if os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0:
return True
return False
##
# Will produce a bash shell with a special prompt for the installer.
def spawn_bash():
os.putenv("PROMPT_COMMAND","echo \"Type 'exit' to return to the installer.\"")
status = spawn("bash")
return status
##
# Will download or copy a file/uri to a location
# @param uri uri to be fetched.
# @param path destination for the file.
def get_uri(uri, path, cc=None):
uri = uri.strip()
status = 0
if re.match('^(ftp|http(s)?)://',uri):
if cc:
status = spawn("wget --progress=dot " + uri + " -O " + path + r""" 2>&1 | sed -u -e 's:^.\+\([0-9]\+\)%.\+$:\1:' | while read line; do [ "$line" = "$tmp_lastline" ] || echo $line | grep -e '^[1-9]'; tmp_lastline=$line; done""", linecount=100, cc=cc, status_message="Fetching " + uri.split('/')[-1])
else:
status = spawn("wget --quiet " + uri + " -O " + path)
elif re.match('^rsync://', uri):
status = spawn("rsync --quiet " + uri + " " + path)
elif uri.startswith("scp://"):
# Get tuple of matches
# 0 - Protocol
# 1 - Username
# 2 - Password
# 3 - Host
# 4 - Port
# 5 - Path
uriparts = parse_uri(uri)
scpcmd = "scp "
if uriparts[4]:
scpcmd += "-P " + uriparts[4] + " "
if uriparts[1]:
scpcmd += uriparts[1] + "@"
scpcmd += uriparts[3] + ":" + uriparts[5] + " " + path
pid, child_fd = pty.fork()
if not pid:
os.execvp("scp", scpcmd.split())
else:
while 1:
r, w, e = select.select([child_fd], [], [])
if child_fd in r:
try:
data = os.read(child_fd, 1024)
except:
pid2, status = os.waitpid(pid, 0)
break
if data.endswith("assword: "):
if uriparts[2]:
os.write(child_fd, uriparts[2] + "\n")
else:
os.write(child_fd, "\n")
elif data.endswith("Are you sure you want to continue connecting (yes/no)? "):
os.write(child_fd, "yes\n")
else:
pid2, status = os.waitpid(pid, os.WNOHANG)
if pid2:
break
elif re.match('^file://', uri):
r_file = uri[7:]
if os.path.isfile(r_file):
shutil.copy(r_file, path)
if not os.path.isfile(path):
raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "Cannot copy " + r_file + " to " + path)
else:
# Just in case a person forgets file://
if os.path.isfile(uri):
shutil.copy(uri, path)
if not os.path.isfile(path):
raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "Cannot copy " + r_file + " to " + path)
else:
raise GLIException("GLIUtilityError", 'fatal', 'get_uri', "File does not exist or URI is invalid!")
if exitsuccess(status) and is_file(path):
return True
return False
##
# Pings a host. Used to test network connectivity.
# @param host host to be pinged.
def ping(host):
host = str(host)
if not (is_hostname(host) or is_ip(host)):
return False #invalid IP or hostname
status = spawn("ping -n -c 2 " + host)
if not exitsuccess(status):
return False
return True
##
# Pass in the eth device's number (0, 1, 2, etc).
# Returns network information in a tuple.
# Order is hw_addr, ip_addr, mask, bcast, route, and
# whether it's up (True or False).
# @param device device to gather info from.
def get_eth_info(device):
"""Pass in the eth device's number (0, 1, 2, etc).
Returns network information in a tuple.
Order is hw_addr, ip_addr, mask, bcast, route, and
whether it's up (True or False).
"""
hw_addr = 'None'
ip_addr = 'None'
mask = 'None'
bcast = 'None'
gw = 'None'
up = False
if len(str(device)) == 1:
device = "eth" + str(device)
if not is_eth_device(device):
raise GLIException("GLIUtilityError", 'fatal', "get_eth_info", device + " is not a valid ethernet device!")
status, device_info = spawn("/sbin/ifconfig " + device, return_output=True)
if exitsuccess(status):
for line in device_info.splitlines():
line = line.strip()
if 'HWaddr' in line:
hw_addr = line.split('HWaddr',1)[1].strip()
if 'inet addr' in line:
ip_addr = line.split(' ')[0].split(':')[1]
if 'Bcast' in line:
bcast = line.split(' ')[1].split(':')[1]
if 'Mask' in line:
mask = line.split(' ')[2].split(':')[1]
if line.startswith('UP'):
up = True
else:
raise GLIException("GLIUtilityError", 'fatal', "get_eth_info", device_info)
gw = spawn(r"/sbin/route -n | grep -e '^0\.0\.0\.0' | sed -e 's:^0\.0\.0\.0 \+::' -e 's: \+.\+$::'", return_output=True)[1].strip()
return (hw_addr, ip_addr, mask, bcast, gw, up)
##
# Will take a uri and get and unpack a tarball into the destination.
# @param tarball_uri URI of tarball
# @param target_directory destination
# @param temp_directory="/tmp" a temporary location (used for dealing with the
# ramdisk size limitations of the livecd env.
# @param keep_permissions=False Whether or not to keep permissions (-p)
def fetch_and_unpack_tarball(tarball_uri, target_directory, temp_directory="/tmp", keep_permissions=False, cc=None):
"Fetches a tarball from tarball_uri and extracts it into target_directory"
# Get tarball info
tarball_filename = tarball_uri.split("/")[-1]
# Get the tarball
if not get_uri(tarball_uri, temp_directory + "/" + tarball_filename, cc):
raise GLIException("GLIUtilityError", 'fatal', 'fetch_and_unpack_tarball',"Could not fetch " + tarball_uri)
# Reset tar options
tar_options = "xv"
# If the tarball is bzip'd
if tarball_filename.split(".")[-1] == "tbz" or tarball_filename.split(".")[-1] == "bz2":
format_option = "j"
# If the tarball is gzip'd
elif tarball_filename.split(".")[-1] == "tgz" or tarball_filename.split(".")[-1] == "gz":
format_option = "z"
tar_options += format_option
# If we want to keep permissions
if keep_permissions:
tar_options = tar_options + "p"
# Get number of files in tarball
tarfiles = 0
if cc:
cc.addNotification("progress", (0, "Determining the number of files in " + tarball_filename))
tarfiles = int(spawn("tar -t" + format_option + "f " + temp_directory + "/" + tarball_filename + " 2>/dev/null | wc -l", return_output=True)[1].strip())
# Unpack the tarball
exitstatus = spawn("tar -" + tar_options + " -f " + temp_directory + "/" + tarball_filename + " -C " + target_directory, display_on_tty8=True, logfile="/tmp/compile_output.log", append_log=True, linecount=tarfiles, cc=cc, status_message="Unpacking " + tarball_filename) # change this to the logfile variable
if not exitsuccess(exitstatus):
raise GLIException("GLIUtilityError", 'fatal', 'fetch_and_unpack_tarball',"Could not unpack " + tarball_uri + " to " + target_directory)
##
# OLD Will generate a random password. Used when the livecd didn't auto-scramble the root password.
# can probably be removed but is good to keep around.
def generate_random_password():
s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$%^&*[]{}-=+_,|'\"<>:/"
s = list(s)
for i in range(0,len(s)/2):
x = random.randint(0,len(s)-1)
y = random.randint(0,len(s)-1)
tmp = s[x]
s[x] = s[y]
s[y] = tmp
passwd = ""
for i in range(0,random.randint(8,12)):
passwd += s[i]
return passwd
##
# Will grab a value from a specified file after sourcing it
# @param filename file to get the value from
# @param value value to look for
def get_value_from_config(filename, value):
#OLD WAY: return string.strip(commands.getoutput("source " + filename + " && echo $" + value))
status, output = spawn("source " + filename + " && echo $" + value, return_output=True)
return string.strip(output)
##
# Will take a password and return it hashed in md5 format
# @param password the password to be hashed
def hash_password(password):
salt = "$1$"
chars = "./abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for i in range(0, 8):
salt += chars[random.randint(0, len(chars)-1)]
salt += "$"
passwd_hash = crypt.crypt(password, salt)
return passwd_hash
##
# Returns the real name (manufacturer and model) of a network interface
# @param interface Name of interface (like in ifconfig)
def get_interface_realname(interface):
# TODO: rewrite with 2.4 support
if is_file("/sys/class/net/" + interface + "/device"):
return spawn("lspci | grep $(basename $(readlink /sys/class/net/" + interface + r"/device)) | sed -e 's|^.\+ Ethernet controller: ||'", return_output=True)[1].strip()
else:
return "No Information Found"
def list_stage_tarballs_from_mirror(mirror, arch, subarch):
return spawn("wget -O - " + mirror + "/releases/" + arch + "/current/stages/" + subarch + r"/ 2> /dev/null | grep 'bz2\"' | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'", return_output=True)[1].strip().split("\n")
def list_subarch_from_mirror(mirror, arch):
return spawn("wget -O - " + mirror + "/releases/" + arch + r"/current/stages/ 2> /dev/null | grep folder.gif | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'", return_output=True)[1].strip().split("\n")
def list_mirrors(http=True, ftp=True, rsync=True):
mirrors = []
mirrortypes = ""
if http:
mirrortypes += "http"
if ftp:
if mirrortypes:
mirrortypes += '\|'
mirrortypes += "ftp"
if rsync:
if mirrortypes:
mirrortypes += '\|'
mirrortypes += "rsync"
mirrorlist = spawn(r"wget -O - 'http://www.gentoo.org/main/en/mirrors.xml?passthru=1' 2>/dev/null | /bin/sed -ne '/^[[:space:]]\+<uri link=\"\(" + mirrortypes + r"\):\/\/[^\"]\+\">/{s/^[[:space:]]\+<uri link=\"\([^\"]\+\)\">\(.*\)<\/uri>.*$/\1|\2/;p}'", return_output=True)[1].strip().split("\n")
for mirror in mirrorlist:
mirror = mirror.strip()
mirrors.append(mirror.split("|"))
return mirrors
def generate_keymap_list():
keymap_list = []
path = "/usr/share/keymaps"
# find /usr/share/keymaps -iname *.map.gz -printf "%f \n"
put, get = os.popen4("find "+path+" -iname *.map.gz -printf \"%f \n\"")
for keymap in get.readlines():
# strip the last 9 chars ( .map.gz\n )
keymap.strip()
keymap = keymap[:-9]
keymap_list.append(keymap)
# sort the keymap list
keymap_list.sort()
return keymap_list
def generate_consolefont_list():
consolefont_list=[]
path = "/usr/share/consolefonts"
# find /usr/share/consolefonts -iname *.gz -printf "%f \n"
put, get = os.popen4("find "+path+" -iname *.gz -printf \"%f \n\"")
for consolefont in get.readlines():
# strip the last 5 chars ( .gz\n )
consolefont.strip()
consolefont = consolefont[:-5]
# test if its psfu or psf or fnt
# and remove it if necessary
if consolefont[-4:]== "psfu":
consolefont = consolefont[:-5]
if consolefont[-3:]== "psf":
consolefont = consolefont[:-4]
if consolefont[-3:]=="fnt":
consolefont = consolefont[:-4]
consolefont_list.append(consolefont)
# sort the keymap list
consolefont_list.sort()
return consolefont_list
def generate_consoletranslation_list():
consoletranslation_list=[]
path = "/usr/share/consoletrans"
# find /usr/share/keymaps -iname *.trans -printf "%f \n"
put, get = os.popen4("find "+path+" -iname *.trans -printf \"%f \n\"")
for consoletran in get.readlines():
# strip the last 8 chars ( .trans\n )
consoletran.strip()
consoletran = consoletran[:-8]
consoletranslation_list.append(consoletran)
consoletranslation_list.sort()
return consoletranslation_list
def get_global_use_flags():
use_desc = {}
f = open("/usr/portage/profiles/use.desc", "r")
for line in f:
line = line.strip()
if line == "# The following flags are NOT to be set or unset by users":
break
if not line or line.startswith("#"): continue
dash_pos = line.find(" - ")
if dash_pos == -1: continue
flagname = line[:dash_pos] or line[dash_pos-1]
desc = line[dash_pos+3:]
use_desc[flagname] = desc
f.close()
return use_desc
def get_local_use_flags():
use_local_desc = {}
f = open("/usr/portage/profiles/use.local.desc", "r")
for line in f:
line = line.strip()
if not line or line.startswith("#"): continue
dash_pos = line.find(" - ")
if dash_pos == -1: continue
colon_pos = line.find(":", 0, dash_pos)
pkg = line[:colon_pos]
flagname = line[colon_pos+1:dash_pos] or line[colon_pos+1]
desc = "(" + pkg + ") " + line[dash_pos+3:]
use_local_desc[flagname] = desc
f.close()
return use_local_desc
def get_cd_snapshot_uri():
snapshot_loc = spawn("ls /mnt/{cdrom,livecd}/snapshots/portage-* 2>/dev/null | head -n 1", return_output=True)[1].strip()
if snapshot_loc:
snapshot_loc = "file://" + snapshot_loc
return snapshot_loc
def validate_uri(uri):
# Get tuple of matches
# 0 - Protocol
# 1 - Username
# 2 - Password
# 3 - Host
# 4 - Port
# 5 - Path
uriparts = parse_uri(uri)
if not uriparts:
return False
if uriparts[0] in ('http', 'https', 'ftp'):
ret = spawn("wget --spider " + uri)
return exitsuccess(ret)
elif uriparts[0] == "file":
return is_file(uriparts[5])
return True
def get_directory_listing_from_uri(uri):
uriparts = parse_uri(uri)
if not uriparts:
return []
if uriparts[0] == "file":
dirlist = os.listdir(uriparts[5])
dirlist.sort()
dirs = []
files = []
for entry in dirlist:
if os.path.isdir(uriparts[5] + entry):
dirs.append(entry + "/")
else:
files.append(entry)
if not uriparts[5] == "/":
dirlist = ["../"]
else:
dirlist = []
dirlist += dirs + files
elif uriparts[0] == "http":
dirlist = spawn("wget -O - http://" + uriparts[3] + uriparts[5] + r" 2> /dev/null | grep -i href | grep -v 'http://' | grep -v 'ftp://' | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i'", return_output=True)[1].strip().split("\n")
dirs = []
files = []
for entry in dirlist:
if not entry.startswith("/") and entry.find("?") == -1:
if entry.endswith("/"):
dirs.append(entry)
else:
files.append(entry)
dirs.sort()
files.sort()
if not uriparts[5] == "/":
dirlist = ["../"]
else:
dirlist = []
dirlist += dirs + files
elif uriparts[0] == "ftp":
dirlist = spawn("wget -O - ftp://" + uriparts[3] + uriparts[5] + r" 2> /dev/null | grep -i href | sed -e 's:^.\+href=\"\(.\+\)\".\+$:\1:i' -e 's|^ftp://[^/]\+/|/|'", return_output=True)[1].strip().split("\n")
dirs = []
files = []
for entry in dirlist:
if entry.startswith(uriparts[5]):
entry = entry[len(uriparts[5]):]
if entry.endswith("/"):
dirs.append(entry)
else:
files.append(entry)
dirs.sort()
files.sort()
if not uriparts[5] == "/":
dirlist = ["../"]
else:
dirlist = []
dirlist += dirs + files
elif uriparts[0] == "scp":
tmpdirlist = ""
dirlist = []
sshcmd = ["ssh"]
if uriparts[4]:
sshcmd.append("-p")
sshcmd.append(uriparts[4])
if uriparts[1]:
sshcmd.append(uriparts[1] + "@" + uriparts[3])
else:
sshcmd.append(uriparts[3])
sshcmd.append("ls --color=no -1F " + uriparts[5] + r" 2>/dev/null | sed -e 's:\*$::'")
# print str(sshcmd)
pid, child_fd = pty.fork()
if not pid:
os.execvp("ssh", sshcmd)
else:
got_password_prompt = False
while 1:
r, w, e = select.select([child_fd], [], [])
if child_fd in r:
try:
data = os.read(child_fd, 1024)
except:
pid2, status = os.waitpid(pid, 0)
break
if data.endswith("assword: "):
if uriparts[2]:
os.write(child_fd, uriparts[2] + "\n")
else:
os.write(child_fd, "\n")
got_password_prompt = True
elif data.endswith("Are you sure you want to continue connecting (yes/no)? "):
os.write(child_fd, "yes\n")
else:
if got_password_prompt:
if not tmpdirlist and data.endswith("assword: "):
raise GLIException("IncorrectPassword", "notice", "get_directory_listing_from_uri", "Your SSH password was incorrect")
tmpdirlist += data
else:
pid2, status = os.waitpid(pid, os.WNOHANG)
if pid2:
break
for tmpentry in tmpdirlist.strip().split("\n"):
dirlist.append(tmpentry.strip())
dirs = []
files = []
for entry in dirlist:
if entry.endswith("/"):
dirs.append(entry)
else:
files.append(entry)
dirs.sort()
files.sort()
if not uriparts[5] == "/":
dirlist = ["../"]
else:
dirlist = []
dirlist += dirs + files
else:
dirlist = ["this/", "type/", "isn't/", "supported", "yet"]
return dirlist
def cdata(text):
if text.startswith("<![CDATA["):
return text
else:
return "<![CDATA[\n" + text + "\n]]>"
def uncdata(text):
if text.startswith("<![CDATA["):
return text[9:-5]
else:
return text
def get_grp_pkgs_from_cd():
if not is_file("/usr/livecd/grppkgs.txt"):
return ""
#raise GLIException("GLIUtilityError", "fatal", "get_grp_pkgs_from_cd", "Required file /usr/livecd/grppkgs.txt does not exist")
status,output = spawn('cat /usr/livecd/grppkgs.txt',return_output=True)
output = output.split()
#remove the first part before a / for comparision
results = []
for pkg in output:
results.append(pkg[(pkg.find('/')+1):])
return results
def get_keymaps(self):
return GLIUtility.spawn(r"find /usr/share/keymaps -iname *.map.gz | sed -e 's:^.\+/::' -e 's:\..\+$::' | sort", return_output=True)[1].strip().split("\n")
def get_chosts(arch):
chosts = []
if arch == "x86":
chosts = ["i386-pc-linux-gnu", "i486-pc-linux-gnu", "i586-pc-linux-gnu", "i686-pc-linux-gnu"]
if arch == "amd64":
chosts = ["x86_64-pc-linux-gnu"]
if arch == "alpha":
chosts = ["alpha-unknown-linux-gnu"]
if arch == "ppc":
chosts = ["powerpc-unknown-linux-gnu"]
if arch == "ppc64":
chosts = ["powerpc64-unknown-linux-gnu"]
if arch in ["sparc", "sparc64"]:
chosts = ["sparc-unknown-linux-gnu"]
if arch == "hppa":
chosts = ["hppa-unknown-linux-gnu", "hppa1.1-unknown-linux-gnu", "hppa2.0-unknown-linux-gnu"]
if arch == "mips":
chosts = ["mips-unknown-linux-gnu"]
return chosts
|