Best Python code snippet using avocado_python
bugs_repartition.py
Source:bugs_repartition.py  
...19            return True20        if 'cuda' in fn or 'cudnn' in fn:21            return True22    return False23def git_cmd(git_dir, cmd, *args, **kwargs):24    assert isinstance(cmd, str)25    if not isinstance(git_dir, Path):26        git_dir = Path(git_dir)27    # logger.info('Dir is {}'.format(git_dir))28    env_copy = os.environ.copy()29    cli_args, cli_options = [], []30    for v in args:31        cli_args.append(v)32    for k,v in kwargs.items():33        if len(k) == 1:34            cli_options.append('-{}'.format(k))35        else:36            _k = k.replace('_', '-')37            cli_options.append('--{}'.format(_k))38        if v is not None:39            cli_options.append(v)40    logger.debug('git {cmd} {options} {args}'.format(cmd=cmd, options=' '.join(cli_options), args=' '.join(cli_args)))41    env_copy['GIT_DIR'] = git_dir42    result_cmd = subprocess.run(['git', cmd, *cli_options ,*cli_args], capture_output=True, env=env_copy, cwd=git_dir.parent)43    logger.debug(result_cmd)44    return result_cmd.returncode, result_cmd.stdout.decode("utf-8")45def long_commit_version(git_dir, sha):46    err, v = git_cmd(git_dir, 'rev-parse', sha, verify=None)47    if err != 0:48        raise Exception('Unable to parse version')49    return v.strip()50# To clear branches locally, you can51# git branch -l 'study*'|xargs git branch -D52def ensure_path(p):53    if not isinstance(p, Path):54        return Path(p)55def file_was_deleted(git_dir, path, pre, post):56    err, name_statuses = git_cmd(git_dir, 'diff', pre, post, name_status=None)57    # TODO: Better58    p = ensure_path(git_dir).parent59    f = str(path.relative_to(p))60    for ns in name_statuses.splitlines():61        if f in ns:62            # First char is status63            if ns[0] in {'D','A'}:64                return True65    return False66@cli.command()67@click.argument('csv_file', type=click.Path(dir_okay=False, exists=True))68@click.option('--git-dir', '-d', type=click.Path(exists=True, file_okay=False, resolve_path=True), required=True)69@click.option('--all', '-a', 'use_all', is_flag=True, help='Look through every row even those who do not qualif for study')70@click.option('--insert-trace/--no-insert-trace', default=True)71@click.option('--setup-branch/--no-setup-branch', default=True)72@click.pass_context73def setup_study(ctx, csv_file, git_dir, use_all, insert_trace, setup_branch):74    """ Reads from CSV file and creates appropriate branching structure75    76    Inserts trace optionnaly.77    """78    # TODO: Get CSV from data_utils79    df = du.get_csv(csv_file)80    logger.debug(df.shape)81    logger.debug(df.columns)82    logger.debug(df.dtypes)83    treatable_c, cuda_related_c = 0, 084    85    original_shape = df.shape86    rdf = df[(df.use_for_study != 'no') & (df.use_for_study != '') & (~pd.isna(df.use_for_study))]87    if use_all:88        logger.info('Using entire dataframe')89        rdf = df90    c_trace_header = """#include <lttng/tracef.h>"""91    # Check if we should customize per study92    c_trace_content= """tracef("TracePoint: Bug Triggered");"""93    py_trace_header = ""94    # Check if we should customize per study95    py_trace_content = """print("Tracepoint Bug Triggered")"""96    97    logger.info('{} rows used as starting point'.format(rdf.shape[0]))98    for i, (loc, row) in enumerate(rdf.iterrows()):99        bid = row['pr_number']  # Bug identifier100        xp_n = 'study-pr{}'.format(bid)101        logger.info('[{}/{}] Bug/Experiment name {}'.format(i, len(rdf), xp_n))102        bg_cm, cor_cm = row['buggy_commit'], row['corrected_commit']103        if pd.isna(bg_cm) or pd.isna(cor_cm):104            logger.info('No buggy or corrected commit found for row {}'.format(xp_n))105            continue106        treatable_c += 1107        logger.info('Buggy commit: {}'.format(bg_cm))108        logger.info('Corrected commit: {}'.format(cor_cm))109        # Get files that changed between both commits110        file_changes = trace_inserter.diff_file_paths(git_dir, bg_cm, cor_cm)111        logger.info(file_changes)112        has_cuda = changes_cuda_related(file_changes)113        if has_cuda:114            cuda_related_c += 1115            logger.info('Experience {} is CUDA related'.format(xp_n))116        study_branch, study_branch_buggy, study_branch_corrected  = xp_n, f'{xp_n}-buggy', f'{xp_n}-corrected'117        long_bug = long_commit_version(git_dir, bg_cm)118        long_cor = long_commit_version(git_dir, cor_cm)119        assert long_bug != '' and long_cor != ''120        do_commit = True121        full_auto_trace = True122        if setup_branch:123            # 1 Check if branch exists124            # 2 create branch `xp_n` at corrected125            # 3 checkout branch at `xp_n`126            # 4 revert corrected127            # 5 create branch `xp_n`-corrected from revert128            # 6 revert revert129            # 7 create branch `xp_n`-buggy130            ret, out = git_cmd(git_dir, 'rev-parse', study_branch, quiet=None, verify=None)131            logger.debug('Got return code {}'.format(ret))132            logger.debug('Got output {}'.format(out))133            if ret == 0:134                # Branch exists, just checkout135                git_cmd(git_dir, 'checkout', study_branch)136            else:137                # Ok to create branch name `study_branch` (study branch) starting at `cor_cm` (corrected commit)138                git_cmd(git_dir, 'checkout', cor_cm , B=study_branch)139            # Now at study branch, ex `study-prXXXX`140            pointing = long_commit_version(git_dir, 'HEAD')141            parent = long_commit_version(git_dir, 'HEAD^')142            if pointing == long_cor:143                # study branch still points at corrected version, we revert it144                # assert long_commit_version(git_dir, 'HEAD')[1] == long_cor145                # Revert long_cor which should be HEAD146                err, out = git_cmd(git_dir, 'revert', long_cor, no_edit=None)147                assert err == 0148            else:149                logger.debug('Pointing commit is {}'.format(pointing))150                logger.debug('Parent commit is {}'.format(parent))151                # Study branch pointing at another commit.152                # Check if parent is corrected commit153                assert parent == long_cor154                # Past this point, the commit should be a revert,155                logger.warning('No revert done because commit {} exists at branch {}'.format(pointing, study_branch))156            # 5 create new branch from `study_branch` and checkout `study_branch_corrected`157            git_cmd(git_dir, 'checkout', study_branch , B=study_branch_corrected)158            # Make sure commit pointed by our corrected study branch is the same as the first revert159            assert long_commit_version(git_dir, study_branch_corrected)[1] == long_commit_version(git_dir, study_branch)[1]160            # Revert `study_branch`, which points at the revert161            git_cmd(git_dir, 'revert', study_branch, no_edit=None)162            # Branch from initial revert to new branch `bt_buggy`163            git_cmd(git_dir, 'checkout', study_branch, B=study_branch_buggy)164            # We should now have 3 branches for the one study, all starting from corrected commit165        top_level = git_cmd(git_dir, 'rev-parse', show_toplevel=None)[1].strip()166        if insert_trace:167            trace_output_p = Path('out/trace_insertions/{}'.format(xp_n))168            trace_output_p.mkdir(exist_ok=True,parents=True)169            for t in {'buggy', 'corrected'}:170                logger.info('Trace inserting for type {}'.format(t))171                if t == 'buggy':172                    # Checkout buggy branch173                    err, _ = git_cmd(git_dir, 'checkout', study_branch_buggy)174                else:175                    err, _ = git_cmd(git_dir, 'checkout', study_branch_corrected)176                assert err == 0177                # TODO: Decide if commit in between178                for f_ix, file_c in enumerate(file_changes):179                    logging.debug('Treating {}'.format(file_c))180                    _f = Path(file_c)181                    repo_file_path = Path(top_level).joinpath(_f)182                    # File deletion does not wrk in this case183                    # if t == 'buggy':184                    #     pre = 185                    if file_was_deleted(git_dir, repo_file_path, bg_cm, cor_cm):186                        logging.info('File {} was deleted in post. Skipping...'.format(repo_file_path))187                        continue188                    assert repo_file_path.exists()189                    prompt_ctx = colorama.Fore.BLUE + "{}[{}/{}] File {}".format(t, f_ix, len(file_changes), repo_file_path) + colorama.Fore.RESET190                    # For buggy version, use insert_pre=True191                    if t == 'buggy':192                        # For buggy version, use insert_pre=True193                        fc, lines_changed = trace_inserter.get_content_and_changed_lines(git_dir, file=file_c, pre=bg_cm, post=cor_cm, insert_pre=True)194                    else:195                        # For corrected version, use inser_post=True196                        fc, lines_changed = trace_inserter.get_content_and_changed_lines(git_dir, file=file_c, pre=bg_cm, post=cor_cm, insert_post=True)197                    if _f.suffix in {'.cpp', '.h'}:198                        trace_content, trace_header = c_trace_content, c_trace_header199                    elif _f.suffix in {'.cu', '.cuh'}:200                        logger.warning('Cannot insert traces in CUDA file at the moment. Skipping file...')201                        continue202                    elif _f.suffix in {'.py'}:203                        trace_content, trace_header = py_trace_content, py_trace_header204                    else:205                        logger.warning("File not supported for trace insertion: {}".format(file_c))206                    # See `trace_inserter.insert_trace` for full signature207                    new_file_content = trace_inserter.insert_trace(fc, lines_changed,208                        what=trace_content, header=trace_header,209                        do_prompt=not full_auto_trace,210                        filename=file_c,211                        prompt_ctx=prompt_ctx)212                    if do_commit:213                        with open(repo_file_path, 'w') as f:214                            f.write(new_file_content)215                if do_commit:216                    commit_message = """ "Study: Add traced content" """217                    # TODO: Treat file individually218                    # err, mes = git_cmd(git_dir, 'add', *file_changes)219                    err, mes = git_cmd(git_dir, 'commit', '-am', commit_message)220                    assert err == 0221    logger.info('CUDA related commit: {}'.format(cuda_related_c))222    logger.info('Treatable commit: {}'.format(treatable_c))223if __name__ == "__main__":224    logging.root.setLevel(logging.NOTSET)225    console_h = logging.StreamHandler()226    console_h.setLevel(logging.INFO)227    debug_fh = logging.FileHandler('debug.log', mode='w')228    debug_fh.setLevel(logging.DEBUG)229    logger.addHandler(debug_fh)230    logger.addHandler(console_h)...update_stm32_package.py
Source:update_stm32_package.py  
1#!/usr/bin/python2# SPDX-License-Identifier: Apache-2.03# Copyright (c) 2019 STMicroelectronics.4# This script will update the stm32cube module for Zephyr5# from the latest version on https://github.com/STMicroelectronics6# get the patch between initial version and latest version7# apply this patch on the zephyr module8# usage : 'python update_stm32_package.py <stm32_serie>'9import os10import sys; sys.dont_write_bytecode = True    # do not generate pyc file11import subprocess12import time13import make_stm32_module14import shutil15# execute with argument16option_clean = True17option_force = False18module_serie = ''19arg_names = ['command', 'a1', 'a2', 'a3']20args = map(None, arg_names, sys.argv)21args = {k: v for (k, v) in args}22if args['a1']:23    if 'stm32' in args['a1']:24        module_serie = args['a1']25    elif '-c' in args['a1']:26        option_clean = False27    elif '-f' in args['a1']:28        option_force = True29    elif '-h' in args['a1']:30        print "usage: python update_stm32_package.py [--help] [-f] [-c] stm32XX\n"31        print "       -c    will NOT clean the working repo"32        print "       -f    will force the merge except .rej files"33        print ""34        sys.exit()35    else:36        print 'unknown option'37if args['a2']:38    if 'stm32' in args['a2']:39        module_serie = args['a2']40    elif '-c' in args['a2']:41        option_clean = False42    elif 'f' in args['a2']:43        option_force = True44    else:45        print 'unknown option'46if args['a3']:47    if 'stm32' in args['a3']:48        module_serie = args['a3']49    elif '-c' in args['a3']:50        option_clean = False51    elif '-f' in args['a3']:52        option_force = True53    else:54        print 'unknown option'55if not module_serie or (not args['a1'] and not args['a2'] and not args['a3']):56    print " Usage: " + sys.argv[0] + " stm32XX <Serie to create>"57    print " --> example : python update_stm32_package.py stm32f3"58    sys.exit()59module_seriexx = module_serie + "xx"        # example 'stm32f3xx'60module_SERIE = module_serie.upper()         # example 'STM32F3'61module_SERIExx = module_SERIE + "xx"        # example 'STM32F3xx'62# prepare the dir where to store the module repo63if os.path.exists(os.getenv('ZEPHYR_BASE')):64    os.chdir(os.getenv('ZEPHYR_BASE'))65else:66    print "Error: cannot find ./zephyr project"67    sys.exit()68# 1) clone full repo from github : get the repo STM32CubeXX69repo_path = os.path.join(os.getenv('HOME'), 'STM32Cube_repo')70if not os.path.exists(repo_path):71    os.mkdir(repo_path)72os.chdir(repo_path)73repo_path = os.path.join(repo_path, 'STM32Cube' + module_SERIE[5:])74print "Create repo in "+repo_path75if not os.path.exists(repo_path):76    git_cmd = 'git clone https://github.com/STMicroelectronics/STM32Cube' + module_SERIE[5:] + '.git'77    while os.system(git_cmd) != 0:78        time.sleep(2)79    os.chdir(repo_path)80else:81    # if already exists, then just clean and fetch82    os.chdir(repo_path)83    os.system('git clean -fdx')84    while os.system('git fetch') != 0:85        time.sleep(2)86    os.system('git reset --hard master')87# get the latest version of cube: git tag -l to get a listing of all tags,88# with the most recent one created being the last entry.89git_cmd = 'git checkout master'90os.system(git_cmd)91version_tag = subprocess.check_output('git tag -l', shell=True).splitlines()92latest_version = version_tag[-1]93# 2) prepare a repo where to store module versions94new_repo = os.path.join(os.getenv('HOME'), 'STM32Cube_repo', 'zephyr_module')95if os.path.exists(new_repo):96    shutil.rmtree(new_repo, ignore_errors=True)97os.mkdir(new_repo)98os.chdir(new_repo)99os.system('git init')100# 3) get the version of cube which is in the zephyr module101os.chdir(os.path.join(os.getenv('ZEPHYR_BASE'), '../modules/hal/stm32/stm32cube',102         module_seriexx))103previous_version = subprocess.check_output('cat README | grep version | head -n1',104                                           shell=True)[1:]105previous_version = previous_version[previous_version.index('version ') + 8:-1]106print "Version " + previous_version + " is the original version for " + os.getcwd()107# 3.1) match previous version and list of existing tags which could be vx.y or x.y108pos_version = [i for i, a in enumerate(version_tag) if previous_version in a]109if pos_version:110    previous_version = version_tag[pos_version[0]]111else:112    print "Error: cannot find version " + previous_version + " in STM32Cube_repo"113    make_stm32_module.cleanup(module_serie)114    sys.exit()115# 3.2) do not process if versions are similar116if (previous_version in latest_version) or (latest_version in previous_version):117    print "Versions are identical: abandoned"118    make_stm32_module.cleanup(module_serie)119    sys.exit()120# 4) build the module from this previous version121# reset the STM32Cube repo to this previous version122os.chdir(repo_path)123git_cmd = 'git reset --hard ' + previous_version124print git_cmd125os.system(git_cmd)126# build the zephyr module from the stm32cube127previous_module = make_stm32_module.module(module_serie)128print "Building module from STM32Cube_repo "+previous_version129# populate the new repo with this previous_version130os.renames(previous_module, os.path.join(new_repo, 'stm32cube', module_seriexx))131print "Transfer previous module from " + previous_module + " to " + new_repo132os.chdir(new_repo)133git_cmd = 'git add -A stm32cube/' + module_seriexx + '/*'134print git_cmd135os.system(git_cmd)136git_cmd = 'git commit -am \"module' + previous_version + '\"'137print git_cmd138os.system(git_cmd)139git_cmd = 'git rebase --whitespace=fix HEAD~1'140print "Remove remove trailing spaces: " + git_cmd141os.system(git_cmd)142# 5) build the module from the current version143# clean-up the module144os.chdir(new_repo)145os.system('rm -rf ./stm32cube/*')146# 5.1) and populate the new repo with this current zephyr module147git_cmd = 'cp -rf ' + os.path.join(os.getenv('ZEPHYR_BASE'),148                                   '../modules/hal/stm32/stm32cube',149                                   module_seriexx) + ' ./stm32cube'150print git_cmd151os.system(git_cmd)152# 5.2) commit this current version module153git_cmd = 'git add * && git commit -am \"module\"'154print git_cmd155os.system(git_cmd)156git_cmd = 'git rebase --whitespace=fix HEAD~1'157print "Remove remove trailing spaces: " + git_cmd158os.system(git_cmd)159# 5.3) generate a patch for files and _hal.conf.h file in the module160print 'Building patch from ' + previous_version + ' to current module'161os.chdir(new_repo)162if os.system('git diff --ignore-space-at-eol HEAD^ >> module.patch') == 0:163    os.system('dos2unix module.patch')164hal_conf = os.path.join('stm32cube', module_seriexx, 'drivers', 'include',165                        module_seriexx + '_hal_conf.h')166if os.path.exists(hal_conf):167    git_cmd = 'git diff HEAD@{1} -- ' + hal_conf + ' >> hal_conf.patch'168    os.system(git_cmd)169    os.system('dos2unix hal_conf.patch')170# 6) build the module from this latest version171# reset the STM32Cube repo to this latest version172os.chdir(repo_path)173git_cmd = 'git reset --hard ' + latest_version174print git_cmd175os.system(git_cmd)176# 6.1) include the commit id of this latest version177git_cmd = 'git rev-parse HEAD'178latest_commit = subprocess.check_output(git_cmd, shell=True)179# 6.2) build the zephyr module from the stm32cube180latest_module = make_stm32_module.module(module_serie)181print "Building module from STM32Cube "+latest_version182# 6.3) clean-up the module183os.chdir(new_repo)184git_cmd = 'rm -rf ' + './stm32cube/*'185os.system(git_cmd)186# and populate the new repo with this latest zephyr module187os.renames(latest_module, os.path.join(new_repo, 'stm32cube', module_seriexx))188# include README except CMakelists files from current module and update189git_cmd = 'cp -rf ' + os.path.join(os.getenv('ZEPHYR_BASE'),190                                   '../modules/hal/stm32/stm32cube',191                                   module_seriexx,192                                   'README') + os.path.join(193                                   ' ./stm32cube',194                                   module_seriexx,195                                   'README')196print git_cmd197os.system(git_cmd)198git_cmd = 'cp -rf ' + os.path.join(os.getenv('ZEPHYR_BASE'),199                                   '../modules/hal/stm32/stm32cube',200                                   module_seriexx,201                                   'CMakeLists.txt') + os.path.join(202                                   ' ./stm32cube',203                                   module_seriexx,204                                   'CMakeLists.txt')205print git_cmd206os.system(git_cmd)207# 6.4) update README and CMakeList and copy release note208make_stm32_module.readme(module_serie, latest_version, latest_commit)209make_stm32_module.makelist(module_serie)210make_stm32_module.release_note(module_serie)211# 6.5) apply previous patch on hal_conf.h file212if os.path.exists('hal_conf.patch'):213    git_cmd = 'git apply --recount --3way ./hal_conf.patch'214    os.system(git_cmd + ' &>/dev/null')215    os.remove('hal_conf.patch')216# 6.6) commit files except log or patch files217git_cmd = 'git add * && git reset -- *.patch && git reset -- *.log'218print git_cmd219os.system(git_cmd)220git_cmd = 'git commit -am \"module' + latest_version + '\"'221print git_cmd222os.system(git_cmd)223# 6.7) remove trailing spaces224git_cmd = 'git rebase --whitespace=fix HEAD~1'225print "Remove remove trailing spaces: " + git_cmd226os.system(git_cmd)227# 6.8) generate a patch for each file in the module228if os.system('git diff  HEAD^ >> new_version.patch') == 0:229    os.system('dos2unix new_version.patch')230# 7) build the patch : in the zephyr module repo231print 'Building zephyr module from ' + previous_version + ' to ' + latest_version232module_path = os.path.join(os.getenv('ZEPHYR_BASE'), '../modules/hal/stm32')233os.chdir(module_path)234# 7.1) copy from new_repo235git_cmd = 'rm -rf ' + os.path.join('stm32cube', module_seriexx)236os.system(git_cmd)237git_cmd = 'cp -r ' + os.path.join(new_repo, 'stm32cube', module_seriexx) + ' ' + os.path.join('stm32cube', module_seriexx)238os.system(git_cmd)239# 7.2) apply patch from new repo240module_log = "module_" + module_serie + ".log"241git_cmd = 'git apply --recount --reject ' + os.path.join(new_repo, 'module.patch') + ' &>' + module_log242if os.system(git_cmd):243    print "Error when applying patch to zephyr module: see "+os.path.join(module_path, module_log)244else:245    option_force = True246# 7.3) add files but do not commit247os.system('git add * && git reset -- *.patch && git reset -- *.log && git reset -- *.rej')248print "README file : --> please check that the Patch list is still valid"249# 7.5) merge & commit if needed250if option_force:251    print "Force commit module "252    # to clean the .rej files, use: make_stm32_module.reject(module_serie)253    git_cmd = 'git commit -asm \"stm32cube: update ' + module_serie + ' to version ' + latest_version.upper() + '\n'254    git_cmd = git_cmd + '\n   Update Cube version for ' + module_SERIExx + ' series'255    git_cmd = git_cmd + '\n   on https://github.com/STMicroelectronics'256    git_cmd = git_cmd + '\n   from version ' + previous_version257    git_cmd = git_cmd + '\n   to version ' + latest_version + '\"'258    print git_cmd259    update_list = subprocess.check_output(git_cmd, shell=True).splitlines()260# 8) end261if option_clean:262    make_stm32_module.cleanup(module_serie)263os.chdir(repo_path)264os.system('git reset --hard HEAD &>/dev/null')...views.py
Source:views.py  
1from django.shortcuts import render2import os3import json4import logging5from controller.models import CatalogHistory6from controller.services import device_search7from controller.services import service_utils as svc8from controller.forms import OperationForm9from controller.forms import SearchForm10from controller.serializer import CatalogSerializer11import django_filters12from rest_framework import viewsets13from rest_framework import filters14from rest_framework.views import APIView15from django.http import HttpResponse16from django.http import JsonResponse17from django.http import QueryDict18logger = logging.getLogger('django')19path_prefix = os.getcwd() + '/'20# Class-based views for REST-API21class CatalogViewSet(viewsets.ModelViewSet):22    queryset = CatalogHistory.objects.all()23    serializer_class = CatalogSerializer24class DeviceSearch(APIView):25    def get(self, request):26        if 'qparam' in request.GET:27            logger.info('Device Searching with query to MongoDB ... Provided key is [ {0} ]'.format(request.GET.get('qparam')))28            mongo_key = request.GET.get('qparam')29            search_result = device_search.get_storage_view_from_mongo(user_input=mongo_key)30            return JsonResponse({'result': search_result})31        else:32            return JsonResponse({'result': 'Query parameters not provided. Please add \'?qparam=\' with your URI.'})33class RunAnsible(APIView):34    def post(self, request):35        logger.info('Getting request-bodies and serializing via application REST-API...')36        request_body = CatalogSerializer(data=request.data)37        if request_body.is_valid():38            request_body.save()39            logger.info('Making modifications to ansible configuration file...')40            data = svc.modify_ansible_conf_file(user_input=request.data)41            logger.info('User confirmation accepted. Add and Commit controller/ansible/group_vars/all.yml.')42            git_cmd = '/usr/bin/git add ' + path_prefix + 'controller/ansible/group_vars/all.yml'43            ec, stdout, stderr = svc.kick_command_from_django(cmd=git_cmd)44            logger.info('Started to run ansible-playbook commands !!')45            data_result = []46            ansible_cmd = 'ansible-playbook ' + path_prefix + 'controller/ansible/add_new_volumes.yml'47            ec, stdout, stderr = svc.kick_command_from_django(cmd=ansible_cmd)48            if ec != 0:49                logger.error('Failed to executed ansible command, reverting back the configuration file.')50                git_cmd = '/usr/bin/git reset HEAD ' + path_prefix + 'controller/ansible/group_vars/all.yml'51                ec, stdout, stderr = svc.kick_command_from_django(cmd=git_cmd)52                git_cmd = '/usr/bin/git checkout ' + path_prefix + 'controller/ansible/group_vars/all.yml'53                ec, stdout, stderr = svc.kick_command_from_django(cmd=git_cmd)54                for line in stderr.splitlines():55                    data_result.append(line)56                return HttpResponse(json.dumps({'result': 'Failed during ansible module executing...', 'stdout': data_result}))57            else:58                for line in stdout.splitlines():59                    data_result.append(line)60                # # Updating and commiting the group_vars/all.yml61                # now = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')62                # git_cmd = '/usr/bin/git commit -m \"{0} Operation complete. {1}\"'.format(now, request.data['message'])63                # ec, stdout, stderr = utils.kick_command_from_django(cmd=git_cmd)64                # logger.info('Local git repository updated.')65                return HttpResponse(json.dumps({'result': 'Success !!', 'stdout': data_result}))66        else:67            return HttpResponse(json.dumps({'result': 'Some wrong data provided. Check request-body.'}))68# Functional views for Web UI69def front_main(request):70    view_action = ''71    data_confirm_storage = ''72    data_confirm_server = ''73    data_confirm_switch = ''74    data_result = ''75    result_summary = ''76    mongodb_key = ''77    find_result = []78    search_form = SearchForm()79    ops_form = OperationForm()80    if request.method == 'POST':81        if 'choice' in request.POST:82            search_form = SearchForm()83            view_action = 'select'84        elif 'precheck' in request.POST:85            search_form = SearchForm()86            view_action = 'precheck'87        elif 'search' in request.POST:88            search_form = SearchForm(data=request.POST)89            if search_form.is_valid:90                mongodb_key = str(request.POST['server_name'])91                logger.info('Start device-search for ' + mongodb_key)92                find_result = device_search.get_storage_view_from_mongo(user_input=mongodb_key)93                view_action = 'search_result'94        elif 'start_ops' in request.POST:95            # Default page with blank value96            ops_form = OperationForm()97            view_action = 'operations'98        elif 'confirm' in request.POST:99            ops_form = OperationForm(data=request.POST)100            view_action = 'check_config'101            if ops_form.is_valid():102                logger.info('Making modifications to ansible configuration file...')103                data = svc.modify_ansible_conf_file(user_input=request.POST)104                data_confirm_server, data_confirm_storage, data_confirm_switch = svc.parse_confirm_data(modified_data=data)105                # Throw input data to Django model106                logger.info('Saving catalog data to Django models...')107                CatalogHistory.objects.create(**ops_form.cleaned_data)108                check_result1, check_result2 = svc.get_device_mismatch_check(data=request.POST)109                if check_result1 and check_result2:110                    pass111                else:112                    view_action = 'invalid'113        elif 'back' in request.POST:114            ops_form = OperationForm()115            # Removed records if rollback116            logger.info('Delete the saved data from Django models...')117            CatalogHistory.objects.order_by('-id')[0].delete()118            logger.info('User confirmation declined. Initiatlize operation form, and git checkouted.')119            git_cmd = '/usr/bin/git checkout ' + path_prefix + 'controller/ansible/group_vars/all.yml'120            ec, stdout, stderr = svc.kick_command_from_django(cmd=git_cmd)121            if ec != 0:122                logger.error('Failed to git checkouted.')123            view_action = 'returned'124        elif 'run' in request.POST:125            ops_form = {}126            logger.info('User confirmation accepted. Add and Commit controller/ansible/group_vars/all.yml.')127            git_cmd = '/usr/bin/git add ' + path_prefix + 'controller/ansible/group_vars/all.yml'128            ec, stdout, stderr = svc.kick_command_from_django(cmd=git_cmd)129            logger.info('Started to run ansible-playbook commands !!')130            data_result = []131            ansible_cmd = 'ansible-playbook -vvv ' + path_prefix + 'controller/ansible/add_new_volumes.yml'132            ec, stdout, stderr = svc.kick_command_from_django(cmd=ansible_cmd)133            if ec != 0:134                result_summary = '>>> Ansible Command failed with some errors. '135                logger.error('Failed to executed ansible command, reverting back the configuration file.')136                git_cmd = '/usr/bin/git reset HEAD ' + path_prefix + 'controller/ansible/group_vars/all.yml'137                ec, stdout, stderr = svc.kick_command_from_django(cmd=git_cmd)138                git_cmd = '/usr/bin/git checkout ' + path_prefix + 'controller/ansible/group_vars/all.yml'139                ec, stdout, stderr = svc.kick_command_from_django(cmd=git_cmd)140                for line in stderr.splitlines():141                    data_result.append(line)142            else:143                result_summary = '>>> Successfully Done. Stdout: '144                for line in stdout.splitlines():145                    data_result.append(line)146                # # Updating and commiting the group_vars/all.yml147                # now = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')148                # git_cmd = '/usr/bin/git commit -m \"{0} Operation complete. {1}\"'.format(now, request.data['message'])149                # ec, stdout, stderr = svc.kick_command_from_django(cmd=git_cmd)150                # logger.info('Local git repository updated.')151            view_action = 'run_ansible'152    return render(request, 'home.html',{153        'search_form': search_form,154        'ops_form': ops_form,155        'view_action': view_action,156        'data_confirm_storage': data_confirm_storage,157        'data_confirm_server': data_confirm_server,158        'data_confirm_switch': data_confirm_switch,159        'data_result': data_result,160        'result_summary': result_summary,161        'mongodb_key': mongodb_key,162        'find_result': find_result,163        })164def upload_menu(request):165    return render(request, 'uploads.html')166def history_menu(request):167    c_histories = CatalogHistory.objects.all()168    return render(request, 'histories.html', {169        'histories': c_histories,170    }) 171def catalog_details(request, pk):172    try:173        details = CatalogHistory.objects.get(pk=pk)174    except CatalogHistory.DoesNotExist:175        msg = []176        msg.append('Selected Item does not exist in Django DB.')177        msg.append('Check ID in your URL : \'/history/<ID>\'')178        return render(request, 'redirect.html', {179            'msg': msg180            })181    return render(request, 'details.html', {182        'details': details183        })184def page_not_found(request):185    msg = []186    msg.append('ERROR MESSAGE HERE')187    return render(request, 'redirect.html', {188        'msg': msg...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
