Make the version module more robust, in the sense that
you can call it from directories other than the autotest
root dir and still get it to correctly read RELEASE-VERSION.

Also, change the interface name get_git_version to get_version,
as it doesn't get version only from git (if running on
an sdist tarball, it'll only read RELEASE-VERSION).

Signed-off-by: Lucas Meneghel Rodrigues <[email protected]>
---
 cli/setup.py             |   2 +-
 client/setup.py          |   2 +-
 client/shared/version.py | 104 ++++++++++++++++++++---------------------------
 database/setup.py        |   2 +-
 frontend/setup.py        |   2 +-
 mirror/setup.py          |   2 +-
 scheduler/setup.py       |   2 +-
 server/setup.py          |   2 +-
 tko/setup.py             |   2 +-
 utils/setup.py           |   2 +-
 10 files changed, 53 insertions(+), 69 deletions(-)

diff --git a/cli/setup.py b/cli/setup.py
index 5bd6883..5d16c51 100644
--- a/cli/setup.py
+++ b/cli/setup.py
@@ -16,7 +16,7 @@ setup(name='autotest',
       description='Autotest framework - cli interface to rpc server',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.cli': cli_dir },
       package_data={'autotest.cli' : ['contrib/*' ] },
diff --git a/client/setup.py b/client/setup.py
index 9276028..5af92ca 100644
--- a/client/setup.py
+++ b/client/setup.py
@@ -33,7 +33,7 @@ setup(name='autotest',
       description='Autotest test framework - local module',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.client': client_dir,
                    'autotest' : autotest_dir,
diff --git a/client/shared/version.py b/client/shared/version.py
index 96f8e5e..1590a80 100755
--- a/client/shared/version.py
+++ b/client/shared/version.py
@@ -1,106 +1,90 @@
-#!/usr/bin/python -u
-# -*- coding: utf-8 -*-
+#!/usr/bin/python
 """
- Author: Douglas Creager <[email protected]>
- This file is placed into the public domain.
-
- Calculates the current version number.  If possible, this is the
- output of “git describe”, modified to conform to the versioning
- scheme that setuptools uses.  If “git describe” returns an error
- (most likely because we're in an unpacked copy of a release tarball,
- rather than in a git working copy), then we fall back on reading the
- contents of the RELEASE-VERSION file.
-
- To use this script, simply import it your setup.py file, and use the
- results of get_git_version() as your package version:
-
- from version import *
-
- setup(
-     version=get_git_version(),
-     .
-     .
-     .
+Based on work from Douglas Creager <[email protected]>
+
+Gets the current version number.  If possible, this is the
+output of "git describe", modified to conform to the versioning
+scheme that setuptools uses.  If "git describe" returns an error
+(most likely because we're in an unpacked copy of a release tarball,
+rather than in a git working copy), then we fall back on reading the
+contents of the RELEASE-VERSION file.
+
+To use this script, simply import it your setup.py file, and use the
+results of get_version() as your package version:
+
+from autotest.client.shared import version
+
+setup(
+    version=get_version(),
+    .
+    .
+    .
 )
 
- This will automatically update the RELEASE-VERSION file, if
- necessary.  Note that the RELEASE-VERSION file should *not* be
- checked into git; please add it to your top-level .gitignore file.
+This will automatically update the RELEASE-VERSION file, if
+necessary.  Note that the RELEASE-VERSION file should *not* be
+checked into git; please add it to your top-level .gitignore file.
 
- You'll probably want to distribute the RELEASE-VERSION file in your
- sdist tarballs; to do this, just create a MANIFEST.in file that
- contains the following line:
+You'll probably want to distribute the RELEASE-VERSION file in your
+sdist tarballs; to do this, just create a MANIFEST.in file that
+contains the following line:
 
-   include RELEASE-VERSION
+include RELEASE-VERSION
 """
-__all__ = ("get_git_version")
+__all__ = ("get_version")
+
 
-from subprocess import Popen, PIPE
+import os, sys
+import common
+from autotest.client import utils
+from autotest.client.shared import error
+
+_ROOT_PATH = os.path.join(sys.modules[__name__].__file__, "..", "..")
+_ROOT_PATH = os.path.abspath(_ROOT_PATH)
+RELEASE_VERSION_PATH = os.path.join(_ROOT_PATH, 'RELEASE-VERSION')
 
 
 def call_git_describe(abbrev=4):
     try:
-        p = Popen(['git', 'describe', '--abbrev=%d' % abbrev],
-                  stdout=PIPE, stderr=PIPE)
-        p.stderr.close()
-        line = p.stdout.readlines()[0]
-        return line.strip()
-
-    except:
+        command = 'git describe --abbrev=%d' % abbrev
+        return utils.system_output(command, verbose=False)
+    except error.CmdError:
         return None
 
 
 def read_release_version():
     try:
-        f = open("RELEASE-VERSION", "r")
-
+        f = open(RELEASE_VERSION_PATH, "r")
         try:
             version = f.readlines()[0]
             return version.strip()
-
         finally:
             f.close()
-
     except:
         return None
 
 
 def write_release_version(version):
-    f = open("RELEASE-VERSION", "w")
+    f = open(RELEASE_VERSION_PATH, "w")
     f.write("%s\n" % version)
     f.close()
 
 
-def get_git_version(abbrev=4):
-    # Read in the version that's currently in RELEASE-VERSION.
-
+def get_version(abbrev=4):
     release_version = read_release_version()
-
-    # First try to get the current version using “git describe”.
-
     version = call_git_describe(abbrev)
 
-    # If that doesn't work, fall back on the value that's in
-    # RELEASE-VERSION.
-
     if version is None:
         version = release_version
 
-    # If we still don't have anything, that's an error.
-
     if version is None:
         raise ValueError("Cannot find the version number!")
 
-    # If the current version is different from what's in the
-    # RELEASE-VERSION file, update the file to be current.
-
     if version != release_version:
         write_release_version(version)
 
-    # Finally, return the current version.
-
     return version
 
 
 if __name__ == "__main__":
-    print get_git_version()
+    print "Current version: %s" % get_version()
diff --git a/database/setup.py b/database/setup.py
index 30517eb..fc663f9 100644
--- a/database/setup.py
+++ b/database/setup.py
@@ -16,7 +16,7 @@ setup(name='autotest',
       description='Autotest test framework - results database module',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.database': db_dir },
       package_data={'autotest.database' : ['*.sql' ] },
diff --git a/frontend/setup.py b/frontend/setup.py
index e6bef67..ab3f631 100644
--- a/frontend/setup.py
+++ b/frontend/setup.py
@@ -39,7 +39,7 @@ setup(name='autotest',
       description='Autotest test framework - rpc server',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.frontend': fe_dir },
       package_data={'autotest.frontend' : pd_filelist },
diff --git a/mirror/setup.py b/mirror/setup.py
index 8a46672..8676349 100644
--- a/mirror/setup.py
+++ b/mirror/setup.py
@@ -16,7 +16,7 @@ setup(name='autotest',
       description='Autotest testing framework - mirror module',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.mirror': mirror_dir },
       packages=['autotest.mirror' ],
diff --git a/scheduler/setup.py b/scheduler/setup.py
index 1b1ecf2..0aba98d 100644
--- a/scheduler/setup.py
+++ b/scheduler/setup.py
@@ -16,7 +16,7 @@ setup(name='autotest',
       description='Autotest testing framework - scheduler module',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.scheduler': scheduler_dir },
       package_data={'autotest.scheduler': ['archive_results.control.srv']},
diff --git a/server/setup.py b/server/setup.py
index 3a24392..3476ce5 100644
--- a/server/setup.py
+++ b/server/setup.py
@@ -33,7 +33,7 @@ setup(name='autotest',
       description='Autotest testing framework - remote module',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.server': server_dir },
       package_data={'autotest.server' : pd_filelist },
diff --git a/tko/setup.py b/tko/setup.py
index c8562c7..062def9 100644
--- a/tko/setup.py
+++ b/tko/setup.py
@@ -18,7 +18,7 @@ setup(name='autotest',
       description='Autotest testing framework - tko module',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.tko': tko_dir },
       package_data={'autotest.tko' : ['*.cgi',
diff --git a/utils/setup.py b/utils/setup.py
index ca74368..04ac43c 100644
--- a/utils/setup.py
+++ b/utils/setup.py
@@ -17,7 +17,7 @@ setup(name='autotest',
       description='Autotest testing framework - utility scripts',
       author='Autotest Team',
       author_email='[email protected]',
-      version=version.get_git_version(),
+      version=version.get_version(),
       url='autotest.kernel.org',
       package_dir={'autotest.utils': utils_dir },
       package_data={'autotest.utils' : ['named_semaphore/*',
-- 
1.8.0

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to