1
0

FluxCommon.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ################################################################################
  2. # $Id: FluxCommon.pm 2241 2007-01-10 02:27:55Z b4rt $
  3. # $Date: 2007-01-09 20:27:55 -0600 (Tue, 09 Jan 2007) $
  4. # $Revision: 2241 $
  5. ################################################################################
  6. # #
  7. # LICENSE #
  8. # #
  9. # This program is free software; you can redistribute it and/or #
  10. # modify it under the terms of the GNU General Public License (GPL) #
  11. # as published by the Free Software Foundation; either version 2 #
  12. # of the License, or (at your option) any later version. #
  13. # #
  14. # This program is distributed in the hope that it will be useful, #
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  17. # GNU General Public License for more details. #
  18. # #
  19. # To read the license please visit http://www.gnu.org/copyleft/gpl.html #
  20. # #
  21. # #
  22. ################################################################################
  23. package FluxCommon;
  24. use Exporter;
  25. @ISA = ('Exporter');
  26. @EXPORT_OK = qw(
  27. getVersion
  28. transferIsRunning
  29. getTimeStamp
  30. niceTimeString
  31. );
  32. ################################################################################
  33. ################################################################################
  34. # fields #
  35. ################################################################################
  36. # version in a var
  37. my $VERSION = do {
  38. my @r = (q$Revision: 2241 $ =~ /\d+/g); sprintf "%d"."%02d" x $#r, @r };
  39. ################################################################################
  40. # subs #
  41. ################################################################################
  42. #------------------------------------------------------------------------------#
  43. # Sub: transferIsRunning #
  44. # Arguments: transfer #
  45. # Return: 0|1 #
  46. #------------------------------------------------------------------------------#
  47. sub transferIsRunning {
  48. my $name = shift;
  49. my $qstring = "ps x -o pid='' -o ppid='' -o command='' -ww 2> /dev/null";
  50. my $pcount = 0;
  51. foreach my $line (grep(/$name/, qx($qstring))) {
  52. $pcount++;
  53. }
  54. if ($pcount > 1) {
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. #------------------------------------------------------------------------------#
  60. # Sub: getTimeStamp #
  61. # Arguments: #
  62. # Return: string #
  63. #------------------------------------------------------------------------------#
  64. sub getTimeStamp {
  65. my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
  66. = localtime(time);
  67. return sprintf("[%4d/%02d/%02d - %02d:%02d:%02d]",
  68. $year + 1900, $mon + 1, $mday,
  69. $hour, $min, $sec
  70. );
  71. }
  72. #------------------------------------------------------------------------------#
  73. # Sub: niceTimeString #
  74. # Arguments: start-time #
  75. # Return: nice Time String #
  76. #------------------------------------------------------------------------------#
  77. sub niceTimeString {
  78. my $startTime = shift;
  79. my ($dura, $duration, $days, $hours, $mins, $secs, $rest);
  80. $dura = ((time) - $startTime);
  81. $rest = $dura;
  82. $days = $hours = $mins = $secs = 0;
  83. $duration = "";
  84. if ($dura >= (24 * 60 * 60)) { # days
  85. $days = int((($rest / 60) / 60) / 24);
  86. $duration .= $days."d ";
  87. $rest = ($dura - ($days * 60 * 60 * 24));
  88. }
  89. if ($dura >= (60 * 60)) { # hours
  90. $hours = int(($rest / 60) / 60);
  91. $duration .= $hours."h ";
  92. $rest = ($dura - ($hours * 60 * 60) - ($days * 60 * 60 * 24));
  93. }
  94. if ($rest >= 60) { # mins
  95. $mins = int($rest / 60);
  96. $duration .= $mins."m ";
  97. $rest = ($dura - ($mins * 60) - ($hours * 60 * 60) - ($days * 60 * 60 * 24));
  98. }
  99. if ($rest > 0) { # secs
  100. $duration .= $rest."s";
  101. }
  102. return $duration;
  103. }
  104. #------------------------------------------------------------------------------#
  105. # Sub: getVersion #
  106. # Arguments: null #
  107. # Returns: VERSION #
  108. #------------------------------------------------------------------------------#
  109. sub getVersion {
  110. return $VERSION;
  111. }
  112. ################################################################################
  113. # make perl happy #
  114. ################################################################################
  115. 1;