Example #1
0
    def process_local_test_settings_closure(logger):
        logger.info('Preparing local test settings for your new instance...')
        template = os.sep.join([
            'jira-func-tests', 'src', 'main', 'resources', 'localtest.template'
        ])

        template_renderings = {
            'jira-func-tests':
            'jira-func-tests',
            'jira-webdriver-tests':
            'jira-webdriver-tests',
            os.sep.join(['jira-distribution', 'jira-integration-tests']):
            'jira-func-tests'
        }

        for project, xml_location in template_renderings.items():
            dir = fileutils.existing_dir(
                os.sep.join(['.', project, 'src', 'main', 'resources']))
            dest = os.sep.join([dir, 'localtest.properties'])

            # just for unit tests this settings dict is not reused:
            settings = {
                '${jira.port}': str(args.port),
                '${jira.context}': args.jira_context,
                '${test.xml.location}': PathUtils.abspath(xml_location)
            }

            logger.debug('Processing ' + template + ' to ' + dest)
            fileutils.filter_file(template, dest, settings)
        return Callable.success
Example #2
0
    def _add(self, src_path, is_first=False, count=0):
        try:
            model_path = str(src_path)
            path_exists = os.path.exists(model_path)
            if not path_exists and count > 0:
                self.logger.error("{} not found, retry attempt is terminated.".format(model_path))
                return
            if 'model_demo.yaml' in model_path:
                self.logger.warning(
                    "\n-------------------------------------------------------------------\n"
                    "- Found that the model_demo.yaml file exists, \n"
                    "- the loading is automatically ignored. \n"
                    "- If it is used for the first time, \n"
                    "- please copy it as a template. \n"
                    "- and do not use the reserved character \"model_demo.yaml\" as the file name."
                    "\n-------------------------------------------------------------------"
                )
                return
            if model_path.endswith("yaml"):
                model_conf = ModelConfig(self.conf, model_path)
                inner_name = model_conf.model_name
                inner_size = model_conf.size_string
                inner_key = PathUtils.get_file_name(model_path)
                for k, v in self.name_map.items():
                    if inner_size in v:
                        self.logger.warning(
                            "\n-------------------------------------------------------------------\n"
                            "- The current model {} is the same size [{}] as the loaded model {}. \n"
                            "- Only one of the smart calls can be called. \n"
                            "- If you want to refer to one of them, \n"
                            "- please use the model key or model type to find it."
                            "\n-------------------------------------------------------------------".format(
                                inner_key, inner_size, k
                            )
                        )
                        break

                inner_value = model_conf.model_name
                graph_session = GraphSession(model_conf)
                if graph_session.loaded:
                    interface = Interface(graph_session)
                    if inner_name == self.conf.default_model:
                        self.interface_manager.set_default(interface)
                    else:
                        self.interface_manager.add(interface)
                    self.logger.info("{} a new model: {} ({})".format(
                        "Inited" if is_first else "Added", inner_value, inner_key
                    ))
                    self.name_map[inner_key] = inner_value
                    if src_path in self.interface_manager.invalid_group:
                        self.interface_manager.invalid_group.pop(src_path)
                else:
                    self.interface_manager.report(src_path)
                    if count < 12 and not is_first:
                        time.sleep(5)
                        return self._add(src_path, is_first=is_first, count=count+1)

        except Exception as e:
            self.interface_manager.report(src_path)
            self.logger.error(e)
Example #3
0
 def jrebel_options(self, log):
     jrebel_lib_location = self.jrebel_lib_location(log)
     if jrebel_lib_location is None:
         log.error(
             'Cannot find jrebel.jar in idea configuration. Please ensure that you have jrebel plugin installed')
         return None
     else:
         log.info('Using jrebel library from ' + jrebel_lib_location)
         return ' -Djira.rebel.root="%s" -javaagent:"%s"' % (PathUtils.abspath('.'), jrebel_lib_location)
Example #4
0
 def delete(self, src_path):
     try:
         model_path = str(src_path)
         if model_path.endswith("yaml"):
             inner_key = PathUtils.get_file_name(model_path)
             graph_name = self.name_map.get(inner_key)
             self.interface_manager.remove_by_name(graph_name)
             self.name_map.pop(inner_key)
             self.logger.info("Unload the model: {} ({})".format(graph_name, inner_key))
     except Exception as e:
         self.logger.error("Config File [{}] does not exist.".format(str(e).replace("'", "")))
Example #5
0
 def jrebel_options(self, log):
     jrebel_lib_location = self.jrebel_lib_location(log)
     if jrebel_lib_location is None:
         log.error(
             'Cannot find jrebel.jar in idea configuration. Please ensure that you have jrebel plugin installed'
         )
         return None
     else:
         log.info('Using jrebel library from ' + jrebel_lib_location)
         return ' -Djira.rebel.root="%s" -javaagent:"%s"' % (
             PathUtils.abspath('.'), jrebel_lib_location)
Example #6
0
 def delete(self, src_path):
     try:
         model_path = str(src_path)
         if model_path.endswith("yaml"):
             inner_key = PathUtils.get_file_name(model_path)
             graph_name = self.name_map.get(inner_key)
             self.interface_manager.remove_by_name(graph_name)
             self.name_map.pop(inner_key)
             self.logger.info("Unload the model: {} ({})".format(graph_name, inner_key))
     except Exception as e:
         print(e)
Example #7
0
 def read_configuration(self, cfg_file):
     """
     Read the configuration from the argument file
     :param cfgfile: filename
     :return: None
     """
     try:
         path_utils = PathUtils()
         config = ConfigParser()
         confirmation = config.read(cfg_file)
         if len(confirmation) == 0:
             sys.exit(u'Configuration file not found. Please execute with the configuration file next to the executable file.')
         _logfile = config.has_option('Logs','Filename') and config.get('Logs','Filename') or './log/application.log'
         _logfile_max_bytes = int(config.has_option('Logs','MaxBytes') and config.get('Logs','MaxBytes') or 5242880)
         log_level = config.has_option('Logs','Level') and config.get('Logs','Level') or 'INFO'
         log_console_option = config.has_option('Logs','Console') and config.get('Logs','Console') or 'FALSE'
         self.log_console = log_console_option.strip().upper() == 'TRUE'
         if log_level == 'INFO':
             log_level = logging.INFO
         if log_level == 'DEBUG':
             log_level = logging.DEBUG
         if log_level == 'WARNING':
             log_level = logging.WARNING
         if log_level == 'CRITICAL':
             log_level = logging.CRITICAL
         if log_level == 'ERROR':
             log_level = logging.ERROR
         if log_level == 'FATAL':
             log_level = logging.FATAL
         self.get_config_logger(_logfile, log_level, _logfile_max_bytes, self.log_console)
         num_task = int(config.has_option('Task', 'Number') and config.get('Task', 'Number') or 0)
         for index in range(0, num_task):
             try:
                 name = config.has_option('Task', str(index)) and config.get('Task', str(index)) or None
                 if name is not None:
                     brokers_str = config.has_option(name, 'Brokers') and config.get(name, 'Brokers') or ''
                     brokers = brokers_str.split(',')
                     topic = config.has_option(name, 'Topic') and config.get(name, 'Topic') or ''
                     retries = int(config.has_option(name, 'Retries') and config.get(name, 'Retries') or 0)
                     partitions = config.has_option(name, 'Partitions') and config.get(name, 'Partitions') or [0]
                     key = config.has_option(name, 'Key') and config.get(name, 'Key') or ''
                     value = config.has_option(name, 'Value') and config.get(name, 'Value') or ''
                     locale = config.has_option(name, 'Locale') and config.get(name, 'Locale') or 'En_US'
                     pause_milliseconds = config.has_option(name, 'Pause_milliseconds') and config.get(name, 'Pause_milliseconds') or 0
                     kafka_task = KafkaTask(name, locale, brokers, topic, retries, partitions, key, value, pause_milliseconds)
                     self.tasks.append(kafka_task)
             except Exception as e:
                 self.logger.error(u'Error found at Kafka task listed at {0} on the configuration file!'.format(index))
                 pass
     except Exception as e:
         if self.logger is not None:
             self.logger.error(u'ReadConfiguration Exception {0}.'.format(e))
         sys.exit(u'ReadConfiguration Exception {0}.'.format(e))
Example #8
0
    def plugin_resources(self, fileutils=FileUtils(),workspace_utils=WorkspaceUtils()):
        if self.args.disable_plugin_resources:
            return ''
        resources_marker = os.sep.join(['src', 'main', 'resources'])
        resources_marker_exclude = '.hg'
        plugins_roots = [os.sep.join(['jira-components', 'jira-plugins'])]
        if self.args.plugin_resources:
            plugins_roots.append(fileutils.abs_path(self.args.plugin_resources))
        plugins_roots.extend(os.sep.join([workspace_utils.get_jira_workspace_dir(), project])
                             for project in workspace_utils.get_workspace_projects_without_jira(self.args))

        return '-Dplugin.resource.directories=%s' % ','.join(
            PathUtils.abspath(path) for plugins_root in plugins_roots
            for (path, dirs, files) in fileutils.walk(plugins_root) if path.endswith(resources_marker)
            and not resources_marker_exclude in path)
    def test_jrebel_is_discovered_and_java_opts_are_set(self):
        #having
        self.args.jrebel = True
        file_utils = MockFileUtils()
        file_utils.expect_possible_idea_plugin_dirs(toReturn=['idea12', 'idea13', 'idea129'])
        jrebel_path = os.path.join('idea13', 'config', 'plugins', 'jr-ide-idea', 'lib', 'jrebel', 'jrebel.jar')
        file_utils.expect_file_exists(jrebel_path, toReturn=True)
        tomcat_starter = MockTomcatStarter(self.args, False, file_utils)

        #when
        return_code = tomcat_starter(Mock())

        #then
        self.assertEqual(return_code, Callable.success, 'Expected successful return code')
        opts = self.__get_opts_dict(tomcat_starter)
        self.__assertParameter(opts, '-javaagent:"' + jrebel_path + '"')
        self.__assertParameter(opts, '-Djira.rebel.root', '"' + PathUtils.abspath('.') + '"')
        self.assertNotIn('-agentlib:jdwp:', opts)
Example #10
0
 def on_deleted(self, event):
     if event.is_directory:
         self.logger.info("directory deleted:{0}".format(event.src_path))
     else:
         model_path = str(event.src_path)
         if model_path in self.interface_manager.invalid_group:
             self.interface_manager.invalid_group.pop(model_path)
         inner_key = PathUtils.get_file_name(model_path)
         if inner_key in self.name_map:
             self.delete(model_path)
         self.logger.info("\n - Number of interfaces: {}"
                          "\n - Current online interface: \n\t - {}"
                          "\n - The default Interface is: {}".format(
                              len(self.interface_manager.group),
                              "\n\t - ".join([
                                  "[{}]".format(v)
                                  for k, v in self.name_map.items()
                              ]), self.interface_manager.default_name))
Example #11
0
    def __get_opts_for_jira(self, dev_mode, layout: WorkspaceLayout):
        args = ' -Xms128m'\
               ' -Xmx' + self.args.xmx + ''\
               ' -XX:MaxPermSize=' + self.args.max_perm_size + ''\
               ' -XX:+HeapDumpOnOutOfMemoryError'\
               ' -Djira.i18n.texthighlight=false'\
               ' -Dmail.debug=false'\
               ' -Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true'\
               ' -Dmail.mime.decodeparameters=true'\
               ' -Djira.dev.mode=' + str(dev_mode).lower() +\
               ' -Djira.plugins.bundled.disable=false' \
               ' -Datlassian.plugins.tenant.smart.patterns=' + \
                                PathUtils.abspath('jira-components/jira-core/src/main/resources/tenant-smart-patterns.txt') +\
               ' -Djira.paths.set.allowed=true'\
               ' -Duser.language=en'\
               ' -Duser.region=AU'\
               ' -Duser.timezone=Australia/Sydney'\
               ' -Dplugin.webresource.batching.off=' + str(dev_mode).lower() +\
               ' -Djava.awt.headless=true'\
               ' -Djira.home=' + layout.jira_home()
        if not (self.args.bundled_plugins or layout.ondemand):
            args += ' -Djira.dev.bundledplugins.url=file://' + BundledPluginsUtility.BUNDLED_PLUGINS_LIST
        if layout.ondemand:
            args += ' -Dstudio.initial.data.xml=' + layout.studio_initial_data() +\
                    ' -Dstudio.home=' + layout.jira_home() +\
                    ' -Datlassian.darkfeature.com.atlassian.jira.config.CoreFeatures.ON_DEMAND=true'\
                    ' -Dcrowd.property.application.login.url=' + self.args.horde_layout.horde_application_login_url() +\
                    ' -Dcrowd.property.crowd.server.url=' + self.args.horde_layout.horde_server_url() +\
                    ' -Dstudio.webdav.directory=' + layout.webdav_dir()
            if self.args.manifesto_psd is not None:
                args += ' -Datlassian.jira.plugin.scan.directory=' + self.args.manifesto_psd

        if hasattr(self.args, 'clustered') and self.args.clustered:
            args += ' -Datlassian.cluster.scale=true' +\
                    ' -DjvmRoute=' + self.args.instance_name + \
                    ' -Datlassian.cache.ehcache=true'

        if self.args.enable_mail:
            args += ' -Datlassian.mail.senddisabled=false'\
                   ' -Datlassian.mail.fetchdisabled=false'

        return args + ' ' + self.plugin_resources()
Example #12
0
    def __get_opts_for_jira(self, dev_mode, layout: WorkspaceLayout):
        args = ' -Xms128m'\
               ' -Xmx' + self.args.xmx + ''\
               ' -XX:MaxPermSize=' + self.args.max_perm_size + ''\
               ' -XX:+HeapDumpOnOutOfMemoryError'\
               ' -Djira.i18n.texthighlight=false'\
               ' -Dmail.debug=false'\
               ' -Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true'\
               ' -Dmail.mime.decodeparameters=true'\
               ' -Djira.dev.mode=' + str(dev_mode).lower() +\
               ' -Djira.plugins.bundled.disable=false' \
               ' -Datlassian.plugins.tenant.smart.patterns=' + \
                                PathUtils.abspath('jira-components/jira-core/src/main/resources/tenant-smart-patterns.txt') +\
               ' -Djira.paths.set.allowed=true'\
               ' -Duser.language=en'\
               ' -Duser.region=AU'\
               ' -Duser.timezone=Australia/Sydney'\
               ' -Dplugin.webresource.batching.off=' + str(dev_mode).lower() +\
               ' -Djava.awt.headless=true'\
               ' -Djira.home=' + layout.jira_home()
        if not (self.args.bundled_plugins or layout.ondemand):
            args += ' -Djira.dev.bundledplugins.url=file://' + BundledPluginsUtility.BUNDLED_PLUGINS_LIST
        if layout.ondemand:
            args += ' -Dstudio.initial.data.xml=' + layout.studio_initial_data() +\
                    ' -Dstudio.home=' + layout.jira_home() +\
                    ' -Datlassian.darkfeature.com.atlassian.jira.config.CoreFeatures.ON_DEMAND=true'\
                    ' -Dcrowd.property.application.login.url=' + self.args.horde_layout.horde_application_login_url() +\
                    ' -Dcrowd.property.crowd.server.url=' + self.args.horde_layout.horde_server_url() +\
                    ' -Dstudio.webdav.directory=' + layout.webdav_dir()
            if self.args.manifesto_psd is not None:
                args += ' -Datlassian.jira.plugin.scan.directory=' + self.args.manifesto_psd

        if hasattr(self.args, 'clustered') and self.args.clustered:
            args += ' -Datlassian.cluster.scale=true' +\
                    ' -DjvmRoute=' + self.args.instance_name + \
                    ' -Datlassian.cache.ehcache=true'

        if self.args.enable_mail:
             args += ' -Datlassian.mail.senddisabled=false'\
                    ' -Datlassian.mail.fetchdisabled=false'

        return args + ' ' + self.plugin_resources()
Example #13
0
    def process_local_test_settings_closure(logger):
        logger.info('Preparing local test settings for your new instance...')
        template = os.sep.join(['jira-func-tests', 'src', 'main', 'resources', 'localtest.template'])

        template_renderings = {'jira-func-tests': 'jira-func-tests',
                               'jira-webdriver-tests': 'jira-webdriver-tests',
                               os.sep.join(['jira-distribution', 'jira-integration-tests']): 'jira-func-tests'}

        for project, xml_location in template_renderings.items():
            dir = fileutils.existing_dir(os.sep.join(['.', project, 'src', 'main', 'resources']))
            dest = os.sep.join([dir, 'localtest.properties'])

            # just for unit tests this settings dict is not reused:
            settings = {'${jira.port}': str(args.port),
                        '${jira.context}': args.jira_context,
                        '${test.xml.location}': PathUtils.abspath(xml_location)}

            logger.debug('Processing ' + template + ' to ' + dest)
            fileutils.filter_file(template, dest, settings)
        return Callable.success
Example #14
0
    def plugin_resources(self,
                         fileutils=FileUtils(),
                         workspace_utils=WorkspaceUtils()):
        if self.args.disable_plugin_resources:
            return ''
        resources_marker = os.sep.join(['src', 'main', 'resources'])
        resources_marker_exclude = '.hg'
        plugins_roots = [os.sep.join(['jira-components', 'jira-plugins'])]
        if self.args.plugin_resources:
            plugins_roots.append(fileutils.abs_path(
                self.args.plugin_resources))
        plugins_roots.extend(
            os.sep.join([workspace_utils.get_jira_workspace_dir(), project])
            for project in workspace_utils.get_workspace_projects_without_jira(
                self.args))

        return '-Dplugin.resource.directories=%s' % ','.join(
            PathUtils.abspath(path) for plugins_root in plugins_roots
            for (path, dirs, files) in fileutils.walk(plugins_root)
            if path.endswith(resources_marker)
            and not resources_marker_exclude in path)
Example #15
0
 def get_config_logger(self, logfile, level=logging.INFO, max_bytes=5242880, log_console=False):
     """
     Setup the logger of the receiver.
     :param logfile: filename of the log
     :param level: level of verbose
     :param max_bytes: max bytes of the log file
     :param log_console: if the log should also printed on the console
     :return:
     """
     PathUtils().ensure_has_paths(logfile, is_filename=True)
     self.logger = logging.getLogger('GREGOR')
     formatter = logging.Formatter(u'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
     self.logger.setLevel(level)
     # Console
     if log_console:
         ch = logging.StreamHandler(sys.stdout)
         ch.setFormatter(formatter)
         self.logger.addHandler(ch)
     # File
     fh = handlers.RotatingFileHandler(logfile, maxBytes=max_bytes, backupCount=100, encoding='utf8')
     fh.setFormatter(formatter)
     self.logger.addHandler(fh)
Example #16
0
    def test_jrebel_is_discovered_and_java_opts_are_set(self):
        #having
        self.args.jrebel = True
        file_utils = MockFileUtils()
        file_utils.expect_possible_idea_plugin_dirs(
            toReturn=['idea12', 'idea13', 'idea129'])
        jrebel_path = os.path.join('idea13', 'config', 'plugins',
                                   'jr-ide-idea', 'lib', 'jrebel',
                                   'jrebel.jar')
        file_utils.expect_file_exists(jrebel_path, toReturn=True)
        tomcat_starter = MockTomcatStarter(self.args, False, file_utils)

        #when
        return_code = tomcat_starter(Mock())

        #then
        self.assertEqual(return_code, Callable.success,
                         'Expected successful return code')
        opts = self.__get_opts_dict(tomcat_starter)
        self.__assertParameter(opts, '-javaagent:"' + jrebel_path + '"')
        self.__assertParameter(opts, '-Djira.rebel.root',
                               '"' + PathUtils.abspath('.') + '"')
        self.assertNotIn('-agentlib:jdwp:', opts)
from Logger import LOG
from utils import PathUtils
from utils.FileUtils import FileUtils

JIRA_PLUGINS_DIR = os.sep.join(['jira-components', 'jira-plugins'])
JIRA_PLUGINS_DIR_ABS = os.path.abspath(
    os.sep.join(['jira-components', 'jira-plugins']))
JIRA_PLUGINS_POM_XML = os.sep.join([JIRA_PLUGINS_DIR, 'pom.xml'])
JIRA_PLUGIN_DIR = os.sep.join([JIRA_PLUGINS_DIR, '{0}'])
PLUGIN_TARGET_DIR = os.sep.join([JIRA_PLUGINS_DIR, '{}', 'target'])
PLUGIN_SRC_DIR = os.sep.join([JIRA_PLUGINS_DIR, '{}', 'src'])
PLUGIN_POM_DIR = os.sep.join([JIRA_PLUGINS_DIR, '{}', 'pom.xml'])
PLUGIN_JAR_FILE = os.sep.join([PLUGIN_TARGET_DIR, '{0}-{1}.jar'])
BUNDLED_PLUGINS_LIST = PathUtils.abspath(
    os.sep.join([
        PLUGIN_TARGET_DIR.format('jira-bundled-plugins'),
        'atlassian-bundled-plugins.list'
    ]))
JMAKE_PROFILES_PLUGINS_LIST = os.path.abspath(
    os.sep.join([
        PLUGIN_TARGET_DIR.format('jira-bundled-plugins'),
        'jmake-bundled-plugins-profiles.list'
    ]))
BUNDLED_PLUGINS_MODULE = JIRA_PLUGIN_DIR.format('jira-bundled-plugins')
BUNDLED_PLUGINS_POM = PLUGIN_POM_DIR.format('jira-bundled-plugins')
PLUGIN_FROM_PATH_EXTRACTOR = r'.+' + JIRA_PLUGINS_DIR + os.sep + '(.+)' + os.sep + "target.+"


class BundledPluginsUtility:
    def __init__(self, file_utils=FileUtils()):
        super().__init__()
import os
import re
from Logger import LOG
from utils import PathUtils
from utils.FileUtils import FileUtils


JIRA_PLUGINS_DIR = os.sep.join(['jira-components', 'jira-plugins'])
JIRA_PLUGINS_DIR_ABS = os.path.abspath(os.sep.join(['jira-components', 'jira-plugins']))
JIRA_PLUGINS_POM_XML = os.sep.join([JIRA_PLUGINS_DIR, 'pom.xml'])
JIRA_PLUGIN_DIR = os.sep.join([JIRA_PLUGINS_DIR, '{0}'])
PLUGIN_TARGET_DIR = os.sep.join([JIRA_PLUGINS_DIR, '{}', 'target'])
PLUGIN_SRC_DIR = os.sep.join([JIRA_PLUGINS_DIR, '{}', 'src'])
PLUGIN_POM_DIR = os.sep.join([JIRA_PLUGINS_DIR, '{}', 'pom.xml'])
PLUGIN_JAR_FILE = os.sep.join([PLUGIN_TARGET_DIR, '{0}-{1}.jar'])
BUNDLED_PLUGINS_LIST = PathUtils.abspath(
    os.sep.join([PLUGIN_TARGET_DIR.format('jira-bundled-plugins'), 'atlassian-bundled-plugins.list']))
JMAKE_PROFILES_PLUGINS_LIST = os.path.abspath(
    os.sep.join([PLUGIN_TARGET_DIR.format('jira-bundled-plugins'), 'jmake-bundled-plugins-profiles.list']))
BUNDLED_PLUGINS_MODULE = JIRA_PLUGIN_DIR.format('jira-bundled-plugins')
BUNDLED_PLUGINS_POM = PLUGIN_POM_DIR.format('jira-bundled-plugins')
PLUGIN_FROM_PATH_EXTRACTOR = r'.+' + JIRA_PLUGINS_DIR + os.sep + '(.+)' + os.sep + "target.+"

class BundledPluginsUtility:
    def __init__(self, file_utils=FileUtils()):
        super().__init__()
        self.file_utils = file_utils


    def get_bundled_plugins_module(self):
        return [BUNDLED_PLUGINS_MODULE]
Example #19
0
 def webdav_dir(self):
     return PathUtils.abspath(
         os.sep.join([self.jira_home_dir,
                      WorkspaceLayout.WEBDAV_DIRECTORY]))
Example #20
0
 def studio_initial_data(self):
     return PathUtils.abspath(
         os.sep.join(
             [self.jira_home_dir, WorkspaceLayout.STUDIO_INIT_DATA_FILE]))
Example #21
0
 def jira_home(self):
     return PathUtils.abspath(self.jira_home_dir)
Example #22
0
    def jira_webapp_dir(self):

        return PathUtils.abspath(WorkspaceLayout.JIRA_OD_WEBAPP) if self.ondemand \
            else PathUtils.abspath(WorkspaceLayout.JIRA_WEBAPP)
Example #23
0
    def save_image(self, user_id, user_category, file_save):
        """
        保存富文本编辑器上传的图片: 图片要压缩, 图片转为jpeg格式
        :param user_id: 图片使用者的id
        :param user_category: 图片使用者的分类信息,通过分类信息dict,可以获取很多有用信息
        :param file_save: 要保存的文件
        :return: 图片保存的路径(含静态路径)
        """
        try:
            # 获取文件
            filename = os.path.splitext(
                file_save['filename'])[0]  # 只有上传文件对应的part才有该键,通过抛出异常来获得有效文件
            # 文件类型
            file_type_full = file_save['content_type']  # u'image/jpeg'
            if '/' in file_type_full:  # image
                file_type = file_type_full.split('/')[0]
            elif '\\' in file_type_full['type']:
                file_type = file_type_full['type'].split('\\')[0]
            elif '\\\\' in file_type_full['type']:
                file_type = file_type_full['type'].split('\\\\')[0]
            else:
                file_type = 'unknown'

            # 生成文件名
            name_use = self.make_file_name_with_user_id(
                user_category=user_category['category'],
                user_id=user_id,
                file_category='u',
                file_suffix='.jpg')
            # 文件夹相对路径: files/upload/2017/3/24
            path_folder_relative_no_static = self.make_path_folder_relative_no_static(
            )
            # 文件夹绝对对路径:..../static/files/upload/2017/3/24
            path_folder_absolute = PathUtils.connect(
                path.PATH_STATIC, path_folder_relative_no_static)
            # 文件保存绝对路径
            path_save_use = PathUtils.connect(path_folder_absolute, name_use)
            # 数据库路径
            path_db_use = PathUtils.to_url(
                PathUtils.connect(path_folder_relative_no_static, name_use))
            # 创建保存路径
            if not os.path.exists(path_folder_absolute):
                os.makedirs(path_folder_absolute)

            # 从字符串生成图片, 并保存为jpg(保存路径后缀决定)
            Image.open(StringIO.StringIO(file_save['body'])).save(
                path_save_use, format='jpeg')
            # 如果原始文件大小超标,压缩
            if os.path.getsize(
                    path_save_use) > user_category['image_size_limit']:
                self.compress_image(image_file_path=path_save_use,
                                    category_info=user_category)
            file_size = os.path.getsize(path_save_use)
            image_id = TimeUtils.time_id()
            date_time = TimeUtils.datetime_date_simple()
            param_origin = {
                'id': image_id,
                'user_id': user_id,
                'user_category': user_category['category'],
                'file_category': 'use',
                'title': name_use,
                'summary': filename,
                'type': file_type,
                'created': date_time,
                'size': file_size,
                'path': path_db_use
            }
            self.insert_one_item(user_category['file_table'], **param_origin)

            # 返回数据
            return self.add_static(path_db_use)
        except Exception as e:
            traceback.print_exc()
            app_log.error(e)
            return ''
Example #24
0
 def abs_path(self, path):
     return PathUtils.abspath(path)
Example #25
0
 def existing_dir(self, path):
     result = PathUtils.abspath(path)
     if not os.path.lexists(result): os.makedirs(result)
     return result
Example #26
0
    def jira_webapp_dir(self):

        return PathUtils.abspath(WorkspaceLayout.JIRA_OD_WEBAPP) if self.ondemand \
            else PathUtils.abspath(WorkspaceLayout.JIRA_WEBAPP)
Example #27
0
 def studio_initial_data(self):
     return PathUtils.abspath(os.sep.join([self.jira_home_dir, WorkspaceLayout.STUDIO_INIT_DATA_FILE]))
Example #28
0
    def save_upload_file(self, post_streamer, user_id, user_category_info,
                         file_title):
        """
        返回文件路径、文件 id
        :param post_streamer:
        :param user_id: 文件使用者id
        :param user_category_info: 文件使用者的分类
        :param file_title: 文件名称
        :return:
        """
        def clean_file_name(file_name):
            """
            去除文件名中不规范的项目(利用正则表达式)
            """
            re_str = r"[\/\\\:\*\?\"\<\>\| _]"  # '/\:*?"<>|'
            return re.sub(re_str, "", file_name)

        # 获取文件信息
        file_info = {}
        for part in post_streamer.parts:
            """
            [
                { headers:[ {params, name, value}, {params, name, value} ], tempile, size },    # part

                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
            ]
            0、poststreamer.parts,为一个 list 对象,其总共有六个 dict(headers、tempfile、size) 元素
            1、size 文件大小
            2、tempfile 值是一个临时文件对象 (转存要用到)
            4、headers 值是一个 list [ {params, name, value}, ]
            5、六个 dict 中,第一个为需要的

            """
            try:
                file_args = {}
                part["tmpfile"].close()
                # 获取文件后缀
                params = part["headers"][0].get("params", None)
                filename = params[
                    'filename']  # 只有上传文件对应的part才有该键,通过抛出异常来获得有效文件
                fill_suffix = PathUtils.get_file_suffix(filename)
                file_args['id'] = TimeUtils.time_id()
                file_args['user_id'] = user_id
                file_args['user_category'] = user_category_info['category']
                file_args['file_category'] = 'attachment'
                file_args['created'] = TimeUtils.datetime_date_simple()
                file_args['size'] = part["size"]
                file_args['title'] = clean_file_name(file_title)
                if len(file_args['title']) == 0:
                    file_args['title'] = str(file_args['id'])
                # 文件类型
                full_file_type = part["headers"][1].get("value", "text/plain")
                if '/' in full_file_type:
                    file_args['type'] = full_file_type.split('/')[0]
                elif '\\' in full_file_type:
                    file_args['type'] = full_file_type.split('\\')[0]
                elif '\\\\' in full_file_type:
                    file_args['type'] = full_file_type.split('\\\\')[0]
                # 文件名:id_user file_title suffix
                name_file = StringUtils.connect(file_args['user_category'],
                                                '_', file_args['user_id'], '-',
                                                file_args['id'], '-',
                                                file_args['title'],
                                                fill_suffix)
                path_folder_relative = self.make_path_folder_relative_no_static(
                )
                path_folder_absolute = PathUtils.connect(
                    path.PATH_STATIC, path_folder_relative)
                file_args['path'] = PathUtils.to_url(
                    PathUtils.connect(path_folder_relative, name_file))
                path_save = PathUtils.connect(path_folder_absolute, name_file)
                if isinstance(path_save, type('')):
                    path_save = path_save.decode('utf-8')
                # 创建文件夹路径
                if not os.path.exists(path_folder_absolute):
                    os.makedirs(path_folder_absolute)
                # 转存文件(这步将临时文件保存为正式文件, 关键)
                os.rename(part["tmpfile"].name, path_save)
                if not os.path.exists(path_save):  # 判断是否转存成功
                    file_info = {}
                    continue
                file_info = file_args
                # break # 不能终止循环,要通过异常,来删除临时文件
            except Exception as e:
                # traceback.print_exc()
                part["tmpfile"].close()
                os.unlink(part["tmpfile"].name)  # 删除临时文件(有多个,只有一个是上传文件)
        # 将信息写入数据库
        if len(file_info) > 0:
            result = self.insert_one_item(user_category_info['file_table'],
                                          **file_info)
            if result != -1:
                return {
                    '': file_info['id'],
                    'file_path': file_info['path'],
                    'file_title': file_info['title']
                }
            else:
                return {}
        else:
            return {}
Example #29
0
 def webdav_dir(self):
     return PathUtils.abspath(os.sep.join([self.jira_home_dir, WorkspaceLayout.WEBDAV_DIRECTORY]))
Example #30
0
PATH_STATIC = os.path.join(PATH_BASE, NAME_STATIC)
# 模板路径
PATH_TEMPLATE = os.path.join(PATH_BASE, NAME_TEMPLATE)


# 以下路径都是不含静态文件夹名的相对路径
# 默认使用文件路径保存的路径(files/default/)(该文件夹下放:默认logo、默认缩略图、验证码图片)
PATH_DEFAULT_FILE = os.path.join(NAME_FILE_ROOT, 'default')
# 上传文件的路径(files/upload/)
PATH_UPLOAD = os.path.join(NAME_FILE_ROOT, 'upload')    # 这样写系统相关,使用时要通过os.path.join转为系统无关
# 临时文件存放夹(该文件夹可随意删)(files/temp/)
# PATH_TEMP = os.path.join(NAME_FILE_ROOT, 'temp')
PATH_TEMP = os.path.join(PATH_STATIC, NAME_FILE_ROOT, 'temp')
# 上传的 logo 保存的路径(保存在 upload 文件夹下)
PATH_LOGO = 'logo'

# 默认缩略图路径(不含静态文件夹名,url 形式)
PATH_THUMB_DEFAULT = 'files/default/default_thumb.jpg'
# 默认logo文件的路径(数据库没有内容时用)
PATH_LOGO_DEFAULT = 'files/default/logo.png'


# 验证码存放相对路径
PATH_VERIFY_SAVE_RELATIVE = PathUtils.connect(PATH_STATIC, 'files/default')
# 验证码存放绝对路径
PATH_VERIFY_SAVE_ABSOLUTE = PathUtils.connect(PATH_STATIC, 'files/default/verify.jpg')
# 验证码网页使用路径
PATH_VERIFY_URL = StringUtils.connect('/', NAME_STATIC, '/', 'files/default/verify.jpg')
# 验证码字体文件路径
PATH_VERIFY_FONT = PathUtils.connect(PATH_STATIC, 'files/default/Monaco.ttf')
Example #31
0
 def jira_home(self):
     return PathUtils.abspath(self.jira_home_dir)
Example #32
0
 def abs_path(self, path):
     return PathUtils.abspath(path)
Example #33
0
 def existing_dir(self, path):
     result = PathUtils.abspath(path)
     if not os.path.lexists(result): os.makedirs(result)
     return result