33@author: frank
44'''
55import os
6+ import os .path
67import socket
78import subprocess
89import datetime
@@ -76,6 +77,18 @@ def rm_file_force(fpath):
7677 except :
7778 pass
7879
80+ def process_exists (pid ):
81+ return os .path .exists ("/proc/" + str (pid ))
82+
83+ def kill_process (pid , sig ):
84+ try :
85+ os .kill (int (pid ), int (sig ))
86+ except :
87+ pass
88+
89+ def kill9_process (pid ):
90+ kill_process (pid , 9 )
91+
7992def cidr_to_netmask (cidr ):
8093 cidr = int (cidr )
8194 return socket .inet_ntoa (struct .pack (">I" , (0xffffffff << (32 - cidr )) & 0xffffffff ))
@@ -167,7 +180,7 @@ def get_used_disk_size(dir_path):
167180 return get_total_disk_size (dir_path ) - get_free_disk_size (dir_path )
168181
169182def get_used_disk_apparent_size (dir_path ):
170- output = shell .ShellCmd ('du --apparent-size --max-depth=1 %s | tail -1' % dir_path )( )
183+ output = shell .call ('du --apparent-size --max-depth=1 %s | tail -1' % dir_path )
171184 return long (output .split ()[0 ])
172185
173186def get_disk_capacity_by_df (dir_path ):
@@ -196,9 +209,7 @@ def is_mounted(path=None, url=None):
196209 else :
197210 raise Exception ('path and url cannot both be None' )
198211
199- cmd = shell .ShellCmd (cmdstr )
200- cmd (is_exception = False )
201- return cmd .return_code == 0
212+ return shell .run (cmdstr ) == 0
202213
203214def mount (url , path , options = None ):
204215 cmd = shell .ShellCmd ("mount | grep '%s'" % path )
@@ -270,7 +281,7 @@ def umount_by_url(url):
270281
271282
272283def get_file_size_by_http_head (url ):
273- output = shell .ShellCmd ('curl --head %s' % url )( )
284+ output = shell .call ('curl --head %s' % url )
274285 for l in output .split ('\n ' ):
275286 if 'Content-Length' in l :
276287 filesize = l .split (':' )[1 ].strip ()
@@ -291,7 +302,7 @@ def get_percentage(filesize, dst):
291302 return None
292303
293304 def get_file_size (url ):
294- output = shell .ShellCmd ('curl --head %s' % url )( )
305+ output = shell .call ('curl --head %s' % url )
295306 for l in output .split ('\n ' ):
296307 if 'Content-Length' in l :
297308 filesize = l .split (':' )[1 ].strip ()
@@ -351,7 +362,7 @@ def get_file_size(url):
351362 return 0
352363
353364def md5sum (file_path ):
354- return 'md5sum is not calculated, because too time cost'
365+ return 'md5sum is not calculated due to time cost'
355366
356367 #cmd = shell.ShellCmd('md5sum %s' % file_path)
357368 #cmd()
@@ -550,25 +561,25 @@ def get_img_fmt(src):
550561
551562def qcow2_clone (src , dst ):
552563 fmt = get_img_fmt (src )
553- shell .ShellCmd ('/usr/bin/qemu-img create -F %s -b %s -f qcow2 %s' % (fmt , src , dst ))( )
554- shell .ShellCmd ('chmod 666 %s' % dst )( )
564+ shell .check_run ('/usr/bin/qemu-img create -F %s -b %s -f qcow2 %s' % (fmt , src , dst ))
565+ shell .check_run ('chmod 666 %s' % dst )
555566
556567def raw_clone (src , dst ):
557- shell .ShellCmd ('/usr/bin/qemu-img create -b %s -f raw %s' % (src , dst ))( )
558- shell .ShellCmd ('chmod 666 %s' % dst )( )
568+ shell .check_run ('/usr/bin/qemu-img create -b %s -f raw %s' % (src , dst ))
569+ shell .check_run ('chmod 666 %s' % dst )
559570
560571def qcow2_create (dst , size ):
561- shell .ShellCmd ('/usr/bin/qemu-img create -f qcow2 %s %s' % (dst , size ))( )
562- shell .ShellCmd ('chmod 666 %s' % dst )( )
572+ shell .check_run ('/usr/bin/qemu-img create -f qcow2 %s %s' % (dst , size ))
573+ shell .check_run ('chmod 666 %s' % dst )
563574
564575def qcow2_create_with_backing_file (backing_file , dst ):
565576 fmt = get_img_fmt (backing_file )
566577 shell .call ('/usr/bin/qemu-img create -F %s -f qcow2 -b %s %s' % (fmt , backing_file , dst ))
567578 shell .call ('chmod 666 %s' % dst )
568579
569580def raw_create (dst , size ):
570- shell .ShellCmd ('/usr/bin/qemu-img create -f raw %s %s' % (dst , size ))( )
571- shell .ShellCmd ('chmod 666 %s' % dst )( )
581+ shell .check_run ('/usr/bin/qemu-img create -f raw %s %s' % (dst , size ))
582+ shell .check_run ('chmod 666 %s' % dst )
572583
573584def create_template (src , dst ):
574585 fmt = get_img_fmt (src )
@@ -692,10 +703,10 @@ def get_all_bridge_interface(bridge_name):
692703def delete_bridge (bridge_name ):
693704 vifs = get_all_bridge_interface (bridge_name )
694705 for vif in vifs :
695- shell .ShellCmd ("brctl delif %s %s" % (bridge_name , vif ))( )
706+ shell .check_run ("brctl delif %s %s" % (bridge_name , vif ))
696707
697- shell .ShellCmd ("ip link set %s down" % bridge_name )( )
698- shell .ShellCmd ("brctl delbr %s" % bridge_name )( )
708+ shell .check_run ("ip link set %s down" % bridge_name )
709+ shell .check_run ("brctl delbr %s" % bridge_name )
699710
700711def find_bridge_having_physical_interface (ifname ):
701712 output = shell .call ("brctl show|sed -n '2,$p'|cut -f 1,6" )
@@ -730,7 +741,7 @@ def check_ip_mask():
730741 return True
731742
732743 routes = []
733- out = shell .ShellCmd ('ip route' )( )
744+ out = shell .call ('ip route' )
734745 for line in out .split ('\n ' ):
735746 line .strip ()
736747 if line :
@@ -861,17 +872,17 @@ def get_process_up_time_in_second(pid):
861872
862873
863874def get_cpu_num ():
864- out = shell .ShellCmd ( "cat /proc/cpuinfo | grep 'processor' | wc -l" )( )
875+ out = shell .call ( "grep -c processor /proc/cpuinfo" )
865876 return int (out )
866877
867878@retry (times = 3 , sleep_time = 3 )
868879def get_cpu_speed ():
869880 max_freq = '/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq'
870881 if os .path .exists (max_freq ):
871- out = shell . ShellCmd ( 'cat %s' % max_freq )()
882+ out = file ( max_freq ). read ()
872883 return int (float (out ) / 1000 )
873884
874- cmd = shell .ShellCmd ("cat /proc/cpuinfo | grep 'cpu MHz' | tail -n 1" )
885+ cmd = shell .ShellCmd ("grep 'cpu MHz' /proc/cpuinfo | tail -n 1" )
875886 out = cmd (False )
876887 if cmd .return_code == - 11 :
877888 raise
0 commit comments