Пример #1
0
    def create_filename(self, execution_id, all_audit_names, script_name, start_time):
        audit_name = get_audit_name(all_audit_names)
        audit_name = file_utils.to_filename(audit_name)

        date_string = ms_to_datetime(start_time).strftime(self._date_format)

        username = get_first_existing(all_audit_names, audit_utils.AUTH_USERNAME, audit_utils.PROXIED_USERNAME)

        mapping = {
            'ID': execution_id,
            'USERNAME': username,
            'HOSTNAME': get_first_existing(all_audit_names, audit_utils.PROXIED_HOSTNAME, audit_utils.HOSTNAME,
                                           default='unknown-host'),
            'IP': get_first_existing(all_audit_names, audit_utils.PROXIED_IP, audit_utils.IP),
            'DATE': date_string,
            'AUDIT_NAME': audit_name,
            'SCRIPT': script_name
        }

        filename = self._filename_template.safe_substitute(mapping)
        if not filename.lower().endswith('.log'):
            filename += '.log'

        filename = filename.replace(" ", "_")

        return filename
Пример #2
0
    def create_log_identifier(audit_name, script_name):
        audit_name = file_utils.to_filename(audit_name)

        date_string = datetime.today().strftime("%y%m%d_%H%M%S")
        script_name = script_name.replace(" ", "_")
        log_identifier = script_name + "_" + audit_name + "_" + date_string
        return log_identifier
Пример #3
0
    def save_job(self, job: SchedulingJob):
        user = job.user
        script_name = job.script_name

        filename = file_utils.to_filename(
            '%s_%s_%s.json' % (script_name, user.get_audit_name(), job.id))
        file_utils.write_file(os.path.join(self._schedules_folder, filename),
                              json.dumps(job.as_serializable_dict(), indent=2))
Пример #4
0
    def _create_log_identifier(audit_name, script_name, start_time):
        audit_name = file_utils.to_filename(audit_name)

        date_string = ms_to_datetime(start_time).strftime("%y%m%d_%H%M%S")

        script_name = script_name.replace(" ", "_")
        log_identifier = script_name + "_" + audit_name + "_" + date_string
        return log_identifier
Пример #5
0
def _script_name_to_file_name(script_name):
    escaped_whitespaces = re.sub('\\s', '_', script_name)
    filename = to_filename(escaped_whitespaces)
    return filename + '.json'
Пример #6
0
def _escape_characters_in_filename(script_name):
    escaped = re.sub('[\\s/]+', '_', script_name).strip("_")
    return to_filename(escaped)
Пример #7
0
    def _preprocess_script_fields(self, config, original_config_json,
                                  uploaded_script, user):
        script_config = config.get('script')
        if not script_config:
            raise InvalidConfigException('script option is required')

        if SCRIPT_PATH_FIELD in config:
            del config[SCRIPT_PATH_FIELD]
        del config['script']

        new_path = strip(script_config.get('path'))
        if is_blank(new_path):
            raise InvalidConfigException('script.path option is required')

        config[SCRIPT_PATH_FIELD] = new_path

        mode = script_config.get('mode')
        if is_blank(mode) or mode == SCRIPT_EDIT_PATH_MODE:
            pass

        elif mode in (SCRIPT_EDIT_UPLOAD_MODE, SCRIPT_EDIT_CODE_MODE):
            if not self._authorizer.can_edit_code(user.user_id):
                raise InvalidAccessException('User ' + str(user) +
                                             ' is not allowed to edit code')

            if mode == SCRIPT_EDIT_UPLOAD_MODE:
                if uploaded_script is None:
                    raise InvalidConfigException(
                        'Uploaded script should be specified')

            if original_config_json is None:  # new config
                if mode == SCRIPT_EDIT_UPLOAD_MODE:
                    # escaped name is needed, when uploaded file and server has different OSes,
                    # thus different special characters
                    escaped_name = to_filename(uploaded_script.filename)
                    target_path = os.path.join(self._scripts_folder,
                                               escaped_name)
                else:
                    filename = os.path.basename(new_path)
                    target_path = os.path.join(
                        self._scripts_folder,
                        _escape_characters_in_filename(filename))

                script_path = file_utils.create_unique_filename(
                    target_path, 100)
                config[SCRIPT_PATH_FIELD] = script_path

            else:
                existing_code = self._load_script_code_by_config(
                    original_config_json)
                script_path = existing_code['file_path']

                if (mode == SCRIPT_EDIT_CODE_MODE
                    ) and existing_code.get('code_edit_error') is not None:
                    raise InvalidConfigException(
                        'Failed to edit code: ' +
                        existing_code.get('code_edit_error'))

                if new_path != original_config_json.get(SCRIPT_PATH_FIELD):
                    raise InvalidConfigException(
                        'script.path override is not allowed for ' + mode +
                        ' mode')

            if mode == SCRIPT_EDIT_UPLOAD_MODE:
                file_utils.write_file(script_path,
                                      uploaded_script.body,
                                      byte_content=True)
            else:
                code = script_config.get('code')
                if code is None:
                    raise InvalidConfigException(
                        'script.code should be specified')
                file_utils.write_file(script_path, code)

            file_utils.make_executable(script_path)

        else:
            raise InvalidConfigException('Unsupported mode: ' + mode)
Пример #8
0
    def test_replace_special_characters_linux(self):
        os_utils.set_linux()

        filename = file_utils.to_filename('!@#$%^&*()_+\|/?.<>,\'"')
        self.assertEqual('!@#$%^&*()_+\\|_?.<>,\'"', filename)
Пример #9
0
    def test_replace_special_characters_windows(self):
        os_utils.set_win()

        filename = file_utils.to_filename('!@#$%^&*()_+\|/?.<>,\'"')
        self.assertEqual('!@#$%^&_()_+____.__,\'_', filename)
Пример #10
0
def _script_name_to_file_name(script_name):
    escaped_whitespaces = re.sub('[\\s/]+', '_', script_name).strip("_")
    filename = to_filename(escaped_whitespaces)
    return filename + '.json'