From 4584f36a4c18c4972d6f224084a5fd350d358b65 Mon Sep 17 00:00:00 2001
From: Robin Jarry <robin.jarry@6wind.com>
Date: Tue, 21 Mar 2017 18:00:38 +0100
Subject: [PATCH] Only read version from git when in scapy

Scapy may be used as third party library by other projects, themselves
managed under git. This leads to surprising results such as this:

    $ git describe
    awesomeproject-v5.22-1-ga1316614c290
    $ virtualenv env
    ...
    $ . env/bin/activate
    (env)$ pip install scapy==2.3.3
    ...
    (env)$ cat env/lib/python2.7/site-packages/scapy/VERSION
    2.3.3
    (env)$ which scapy
    /home/.../env/bin/scapy
    (env)$ scapy
    ...
    Welcome to Scapy (awesomeproject-v5.22.dev1)
    >>>

Scapy's version is wrongly set to the current project's version.

When trying to determine scapy's version from git, make sure that the
code is executed from the root of a git repo. If not, read the version
from the scapy/VERSION file which has been generated when packaging
scapy source archive.

Fixes: 4f71027fcdcb ("enhance version management")
Signed-off-by: Robin Jarry <robin.jarry@6wind.com>
---
 scapy/__init__.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/scapy/__init__.py b/scapy/__init__.py
index b3bb3d0d..a0e7094d 100644
--- a/scapy/__init__.py
+++ b/scapy/__init__.py
@@ -38,6 +38,9 @@ def _version_from_git_describe():
         >>> _version_from_git_describe()
         '2.3.2.dev346'
     """
+    if not os.path.isdir(os.path.join(_SCAPY_PKG_DIR, '../.git')):
+        raise ValueError('not in scapy git repo')
+
     p = subprocess.Popen(['git', 'describe', '--always'], cwd=_SCAPY_PKG_DIR,
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
-- 
GitLab