22
33# this scan file has utilities to find pip, npm, gem package updates
44
5- import scan_lib
65
76import sys
87
9-
10- def binary_does_not_exist (response ):
11- """
12- Used to figure if the npm, pip, gem binary exists in the container image
13- """
14- if 'executable file not found in' in response or \
15- 'not found' in response or \
16- 'No such file or directory' in response :
17- return True
18- return False
8+ from scanners .base_scanner import BaseScanner , BinaryDoesNotExist
199
2010
21- def find_pip_updates ( executable = "/usr/bin/pip" ):
11+ class MiscPackageUpdates ( BaseScanner ):
2212 """
23- Finds out outdated installed packages of pip
13+ Misc package updates scanner
2414 """
25- command = [executable , "list" , "--outdated" , "--disable-pip-version-check" ]
26- out , err = [], ""
27-
28- try :
29- out , err = scan_lib .run_cmd_out_err (command )
30- except Exception as e :
31- err = e
32-
33- if err :
34- if binary_does_not_exist (err ):
35- return "{0} is not installed" .format (executable )
36- else :
15+ NAME = "Misc-package-updates"
16+ DESCRIPTION = "Find updates available for pip, npm, and gem."
17+
18+ def __init__ (self , image ):
19+ super (MiscPackageUpdates , self ).__init__ ()
20+
21+ def find_pip_updates (self , binary = "pip" ):
22+ """
23+ Finds out outdated installed packages of pip
24+ """
25+ # figure out the absolute path of binary in target system
26+ binary = self .which (binary )
27+ command = [binary , "list" , "--outdated" , "--disable-pip-version-check" ]
28+ out , err = [], ""
29+
30+ try :
31+ out , err = self .run_cmd_out_err (command )
32+ except Exception as e :
33+ err = e
34+
35+ if err :
3736 return "Failed to find the pip updates."
38- else :
39- if out .strip ():
40- return out .strip ().split ("\n " )
41- else :
42- return []
43-
44-
45- def find_npm_updates (executable = "/usr/bin/npm" ):
46- """
47- Finds out outdated installed packages of npm
48- """
49- command = [executable , "-g" , "outdated" ]
50- out , err = [], ""
51-
52- try :
53- out , err = scan_lib .run_cmd_out_err (command )
54- except Exception as e :
55- err = e
56-
57- if err :
58- if binary_does_not_exist (err ):
59- return "{0} is not installed" .format (executable )
6037 else :
38+ if out .strip ():
39+ return out .strip ().split ("\n " )
40+ else :
41+ return []
42+
43+ def find_npm_updates (self , binary = "npm" ):
44+ """
45+ Finds out outdated installed packages of npm
46+ """
47+ # figure out the absolute path of binary in target system
48+ binary = self .which (binary )
49+ command = [binary , "-g" , "outdated" ]
50+ out , err = [], ""
51+
52+ try :
53+ out , err = self .run_cmd_out_err (command )
54+ except Exception as e :
55+ err = e
56+
57+ if err :
6158 return "Failed to find the npm updates."
62- else :
63- if out .strip ():
64- return out .strip ().split ("\n " )
65- else :
66- return []
67-
68-
69- def find_gem_updates (executable = "/usr/bin/gem" ):
70- """
71- Finds out outdated installed packages of gem
72- """
73- command = [executable , "outdated" ]
74- out , err = [], ""
75-
76- try :
77- out , err = scan_lib .run_cmd_out_err (command )
78- except Exception as e :
79- err = e
80-
81- if err :
82- if binary_does_not_exist (err ):
83- return "{0} is not installed" .format (executable )
8459 else :
60+ if out .strip ():
61+ return out .strip ().split ("\n " )
62+ else :
63+ return []
64+
65+ def find_gem_updates (self , binary = "gem" ):
66+ """
67+ Finds out outdated installed packages of gem
68+ """
69+ # figure out the absolute path of binary in target system
70+ binary = self .which (binary )
71+ command = [binary , "outdated" ]
72+ out , err = [], ""
73+
74+ try :
75+ out , err = self .run_cmd_out_err (command )
76+ except Exception as e :
77+ err = e
78+
79+ if err :
8580 return "Failed to find the gem updates."
86- else :
87- if out .strip ():
88- return out .strip ().split ("\n " )
8981 else :
90- return []
91-
92-
93- def print_updates (binary ):
94- """
95- Prints the updates found using given binary
96- """
97- print ("\n {0} updates scan:" .format (binary ))
98-
99- if binary == "npm" :
100- result = find_npm_updates ()
101- elif binary == "gem" :
102- result = find_gem_updates ()
103- elif binary == "pip" :
104- result = find_pip_updates ()
105- else :
106- return
107-
108- if result :
109- # prints errors
110- if isinstance (result , str ):
111- print (result )
82+ if out .strip ():
83+ return out .strip ().split ("\n " )
84+ else :
85+ return []
86+
87+ def print_updates (self , binary ):
88+ """
89+ Prints the updates found using given binary
90+ """
91+ print ("\n {0} updates scan:" .format (binary ))
92+
93+ if binary == "npm" :
94+ result = self .find_npm_updates ()
95+ elif binary == "gem" :
96+ result = self .find_gem_updates ()
97+ elif binary == "pip" :
98+ result = self .find_pip_updates ()
99+ else :
112100 return
113- # prints result
114- for line in result :
115- print (line )
116- else :
117- print ("No updates required." )
101+
102+ if result :
103+ # prints errors
104+ if isinstance (result , str ):
105+ print (result )
106+ return
107+ # prints result
108+ for line in result :
109+ print (line )
110+ else :
111+ print ("No updates required." )
118112
119113
120114if __name__ == "__main__" :
@@ -133,12 +127,17 @@ def print_updates(binary):
133127 sys .exit (1 )
134128
135129 try :
130+ misc_pkg_updates = MiscPackageUpdates ('' )
136131 if cli_arg == "all" :
137- print_updates ("pip" )
138- print_updates ("npm" )
139- print_updates ("gem" )
132+ misc_pkg_updates . print_updates ("pip" )
133+ misc_pkg_updates . print_updates ("npm" )
134+ misc_pkg_updates . print_updates ("gem" )
140135 else :
141- print_updates (cli_arg )
136+ misc_pkg_updates .print_updates (cli_arg )
137+ except BinaryDoesNotExist as e :
138+ print (e )
139+ print ("Scan is aborted!" )
140+ sys .exit (1 )
142141 except Exception as e :
143142 print ("Error occurred in Misc Package Updates scanner execution." )
144143 print ("Error: {0}" .format (e ))
0 commit comments