From 526e82feafa8f359d0db4f723cdef211450ab8b7 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 14 Oct 2012 14:09:03 -0700 Subject: [PATCH] move cpu_count to util module; credit @storrgie --- beets/util/__init__.py | 26 ++++++++++++++++++++++++++ beetsplug/convert.py | 27 +-------------------------- docs/changelog.rst | 3 ++- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 313313cf2..9be05ee18 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -524,3 +524,29 @@ def plurality(objs): res = obj return res, max_freq + +def cpu_count(): + """Return the number of hardware thread contexts (cores or SMT + threads) in the system. + """ + # Adapted from observing the soundconverter project: + # https://github.com/kassoulet/soundconverter + if sys.platform == 'win32': + try: + num = int(os.environ['NUMBER_OF_PROCESSORS']) + except (ValueError, KeyError): + num = 0 + elif sys.platform == 'darwin': + try: + num = int(os.popen('sysctl -n hw.ncpu').read()) + except ValueError: + num = 0 + else: + try: + num = os.sysconf('SC_NPROCESSORS_ONLN') + except (ValueError, OSError, AttributeError): + num = 0 + if num >= 1: + return num + else: + return 1 diff --git a/beetsplug/convert.py b/beetsplug/convert.py index 54210c417..de2fdf10c 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -29,31 +29,6 @@ DEVNULL = open(os.devnull, 'wb') conf = {} -def _cpu_count(): - """ Returns the number of CPUs in the system. - Code was adapted from observing the soundconverter project: - https://github.com/kassoulet/soundconverter - """ - if sys.platform == 'win32': - try: - num = int(os.environ['NUMBER_OF_PROCESSORS']) - except (ValueError, KeyError): - num = 0 - elif sys.platform == 'darwin': - try: - num = int(os.popen('sysctl -n hw.ncpu').read()) - except ValueError: - num = 0 - else: - try: - num = os.sysconf('SC_NPROCESSORS_ONLN') - except (ValueError, OSError, AttributeError): - num = 0 - if num >= 1: - return num - else: - return 1 - def encode(source, dest): log.info('Started encoding ' + source) @@ -144,7 +119,7 @@ class ConvertPlugin(BeetsPlugin): def configure(self, config): conf['dest'] = ui.config_val(config, 'convert', 'dest', None) conf['threads'] = ui.config_val(config, 'convert', 'threads', - _cpu_count()) + util.cpu_count()) conf['flac'] = ui.config_val(config, 'convert', 'flac', 'flac') conf['lame'] = ui.config_val(config, 'convert', 'lame', 'lame') conf['opts'] = ui.config_val(config, 'convert', diff --git a/docs/changelog.rst b/docs/changelog.rst index ec27a314c..b3929941b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,7 +5,8 @@ Changelog ----------------------- * New plugin: :doc:`/plugins/convert` transcodes music and embeds album art - while copying to a separate directory. Thanks to Jakob Schnitzer. + while copying to a separate directory. Thanks to Jakob Schnitzer and Andrew G. + Dunn. * New plugin: :doc:`/plugins/fuzzy_search` lets you find albums and tracks using fuzzy string matching so you don't have to type (or even remember) their exact names. Thanks to Philippe Mongeau.