File: git_parser.py

package info (click to toggle)
haproxy 3.2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,924 kB
  • sloc: ansic: 267,927; sh: 3,466; xml: 1,756; python: 1,345; makefile: 1,155; perl: 168; cpp: 21
file content (43 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Methods to get the version tag of a git directory.
Really nothing's fancy here
"""

import os
import sys
import subprocess
import re

def get_git_version_from_cwd():
    """
    Fetch the last known version of the HAProxy current repository
    """
    return get_git_version_in_path(os.getcwd())


def get_git_version_in_path(path):
    """
    Fetch the last known version of the git repository given as an argument
    """
    if not path or not os.path.isdir(os.path.join(path,".git")):
        print("This does not appear to be a Git repository.", file=sys.stderr)
        return

    try:
        p = subprocess.Popen(["git", "describe", "--tags", "--match", "v*"],
                             cwd=path,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    except EnvironmentError:
        return False
    version = p.communicate()[0]

    if p.returncode != 0:
        return False

    if len(version) < 2:
        return False

    version = version.decode().lstrip('v').rstrip()  # remove the 'v' tag and the EOL char
    version = re.sub(r'-g.*', '', version)
    return version