<commit_before>#!/usr/bin/env python
# coding: utf-8

from __future__ import print_function

import os
import subprocess
import sys

from odoo_connection import Odoo10Context, context_mapping
from test_server import (get_addons_path, get_addons_to_check, get_depends,
                         get_server_path, parse_list)
from travis_helpers import red, yellow, yellow_light
from weblate_client import get_projects, lock, wl_pull, wl_push


def po_rm_header(po_content):
    is_header = True
    rm_header = str()
    header = str()
    for line in po_content.splitlines():
        if line.startswith('#.'):
            is_header = False
        if is_header:
            header += line + '\n'
            continue
        rm_header += line + '\n'
    rm_header = rm_header.rstrip('\n') + '\n'
    return header, rm_header


def main(argv=None):
    """
    Export translation files and push them to weblate
    The weblate token should be encrypted in .travis.yml
    If not, export exits early.
    """
    if argv is None:
        argv = sys.argv

    weblate_token = os.environ.get("WEBLATE_TOKEN")
    weblate_host = os.environ.get(
        "WEBLATE_HOST", "https://weblate.vauxoo.com/api/")

    if not weblate_token:
        print(yellow_light("WARNING! Weblate token not defined- "
              "exiting early."))
        return 1

    if not weblate_host:
        print(yellow_light("WARNING! Weblate host not recognized- "
              "exiting early."))
        return 1

    travis_home = os.environ.get("HOME", "~/")
    travis_build_dir = os.environ.get("TRAVIS_BUILD_DIR", "../..")
    travis_repo_slug = os.environ.get("TRAVIS_REPO_SLUG")
    gh_token = os.environ.get("GH_TOKEN")
    travis_branch = os.environ.get("TRAVIS_BRANCH")
    travis_repo_shortname = travis_repo_slug.split("/")[1]
    odoo_exclude = os.environ.get("EXCLUDE")
    odoo_include = os.environ.get("INCLUDE")
    odoo_version = os.environ.get("VERSION")
    langs = parse_list(os.environ.get("LANG_ALLOWED", ""))

    odoo_full = os.environ.get("ODOO_REPO", "odoo/odoo")
    server_path = get_server_path(odoo_full, odoo_version, travis_home)
    addons_path = get_addons_path(travis_home, travis_build_dir, server_path)
    addons_list = get_addons_to_check(travis_build_dir, odoo_include,
                                      odoo_exclude)
    addons_path_list = parse_list(addons_path)
    all_depends = get_depends(addons_path_list, addons_list)
    main_modules = set(os.listdir(travis_build_dir))
    main_depends = main_modules & all_depends
    addons_list = list(main_depends)
    database = "openerp_test"
    # Use by default version 10 connection context
    connection_context = context_mapping.get(odoo_version, Odoo10Context)
    current_branch = travis_branch
    wlprojects = get_projects(travis_repo_shortname, current_branch)
    try:
        # first project found
        wlproject = wlprojects.next()
    except StopIteration as sie:
        print(red("Project Weblate %s not found for branch %s" % (
            travis_repo_shortname, current_branch)))
        raise sie
    with connection_context(server_path, addons_path, database) \
            as odoo_context, lock(wlproject, addons_list):
        try:
            subprocess.check_output([<commit_msg>[FIX] travis: check enviroment variable ODOO_BRANCH (#194)<commit_after>#!/usr/bin/env python
# coding: utf-8

from __future__ import print_function

import os
import subprocess
import sys

from odoo_connection import Odoo10Context, context_mapping
from test_server import (get_addons_path, get_addons_to_check, get_depends,
                         get_server_path, parse_list)
from travis_helpers import red, yellow, yellow_light
from weblate_client import get_projects, lock, wl_pull, wl_push


def po_rm_header(po_content):
    is_header = True
    rm_header = str()
    header = str()
    for line in po_content.splitlines():
        if line.startswith('#.'):
            is_header = False
        if is_header:
            header += line + '\n'
            continue
        rm_header += line + '\n'
    rm_header = rm_header.rstrip('\n') + '\n'
    return header, rm_header


def main(argv=None):
    """
    Export translation files and push them to weblate
    The weblate token should be encrypted in .travis.yml
    If not, export exits early.
    """
    if argv is None:
        argv = sys.argv

    weblate_token = os.environ.get("WEBLATE_TOKEN")
    weblate_host = os.environ.get(
        "WEBLATE_HOST", "https://weblate.vauxoo.com/api/")

    if not weblate_token:
        print(yellow_light("WARNING! Weblate token not defined- "
              "exiting early."))
        return 1

    if not weblate_host:
        print(yellow_light("WARNING! Weblate host not recognized- "
              "exiting early."))
        return 1

    travis_home = os.environ.get("HOME", "~/")
    travis_build_dir = os.environ.get("TRAVIS_BUILD_DIR", "../..")
    travis_repo_slug = os.environ.get("TRAVIS_REPO_SLUG")
    gh_token = os.environ.get("GH_TOKEN")
    travis_branch = os.environ.get("TRAVIS_BRANCH")
    travis_repo_shortname = travis_repo_slug.split("/")[1]
    odoo_exclude = os.environ.get("EXCLUDE")
    odoo_include = os.environ.get("INCLUDE")
    odoo_version = os.environ.get("VERSION")
    odoo_branch = os.environ.get("ODOO_BRANCH")
    langs = parse_list(os.environ.get("LANG_ALLOWED", ""))

    odoo_full = os.environ.get("ODOO_REPO", "odoo/odoo")
    server_path = get_server_path(
        odoo_full, odoo_branch or odoo_version, travis_home)
    addons_path = get_addons_path(travis_home, travis_build_dir, server_path)
    addons_list = get_addons_to_check(travis_build_dir, odoo_include,
                                      odoo_exclude)
    addons_path_list = parse_list(addons_path)
    all_depends = get_depends(addons_path_list, addons_list)
    main_modules = set(os.listdir(travis_build_dir))
    main_depends = main_modules & all_depends
    addons_list = list(main_depends)
    database = "openerp_test"
    # Use by default version 10 connection context
    connection_context = context_mapping.get(odoo_version, Odoo10Context)
    current_branch = travis_branch
    wlprojects = get_projects(travis_repo_shortname, current_branch)
    try:
        # first project found
        wlproject = wlprojects.next()
    except StopIteration as sie:
        print(red("Project Weblate %s not found for branch %s" % (
            travis_repo_shortname, current_branch)))
        raise sie
    with connection_context(server_path, addons_path, database) \
            as odoo_context, lock(wlproject, addons_list):
        try:
            subprocess.check_output([
{'fraction_non_alphanumeric': 0.09713256267827654, 'fraction_numerical': 0.0034529349947455337, 'mean_word_length': 3.0996923076923077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1648922', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2245, 'n_words': 462}