about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2019-03-07 16:18:43 +0100
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2019-03-07 16:18:43 +0100
commit7508a7f892ef9c9db7b4e2ec73bc6cd0da0121c5 (patch)
tree0e83f6e4334a32acce2ab67d1b2587a5f2ceb44e
parent1c2dfa3b999fa693d4bc3c10c06f01c896fb7365 (diff)
downloadmiasm-7508a7f892ef9c9db7b4e2ec73bc6cd0da0121c5.tar.gz
miasm-7508a7f892ef9c9db7b4e2ec73bc6cd0da0121c5.zip
Setup: add version to setup for future pip
-rw-r--r--miasm/__init__.py82
-rwxr-xr-xsetup.py7
2 files changed, 86 insertions, 3 deletions
diff --git a/miasm/__init__.py b/miasm/__init__.py
index b7dbe3b4..417a6268 100644
--- a/miasm/__init__.py
+++ b/miasm/__init__.py
@@ -1 +1,83 @@
 "Reverse engineering framework in Python"
+
+# Version generation stolen from Scapy
+
+import os
+import re
+import subprocess
+
+_MIASM_PKG_DIR = os.path.dirname(__file__)
+
+def _version_from_git_describe():
+    """
+    Read the version from ``git describe``. It returns the latest tag with an
+    optional suffix if the current directory is not exactly on the tag.
+
+    Example::
+
+        $ git describe --always
+        v2.3.2-346-g164a52c075c8
+
+    The tag prefix (``v``) and the git commit sha1 (``-g164a52c075c8``) are
+    removed if present.
+
+    If the current directory is not exactly on the tag, a ``.devN`` suffix is
+    appended where N is the number of commits made after the last tag.
+
+    Example::
+
+        >>> _version_from_git_describe()
+        '2.3.2.dev346'
+    """
+    if not os.path.isdir(os.path.join(os.path.dirname(_MIASM_PKG_DIR), '.git')):  # noqa: E501
+        raise ValueError('not in miasm git repo')
+
+    process = subprocess.Popen(['git', 'describe', '--always', '--tags'],
+                               cwd=_MIASM_PKG_DIR,
+                               stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+    out, err = process.communicate()
+
+    if process.returncode == 0:
+        tag = out.decode().strip()
+        match = re.match('^v?(.+?)-(\\d+)-g[a-f0-9]+$', tag)
+        if match:
+            # remove the 'v' prefix and add a '.devN' suffix
+            return '%s.dev%s' % (match.group(1), match.group(2))
+        else:
+            # just remove the 'v' prefix
+            return re.sub('^v', '', tag)
+    else:
+        raise subprocess.CalledProcessError(process.returncode, err)
+
+
+def _version():
+    version_file = os.path.join(_MIASM_PKG_DIR, 'VERSION')
+    try:
+        tag = _version_from_git_describe()
+        # successfully read the tag from git, write it in VERSION for
+        # installation and/or archive generation.
+        with open(version_file, 'w') as fdesc:
+            fdesc.write(tag)
+        return tag
+    except Exception:
+        # failed to read the tag from git, try to read it from a VERSION file
+        try:
+            with open(version_file, 'r') as fdsec:
+                tag = fdsec.read()
+            return tag
+        except Exception:
+            # Rely on git archive "export-subst" git attribute.
+            # See 'man gitattributes' for more details.
+            git_archive_id = '$Format:%h %d$'
+            sha1 = git_archive_id.strip().split()[0]
+            match = re.search('tag:(\\S+)', git_archive_id)
+            if match:
+                return "git-archive.dev" + match.group(1)
+            elif sha1:
+                return "git-archive.dev" + sha1
+            else:
+                return 'unknown.version'
+
+
+VERSION = __version__ = _version()
diff --git a/setup.py b/setup.py
index 81f8e4a2..9a687160 100755
--- a/setup.py
+++ b/setup.py
@@ -140,13 +140,14 @@ def buil_all():
         print("build with", repr(name))
         try:
             s = setup(
-                name = "Miasm",
-                version = "2.0",
+                name = "miasm",
+                version = __import__("miasm").VERSION,
                 packages = packages,
                 package_data = {
-                    "miasm":[
+                    "miasm": [
                         "jitter/*.h",
                         "jitter/arch/*.h",
+                        "VERSION"
                     ]
                 },
                 ext_modules = ext_modules,