mirror of https://github.com/nicolargo/glances
Correct unitary test failed
parent
7dbe2ffd3d
commit
b3c009b22e
|
@ -42,7 +42,6 @@ except ImportError:
|
||||||
|
|
||||||
# Import Glances libs
|
# Import Glances libs
|
||||||
# Note: others Glances libs will be imported optionally
|
# Note: others Glances libs will be imported optionally
|
||||||
from glances.compat import PY3
|
|
||||||
from glances.logger import logger
|
from glances.logger import logger
|
||||||
from glances.main import GlancesMain
|
from glances.main import GlancesMain
|
||||||
from glances.timer import Counter
|
from glances.timer import Counter
|
||||||
|
@ -66,8 +65,7 @@ if psutil_version_info < psutil_min_version:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Trac malloc is only available on Python 3.4 or higher
|
# Trac malloc is only available on Python 3.4 or higher
|
||||||
if PY3:
|
import tracemalloc
|
||||||
import tracemalloc
|
|
||||||
|
|
||||||
|
|
||||||
def __signal_handler(signal, frame):
|
def __signal_handler(signal, frame):
|
||||||
|
|
|
@ -33,196 +33,111 @@ from datetime import datetime
|
||||||
|
|
||||||
from glances.logger import logger
|
from glances.logger import logger
|
||||||
|
|
||||||
PY3 = sys.version_info[0] == 3
|
import queue
|
||||||
|
from configparser import ConfigParser, NoOptionError, NoSectionError
|
||||||
|
from statistics import mean
|
||||||
|
from xmlrpc.client import Fault, ProtocolError, ServerProxy, Transport, Server
|
||||||
|
from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
|
||||||
|
from urllib.request import urlopen
|
||||||
|
from urllib.error import HTTPError, URLError
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
if PY3:
|
# Correct issue #1025 by monkey path the xmlrpc lib
|
||||||
import queue
|
from defusedxml.xmlrpc import monkey_patch
|
||||||
from configparser import ConfigParser, NoOptionError, NoSectionError
|
|
||||||
from statistics import mean
|
|
||||||
from xmlrpc.client import Fault, ProtocolError, ServerProxy, Transport, Server
|
|
||||||
from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
|
|
||||||
from urllib.request import urlopen
|
|
||||||
from urllib.error import HTTPError, URLError
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
# Correct issue #1025 by monkey path the xmlrpc lib
|
monkey_patch()
|
||||||
from defusedxml.xmlrpc import monkey_patch
|
|
||||||
|
|
||||||
monkey_patch()
|
input = input
|
||||||
|
range = range
|
||||||
|
map = map
|
||||||
|
|
||||||
input = input
|
text_type = str
|
||||||
range = range
|
binary_type = bytes
|
||||||
map = map
|
bool_type = bool
|
||||||
|
long = int
|
||||||
|
|
||||||
text_type = str
|
PermissionError = OSError
|
||||||
binary_type = bytes
|
|
||||||
bool_type = bool
|
|
||||||
long = int
|
|
||||||
|
|
||||||
PermissionError = OSError
|
viewkeys = operator.methodcaller('keys')
|
||||||
|
viewvalues = operator.methodcaller('values')
|
||||||
|
viewitems = operator.methodcaller('items')
|
||||||
|
|
||||||
viewkeys = operator.methodcaller('keys')
|
|
||||||
viewvalues = operator.methodcaller('values')
|
|
||||||
viewitems = operator.methodcaller('items')
|
|
||||||
|
|
||||||
def printandflush(string):
|
def printandflush(string):
|
||||||
"""Print and flush (used by stdout* outputs modules)"""
|
"""Print and flush (used by stdout* outputs modules)"""
|
||||||
print(string, flush=True)
|
print(string, flush=True)
|
||||||
|
|
||||||
def to_ascii(s):
|
|
||||||
"""Convert the bytes string to a ASCII string
|
|
||||||
|
|
||||||
Useful to remove accent (diacritics)
|
def to_ascii(s):
|
||||||
"""
|
"""Convert the bytes string to a ASCII string
|
||||||
if isinstance(s, binary_type):
|
|
||||||
return s.decode()
|
|
||||||
return s.encode('ascii', 'ignore').decode()
|
|
||||||
|
|
||||||
def listitems(d):
|
Useful to remove accent (diacritics)
|
||||||
return list(d.items())
|
"""
|
||||||
|
if isinstance(s, binary_type):
|
||||||
|
return s.decode()
|
||||||
|
return s.encode('ascii', 'ignore').decode()
|
||||||
|
|
||||||
def listkeys(d):
|
|
||||||
return list(d.keys())
|
|
||||||
|
|
||||||
def listvalues(d):
|
def listitems(d):
|
||||||
return list(d.values())
|
return list(d.items())
|
||||||
|
|
||||||
def iteritems(d):
|
|
||||||
return iter(d.items())
|
|
||||||
|
|
||||||
def iterkeys(d):
|
def listkeys(d):
|
||||||
return iter(d.keys())
|
return list(d.keys())
|
||||||
|
|
||||||
def itervalues(d):
|
|
||||||
return iter(d.values())
|
|
||||||
|
|
||||||
def u(s, errors='replace'):
|
def listvalues(d):
|
||||||
if isinstance(s, text_type):
|
return list(d.values())
|
||||||
return s
|
|
||||||
return s.decode('utf-8', errors=errors)
|
|
||||||
|
|
||||||
def b(s, errors='replace'):
|
|
||||||
if isinstance(s, binary_type):
|
|
||||||
return s
|
|
||||||
return s.encode('utf-8', errors=errors)
|
|
||||||
|
|
||||||
def n(s):
|
def iteritems(d):
|
||||||
'''Only in Python 2...
|
return iter(d.items())
|
||||||
from future.utils import bytes_to_native_str as n
|
|
||||||
'''
|
|
||||||
|
def iterkeys(d):
|
||||||
|
return iter(d.keys())
|
||||||
|
|
||||||
|
|
||||||
|
def itervalues(d):
|
||||||
|
return iter(d.values())
|
||||||
|
|
||||||
|
|
||||||
|
def u(s, errors='replace'):
|
||||||
|
if isinstance(s, text_type):
|
||||||
return s
|
return s
|
||||||
|
return s.decode('utf-8', errors=errors)
|
||||||
|
|
||||||
def nativestr(s, errors='replace'):
|
|
||||||
if isinstance(s, text_type):
|
|
||||||
return s
|
|
||||||
elif isinstance(s, (int, float)):
|
|
||||||
return s.__str__()
|
|
||||||
else:
|
|
||||||
return s.decode('utf-8', errors=errors)
|
|
||||||
|
|
||||||
def system_exec(command):
|
def b(s, errors='replace'):
|
||||||
"""Execute a system command and return the result as a str"""
|
if isinstance(s, binary_type):
|
||||||
try:
|
return s
|
||||||
res = subprocess.run(command.split(' '), stdout=subprocess.PIPE).stdout.decode('utf-8')
|
return s.encode('utf-8', errors=errors)
|
||||||
except Exception as e:
|
|
||||||
logger.debug('Can not evaluate command {} ({})'.format(command, e))
|
|
||||||
res = ''
|
|
||||||
return res.rstrip()
|
|
||||||
|
|
||||||
else:
|
|
||||||
|
def n(s):
|
||||||
|
'''Only in Python 2...
|
||||||
from future.utils import bytes_to_native_str as n
|
from future.utils import bytes_to_native_str as n
|
||||||
import Queue as queue
|
'''
|
||||||
from itertools import imap as map
|
return s
|
||||||
from ConfigParser import SafeConfigParser as ConfigParser, NoOptionError, NoSectionError
|
|
||||||
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
|
|
||||||
from xmlrpclib import Fault, ProtocolError, ServerProxy, Transport, Server
|
|
||||||
from urllib2 import urlopen, HTTPError, URLError
|
|
||||||
from urlparse import urlparse
|
|
||||||
|
|
||||||
# Correct issue #1025 by monkey path the xmlrpc lib
|
|
||||||
from defusedxml.xmlrpc import monkey_patch
|
|
||||||
|
|
||||||
monkey_patch()
|
def nativestr(s, errors='replace'):
|
||||||
|
if isinstance(s, text_type):
|
||||||
input = raw_input
|
return s
|
||||||
range = xrange
|
elif isinstance(s, (int, float)):
|
||||||
ConfigParser.read_file = ConfigParser.readfp
|
return s.__str__()
|
||||||
|
else:
|
||||||
text_type = unicode
|
|
||||||
binary_type = str
|
|
||||||
bool_type = types.BooleanType
|
|
||||||
long = long
|
|
||||||
|
|
||||||
PermissionError = OSError
|
|
||||||
|
|
||||||
viewkeys = operator.methodcaller('viewkeys')
|
|
||||||
viewvalues = operator.methodcaller('viewvalues')
|
|
||||||
viewitems = operator.methodcaller('viewitems')
|
|
||||||
|
|
||||||
def printandflush(string):
|
|
||||||
"""Print and flush (used by stdout* outputs modules)"""
|
|
||||||
print(string)
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
def mean(numbers):
|
|
||||||
return float(sum(numbers)) / max(len(numbers), 1)
|
|
||||||
|
|
||||||
def to_ascii(s):
|
|
||||||
"""Convert the unicode 's' to a ASCII string
|
|
||||||
|
|
||||||
Useful to remove accent (diacritics)
|
|
||||||
"""
|
|
||||||
if isinstance(s, binary_type):
|
|
||||||
return s
|
|
||||||
return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore')
|
|
||||||
|
|
||||||
def listitems(d):
|
|
||||||
return d.items()
|
|
||||||
|
|
||||||
def listkeys(d):
|
|
||||||
return d.keys()
|
|
||||||
|
|
||||||
def listvalues(d):
|
|
||||||
return d.values()
|
|
||||||
|
|
||||||
def iteritems(d):
|
|
||||||
return d.iteritems()
|
|
||||||
|
|
||||||
def iterkeys(d):
|
|
||||||
return d.iterkeys()
|
|
||||||
|
|
||||||
def itervalues(d):
|
|
||||||
return d.itervalues()
|
|
||||||
|
|
||||||
def u(s, errors='replace'):
|
|
||||||
if isinstance(s, text_type):
|
|
||||||
return s.encode('utf-8', errors=errors)
|
|
||||||
return s.decode('utf-8', errors=errors)
|
return s.decode('utf-8', errors=errors)
|
||||||
|
|
||||||
def b(s, errors='replace'):
|
|
||||||
if isinstance(s, binary_type):
|
|
||||||
return s
|
|
||||||
return s.encode('utf-8', errors=errors)
|
|
||||||
|
|
||||||
def nativestr(s, errors='replace'):
|
def system_exec(command):
|
||||||
if isinstance(s, binary_type):
|
"""Execute a system command and return the result as a str"""
|
||||||
return s
|
try:
|
||||||
elif isinstance(s, (int, float)):
|
res = subprocess.run(command.split(' '), stdout=subprocess.PIPE).stdout.decode('utf-8')
|
||||||
return s.__str__()
|
except Exception as e:
|
||||||
else:
|
logger.debug('Can not evaluate command {} ({})'.format(command, e))
|
||||||
return s.encode('utf-8', errors=errors)
|
res = ''
|
||||||
|
return res.rstrip()
|
||||||
def system_exec(command):
|
|
||||||
"""Execute a system command and return the resul as a str"""
|
|
||||||
try:
|
|
||||||
res = subprocess.check_output(command.split(' '))
|
|
||||||
except Exception as e:
|
|
||||||
logger.debug('Can not execute command {} ({})'.format(command, e))
|
|
||||||
res = ''
|
|
||||||
return res.rstrip()
|
|
||||||
|
|
||||||
|
|
||||||
# Globals functions for both Python 2 and 3
|
|
||||||
|
|
||||||
|
|
||||||
def subsample(data, sampling):
|
def subsample(data, sampling):
|
||||||
|
|
|
@ -24,7 +24,7 @@ import csv
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from glances.globals import PY3, iterkeys, itervalues
|
from glances.globals import iterkeys, itervalues
|
||||||
from glances.logger import logger
|
from glances.logger import logger
|
||||||
from glances.exports.export import GlancesExport
|
from glances.exports.export import GlancesExport
|
||||||
|
|
||||||
|
@ -129,8 +129,4 @@ class Export(GlancesExport):
|
||||||
|
|
||||||
|
|
||||||
def open_csv_file(file_name, file_mode):
|
def open_csv_file(file_name, file_mode):
|
||||||
if PY3:
|
return open(file_name, file_mode, newline='')
|
||||||
csv_file = open(file_name, file_mode, newline='')
|
|
||||||
else:
|
|
||||||
csv_file = open(file_name, file_mode + 'b')
|
|
||||||
return csv_file
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from glances.globals import PY3, listkeys
|
from glances.globals import listkeys
|
||||||
from glances.logger import logger
|
from glances.logger import logger
|
||||||
from glances.exports.export import GlancesExport
|
from glances.exports.export import GlancesExport
|
||||||
|
|
||||||
|
@ -21,12 +21,8 @@ class Export(GlancesExport):
|
||||||
|
|
||||||
# Set the JSON output file
|
# Set the JSON output file
|
||||||
try:
|
try:
|
||||||
if PY3:
|
self.json_file = open(self.json_filename, 'w')
|
||||||
self.json_file = open(self.json_filename, 'w')
|
self.json_file.close()
|
||||||
self.json_file.close()
|
|
||||||
else:
|
|
||||||
self.json_file = open(self.json_filename, 'wb')
|
|
||||||
self.json_file.close()
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
logger.critical("Cannot create the JSON file: {}".format(e))
|
logger.critical("Cannot create the JSON file: {}".format(e))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
@ -53,12 +49,8 @@ class Export(GlancesExport):
|
||||||
logger.debug("Exporting stats ({}) to JSON file ({})".format(listkeys(self.buffer), self.json_filename))
|
logger.debug("Exporting stats ({}) to JSON file ({})".format(listkeys(self.buffer), self.json_filename))
|
||||||
|
|
||||||
# Export stats to JSON file
|
# Export stats to JSON file
|
||||||
if PY3:
|
with open(self.json_filename, "w") as self.json_file:
|
||||||
with open(self.json_filename, "w") as self.json_file:
|
self.json_file.write("{}\n".format(json.dumps(self.buffer)))
|
||||||
self.json_file.write("{}\n".format(json.dumps(self.buffer)))
|
|
||||||
else:
|
|
||||||
with open(self.json_filename, "wb") as self.json_file:
|
|
||||||
self.json_file.write("{}\n".format(json.dumps(self.buffer)))
|
|
||||||
|
|
||||||
# Reset buffer
|
# Reset buffer
|
||||||
self.buffer = {}
|
self.buffer = {}
|
||||||
|
|
|
@ -697,7 +697,7 @@ Examples of use:
|
||||||
if getattr(self.args, 'memory_leak', True) and not self.is_standalone():
|
if getattr(self.args, 'memory_leak', True) and not self.is_standalone():
|
||||||
logger.critical("Option --memory-leak is only available in the terminal mode")
|
logger.critical("Option --memory-leak is only available in the terminal mode")
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
elif getattr(self.args, 'memory_leak', True) and (PY3 or self.is_standalone()):
|
elif getattr(self.args, 'memory_leak', True) and self.is_standalone():
|
||||||
logger.info('Memory leak detection enabled')
|
logger.info('Memory leak detection enabled')
|
||||||
self.args.quiet = True
|
self.args.quiet = True
|
||||||
if not self.args.stop_after:
|
if not self.args.stop_after:
|
||||||
|
|
Loading…
Reference in New Issue