• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1## This file is part of Scapy
2## See http://www.secdev.org/projects/scapy for more informations
3## Copyright (C) Philippe Biondi <phil@secdev.org>
4## This program is published under a GPLv2 license
5
6"""
7Scapy: create, send, sniff, dissect and manipulate network packets.
8
9Usable either from an interactive console or as a Python library.
10http://www.secdev.org/projects/scapy
11"""
12
13import os
14import re
15import subprocess
16
17
18_SCAPY_PKG_DIR = os.path.dirname(__file__)
19
20def _version_from_git_describe():
21    """
22    Read the version from ``git describe``. It returns the latest tag with an
23    optional suffix if the current directory is not exactly on the tag.
24
25    Example::
26
27        $ git describe --always
28        v2.3.2-346-g164a52c075c8
29
30    The tag prefix (``v``) and the git commit sha1 (``-g164a52c075c8``) are
31    removed if present.
32
33    If the current directory is not exactly on the tag, a ``.devN`` suffix is
34    appended where N is the number of commits made after the last tag.
35
36    Example::
37
38        >>> _version_from_git_describe()
39        '2.3.2.dev346'
40    """
41    if not os.path.isdir(os.path.join(os.path.dirname(_SCAPY_PKG_DIR), '.git')):
42        raise ValueError('not in scapy git repo')
43
44    p = subprocess.Popen(['git', 'describe', '--always'], cwd=_SCAPY_PKG_DIR,
45                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
46
47    out, err = p.communicate()
48
49    if p.returncode == 0:
50        tag = out.decode().strip()
51        match = re.match('^v?(.+?)-(\\d+)-g[a-f0-9]+$', tag)
52        if match:
53            # remove the 'v' prefix and add a '.devN' suffix
54            return '%s.dev%s' % (match.group(1), match.group(2))
55        else:
56            # just remove the 'v' prefix
57            return re.sub('^v', '', tag)
58    else:
59        raise subprocess.CalledProcessError(p.returncode, err)
60
61def _version():
62    version_file = os.path.join(_SCAPY_PKG_DIR, 'VERSION')
63    try:
64        tag = _version_from_git_describe()
65        # successfully read the tag from git, write it in VERSION for
66        # installation and/or archive generation.
67        with open(version_file, 'w') as f:
68            f.write(tag)
69        return tag
70    except:
71        # failed to read the tag from git, try to read it from a VERSION file
72        try:
73            with open(version_file, 'r') as f:
74                tag = f.read()
75            return tag
76        except:
77            # Rely on git archive "export-subst" git attribute.
78            # See 'man gitattributes' for more details.
79            git_archive_id = '$Format:%h %d$'
80            sha1 = git_archive_id.strip().split()[0]
81            match = re.search('tag:(\\S+)', git_archive_id)
82            if match:
83                return "git-archive.dev" + match.group(1)
84            elif sha1:
85                return "git-archive.dev" + sha1
86            else:
87                return 'unknown.version'
88
89VERSION = _version()
90
91if __name__ == "__main__":
92    from scapy.main import interact
93    interact()
94