[asterisk-commits] pabelanger: testsuite/asterisk/trunk r3021 - /asterisk/trunk/lib/python/aster...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jan 26 16:59:38 CST 2012


Author: pabelanger
Date: Thu Jan 26 16:59:35 2012
New Revision: 3021

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=3021
Log:
Get version number from asterisk binary, not development headers

Modified:
    asterisk/trunk/lib/python/asterisk/version.py

Modified: asterisk/trunk/lib/python/asterisk/version.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/lib/python/asterisk/version.py?view=diff&rev=3021&r1=3020&r2=3021
==============================================================================
--- asterisk/trunk/lib/python/asterisk/version.py (original)
+++ asterisk/trunk/lib/python/asterisk/version.py Thu Jan 26 16:59:35 2012
@@ -15,6 +15,8 @@
 import re
 import unittest
 import logging
+import subprocess
+import utils
 
 logger = logging.getLogger(__name__)
 
@@ -23,31 +25,44 @@
 
     This class handles Asterisk version strings.
     """
-
-    def __init__(self, version=None, path=None):
+    ast_version = ''
+
+    def __init__(self, version=None):
         """Construct an Asterisk Version parser.
 
         Keyword Arguments:
         version -- The Asterisk version string to parse.
-        path -- If an Asterisk version string is not directly provided, look
-        for a version.h file in this location that will contain the Asterisk
-        version string to pull out and Parse.
         """
         self.svn = False
-        if version is not None:
+
+        if version is None and AsteriskVersion.ast_version == '':
+            self.ast_binary = utils.which("asterisk") or "/usr/sbin/asterisk"
+            cmd = [
+                self.ast_binary,
+                "-V",
+            ]
+
+            try:
+                process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                        stderr=subprocess.STDOUT)
+            except OSError:
+                print "Failed to execute command: %s" % str(cmd)
+                return
+
+            try:
+                AsteriskVersion.ast_version = process.stdout.read()
+            except OSError:
+                print "Failed to execute command: %s" % str(cmd)
+                return
+
+            AsteriskVersion.ast_version = AsteriskVersion.ast_version.replace("Asterisk ", "")
+            self.version_str = AsteriskVersion.ast_version
+
+        elif version is not None:
             self.version_str = version
+
         else:
-            version_hdr_paths = [
-                "../include/asterisk/version.h",
-                "/usr/include/asterisk/version.h",
-                "/usr/local/include/asterisk/version.h"
-            ]
-            if path:
-                version_hdr_paths.insert(0, path)
-            for p in version_hdr_paths:
-                self.version_str = self.__get_ast_version(p)
-                if self.version_str:
-                    break
+            self.version_str = AsteriskVersion.ast_version
 
         if not self.version_str:
             return
@@ -144,24 +159,6 @@
 
         return ret
 
-    def __get_ast_version(self, path):
-        '''
-        Determine the version of Asterisk installed from the installed version.h.
-        '''
-        v = None
-        try:
-            f = open(path, "r")
-            match = re.search("ASTERISK_VERSION\s+\"(.*)\"", f.read())
-            if match is not None:
-                v = match.group(1)
-            f.close()
-        except IOError:
-            logger.error("I/O Error getting Asterisk version from %s" % path)
-        except:
-            logger.error("Unexpected error getting version from %s: %s" % (path,
-                    sys.exc_info()[0]))
-        return v
-
 
 class AsteriskVersionTests(unittest.TestCase):
     def test_version(self):




More information about the asterisk-commits mailing list