-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshow_version.py
More file actions
executable file
·227 lines (206 loc) · 6.19 KB
/
show_version.py
File metadata and controls
executable file
·227 lines (206 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
########################################################################
# show_version.py: Show Python Modules Info and Version
#
# Description:
# This script displays detailed information and versions for a list of
# specified Python modules. It also shows the Python version upon request.
# Instead of displaying 'not found' messages immediately, it collects them
# and displays a summary of missing modules at the end.
# It's useful for quickly checking the installed versions of various packages.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# show_version.py [options] file
#
# Options:
# -h, --help show this help message and exit
# -i, --info show detail info
# -p, --python show python version
#
# To display the versions of predefined modules:
# python show_version.py
#
# To display detailed information of predefined modules:
# python show_version.py -i
#
# To display the Python version:
# python show_version.py -p
#
# Requirements:
# - Python Version: 3.1 or later
#
# Version History:
# v2.7 2025-07-01
# Standardized termination behavior for consistent script execution.
# v2.6 2025-06-23
# Unified usage output to display full script header and support common help/version options.
# v2.5 2024-01-18
# Refactored function names for clarity.
# Improved comments for better understanding.
# v2.4 2024-01-06
# Added functionality to display a summary of not found modules at the end.
# v2.3 2020-11-05
# Using importlib instead of imp.
# v2.2 2015-09-28
# Refactoring.
# v2.1 2014-03-19
# Show python version.
# v2.0 2014-02-11
# Simple version listing.
# Using -i option for detailed info.
# v1.2 2014-02-10
# Remove install process.
# v1.1 2011-06-29
# Add some packages.
# v1.0 2008-08-15
# Stable.
#
########################################################################
import importlib
import os
import sys
import warnings
def usage():
""" Display the script header as usage information and exit. """
script_path = os.path.abspath(__file__)
in_header = False
try:
with open(script_path, 'r', encoding='utf-8') as f:
for line in f:
if line.strip().startswith('#' * 10):
if not in_header:
in_header = True
continue
else:
break
if in_header and line.startswith('#'):
if line.startswith('# '):
print(line[2:], end='')
else:
print(line[1:], end='')
except Exception as e:
print("Error reading usage information: %s" % str(e), file=sys.stderr)
sys.exit(1)
sys.exit(0)
class PythonModuleInfo:
def __init__(self, options):
self.info = options.info
self.python = options.python
self.not_found = []
def display_module_help(self, module_name):
""" Display help information for the specified module. """
try:
module = importlib.import_module(module_name)
help(module)
except ImportError:
self.not_found.append(module_name)
def get_module_version(self, module_name):
""" Get and display the version of the specified module. """
try:
obj = importlib.import_module(module_name)
if hasattr(obj, "__version__"):
print(module_name, obj.__version__)
elif hasattr(obj, "VERSION"):
print(module_name, obj.VERSION)
else:
print(module_name, "unknown version")
except ImportError:
self.not_found.append(module_name)
def get_info(self, module_name):
if self.info:
self.display_module_help(module_name)
else:
self.get_module_version(module_name)
def get_python_version(self):
if self.python:
python_version = sys.version
print("Python %(python_version)s" % locals())
def show_not_found(self):
if self.not_found:
print("\nThese modules were not found:")
for module in self.not_found:
print(module)
def main():
from optparse import OptionParser
usage = "usage: %prog [options] file"
parser = OptionParser(usage)
parser.add_option("-i", "--info", help="show detail info",
action="store_true", dest="info")
parser.add_option("-p", "--python", help="show python version",
action="store_true", dest="python")
(options, args) = parser.parse_args()
m = PythonModuleInfo(options)
m.get_python_version()
packages = [
'pip',
'IPython',
'pyflakes',
'flake8',
'pytest',
'autopep8',
'autoflake',
'cython',
'docutils',
'docopt',
'simplejson',
'numpy',
'scipy',
'sklearn',
'matplotlib',
'pandas',
'tensorflow',
'keras',
'joblib',
'dask',
'patsy',
'statsmodels',
'sympy',
'pystan',
'seaborn',
'bokeh',
'twisted',
'flask',
'django',
'sqlalchemy',
'lmdb',
'migrate',
'pygments',
'babel',
'genshi',
'bottle',
'cherrypy',
'bs4',
'lxml',
'requests',
'pysolr',
'html5lib',
'PIL',
'pyper',
'awscli',
'openpyxl',
'simpy',
'networkx',
'fabric',
'talib',
'zipline',
'instaloader',
'nltk',
'MeCab',
'CaboCha'
]
warnings.resetwarnings()
with warnings.catch_warnings():
warnings.simplefilter('ignore')
for p in packages:
m.get_info(p)
m.show_not_found()
return 0
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] in ('-h', '--help', '-v', '--version'):
usage()
sys.exit(main())