From 32ef6e6e9d4ead11cc6356fd357591f7eb91e7a7 Mon Sep 17 00:00:00 2001 From: Tomas Tomecek Date: Thu, 28 Jul 2016 14:17:18 +0200 Subject: [npm] find modules in npm cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit npm installs modules: 1. globally — so everyone can use those 2. per project If you don't know the path to the project, the only way to see the list of those modules is by traversing the whole filesystem, which is pretty unreal. This patch browses npm cache of every user on the system and saves module names and all available versions into sosreport. Signed-off-by: Tomas Tomecek Signed-off-by: Bryn M. Reeves Closes: #879. --- sos/plugins/npm.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/sos/plugins/npm.py b/sos/plugins/npm.py index 77464c22..f76bafee 100644 --- a/sos/plugins/npm.py +++ b/sos/plugins/npm.py @@ -14,6 +14,8 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import os +import pwd +import json from sos.plugins import Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin, \ SuSEPlugin @@ -44,6 +46,41 @@ class Npm(Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin, SuSEPlugin): runat=working_directory ) + def _find_modules_in_npm_cache(self): + """ + since format of `~/.npm` is changing with newer npm releases, let's do + the easiest thing and don't predict nor expect anything, the structure + is suppose to be: + + ~/.npm + |-- asap + | `-- 2.0.4 + |-- dezalgo + | |-- 1.0.2 + | `-- 1.0.3 + """ + output = [] + + for p in pwd.getpwall(): + user_cache = os.path.join(p.pw_dir, ".npm") + try: + # most of these stand for names of modules + module_names = os.listdir(user_cache) + except OSError: + self._log_debug("Skipping directory %s" % user_cache) + continue + else: + for m in module_names: + module_path = os.path.join(user_cache, m) + try: + versions = os.listdir(module_path) + except OSError: + continue + else: + output.append({m: versions}) + outfn = self._make_command_filename("npm-cache-modules") + self.archive.add_string(json.dumps(output), outfn) + def setup(self): if self.get_option("project_path") != 0: project_path = os.path.abspath(os.path.expanduser( @@ -51,6 +88,7 @@ class Npm(Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin, SuSEPlugin): self._get_npm_output("npm ls --json", "npm-ls-project", working_directory=project_path) self._get_npm_output("npm ls -g --json", "npm-ls-global") + self._find_modules_in_npm_cache() class NpmViaNodeJS(Npm): -- cgit