<commit_before># -*- coding: utf-8 -*-
import os
import sys
import re
import fnmatch
import json
import shutil
from tempfile import mkstemp

from paver.easy import *
from paver.doctools import html

options(
    gee=Bunch(
    )
)


# Function to find and replace in a file
def _replace(file_path, regex, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    with os.fdopen(fh, 'w') as new_file:
        with open(file_path) as old_file:
            for line in old_file:
                new_file.write(regex.sub(subst, line))
    os.remove(file_path)
    shutil.move(abs_path, file_path)


@task
@cmdopts([
    ('version=', 'v', 'Version to set'),
])
def set_version(options):
    v = getattr(options, 'version', False)
    if not v or not re.match("[0-9]+_[0-9]+", v):
        print('Must specify a valid version (example: 0_36)')
        return

    f = 'land_cover/configuration.json'
    # Validate the version matches the regex

    print('Setting version to {} in {}'.format(v, f))
    id_regex = re.compile(', "id": "[0-9a-z-]*"')
    _replace(f, id_regex, '')

    name_regex = re.compile('("name": "[0-9a-zA-Z -]*)( [0-9]+_[0-9]+)?"')
    name_sub = '\g<1> ' + v + '"'
    _replace(f, name_regex, name_sub)

    # Update the trends.earth scripts.json


# def login(options):
#
#
# def publish_all(options):
#
# def new_version(options):
#     # Go through all GEE scripts and


@task
@consume_args
def pep8(args):
    """Check code for PEP8 violations"""
    try:
        import pep8
    except:
        error('pep8 not found! Run "pip install autopep8".')
        sys.exit(1)<commit_msg>Add function to publish all scripts and set versions.<commit_after># -*- coding: utf-8 -*-
import os
import sys
import re
import fnmatch
import json
import shutil
import subprocess
from tempfile import mkstemp

from paver.easy import *
from paver.doctools import html

options(
    gefcli=path('C:/Users/azvol/Code/LandDegradation/gef-cli/gefcli'),
    gefcli_venv_script=path('C:/Users/azvol/Code/LandDegradation/gef-cli/venv/Scripts/activate'),
)


# Function to find and replace in a file
def _replace(file_path, regex, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    with os.fdopen(fh, 'w') as new_file:
        with open(file_path) as old_file:
            for line in old_file:
                new_file.write(regex.sub(subst, line))
    os.remove(file_path)
    shutil.move(abs_path, file_path)


@task
@cmdopts([
    ('version=', 'v', 'Version to set'),
])
def set_version(options):
    v = getattr(options, 'version', False)
    v = v.replace('.', '_')
    if not v or not re.match("[0-9]+_[0-9]+", v):
        print('Must specify a valid version (example: 0.36)')
        return

    id_regex = re.compile('(, )?"id": "[0-9a-z-]*"(, )?')
    name_regex = re.compile('("name": "[0-9a-zA-Z -]*)( [0-9]+_[0-9]+)?"')

    for subdir, dirs, files in os.walk('.'):
        for file in files:
            if file == 'configuration.json':
                filepath = os.path.join(subdir, file)
                # Validate the version matches the regex
                print('Setting version to {} in {}'.format(v, filepath))
                # Update the version string
                _replace(filepath, name_regex, '\g<1> ' + v + '"')
                # Clear the ID since a new one will be assigned due to the new name
                _replace(filepath, id_regex, '')


@task
def publish_all(options):
    dirs = next(os.walk('.'))[1]
    # print('Activating venv...')
    # subprocess.check_call(['source', options.gefcli_venv_script])
    subprocess.check_call(['python', options.gefcli, 'login'])
    for dir in dirs:
        if os.path.exists(os.path.join(dir, 'configuration.json')):
            print('Publishing {}...'.format(dir))
            subprocess.check_call(['python', options.gefcli, 'publish'], cwd=dir)

    #TODO: Make the scripts public after publishing


@task
@consume_args
def pep8(args):
    """Check code for PEP8 violations"""
    try:
        import pep8
    except:
        error('pep8 not found! Run "pip install autopep8".')
        sys.exit(1)
{'fraction_non_alphanumeric': 0.148193359375, 'fraction_numerical': 0.011474609375, 'mean_word_length': 2.8253968253968256, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, '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': '2697898', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1391, 'n_words': 377}