<commit_before># coding=utf-8
from __future__ import absolute_import

__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms of the AGPLv3 License"

import copy
import logging
import os
import platform
import sys
import threading
import yaml

import psutil

from octoprint.plugin import EnvironmentDetectionPlugin
from octoprint.util.platform import get_os

class EnvironmentDetector(object):

	def __init__(self, plugin_manager):
		self._plugin_manager = plugin_manager

		self._cache = None
		self._cache_lock = threading.RLock()

		self._environment_plugins = self._plugin_manager.get_implementations(EnvironmentDetectionPlugin)

		self._logger = logging.getLogger(__name__)

	@property
	def environment(self):
		with self._cache_lock:
			if self._cache is None:
				self.run_detection()
			return copy.deepcopy(self._cache)

	def run_detection(self, notify_plugins=True):
		try:
			environment = dict()
			environment["os"] = self._detect_os()
			environment["python"] = self._detect_python()
			environment["hardware"] = self._detect_hardware()

			plugin_result = self._detect_from_plugins()
			if plugin_result:
				environment["plugins"] = plugin_result

			with self._cache_lock:
				self._cache = environment

			if notify_plugins:
				self.notify_plugins()

			return environment
		except:
			self._logger.exception("Unexpected error while detecting environment")
			with self._cache_lock:
				self._cache = dict()
				return self._cache

	def _detect_os(self):
		return dict(id=get_os(),
		            platform=sys.platform)

	def _detect_python(self):
		result = dict(version="unknown",
		              pip="unknown")

		# determine python version
		try:
			result["version"] = platform.python_version()
		except:
			self._logger.exception("Error detecting python version")

		# determine if we are running from a virtual environment
		try:
			if hasattr(sys, "real_prefix") or (hasattr(sys, "base_prefix") and os.path.realpath(sys.prefix) != os.path.realpath(sys.base_prefix)):
				result["virtualenv"] = sys.prefix
		except:
			self._logger.exception("Error detecting whether we are running in a virtual environment")

		# try to find pip version
		try:
			import pip
			result["pip"] = pip.__version__
		except:
			self._logger.exception("Error detecting pip version")

		return result

	def _detect_hardware(self):
		result = dict(cores="unknown",
		              freq="unknown",
		              ram="unknown")

		try:
			cores = psutil.cpu_count()
			cpu_freq = psutil.cpu_freq()<commit_msg>Use pkg_resources to find out local pip version<commit_after># coding=utf-8
from __future__ import absolute_import

__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms of the AGPLv3 License"

import copy
import logging
import os
import platform
import sys
import threading
import yaml

import psutil

from octoprint.plugin import EnvironmentDetectionPlugin
from octoprint.util.platform import get_os

class EnvironmentDetector(object):

	def __init__(self, plugin_manager):
		self._plugin_manager = plugin_manager

		self._cache = None
		self._cache_lock = threading.RLock()

		self._environment_plugins = self._plugin_manager.get_implementations(EnvironmentDetectionPlugin)

		self._logger = logging.getLogger(__name__)

	@property
	def environment(self):
		with self._cache_lock:
			if self._cache is None:
				self.run_detection()
			return copy.deepcopy(self._cache)

	def run_detection(self, notify_plugins=True):
		try:
			environment = dict()
			environment["os"] = self._detect_os()
			environment["python"] = self._detect_python()
			environment["hardware"] = self._detect_hardware()

			plugin_result = self._detect_from_plugins()
			if plugin_result:
				environment["plugins"] = plugin_result

			with self._cache_lock:
				self._cache = environment

			if notify_plugins:
				self.notify_plugins()

			return environment
		except:
			self._logger.exception("Unexpected error while detecting environment")
			with self._cache_lock:
				self._cache = dict()
				return self._cache

	def _detect_os(self):
		return dict(id=get_os(),
		            platform=sys.platform)

	def _detect_python(self):
		result = dict(version="unknown",
		              pip="unknown")

		# determine python version
		try:
			result["version"] = platform.python_version()
		except:
			self._logger.exception("Error detecting python version")

		# determine if we are running from a virtual environment
		try:
			if hasattr(sys, "real_prefix") or (hasattr(sys, "base_prefix") and os.path.realpath(sys.prefix) != os.path.realpath(sys.base_prefix)):
				result["virtualenv"] = sys.prefix
		except:
			self._logger.exception("Error detecting whether we are running in a virtual environment")

		# try to find pip version
		try:
			import pkg_resources
			result["pip"] = pkg_resources.get_distribution("pip").version
		except:
			self._logger.exception("Error detecting pip version")

		return result

	def _detect_hardware(self):
		result = dict(cores="unknown",
		              freq="unknown",
		              ram="unknown")

		try:
			cores = psutil.cpu_count()
			cpu_freq = psutil.cpu_freq()
{'fraction_non_alphanumeric': 0.09680451127819549, 'fraction_numerical': 0.002255639097744361, 'mean_word_length': 4.171039844509233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5873306', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1526, 'n_words': 402}