Created
January 15, 2015 03:19
-
-
Save tanbro/53db22b2e536c5a39671 to your computer and use it in GitHub Desktop.
printver -- print version and date of python modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
''' | |
printver -- print version and date of python modules | |
print module's __version__, __date__ and __updated__ variable | |
:author: Liu Xue Yan | |
:mail: [email protected] | |
''' | |
from __future__ import print_function, absolute_import | |
import sys | |
import os | |
from argparse import ArgumentParser | |
from argparse import RawDescriptionHelpFormatter | |
from importlib import import_module | |
__all__ = [] | |
__version__ = 0.1 | |
__date__ = '2015-01-15' | |
__updated__ = '2015-01-15' | |
DEBUG = 0 | |
TESTRUN = 0 | |
PROFILE = 0 | |
class CLIError(Exception): | |
'''Generic exception to raise and log different fatal errors.''' | |
def __init__(self, msg): | |
super(CLIError).__init__(type(self)) | |
self.msg = "E: %s" % msg | |
def __str__(self): | |
return self.msg | |
def __unicode__(self): | |
return self.msg | |
def main(argv=None): # IGNORE:C0111 | |
'''Command line options.''' | |
if argv is None: | |
argv = sys.argv | |
else: | |
sys.argv.extend(argv) | |
program_name = os.path.basename(sys.argv[0]) | |
program_version = "v%s" % __version__ | |
program_build_date = str(__updated__) | |
program_version_message = '%%(prog)s %s (%s)' % (program_version, program_build_date) | |
program_license = '''printver -- print version and date of python modules | |
Created by Liu Xue Yan on %s. | |
USAGE | |
''' % (str(__date__)) | |
try: | |
# Setup argument parser | |
parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter) | |
parser.add_argument('-V', '--version', action='version', version=program_version_message) | |
parser.add_argument('name', type=str, nargs='+', | |
help="Specifies what module to import in absolute or relative terms (e.g. either pkg.mod or ..mod). If the name is specified in relative terms, then the package argument must be set to the name of the package which is to act as the anchor for resolving the package name (e.g. import_module('..mod', 'pkg.subpkg') will import pkg.mod).") | |
# Process arguments | |
args = parser.parse_args() | |
except KeyboardInterrupt: | |
### handle keyboard interrupt ### | |
return 0 | |
except Exception as e: | |
if DEBUG or TESTRUN: | |
raise(e) | |
indent = len(program_name) * " " | |
sys.stderr.write(program_name + ": " + repr(e) + "\n") | |
sys.stderr.write(indent + " for help use --help") | |
return 2 | |
# | |
modnames = args.name | |
for modname in modnames: | |
modobj = import_module(modname) | |
try: | |
version_str = str(modobj.__version__) | |
except AttributeError: | |
version_str = None | |
try: | |
updated_str = str(modobj.__updated__) | |
except AttributeError: | |
updated_str = None | |
try: | |
date_str = str(modobj.__date__) | |
except AttributeError: | |
date_str = None | |
print('Module<{}>\n |-version: {}\n |-date: {}\n `-updated: {}\n'.format(modobj.__name__, version_str, date_str, updated_str)) | |
# | |
return 0 | |
if __name__ == "__main__": | |
if DEBUG: | |
pass | |
if TESTRUN: | |
import doctest | |
doctest.testmod() | |
if PROFILE: | |
import cProfile | |
import pstats | |
profile_filename = 'printver_profile.txt' | |
cProfile.run('main()', profile_filename) | |
statsfile = open("profile_stats.txt", "wb") | |
p = pstats.Stats(profile_filename, stream=statsfile) | |
stats = p.strip_dirs().sort_stats('cumulative') | |
stats.print_stats() | |
statsfile.close() | |
sys.exit(0) | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment