00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import os
00036 import sys
00037 import platform
00038 import subprocess
00039
00040 import roslib.rosenv
00041 from roslib.scriptutil import ask_and_call
00042
00043 import roslib.exceptions
00044 class ROSCleanException(roslib.exceptions.ROSLibException): pass
00045
00046 def _usage():
00047 print """Usage: rosclean <command>
00048
00049 Commands:
00050 \trosclean check\tCheck usage of log files
00051 \trosclean purge\tRemove log files
00052 """
00053 sys.exit(os.EX_USAGE)
00054
00055 def _get_check_dirs():
00056 home_dir = roslib.rosenv.get_ros_home()
00057 log_dir = roslib.rosenv.get_log_dir()
00058 dirs = [ (log_dir, 'ROS node logs'),
00059 (os.path.join(home_dir, 'rosmake'), 'rosmake logs')]
00060 return [x for x in dirs if os.path.isdir(x[0])]
00061
00062 def _rosclean_cmd_check(argv):
00063 dirs = _get_check_dirs()
00064 for d, label in dirs:
00065 desc = get_human_readable_disk_usage(d)
00066 print "%s %s"%(desc, label)
00067
00068 def get_human_readable_disk_usage(d):
00069 """
00070 Get human-readable disk usage for directory
00071 @param d: directory path
00072 @type d: str
00073 @return: human-readable disk usage (du -h)
00074 @rtype: str
00075 """
00076
00077 if platform.system() in ['Linux', 'FreeBSD']:
00078 try:
00079 return subprocess.Popen(['du', '-sh', d], stdout=subprocess.PIPE).communicate()[0].split()[0]
00080 except:
00081 raise ROSCleanException("rosclean is not supported on this platform")
00082 else:
00083 raise ROSCleanException("rosclean is not supported on this platform")
00084
00085 def get_disk_usage(d):
00086 """
00087 Get disk usage in bytes for directory
00088 @param d: directory path
00089 @type d: str
00090 @return: disk usage in bytes (du -b) or (du -A) * 1024
00091 @rtype: int
00092 @raise ROSCleanException: if get_disk_usage() cannot be used on this platform
00093 """
00094
00095 if platform.system() == 'Linux':
00096 try:
00097 return int(subprocess.Popen(['du', '-sb', d], stdout=subprocess.PIPE).communicate()[0].split()[0])
00098 except:
00099 raise ROSCleanException("rosclean is not supported on this platform")
00100 elif platform.system() == 'FreeBSD':
00101 try:
00102 return int(subprocess.Popen(['du', '-sA', d], stdout=subprocess.PIPE).communicate()[0].split()[0]) * 1024
00103 except:
00104 raise ROSCleanException("rosclean is not supported on this platform")
00105 else:
00106 raise ROSCleanException("rosclean is not supported on this platform")
00107
00108 def _rosclean_cmd_purge(argv):
00109 dirs = _get_check_dirs()
00110
00111 for d, label in dirs:
00112 print "Purging %s.\nPLEASE BE CAREFUL TO VERIFY THE COMMAND BELOW!"%label
00113 cmds = [['rm', '-rf', d]]
00114 try:
00115 ask_and_call(cmds)
00116 except:
00117 print >> sys.stderr, "FAILED to execute command"
00118
00119 def rosclean_main(argv=None):
00120 if argv == None:
00121 argv = sys.argv
00122 if len(argv) < 2:
00123 _usage()
00124 command = argv[1]
00125 if command == 'check':
00126 _rosclean_cmd_check(argv)
00127 elif command == 'purge':
00128 _rosclean_cmd_purge(argv)
00129 else:
00130 _usage()
00131
00132 if __name__ == '__main__':
00133 rosclean_main()