Esempio n. 1
0
File: test_r.py Progetto: skywalka/r
    def test_r_package_usage_from_custom_script(self):
        import r_index_logging
        r_index_logging.clear_log_entries()

        service = Service([
            Stanza('package://race', {}),
            Stanza('package://boot', {})
        ])
        scriptlib.add(service, 'test', """
        library(race)
        add <- function(a,b) {
          c = a+b
          return(c)
        }
        result = add(4,10)
        output = data.frame(Result=c(result))
        """)
        _, rows = r(service, None, 'test.r')
        indexed_events = r_index_logging.get_log_entries()

        def is_indexed(text):
            for e in indexed_events:
                if text in e:
                    return True
            return False
        self.assertTrue(is_indexed('action=\"package_usage\"'))
        self.assertTrue(is_indexed('package_name=\"race\"'))
Esempio n. 2
0
def scripts(request):

    upload_new_script_action = 'upload_new_script'
    new_script_field_name = 'new_script'
    delete_script_action_prefix = 'delete_script_'

    if request.method == 'POST':
        try:
            # add script stanza
            if upload_new_script_action in request.POST:
                if new_script_field_name in request.FILES:
                    source_file = request.FILES[new_script_field_name]
                    if source_file.name.lower().endswith('.r'):
                        source_file_noext, _ = os.path.splitext(
                            source_file.name)
                        scriptlib.add(request.service, source_file_noext,
                                      source_file.read())
                    else:
                        raise r_errors.Error(
                            'Wrong file extension. It has to be \'r\'.')
                else:
                    raise r_errors.Error('File missing')
            # delete script stanza
            else:
                for key in request.POST:
                    if key.startswith(delete_script_action_prefix):
                        file_name = key[len(delete_script_action_prefix):]
                        source_file_noext, _ = os.path.splitext(file_name)
                        scriptlib.remove(request.service, source_file_noext)
        except r_errors.Error as e:
            return HttpResponseRedirect('./?add_error=%s' % str(e))
        except Exception as e:
            return HttpResponseRedirect('./?add_fatal_error=%s' % str(e))
        return HttpResponseRedirect('./')

    # scan for R script stanzas
    r_scripts = []
    for stanza, name in scriptlib.iter_stanzas(request.service):
        r_scripts.append({
            'file_name': name + '.r',
            'name': name,
            'is_removable': stanza.access['removable'] == '1',
            'owner': stanza.access['owner'],
        })

    return {
        'scripts': r_scripts,
        'app_label': request.service.apps[app_id].label,
        'request': request,
        'can_upload': scriptlib.can_upload(request.service),
        'new_script_field_name': new_script_field_name,
        'upload_new_script_action': upload_new_script_action,
        'delete_script_action_prefix': delete_script_action_prefix,
        'add_error': request.GET.get('add_error', ''),
        'add_fatal_error': request.GET.get('add_fatal_error', ''),
    }
Esempio n. 3
0
def scripts(request):

    upload_new_script_action = 'upload_new_script'
    new_script_field_name = 'new_script'
    delete_script_action_prefix = 'delete_script_'

    if request.method == 'POST':
        try:
            # add script stanza
            if upload_new_script_action in request.POST:
                if new_script_field_name in request.FILES:
                    source_file = request.FILES[new_script_field_name]
                    if source_file.name.lower().endswith('.r'):
                        source_file_noext, _ = os.path.splitext(source_file.name)
                        scriptlib.add(request.service, source_file_noext, source_file.read())
                    else:
                        raise r_errors.Error('Wrong file extension. It has to be \'r\'.')
                else:
                    raise r_errors.Error('File missing')
            # delete script stanza
            else:
                for key in request.POST:
                    if key.startswith(delete_script_action_prefix):
                        file_name = key[len(delete_script_action_prefix):]
                        source_file_noext, _ = os.path.splitext(file_name)
                        scriptlib.remove(request.service, source_file_noext)
        except r_errors.Error as e:
            return HttpResponseRedirect('./?add_error=%s' % str(e))
        except Exception as e:
            return HttpResponseRedirect('./?add_fatal_error=%s' % str(e))
        return HttpResponseRedirect('./')

    # scan for R script stanzas
    r_scripts = []
    for stanza, name in scriptlib.iter_stanzas(request.service):
        r_scripts.append({
            'file_name': name+'.r',
            'name': name,
            'is_removable': stanza.access['removable'] == '1',
            'owner': stanza.access['owner'],
        })

    return {
        'scripts': r_scripts,
        'app_label': request.service.apps[app_id].label,
        'request': request,
        'can_upload': scriptlib.can_upload(request.service),
        'new_script_field_name': new_script_field_name,
        'upload_new_script_action': upload_new_script_action,
        'delete_script_action_prefix': delete_script_action_prefix,
        'add_error': request.GET.get('add_error', ''),
        'add_fatal_error': request.GET.get('add_fatal_error', ''),
    }
Esempio n. 4
0
    def test_update_library(self):
        service = Service([
            Stanza('script://test', {
                'content': 'YWRkIDwtIGZ1bmN0aW9uKGEsYikgew0KICBjID0gYStiDQogIHJldHVybihjKQ0KfQ0KcmVzdWx0ID0gY'
                           'WRkKDQsMjAwKQ0Kb3V0cHV0ID0gZGF0YS5mcmFtZShSZXN1bHQ9YyhyZXN1bHQpKQ==',
            }),
        ])
        r_scripts.create_files(service)
        installed_scripts = os.listdir(r_scripts.get_custom_scripts_path())
        self.assertEqual(len(installed_scripts), 1)
        self.assertEqual(installed_scripts[0], 'test.r')

        mtime = os.path.getmtime(os.path.join(r_scripts.get_custom_scripts_path(), installed_scripts[0]))
        r_scripts.create_files(service)
        self.assertEqual(
            mtime,
            os.path.getmtime(os.path.join(r_scripts.get_custom_scripts_path(), installed_scripts[0]))
        )

        time.sleep(1.5)
        r_scripts.add(service, 'test', """
add <- function(a,b) {
  c = a+b
  return(c)
}
result = add(4,10)
output = data.frame(Result=c(result))
        """)
        time.sleep(1.5)
        r_scripts.create_files(service)
        installed_scripts = os.listdir(r_scripts.get_custom_scripts_path())
        self.assertEqual(len(installed_scripts), 1)
        self.assertEqual(installed_scripts[0], 'test.r')

        self.assertNotEqual(
            mtime,
            os.path.getmtime(os.path.join(r_scripts.get_custom_scripts_path(), installed_scripts[0]))
        )
        mtime = os.path.getmtime(os.path.join(r_scripts.get_custom_scripts_path(), installed_scripts[0]))
        r_scripts.create_files(service)
        self.assertEqual(
            mtime,
            os.path.getmtime(os.path.join(r_scripts.get_custom_scripts_path(), installed_scripts[0]))
        )
Esempio n. 5
0
def script(request, name):

    if request.method == 'POST':
        try:
            if 'cancel' in request.POST:
                return HttpResponseRedirect('../../scripts/')
            elif 'save' in request.POST:
                content = request.POST['content']
                scriptlib.add(request.service, name, content)
                return HttpResponseRedirect('../../scripts/')
        except Exception as e:
            return HttpResponseRedirect('./?add_error=%s' % str(e))
        return HttpResponseRedirect('./')

    return {
        'app_label': request.service.apps[app_id].label,
        'request': request,
        'name': name,
        'script': scriptlib.get(request.service, name),
        'file_name': name+'.r',
        'add_error': request.GET.get('add_error', ''),
        'add_unknown_error': request.GET.get('add_unknown_error', ''),
    }
Esempio n. 6
0
def script(request, name):

    if request.method == 'POST':
        try:
            if 'cancel' in request.POST:
                return HttpResponseRedirect('../../scripts/')
            elif 'save' in request.POST:
                content = request.POST['content']
                scriptlib.add(request.service, name, content)
                return HttpResponseRedirect('../../scripts/')
        except Exception as e:
            return HttpResponseRedirect('./?add_error=%s' % str(e))
        return HttpResponseRedirect('./')

    return {
        'app_label': request.service.apps[app_id].label,
        'request': request,
        'name': name,
        'script': scriptlib.get(request.service, name),
        'file_name': name + '.r',
        'add_error': request.GET.get('add_error', ''),
        'add_unknown_error': request.GET.get('add_unknown_error', ''),
    }