<commit_before>#!/usr/bin/env python
"""
Determine package version for git repositories.

Each time this file is imported it checks if the ".git" folder is
present and if so, obtains the version from the git history using
`git describe`. This information is then stored in the file
`_version_save.py` which is not versioned by git, but distributed
along with e.g. pypi.
"""
from __future__ import print_function

import imp
import os
from os.path import join, abspath, dirname
import subprocess
import time
import traceback

def git_describe():
    """
    Returns a string describing the version returned by the
    command `git describe --tags HEAD`.
    If it is not possible to determine the correct version,
    then an empty string is returned.
    """
    # make sure we are in a directory that belongs to the correct
    # repository.
    ourdir  = dirname(abspath(__file__))
    
    def _minimal_ext_cmd(cmd):
        # construct minimal environment
        env = {}
        for k in ['SYSTEMROOT', 'PATH']:
            v = os.environ.get(k)
            if v is not None:
                env[k] = v
        # LANGUAGE is used on win32
        env['LANGUAGE'] = 'C'
        env['LANG'] = 'C'
        env['LC_ALL'] = 'C'
        out = subprocess.Popen(cmd, stdout = subprocess.PIPE, env=env).communicate()[0]
        return out

    # change directory
    olddir = abspath(os.curdir)
    os.chdir(ourdir)
    
    try:
        out = _minimal_ext_cmd(['git', 'describe', '--tags', 'HEAD'])
        GIT_REVISION = out.strip().decode('ascii')
    except OSError:
        GIT_REVISION = ""
        
    # go back to original directory
    os.chdir(olddir)
    
    return GIT_REVISION


def save_version(version, versionfile):
    data="""#!/usr/bin/env python
# This file was created automatically.
longversion="{VERSION}"
"""
    with open(versionfile, "w") as fd:
        fd.write(data.format(VERSION=version))

versionfile = join(dirname(abspath(__file__)), "_version_save.py")

## Determine the accurate version
longversion = ""

# 1. git describe
try:
    # Get the version using `git describe`
    longversion = git_describe()
except:
    pass

# 2. previously created version file
if len(longversion) == "":
    # Either this is this is not a git repository or we are in the
    # wrong git repository.
    # Get the version from the previously generated `_version_save.py`
    try:
        _version_save = imp.load_source("_version_save", versionfile)
        longversion = _version_save.longversion
    except:
        pass

# 3. last resort: date
if len(longversion) == "":
    print("Could not determine version. Reason:")
    print(traceback.format_exc())
    ctime = os.stat(__file__)[8]
    longversion = time.strftime("%Y.%m.%d-%H-%M-%S", time.gmtime(ctime))
    print("Using creation time to determine version: {}".format(longversion))

# Save the version to `_version_save.py` to allow distribution using
# `python setup.py sdist`.
save_version(longversion, versionfile)
<commit_msg>update odtbrain/_version.py with git_cast_file2repos.py<commit_after>#!/usr/bin/env python
"""
Determine package version for git repositories.

Each time this file is imported it checks if the ".git" folder is
present and if so, obtains the version from the git history using
`git describe`. This information is then stored in the file
`_version_save.py` which is not versioned by git, but distributed
along with e.g. pypi.
"""
from __future__ import print_function

import imp
import os
from os.path import join, abspath, dirname
import subprocess
import sys
import time
import traceback

def git_describe():
    """
    Returns a string describing the version returned by the
    command `git describe --tags HEAD`.
    If it is not possible to determine the correct version,
    then an empty string is returned.
    """
    # make sure we are in a directory that belongs to the correct
    # repository.
    ourdir  = dirname(abspath(__file__))
    
    def _minimal_ext_cmd(cmd):
        # construct minimal environment
        env = {}
        for k in ['SYSTEMROOT', 'PATH']:
            v = os.environ.get(k)
            if v is not None:
                env[k] = v
        # LANGUAGE is used on win32
        env['LANGUAGE'] = 'C'
        env['LANG'] = 'C'
        env['LC_ALL'] = 'C'
        out = subprocess.Popen(cmd, stdout = subprocess.PIPE, env=env).communicate()[0]
        return out

    # change directory
    olddir = abspath(os.curdir)
    os.chdir(ourdir)
    
    try:
        out = _minimal_ext_cmd(['git', 'describe', '--tags', 'HEAD'])
        GIT_REVISION = out.strip().decode('ascii')
    except OSError:
        GIT_REVISION = ""
        
    # go back to original directory
    os.chdir(olddir)
    
    return GIT_REVISION


def save_version(version, versionfile):
    data="""#!/usr/bin/env python
# This file was created automatically.
longversion="{VERSION}"
"""
    with open(versionfile, "w") as fd:
        fd.write(data.format(VERSION=version))

versionfile = join(dirname(abspath(__file__)), "_version_save.py")

## Determine the accurate version
longversion = ""

# 1. git describe
try:
    # Get the version using `git describe`
    longversion = git_describe()
except:
    pass

# 2. previously created version file
if len(longversion) == "":
    # Either this is this is not a git repository or we are in the
    # wrong git repository.
    # Get the version from the previously generated `_version_save.py`
    try:
        _version_save = imp.load_source("_version_save", versionfile)
        longversion = _version_save.longversion
    except:
        try:
            from ._version_save import longversion
        except:
            try:
                from _version_save import longversion
            except:
                pass

# 3. last resort: date
if len(longversion) == "":
    print("Could not determine version. Reason:")
    print(traceback.format_exc())
    ctime = os.stat(__file__)[8]
    longversion = time.strftime("%Y.%m.%d-%H-%M-%S", time.gmtime(ctime))
    print("Using creation time to determine version: {}".format(longversion))

if not hasattr(sys, 'frozen'):
    # Save the version to `_version_save.py` to allow distribution using
    # `python setup.py sdist`.
    # This is only done if the program is not frozen (with e.g. pyinstaller),
    # because in that case we assume that we
    save_version(longversion, versionfile)

{'fraction_non_alphanumeric': 0.10284019975031211, 'fraction_numerical': 0.0023408239700374533, 'mean_word_length': 2.919877675840979, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2184326', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1892, 'n_words': 646}