Package ClusterShell :: Package CLI :: Module Utils
[hide private]
[frames] | no frames]

Source Code for Module ClusterShell.CLI.Utils

 1  #!/usr/bin/env python 
 2  # 
 3  # Copyright (C) 2010-2015 CEA/DAM 
 4  # 
 5  # This file is part of ClusterShell. 
 6  # 
 7  # ClusterShell is free software; you can redistribute it and/or 
 8  # modify it under the terms of the GNU Lesser General Public 
 9  # License as published by the Free Software Foundation; either 
10  # version 2.1 of the License, or (at your option) any later version. 
11  # 
12  # ClusterShell is distributed in the hope that it will be useful, 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
15  # Lesser General Public License for more details. 
16  # 
17  # You should have received a copy of the GNU Lesser General Public 
18  # License along with ClusterShell; if not, write to the Free Software 
19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
20   
21  """ 
22  CLI utility functions 
23  """ 
24   
25  import sys 
26   
27  # CLI modules might safely import the NodeSet class from here. 
28  from ClusterShell.NodeUtils import GroupResolverConfigError 
29  try: 
30      from ClusterShell.NodeSet import NodeSet 
31  except GroupResolverConfigError, exc: 
32      print >> sys.stderr, \ 
33          "ERROR: ClusterShell node groups configuration error:\n\t%s" % exc 
34      sys.exit(1) 
35   
36  (KIBI, MEBI, GIBI, TEBI) = (1024.0, 1024.0 ** 2, 1024.0 ** 3, 1024.0 ** 4) 
37   
38 -def human_bi_bytes_unit(value):
39 """ 40 Format numerical `value` to display it using human readable unit with 41 binary prefix like (KiB, MiB, GiB, ...). 42 """ 43 if value >= TEBI: 44 fmt = "%.2f TiB" % (value / TEBI) 45 elif value >= GIBI: 46 fmt = "%.2f GiB" % (value / GIBI) 47 elif value >= MEBI: 48 fmt = "%.2f MiB" % (value / MEBI) 49 elif value >= KIBI: 50 fmt = "%.2f KiB" % (value / KIBI) 51 else: 52 fmt = "%d B" % value 53 return fmt
54
55 -def nodeset_cmp(ns1, ns2):
56 """Compare 2 nodesets by their length (we want larger nodeset 57 first) and then by first node.""" 58 len_cmp = cmp(len(ns2), len(ns1)) 59 if not len_cmp: 60 smaller = NodeSet.fromlist([ns1[0], ns2[0]])[0] 61 if smaller == ns1[0]: 62 return -1 63 else: 64 return 1 65 return len_cmp
66
67 -def bufnodeset_cmp(bn1, bn2):
68 """Convenience function to compare 2 (buf, nodeset) tuples by their 69 nodeset length (we want larger nodeset first) and then by first 70 node.""" 71 # Extract nodesets and call nodeset_cmp 72 return nodeset_cmp(bn1[1], bn2[1])
73