@@ -50,9 +50,9 @@ class ScriptBase(object):
5050 ADDITIONAL_HELP = []
5151
5252 # Can be empty or None in derived classes
53- COPYRIGHT = "Copyright (c) 2009 - 2016 Pyroscope Project"
53+ COPYRIGHT = "Copyright (c) 2009 - 2017 Pyroscope Project"
5454
55- # Can be made explicit in derived classes
55+ # Can be made explicit in derived classes (for external tools)
5656 VERSION = None
5757
5858
@@ -75,6 +75,45 @@ def setup(cls, cron_cfg="cron"):
7575 logging .getLogger ().debug ("Logging config read from '%s'" % logging_cfg )
7676
7777
78+ def _get_pkg_meta (self ):
79+ """ Try to find package metadata.
80+ """
81+ pkg_info = "Version: 0.0.0\n "
82+ for info_ext , info_name in (('.egg-info' , 'PKG-INFO' ), ('.dist-info' , 'METADATA' )):
83+ try :
84+ # Development setup
85+ pkg_path = os .path .join (
86+ __file__ .split (__name__ .replace ('.' , os .sep ))[0 ], # containing path
87+ __name__ .split ("." )[0 ] # package name
88+ )
89+ if os .path .exists (pkg_path + info_ext ):
90+ pkg_path += info_ext
91+ else :
92+ globbed_paths = glob .glob (pkg_path + "-*-py%d.%d" % sys .version_info [:2 ] + info_ext )
93+ if len (globbed_paths ) == 1 :
94+ pkg_path = globbed_paths [0 ]
95+ elif globbed_paths :
96+ self .LOG .warn ("Found %d release-specific candidate versions" % len (globbed_paths ))
97+ pkg_path = None
98+ else :
99+ globbed_paths = glob .glob (pkg_path + "-*" + info_ext )
100+ if len (globbed_paths ) == 1 :
101+ pkg_path = globbed_paths [0 ]
102+ else :
103+ self .LOG .warn ("Found %d candidate versions" % len (globbed_paths ))
104+ pkg_path = None
105+ if pkg_path :
106+ with open (os .path .join (pkg_path , info_name )) as handle :
107+ pkg_info = handle .read ()
108+ break
109+ else :
110+ self .LOG .warn ("Software version cannot be determined!" )
111+ except IOError :
112+ self .LOG .warn ("Software version cannot be determined!" )
113+
114+ return pkg_info
115+
116+
78117 def __init__ (self ):
79118 """ Initialize CLI.
80119 """
@@ -86,50 +125,31 @@ def __init__(self):
86125 if not self .version :
87126 # Take version from package
88127 provider = pkg_resources .get_provider (__name__ )
89- pkg_info = provider .get_metadata ("PKG-INFO" )
90-
91- if not pkg_info :
92- pkg_info = "Version: 0.0.0\n "
93- try :
94- # Development setup
95- pkg_path = os .path .join (
96- __file__ .split (__name__ .replace ('.' , os .sep ))[0 ], # containing path
97- __name__ .split ("." )[0 ] # package name
98- )
99- if os .path .exists (pkg_path + ".egg-info" ):
100- pkg_path += ".egg-info"
101- else :
102- pkg_path = glob .glob (pkg_path + "-*-py%d.%d.egg-info" % sys .version_info [:2 ])
103- if len (pkg_path ) == 1 :
104- pkg_path = pkg_path [0 ]
105- else :
106- self .LOG .warn ("Found %d candidate versions" % len (pkg_path ))
107- pkg_path = None
108- if pkg_path :
109- with open (os .path .join (pkg_path , "PKG-INFO" )) as handle :
110- pkg_info = handle .read ()
111- else :
112- self .LOG .warn ("Software version cannot be determined!" )
113- except IOError :
114- self .LOG .warn ("Software version cannot be determined!" )
115-
116- pkg_info = dict (line .split (": " , 1 )
117- for line in pkg_info .splitlines ()
128+ pkg_meta = (provider .get_metadata ("PKG-INFO" )
129+ or provider .get_metadata ("METADATA" )
130+ or self ._get_pkg_meta ())
131+ pkg_dict = dict (line .split (": " , 1 )
132+ for line in pkg_meta .splitlines ()
118133 if ": " in line
119134 )
120- self .version = pkg_info .get ("Version" , "DEV" )
135+ self .version = pkg_dict .get ("Version" , "DEV" )
136+
137+ where = os .path .commonprefix ([__file__ , os .path .realpath (sys .argv [0 ]), sys .prefix ])
138+ where = (where + os .sep ).replace (os .path .expanduser ('~' + os .sep ), '~' + os .sep ).rstrip (os .sep )
139+ self .version_info = '{}{}{} on Python {}' .format (
140+ self .version , ' from ' if where else '' , where , sys .version .split ()[0 ])
121141
122142 self .args = None
123143 self .options = None
124144 self .return_code = 0
125145 self .parser = OptionParser (
126146 "%prog [options] " + self .ARGS_HELP + "\n \n "
127- "%prog " + self .version + (", " + self .COPYRIGHT if self .COPYRIGHT else "" ) + "\n \n "
147+ "%prog " + self .version_info + (' \n ' + self .COPYRIGHT if self .COPYRIGHT else "" ) + "\n \n "
128148 + textwrap .dedent (self .__doc__ .rstrip ()).lstrip ('\n ' )
129149 + '\n ' .join (self .ADDITIONAL_HELP )
130150 + "\n \n For more details, see the full documentation at"
131151 + "\n \n https://pyrocore.readthedocs.io/" ,
132- version = "%prog " + self .version )
152+ version = "%prog " + self .version_info )
133153
134154
135155 def add_bool_option (self , * args , ** kwargs ):
0 commit comments