argo_create_userjob_class.py 1.55 KB
Newer Older
jtchilders's avatar
jtchilders committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
from django.core.management.base import BaseCommand, CommandError
from argo import models
try:
    input = raw_input
except NameError:
    pass
import os,logging
logger = logging.getLogger('console')

fields_to_skip = [
   'time_finished',
   'current_subjob_pk_index',
   'time_created',
   'state',
   'subjob_pk_list',
   'working_directory',
   'time_modified',
   'id',
jtchilders's avatar
jtchilders committed
19
   'argo_job_id',
jtchilders's avatar
jtchilders committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   ]

class Command(BaseCommand):
   help = 'Dump a python class file which a User can use to submit an ARGO job.'

   def add_arguments(self,parser):
      parser.add_argument('-o','--output-file',dest='output_filename',help='The filename to which to write the output.',default='ArgoUserJob.py')

   def handle(self, *args, **options):
      logger.info('Dump a ArgoUserJob python class.')

      bj = models.ArgoJob()
      output_filename = options['output_filename']
      if os.path.exists(output_filename):
         raise Exception(' File already exists: ' + output_filename)

      with open(output_filename,'w') as outfile:
jtchilders's avatar
jtchilders committed
37 38
         outfile.write('''import json

jtchilders's avatar
jtchilders committed
39 40 41 42 43 44 45 46 47
class ArgoUserJob:
   def __init__(self):
''')
         for var,val in bj.__dict__.iteritems():
            if var[0] == '_': continue
            if var in fields_to_skip: continue
            if isinstance(val,str):
               val = "'" + val + "'"
            outfile.write('      self.' + var + ' = ' + str(val) + '\n')
jtchilders's avatar
jtchilders committed
48 49 50 51
         outfile.write('''      self.subjobs = []

   def serialize(self):
      return json.dumps(self.__dict__)
jtchilders's avatar
jtchilders committed
52

jtchilders's avatar
jtchilders committed
53 54 55 56
   def add_subjob(self,subjob):
      self.subjobs.append(subjob.__dict__)

''')
jtchilders's avatar
jtchilders committed
57