<s> import json <EOL> import logging <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . csrf import ensure_csrf_cookie <EOL> from django . views . decorators . http import require_http_methods <EOL> from desktop . lib . django_util import JsonResponse , render <EOL> from desktop . lib . exceptions_renderable import PopupException <EOL> from desktop . lib . rest . http_client import RestException <EOL> from desktop . models import Document <EOL> from oozie . views . dashboard import show_oozie_error , check_job_access_permission , check_job_edition_permission <EOL> from pig import api <EOL> from pig . management . commands import pig_setup <EOL> from pig . models import get_workflow_output , hdfs_link , PigScript , create_or_update_script , get_scripts <EOL> LOG = logging . getLogger ( __name__ ) <EOL> @ ensure_csrf_cookie <EOL> def app ( request ) : <EOL> autocomplete_base_url = '<STR_LIT>' <EOL> try : <EOL> autocomplete_base_url = reverse ( '<STR_LIT>' , kwargs = { } ) + '<STR_LIT:/>' <EOL> except : <EOL> LOG . exception ( '<STR_LIT>' ) <EOL> return render ( '<STR_LIT>' , request , { <EOL> '<STR_LIT>' : autocomplete_base_url , <EOL> } ) <EOL> def scripts ( request ) : <EOL> return JsonResponse ( get_scripts ( request . user , is_design = True ) , safe = False ) <EOL> @ show_oozie_error <EOL> def dashboard ( request ) : <EOL> pig_api = api . get ( request . fs , request . jt , request . user ) <EOL> jobs = pig_api . get_jobs ( ) <EOL> hue_jobs = Document . objects . available ( PigScript , request . user , with_history = True ) <EOL> massaged_jobs = pig_api . massaged_jobs_for_json ( request , jobs , hue_jobs ) <EOL> return JsonResponse ( massaged_jobs , safe = False ) <EOL> def save ( request ) : <EOL> if request . method != '<STR_LIT:POST>' : <EOL> raise PopupException ( _ ( '<STR_LIT>' ) ) <EOL> attrs = { <EOL> '<STR_LIT:id>' : request . POST . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:name>' : request . POST . get ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : json . loads ( request . POST . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : json . loads ( request . POST . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : json . loads ( request . POST . get ( '<STR_LIT>' ) ) , <EOL> } <EOL> pig_script = create_or_update_script ( ** attrs ) <EOL> pig_script . is_design = True <EOL> pig_script . save ( ) <EOL> response = { <EOL> '<STR_LIT:id>' : pig_script . id , <EOL> '<STR_LIT>' : pig_script . doc . get ( ) . id <EOL> } <EOL> return JsonResponse ( response , content_type = "<STR_LIT>" ) <EOL> @ show_oozie_error <EOL> def stop ( request ) : <EOL> if request . method != '<STR_LIT:POST>' : <EOL> raise PopupException ( _ ( '<STR_LIT>' ) ) <EOL> pig_script = PigScript . objects . get ( id = request . POST . get ( '<STR_LIT:id>' ) ) <EOL> job_id = pig_script . dict [ '<STR_LIT>' ] <EOL> job = check_job_access_permission ( request , job_id ) <EOL> check_job_edition_permission ( job , request . user ) <EOL> try : <EOL> api . get ( request . fs , request . jt , request . user ) . stop ( job_id ) <EOL> except RestException , e : <EOL> raise PopupException ( _ ( "<STR_LIT>" ) % e . message ) <EOL> return watch ( request , job_id ) <EOL> @ show_oozie_error <EOL> def run ( request ) : <EOL> if request . method != '<STR_LIT:POST>' : <EOL> raise PopupException ( _ ( '<STR_LIT>' ) ) <EOL> attrs = { <EOL> '<STR_LIT:id>' : request . POST . get ( '<STR_LIT:id>' ) , <EOL> '<STR_LIT:name>' : request . POST . get ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : json . loads ( request . POST . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : json . loads ( request . POST . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : json . loads ( request . POST . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : False <EOL> } <EOL> pig_script = create_or_update_script ( ** attrs ) <EOL> params = request . POST . get ( '<STR_LIT>' ) <EOL> oozie_id = api . get ( request . fs , request . jt , request . user ) . submit ( pig_script , params ) <EOL> pig_script . update_from_dict ( { '<STR_LIT>' : oozie_id } ) <EOL> pig_script . save ( ) <EOL> response = { <EOL> '<STR_LIT:id>' : pig_script . id , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : oozie_id } ) + '<STR_LIT>' <EOL> } <EOL> return JsonResponse ( response , content_type = "<STR_LIT>" ) <EOL> def copy ( request ) : <EOL> if request . method != '<STR_LIT:POST>' : <EOL> raise PopupException ( _ ( '<STR_LIT>' ) ) <EOL> pig_script = PigScript . objects . get ( id = request . POST . get ( '<STR_LIT:id>' ) ) <EOL> doc = pig_script . doc . get ( ) <EOL> try : <EOL> doc . can_read_or_exception ( request . user ) <EOL> except Exception , e : <EOL> raise PopupException ( e ) <EOL> existing_script_data = pig_script . dict <EOL> owner = request . user <EOL> name = existing_script_data [ "<STR_LIT:name>" ] + _ ( '<STR_LIT>' ) <EOL> script = existing_script_data [ "<STR_LIT>" ] <EOL> parameters = existing_script_data [ "<STR_LIT>" ] <EOL> resources = existing_script_data [ "<STR_LIT>" ] <EOL> hadoopProperties = existing_script_data [ "<STR_LIT>" ] <EOL> script_copy = PigScript . objects . create ( owner = owner ) <EOL> script_copy . update_from_dict ( { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : script , <EOL> '<STR_LIT>' : parameters , <EOL> '<STR_LIT>' : resources , <EOL> '<STR_LIT>' : hadoopProperties <EOL> } ) <EOL> script_copy . save ( ) <EOL> copy_doc = doc . copy ( content_object = script_copy , name = name , owner = owner ) <EOL> response = { <EOL> '<STR_LIT:id>' : script_copy . id , <EOL> '<STR_LIT>' : copy_doc . id , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : script , <EOL> '<STR_LIT>' : parameters , <EOL> '<STR_LIT>' : resources , <EOL> '<STR_LIT>' : hadoopProperties <EOL> } <EOL> return JsonResponse ( response , content_type = "<STR_LIT>" ) <EOL> def delete ( request ) : <EOL> if request . method != '<STR_LIT:POST>' : <EOL> raise PopupException ( _ ( '<STR_LIT>' ) ) <EOL> ids = request . POST . get ( '<STR_LIT>' ) . split ( "<STR_LIT:U+002C>" ) <EOL> for script_id in ids : <EOL> try : <EOL> pig_script = PigScript . objects . get ( id = script_id ) <EOL> pig_script . can_edit_or_exception ( request . user ) <EOL> pig_script . doc . all ( ) . delete ( ) <EOL> pig_script . delete ( ) <EOL> except : <EOL> LOG . exception ( '<STR_LIT>' ) <EOL> None <EOL> response = { <EOL> '<STR_LIT>' : ids , <EOL> } <EOL> return JsonResponse ( response , content_type = "<STR_LIT>" ) <EOL> @ show_oozie_error <EOL> def watch ( request , job_id ) : <EOL> oozie_workflow = check_job_access_permission ( request , job_id ) <EOL> logs , workflow_actions , is_really_done = api . get ( request . jt , request . jt , request . user ) . get_log ( request , oozie_workflow ) <EOL> output = get_workflow_output ( oozie_workflow , request . fs ) <EOL> workflow = { <EOL> '<STR_LIT>' : oozie_workflow . id , <EOL> '<STR_LIT:status>' : oozie_workflow . status , <EOL> '<STR_LIT>' : oozie_workflow . get_progress ( ) , <EOL> '<STR_LIT>' : oozie_workflow . is_running ( ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : oozie_workflow . id , '<STR_LIT:action>' : '<STR_LIT>' } ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : oozie_workflow . id , '<STR_LIT>' : oozie_workflow . appPath } ) , <EOL> '<STR_LIT>' : workflow_actions <EOL> } <EOL> response = { <EOL> '<STR_LIT>' : workflow , <EOL> '<STR_LIT>' : logs , <EOL> '<STR_LIT>' : is_really_done , <EOL> '<STR_LIT>' : hdfs_link ( output ) <EOL> } <EOL> return JsonResponse ( response , content_type = "<STR_LIT>" ) <EOL> def install_examples ( request ) : <EOL> result = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> if request . method != '<STR_LIT:POST>' : <EOL> result [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> pig_setup . Command ( ) . handle_noargs ( ) <EOL> result [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> except Exception , e : <EOL> LOG . exception ( e ) <EOL> result [ '<STR_LIT:message>' ] = str ( e ) <EOL> return JsonResponse ( result ) </s>
