Beispiel #1
0
    def __init__(self):
        c = Configurator()
        self.cfgsection = self.__class__.__name__.upper()
        self._fc = Storage()
        self._tmpdir = c.get('STORAGE', 'temp_dir')
        self.output = ""
        self.max_output = 256*1024
        self.lastlog = 0
        self.exe = None

        try:
            self._overwrite = c.getboolean(self.cfgsection, 'overwrite')
        except (NoOptionError, NoSectionError):
            self._overwrite = True

        try:
            self.progress_regex = c.get(self.cfgsection, 'progress_regex')
        except (NoOptionError, NoSectionError):
            self.progress_regex = None
Beispiel #2
0
class XMPExtractor(object):
    def __init__(self):
        self._fc = Storage()

    def extract(self, infile):
        """ extract the requested feature and returns a dictionary of extracted features """
        infile = self._fc.abspath(infile)
        d = {}
        try:
            xmpfile = XMPFiles(file_path=infile)
            xmp = xmpfile.get_xmp()
            for x in xmp:            
                if x[-1]['IS_SCHEMA']:
                    d[x[0]] = []
                else:
                    d[x[0]].append(x[1:])
            xmpfile.close_file()
            XMPFiles.terminate()
        except:
            pass
        return d
Beispiel #3
0
class XMPEmbedder(object):
    def __init__(self):
        self._fc = Storage()

    def metadata_synch(self, component_id, changes):

        # get xmp

        # the filename is needed because the extension is unknown, the following line of code is
        # tmp code because c.ID will include extension file (not only basename)
        # in the new MediaDART release

        try:
            myxmpfilename = str(self._fc.abspath(component_id))
        except Exception, err:
            log.error("Found some problems getting filename: %s" % err)

        xmpfile = XMPFiles(file_path=myxmpfilename, open_forupdate=files.XMP_OPEN_FORUPDATE)
        xmp = xmpfile.get_xmp()

        if not xmp:
            xmp = XMPMeta()

        for ns in changes.keys():
            # first of all check if namespace str(i[0]) and property name str(i[1]) exist
            prefix = None
            try:
                prefix = xmp.get_prefix_for_namespace(str(ns))
            except XMPError, err:
                log.debug("Error in get_prefix_for namespace: %s" % str(ns))
            if prefix == None:
                # prefix does not exist so it must be created
                try:
                    log.debug("%s %s" % (str(ns), str(changes[ns]["prefix"])))
                    res = xmp.register_namespace(str(ns), str(changes[ns]["prefix"]))  # CHANGE ME
                except XMPError, err:
                    log.error("Error in register_namespace: %s" % err)
Beispiel #4
0
 def __init__(self):
     self._fc = Storage()
Beispiel #5
0
class GenericCmdline(object):
    def __init__(self):
        c = Configurator()
        self.cfgsection = self.__class__.__name__.upper()
        self._fc = Storage()
        self._tmpdir = c.get('STORAGE', 'temp_dir')
        self.output = ""
        self.max_output = 256*1024
        self.lastlog = 0
        self.exe = None

        try:
            self._overwrite = c.getboolean(self.cfgsection, 'overwrite')
        except (NoOptionError, NoSectionError):
            self._overwrite = True

        try:
            self.progress_regex = c.get(self.cfgsection, 'progress_regex')
        except (NoOptionError, NoSectionError):
            self.progress_regex = None

    def __process_args(self, args):
        clean_args = []
        outfile_map = {}
        for arg in args:
            arg = str(arg).strip()
            cleaned = None
            if arg.startswith('..') or arg.startswith(os.sep):
                raise MProcessorError('invalid argument %s' % arg)
            elif arg.startswith('file://'):
                filename = arg[7:]
                if filename:
                    cleaned = str(self._fc.abspath(filename))   # strip file:// 
                else:
                    cleaned = ''
            elif arg.startswith('outfile://'):
                filename = arg[10:]
                if filename:
                    outfile = str(self._fc.abspath(filename))       # strip outfile:// 
                    if not self._overwrite and os.path.exists(outfile):
                        raise MProcessorError('ERROR: %s exists' % outfile)
                    cleaned = os.path.join(os.path.normpath(os.path.dirname(outfile)),
                                           unique() + os.path.basename(outfile))
                    outfile_map[cleaned] = outfile
                else:
                    cleaned = ''
            else:
                cleaned = arg
            clean_args.append(cleaned)
        return clean_args, outfile_map

    def filter_exe(self, cmd):
        """ Check if cmd is a valid/allowed executable in the local machine 
        
        Returns standardized path of executable and the boolean b_require_output.
        If b_require_output is True, the call fails if there are no output files.
        """
        return cmd, False     

    def filter_env(self, env):
        """ Check if env is a valid/allowed environment for the currnet command (self.exe) """
        return {}

    def _cb_done(self, result, tmpfile_map, stdout):
        for tmpfile, outfile in tmpfile_map.items():
            move(tmpfile, outfile)
        # if the script has no return value, return the name of the generated files

        # Try to access result['data'], and return a dictionary with a 'data'
        # field in case it does not work or it's empty
        # FIXME: just for retro-compatibility.  What the heck is going on here?
        try:
            _x = result['data']
            if not _x:
                raise Exception('FIXME: just to trigger "except" below')
        except:
            result = {}
            result['data'] = stdout[0] #"%s" % ':'.join(tmpfile_map.values())
        return result

    def _cb_err(self, result, tmpfile_map):
        for tmpfile in tmpfile_map:
            try:
                os.unlink(tmpfile)
            except:
                pass
        return result

    def accumulate_stdout(self, data, stdout):
        #log.debug('script output:\n%s\n' % data)
        data = data.decode('latin-1')
        stdout[0] = stdout[0][-self.max_output:] + data
        now = time.time()
        if now - self.lastlog > 2:
            log.debug('%s: still running: last output:\n%s' % (self.exe, data.strip()))
            self.lastlog = now

    def call(self, cmd, args, env={}, progress_url=None,):
        stdout = [""]
        self.exe, b_require_output = self.filter_exe(cmd)
        env = self.filter_env(env)
        args, tmpfile_map = self.__process_args(args)
        #log.debug('####### args: %s' % args)
        if b_require_output and not tmpfile_map:
            raise MProcessorError('No output files specified among the arguments')
        if progress_url and not self.progress_regex:
            raise MProcessorError('No configuration for progress report found (option %s/%s)' %
                (self.cfgsection, 'progress_regex'))
        elif progress_url:
            cb_func = get_progress_cb(progress_url, 'gstreamer-progressreport')
            cb_arg = []
        else:
            stdout = [u""]
            cb_arg = [stdout]
            cb_func = self.accumulate_stdout

        log.debug('Executing RunProc')
        try:
            result = RunProc.run(self.exe, args, env, cb=cb_func, cbargv=cb_arg, do_log=1)
            result = self._cb_done(result, tmpfile_map, stdout)
        except Exception as e:
            result = self._cb_err({'data' : ''}, tmpfile_map)
            raise
        return result