Ejemplo n.º 1
0
def run_job(target):
    ret = 0
    err = False
    jobs_path = kremtree.find_common_dir(c.PROJECT_JOBS_DIR)

    idx = 0
    job_num = check_if_job_number(target)

    if not job_num < 0:
        jobs = kremtree.list_dir(jobs_path)
        jobs.sort()
        if not job_num + 1 > len(jobs):
            for job in jobs:
                if idx == job_num:
                    print("\nRunning job: " + str(job) + "\n")
                    target = job
                    break
                idx = idx + 1

        else:
            print("Invalid job number: " + str(job_num))
            err = True

    if not err:
        #this will add project path to sys path so jobs can acces library, config, etc
        current_env = os.environ.copy()
        current_env['PYTHONPATH'] += os.pathsep + os.path.join(
            os.path.dirname("."))
        target_path = os.path.join(jobs_path, target, c.TEMPLATE_JOB_SCRIPT)
        try:
            ret = subprocess.call(['python', target_path], env=current_env)
        except Exception as e:
            print("Invalid job: " + str(target))

    return ret
Ejemplo n.º 2
0
def list_tasks():
    tasks_path = kremtree.find_common_dir(c.PROJECT_TASKS_DIR)
    if tasks_path is not None:
        tasks_path = os.path.abspath(tasks_path)
        tasks_list = kremtree.list_dir(tasks_path)
        tasks_list.sort()

        print("\nAvailable tasks:\n")
        print("[nr]\tname\n")

        idx = 0
        for task in tasks_list:
            missingFiles = []
            printstring = "[" + str(idx) + "]\t" + str(task)
            # List scripts in task, to quickly see if something is missing
            task_path = os.path.join(tasks_path, task)

            if not os.path.isfile(os.path.join(task_path,
                                               c.TEMPLATE_TASK_FILE)):
                missingFiles.append(c.TEMPLATE_TASK_FILE)
            if not os.path.isfile(os.path.join(task_path,
                                               c.INIT_PACKAGE_FILE)):
                missingFiles.append(c.INIT_PACKAGE_FILE)

            if len(missingFiles) > 0:
                printstring = printstring + '\t' + "missing files: " + str(
                    missingFiles)

            print(printstring)
            idx = idx + 1
        print("")
Ejemplo n.º 3
0
def list_jobs():
    jobs_path = os.path.abspath(kremtree.find_common_dir(c.PROJECT_JOBS_DIR))
    job_list = kremtree.list_dir(jobs_path)
    job_list.sort()

    print("\nAvailable jobs: \n")
    print("[nr]\tname\n")
    idx = 0
    for job in job_list:
        print("[" + str(idx) + "]\t" + str(job))
        idx = idx + 1
    print('\n')
Ejemplo n.º 4
0
def deploy_template_job(jobName):
    templatePath = os.path.join(c.TEMPLATES_PATH, c.TEMPLATE_JOB)
    jobsDir = kremtree.find_common_dir(c.PROJECT_JOBS_DIR)

    thisJobDir = os.path.join(jobsDir, jobName)

    if os.path.exists(thisJobDir):
        print('ERROR: Failed to deploy job. Job name already in use.')
    else:
        os.mkdir(thisJobDir)
        print('Initializing new job')
        copy_files(templatePath, thisJobDir)
        print('New job located in: ' + os.path.abspath(thisJobDir))
Ejemplo n.º 5
0
def deploy_template_task(taskName):
    templatePath = os.path.join(c.TEMPLATES_PATH, c.TEMPLATE_TASK)
    tasksDir = kremtree.find_common_dir(c.PROJECT_TASKS_DIR)

    thisTaskDir = os.path.join(tasksDir, taskName)

    if os.path.exists(thisTaskDir):
        print('ERROR: Failed to deploy task. Task name already in use.')
    else:
        os.mkdir(thisTaskDir)
        print('Initializing new task')
        copy_files(templatePath, thisTaskDir)
        print('New task located in: ' + os.path.abspath(thisTaskDir))
Ejemplo n.º 6
0
def run_job(target):
    ret = 0        

    jobs_path = kremtree.find_common_dir(c.PROJECT_JOBS_DIR)
    
    if not os.path.isdir(os.path.join(kremtree.find_krem_root("./"), c.PROJECT_OUTPUT_DIR)):        
        templatePath = os.path.join(c.TEMPLATES_PATH, c.TEMPLATE_PROJECT)
        shutil.copytree(os.path.join(templatePath, c.PROJECT_OUTPUT_DIR), os.path.join(kremtree.find_krem_root("./"), c.PROJECT_OUTPUT_DIR))
    
    idx = 0
    job_num = check_if_job_number(target)
    
    if job_num >= 0:
        jobs = kremtree.list_dir(jobs_path)
        jobs.sort()
        if not job_num + 1 > len(jobs):
            for job in jobs:
                if idx == job_num:
                    print("\nRunning job: " + str(job) +"\n")
                    target = job
                    break
                idx = idx + 1                
        else:
            print("Invalid job number: " + str(job_num))   
            ret = 1         

    if ret == 0:
        target_path = os.path.join(jobs_path, target, c.TEMPLATE_JOB_SCRIPT)
        if not os.path.exists(target_path):
            print("Invalid job: " + str(target)) 
            ret = 1


    if ret == 0:
        #this will add project path to sys path so jobs can acces library, config, etc
        current_env = os.environ.copy()
        current_env['PYTHONPATH'] += os.pathsep + os.path.join(os.path.dirname("."))

        if os.stat(target_path).st_size == 0:
            print("[ERROR]: Target job-script is empty.")
            exit(1)

        try:
            ret = subprocess.call(['python', target_path], env=current_env)
        except Exception:
            print("Invalid job: " + str(target))
            ret = 1        

    return ret
Ejemplo n.º 7
0
    def compile_task_module_name_and_path(self, task):
        task_path = None
        module_name = None

        task_path = os.path.join(kremtree.find_common_dir(c.PROJECT_TASKS_DIR), task.get_target(), c.TEMPLATE_TASK_FILE)

        if not os.path.isfile(task_path):
            self.log.write("Path to module in task: " + str(task.get_target()) + " not found in task.cfg file", 'error')
            self.log.write("Ensure task.cfg exists in task folder, and correct name/path is given", 'error')
            exit(1)

        module_name = c.TEMPLATE_TASK_FILE
        module_name = module_name.strip('.py')
        module_name = task.get_target() + '.' + module_name

        task.set_target_module_name(module_name)
        task.set_target_module_path(task_path)
Ejemplo n.º 8
0
    def execute(self, target):
        instance_output_name = self.time_stamp()
        output_path = kremtree.find_common_dir(c.PROJECT_OUTPUT_DIR)
        job_output_path = os.path.join(output_path, target)
        instance_output_path = os.path.join(job_output_path, instance_output_name)
        
        # Create job output directory
        if not os.path.isdir(job_output_path):
            try:
                os.mkdir(job_output_path)
            except Exception as e:
                print("[INTERNAL_ERROR]: Unable to create " + job_output_path)
                exit(1)            
        
        # Create instance output directory
        try:
            os.mkdir(instance_output_path)

        except Exception as e:
            print("[INTERNAL_ERROR]: Unable to create " + instance_output_path)
            exit(1)
          
        # Create global info file in output directory
        try:
            info_path = os.path.join(output_path, c.INFO_FILE)
            f = open(info_path, 'w')
            f.write('LAST_JOB = ' + str(target) + '/' + str(instance_output_name) + '\n')
            f.close()            
              
        except:
            print("[INTERNAL_WARNING]: Unable to create file " + info_path)
            
        # Add project path to syspath
        projectPath = os.path.join(kremtree.find_krem_root('./'))
        projectPath = os.path.realpath(projectPath)
        sys.path.append(projectPath)
            
        
        self.rotateSubdirs( job_output_path, '\d+_\d+', c.PROJECT_KEEP_OUTPUT_DIRS )
        
            
        return instance_output_path
Ejemplo n.º 9
0

import os
from krempack.core import plugin
from krempack.common import kremtree

output_dir = kremtree.find_common_dir("output")

def write_output_file(file, input):
    file = open(os.path.join(output_dir, file), 'w')
    file.write(input)
    file.close()
    print("File written: " + str(file))


class TestPluginAllHooks(plugin.Plugin):
    name = "Test-plugin-hooks"

    def __init__(self):
        None
 
    def job_start(self, job):
        try:
            # the first line is to check that we get the job object and not just task name
            job.name
            write_output_file("job_start", "test_line")
        except Exception as e:
            pass

    def pre_task_execution(self, task, job):
        try: