blob: 4f38578082867078af4a8a149a0e71b2abd9409f [file] [log] [blame]
## This file is part of Scapy
## See for more informations
## Copyright (C) Philippe Biondi <>
## This program is published under a GPLv2 license
Scapy: create, send, sniff, dissect and manipulate network packets.
Usable either from an interactive console or as a Python library.
import os
import re
import subprocess
_SCAPY_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.
$ git describe --always
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.
>>> _version_from_git_describe()
if not os.path.isdir(os.path.join(os.path.dirname(_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)
out, err = p.communicate()
if p.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 '' % (,
# just remove the 'v' prefix
return re.sub('^v', '', tag)
raise subprocess.CalledProcessError(p.returncode, err)
def _version():
version_file = os.path.join(_SCAPY_PKG_DIR, 'VERSION')
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 f:
return tag
# failed to read the tag from git, try to read it from a VERSION file
with open(version_file, 'r') as f:
tag =
return tag
# 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 ='tag:(\\S+)', git_archive_id)
if match:
return "" +
elif sha1:
return "" + sha1
return 'unknown.version'
VERSION = _version()
if __name__ == "__main__":
from scapy.main import interact