Ejemplo n.º 1
0
    def _parse_line_entry(self, line, type):
        """
        Parse a section entry line into its components. In case of a 'vars'
        section, the first field will be None. Otherwise, the first field will
        be the unexpanded host or group name the variables apply to.

        For example:
            [production:children]
            frontend  purpose="web"    # The line we process
        Returns:
            ('frontend', {'purpose': 'web'})

        For example:
            [production:vars]
            purpose="web"              # The line we process
        Returns:
            (None, {'purpose': 'web'})

        Undocumented feature:
            [prod:vars]
            json_like_vars=[{'name': 'htpasswd_auth'}]
        Returns:
            (None, {'name': 'htpasswd_auth'})
        """

        name = None
        key_values = {}

        if type == 'vars':
            key_values = self._parse_line_vars(line)
        else:
            tokens = shlex.split(line.strip())
            name = tokens.pop(0)
            key_values = self._parse_vars(tokens)
        return (name, key_values)
Ejemplo n.º 2
0
    def _parse_line_vars(self, line):
        """
        Parse a line in a [XXXXX:vars] section.
        """
        key_values = {}

        # Undocumented feature allows json in vars sections like so:
        #   [prod:vars]
        #   json_like_vars=[{'name': 'htpasswd_auth'}]
        # We'll try this first. If it fails, we'll fall back to normal var
        # lines. Since it's undocumented, we just assume some things.
        k, v = line.strip().split('=', 1)
        if v.startswith('['):
            try:
                list_res = yaml.safe_load(v)
                if isinstance(list_res[0], dict):
                    key_values = list_res[0]
                    return key_values
            except ValueError:
                pass

        # Guess it's not YAML. Parse as normal host variables
        tokens = shlex.split(line.strip())
        key_values = self._parse_vars(tokens)
        return key_values
Ejemplo n.º 3
0
    def process_file(self, in_file):
        self.iline = 0
        file = open(in_file, 'rt')
        self.nerrs = 0

        for line in file.readlines():
            try:
                line = line.strip()
                #            	print line
                items = shlex.split(unicode(line, 'utf-8'))
                if len(items) < 1:
                    self.nerrs += 1
                    continue
                time = int(items[0])
                #                print time,self.min_start,self.max_start
                if time < self.min_start:
                    continue
                if time >= self.max_start:
                    break
                items = [i.encode('utf-8') for i in items]
                #print u'processing ' + u'|'.join([unicode(i,'utf-8') for i in items])
                self.process(items, time, 1000000000)

            except ValueError as e:
                self.nerrs += 1
                #print("%s(%d): syntax error %s "%(in_file,self.iline,e))
                #traceback.print_exc()


#               raise Error(e)
        print "processed %s errors %d" % (in_file, self.nerrs)
Ejemplo n.º 4
0
    def _parse_line_entry(self, line, type):
        """
        Parse a section entry line into its components. In case of a 'vars'
        section, the first field will be None. Otherwise, the first field will
        be the unexpanded host or group name the variables apply to.

        For example:
            [production:children]
            frontend  purpose="web"    # The line we process
        Returns:
            ('frontend', {'purpose': 'web'})

        For example:
            [production:vars]
            purpose="web"              # The line we process
        Returns:
            (None, {'purpose': 'web'})

        Undocumented feature:
            [prod:vars]
            json_like_vars=[{'name': 'htpasswd_auth'}]
        Returns:
            (None, {'name': 'htpasswd_auth'})
        """

        name = None
        key_values = {}

        if type == 'vars':
            key_values = self._parse_line_vars(line)
        else:
            tokens = shlex.split(line.strip())
            name = tokens.pop(0)
            key_values = self._parse_vars(tokens)
        return (name, key_values)
Ejemplo n.º 5
0
    def _parse_line_vars(self, line):
        """
        Parse a line in a [XXXXX:vars] section.
        """
        key_values = {}

        # Undocumented feature allows json in vars sections like so:
        #   [prod:vars]
        #   json_like_vars=[{'name': 'htpasswd_auth'}]
        # We'll try this first. If it fails, we'll fall back to normal var
        # lines. Since it's undocumented, we just assume some things.
        k, v = line.strip().split('=', 1)
        if v.startswith('['):
            try:
                list_res = yaml.load(v)
                if isinstance(list_res[0], dict):
                    key_values = list_res[0]
                    return key_values
            except ValueError:
                pass

        # Guess it's not YAML. Parse as normal host variables
        tokens = shlex.split(line.strip())
        key_values = self._parse_vars(tokens)
        return key_values
Ejemplo n.º 6
0
    def process_score_command(self, channel, message, user):
        self.split_message = shlex.split(message)

        for msg in self.split_message:

            if re.match(r'top', msg):
                self.top_num = re.search(r'(\d)', message).group()
                return self.get_scores(channel, self.top_num)
Ejemplo n.º 7
0
def quote_aware_split(value):
    try:
        # shlex.split preserves quoting when splitting, and removes quotes
        res = shlex.split(value)
    except ValueError:
        # May happen when unbalanced quoting
        res = value.split()
    return res
Ejemplo n.º 8
0
def quote_aware_split(value):
    try:
        # shlex.split preserves quoting when splitting, and removes quotes
        res = shlex.split(value)
    except ValueError:
        # May happen when unbalanced quoting
        res = value.split()
    return res
Ejemplo n.º 9
0
    def __initfilterdictionary(self, filterlist):

        # make it lower as python is case-sensitive for string comparison
        filterlist = list(map(lambda x: x.lower(), filterlist))

        self.filter_dictionary.clear()

        for filterword in filterlist:
            filter = ushlex.split(filterword)
            self.filter_dictionary[filter[0]] = filter
Ejemplo n.º 10
0
    def __initfilterdictionary(self, filterlist):

        # make it lower as python is case-sensitive for string comparison
        filterlist = list(map(lambda x:x.lower(), filterlist))

        self.filter_dictionary.clear()

        for filterword in filterlist:
            filter = ushlex.split(filterword)
            self.filter_dictionary[filter[0]] = filter
Ejemplo n.º 11
0
def invocarComando(comando, params, **sustituciones):

    # IDEA patrón decorador
    if config['entorno'] == 'D':
        rsdo = '{"download": 37788675.986478955, "timestamp": "2017-02-06T18:55:18.024380", "ping": 88.011, "upload": 2796322.6507512033, "server": {"latency": 88.011, "name": "Sevilla", "url": "http://speedtest.sev.adamo.es/speedtest/upload.php", "country": "Spain", "lon": "-5.9869", "cc": "ES", "host": "speedtest.sev.adamo.es:8080", "sponsor": "Adamo", "url2": "http://speed.sev.adamo.es/speedtest/upload.php", "lat": "37.3772", "id": "5487", "d": 41.073372033169306}}'
    else:
        vals = shlex.split(params)
        if len(sustituciones) > 0:
            vals = [x.format(**sustituciones) for x in vals]
        convert = subprocess.check_output([comando] + vals)
        rsdo = str(convert)

    return json.loads(rsdo)
Ejemplo n.º 12
0
    def OnItemActivated(
        self,
        event=None
    ):  # Polemos: Many changes here. Well, unicode and thingathingies.
        """Launching the utility."""
        ID = event.GetText()
        if str(ID) in str(self.data.keys()):
            u = self.data[ID][0]
            cwd_po = os.path.dirname(u)
            args_tmp_po = ushlex.split(self.data[ID][1])
            try:
                args_po = ''
                for x in args_tmp_po:
                    args_po = '%s %s' % (args_po, x)
                if args_po != '': args_po = ' "%s"' % (args_po)
            except:
                args_po = ''

            try:
                # Polemos: todo: temporarily removed - reinstate at some point
                '''if u.lower() == "mish":
                    import mish
                    argsList = self.data[ID][1].split()
                    sys_argv_org = sys.argv
                    mish.sys.argv = ["mish"] + argsList
                    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nMISH output.\n\nArguments: %s\n" % self.arguments.Value
                    mish.callables.main()
                    sys.argv = sys_argv_org
                    #
                    return # <-- bad hack ?'''

                try:
                    command = 'start "Launching..." /D "%s" "%s"%s' % (
                        cwd_po.decode('utf-8'), u.decode('utf-8'),
                        args_po.decode('utf-8'))
                    Popen(command, shell=True, stdin=PIPE, stdout=PIPE)

                except:
                    command = ('start "Launching..." /D "%s" "%s"%s' %
                               (cwd_po.encode('utf-8').decode('utf-8'),
                                u.encode('utf-8').decode('utf-8'),
                                args_po.encode('utf-8').decode('utf-8')))
                    Popen(command, shell=True, stdin=PIPE, stdout=PIPE)
            except:
                gui.dialog.WarningMessage(
                    self,
                    _(u"A problem "
                      u"has occurred while opening '%s'.\nYou should edit 'utils.dcg' and update the corresponding line."
                      % u))
Ejemplo n.º 13
0
 def combine_search(self, query, entity_type=None):
     rs = []
     entities = set(list(self.search_entities(query, entity_type)))
     tags = shlex.split(query)
     tagged_entities = set(list(self.get_tagged_entities(tags,
                                                         entity_type)))
     # This should rank higher as it has matched tags and name | description
     common = entities & tagged_entities
     # Remove entities that are common from entities (this could have matched name | description)
     res_entities = list(entities - common)
     # Remove entities that are common from tagged_entities
     res_tagged = list(tagged_entities - common)
     # Ranking is common -> entities (could have matched name  \ description) -> entities(that have matched tags)
     # One downside is that if an entity has matched in description but not in name that will be ranked higher than tag match.
     rs = list(common) + res_entities + res_tagged
     return rs, len(rs)
Ejemplo n.º 14
0
    def _parse_line_entry(self, line, type):
        """
        Parse a section entry line into its components. In case of a 'vars'
        section, the first field will be None.

        For example:
            [production:children]
            frontend  purpose="web"
        Returns:
            ('frontend', {'purpose': 'web'})
        """
        tokens = shlex.split(line.strip())
        name = None
        if type != 'vars':
            name = tokens.pop(0)
        key_values = self._parse_vars(tokens)
        return (name, key_values)
Ejemplo n.º 15
0
    def _parse_line_entry(self, line, type):
        """
        Parse a section entry line into its components. In case of a 'vars'
        section, the first field will be None.

        For example:
            [production:children]
            frontend  purpose="web"
        Returns:
            ('frontend', {'purpose': 'web'})
        """
        tokens = shlex.split(line.strip())
        name = None
        if type != "vars":
            name = tokens.pop(0)
        key_values = self._parse_vars(tokens)
        return (name, key_values)
Ejemplo n.º 16
0
def ffprobe_file(file):
    """check_output was introduced in python 2.7 so won't work for < 2.7.
        You can use Popen with communicate to get the output
        The file argument must be a full path to a file"""
    logger = logging.getLogger("media")

    if hardcoded_ffprobe_path is None:
        cmd = "which ffprobe"
        args = shlex.split(cmd)

        # We will use a different method to get ffprobe_path depending on python version
        if sys.version_info[0] == 2 and sys.version_info[1] == 7:
            ffprobe_path = subprocess.check_output(args).decode('utf-8')
        else:
            x = subprocess.Popen(args, stdout=subprocess.PIPE)
            ffprobe_path = x.communicate()[0].split()[0]
    else:
        ffprobe_path = hardcoded_ffprobe_path

    #cmd = ffprobe_path + ' "' + file.replace('"', r'\"') + '"' + " -v quiet -show_streams -show_format -print_format json"
    cmd = ffprobe_path + ' "' + file + '"' + " -v quiet -show_streams -show_format -print_format json"

    # /usr/local/bin/ffprobe '/Volumes/travolta/Expedat/1c_DigitalDelivery/strandreleasing/Daughter of Mine/Daughter of Mine Feature CLEAN ProRes.mov' -v quiet -show_streams -show_format -print_format json
    #args = shlex.split(cmd)    #original, worked great for ascii file names
    #args = shlex.split(unicode(cmd, "utf-8"))
    """The shlex.split() code wraps both unicode() and str() instances in a StringIO() object, which can only handle Latin-1 bytes (so not the full unicode codepoint range).
    You'll have to encode (to UTF-8 should work) if you still want to use shlex.split(); 
    """
    #print cmd # /usr/local/bin/ffprobe "/Users/john/temp/test/PKG - PKG - EUPHORIA-102_FRP_23/EUPHORIA-102_FRP_23_DIA_REC/Audio Files/EUPHORIA-102_FRP_23_DIA_REC_N°24.wav" -v quiet -show_streams -show_format -print_format json

    args = ushlex.split(cmd)

    # Log the command
    if debug is True:
        logger.debug(
            'ffprobe cmd: {} {} -v quiet -show_streams -show_format -print_format json'
            .format(ffprobe_path, file))
    """
    output = subprocess.check_output(args).decode('utf-8')
    """
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = p.communicate()
    if error:
        logger.error(error)
    return output
Ejemplo n.º 17
0
    def enrich_command(self, command):
        """
        Enriches the command by replacing recognized words with their model instance counterparts.

        If multiple terms referring to known classes are found, only one is enriched; the rest are not
        enriched but returned as-is.

        :param command: A string understood by shlex
        :returns: The command split into words and model instances
        :rtype: Iterable[str|django.db.models.Model|Command]
        """
        found_classes = set()
        for part in shlex.split(command):
            e_part = self.enrich_part(part)
            if not isinstance(e_part, six.string_types):
                if isclass(e_part):
                    cls = e_part
                else:
                    cls = e_part.__class__
                if cls not in found_classes:
                    found_classes.add(cls)
                else:
                    e_part = part  # Revert back to yielding the string
            yield e_part
Ejemplo n.º 18
0
    def parse(command_string):
        def f(cmd):
            if not cmd:
                return None, []
            if len(cmd) == 1:
                return cmd[0], []
            return cmd[0], cmd[1:]

        if not command_string:
            return [None, []]
        cmds = []
        cmd = []
        parts = ushlex.split(command_string)
        parts.reverse()
        special = False
        while len(parts) > 0:
            part = parts.pop()
            if part in CommandParser._specials:
                if part == '>':
                    special = True
                    filename = None
                    if len(parts) >= 1:
                        filename = parts.pop()
                    cmds.append(('rstdout', ['-f', filename, '-m', 'ab']))
                    cmds.append((f(cmd)))
                    cmds.append(('rstdout', ['-r']))
                    cmd = []
                elif part == '>>':
                    special = True
                    filename = None
                    if len(parts) >= 1:
                        filename = parts.pop()
                    cmds.append(('rstdout', ['-f', filename, '-m', 'wb']))
                    cmds.append((f(cmd)))
                    cmds.append(('rstdout', ['-r']))
                    cmd = []
                elif part == '&':
                    special = True
                    cmds.append(
                        ('var',
                         ['-n', 'pyco_unconditional_execution', '-v', 'True']))
                    cmds.append((f(cmd)))
                    cmds.append(('var', ['-u',
                                         'pyco_unconditional_execution']))
                    cmd = []
                elif part == '&&':
                    special = True
                    cmds.append((f(cmd)))
                    cmd = []
                elif part == '|':
                    special = True
                    cmds.append(
                        ('var',
                         ['-n', 'pyco_unconditional_execution', '-v', 'True']))
                    cmds.append(('rstdout', ['-b']))
                    cmds.append((f(cmd)))
                    cmds.append(('rstdout', ['-r']))
                    cmds.append(('var', ['-u',
                                         'pyco_unconditional_execution']))
                    cmd = []
                elif part == '||':
                    special = True
                    cmds.append(('rstdout', ['-b']))
                    cmds.append((f(cmd)))
                    cmds.append(('rstdout', ['-r']))
                    cmd = []
                else:
                    cmds.append((f(cmd)))
                    cmd = []
            else:
                cmd.append(part)
        if cmd:
            cmds.append((f(cmd)))
        cmds = [(cmd_name, cmd_opts) for cmd_name, cmd_opts in cmds
                if cmd_name is not None]

        # HACK to compute duration on the entire set of commands but I really do not like this!
        # if special and GlobalVariables.get_instance().get('timeit_commands') == 'True':
        #     c = [('var', ['-n', 'timeit_commands', '-v', 'False']),
        #          ('time', ['--start']), ]
        #     c += cmds
        #     c.extend([
        #         ('time', ['--stop']),
        #         ('var', ['-n', 'timeit_commands', '-v', 'True']), ]
        #     )
        #     return c
        return cmds
Ejemplo n.º 19
0
def stream_media():

    @after_this_request
    def add_header(resp):
        if 'X-Sendfile' in resp.headers:
            app.logger.debug('Using X-Sendfile or X-Accel-Redirect')
            resp.headers['X-Accel-Charset'] = 'utf-8'
            resp.headers['X-Accel-Redirect'] = resp.headers['X-Sendfile']

        return resp

    def transcode(process):
        try:
            for chunk in iter(process.stdout.readline, ''):
                yield chunk
            process.wait()
        except:
            traceback.print_exc()
            process.terminate()
            process.wait()

    status, res = get_entity(request, Track)

    if not status:
        return res

    maxBitRate, format, timeOffset, size, estimateContentLength, client = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength', 'c' ])
    if format:
        format = format.lower()

    do_transcoding = False
    src_suffix = res.suffix()
    dst_suffix = src_suffix
    dst_bitrate = res.bitrate
    dst_mimetype = mimetypes.guess_type('a.' + src_suffix)

    if maxBitRate:
        try:
            maxBitRate = int(maxBitRate)
        except:
            return request.error_formatter(0, 'Invalid bitrate value')

        if dst_bitrate > maxBitRate and maxBitRate != 0:
            do_transcoding = True
            dst_bitrate = maxBitRate

    if format and format != 'raw' and format != src_suffix:
        do_transcoding = True
        dst_suffix = format
        dst_mimetype = mimetypes.guess_type(dst_suffix)

    if client:
        prefs = session.query(ClientPrefs).get((request.user.id, client))
        if not prefs:
            prefs = ClientPrefs(user_id = request.user.id, client_name = client)
            session.add(prefs)

        if prefs.format:
            dst_suffix = prefs.format
        if prefs.bitrate and prefs.bitrate < dst_bitrate:
            dst_bitrate = prefs.bitrate


    if not format and src_suffix == 'flac':
        dst_suffix = 'ogg'
        dst_bitrate = 320
        dst_mimetype = 'audio/ogg'
        do_transcoding = True

    duration = mutagen.File(res.path).info.length

    if do_transcoding:
        transcoder = config.get('transcoding', 'transcoder_{}_{}'.format(src_suffix, dst_suffix))

        decoder = config.get('transcoding', 'decoder_' + src_suffix) or config.get('transcoding', 'decoder')
        encoder = config.get('transcoding', 'encoder_' + dst_suffix) or config.get('transcoding', 'encoder')

        if not transcoder and (not decoder or not encoder):
            transcoder = config.get('transcoding', 'transcoder')
            if not transcoder:
                return request.error_formatter(0, 'No way to transcode from {} to {}'.format(src_suffix, dst_suffix))

        transcoder, decoder, encoder = map(lambda x: prepare_transcoding_cmdline(x, res.path, src_suffix, dst_suffix, dst_bitrate), [ transcoder, decoder, encoder ])

        if '|' in transcoder:
            pipe_index = transcoder.index('|')
            decoder = transcoder[:pipe_index]
            encoder = transcoder[pipe_index+1:]
            transcoder = None

        try:
            if not transcoder:
                decoder = map(lambda s: s.decode('UTF8'), shlex.split(decoder.encode('utf8')))
                encoder = map(lambda s: s.decode('UTF8'), shlex.split(encoder.encode('utf8')))
                dec_proc = subprocess.Popen(decoder, stdout = subprocess.PIPE, shell=False)
                proc = subprocess.Popen(encoder, stdin=dec_proc.stdout, stdout=subprocess.PIPE, shell=False)
            else:
                transcoder = map(lambda s: s.decode('UTF8'), shlex.split(transcoder.encode('utf8')))
                proc = subprocess.Popen(transcoder, stdout = subprocess.PIPE, shell=False)

            response = Response(transcode(proc), 200, {'Content-Type': dst_mimetype, 'X-Content-Duration': str(duration)})
        except:
            traceback.print_exc()
            return request.error_formatter(0, 'Error while running the transcoding process: {}'.format(sys.exc_info()[1]))

    else:
        response = send_file(res.path.encode('utf-8'))
        response.headers['Content-Type'] = dst_mimetype
        response.headers['Accept-Ranges'] = 'bytes'
        response.headers['X-Content-Duration'] = str(duration)

    res.play_count = res.play_count + 1
    res.last_play = now()
    request.user.last_play = res
    request.user.last_play_date = now()
    session.commit()

    return response
Ejemplo n.º 20
0
print(
    "Simple script to output IPs in CIDR Range, Parse List and Send to Eyewitness"
)
print("Only scans for ports 80 and 443")

print("\n")
list = raw_input("Input CIDR Range: ")
dir = raw_input("Enter current directory (with trailing /): ")

cidr = list.replace(".", "_") + ".txt"
cidr2 = cidr.replace("/", "_")
ips = open(cidr2, "w")

cmd1 = "prips " + list
args1 = shlex.split(cmd1)
subprocess.call(args1, stdout=ips)
ips.close()
targets = open(cidr2, "r")
new_name = cidr2.strip(".txt") + "_parsed.txt"
new_file = open(new_name, "a")

for ip in targets:
    http = "http://" + ip.strip("\n")
    https = "https://" + ip.strip("\n")
    # print(http)
    # print(https)
    new_file.write(http + "\n")
    new_file.write(https + "\n")

new_file.close()
Ejemplo n.º 21
0
    def processMessage(self, message, ticket_num, orig_user, _channel):
        self.split_message = shlex.split(message)
        self.options = self.processOptions(self.split_message)

        print self.split_message
        print self.options
        print ticket_num

        if self.options[1] or self.options[2]:
            for msg in self.split_message:
                if re.match(r'(?:<@\w.*?>)', msg):
                    self.touser = re.search(r'((?<=\<@)\w*)', msg).group(0)
                    print self.touser

        if self.options[2]:  # assignto
            #  this is going to be fun...
            if not self.options[3]:
                self.touser_email = sc.find_user(self.touser)[2]
                self._orig_user = sc.find_user(orig_user)
                self.ticket = Bugz().assignTicket(ticket_num, self.touser_email, self._orig_user)

                if self.ticket == "User not Found":
                    sc.client.api_call('chat.postMessage', channel=_channel,
                        as_user=True, text="Something went wrong and the ticket could not be assigned" +
                            " to that user for some reason, make sure it did not get stuck in the API user's list.")
                else:
                    self.fromuser_name = sc.find_user(orig_user)[1]

                    sc.client.api_call('chat.postMessage', channel=self.touser,
                        as_user=True, text="Slack user " + self.fromuser_name + " assigned you a ticket on Fogbugz!")
                    sc.client.api_call('chat.postMessage', channel=_channel,
                        as_user=True, text="The ticket you requested has been sent to the Slack user on Fogbugz.")

        if self.options[1]:  # sendto

            if self.options[0]:  # sendto short
                self.ticket = Bugz().returnTicket(True, ticket_num)
            else:
                self.ticket = Bugz().returnTicket(False, ticket_num)

            try:
                self.fromuser = sc.find_user(orig_user)

                if self.ticket.startswith("Something"):
                    sc.client.api_call('chat.postMessage', channel=self.from_user[0],
                        as_user=True, text=self.ticket)
                else:
                    self.msg = "This ticket was sent to you by:  " + self.fromuser[1] + "\n\n" + self.ticket
                    sc.client.api_call('chat.postMessage', channel=self.touser, as_user=True, text=self.msg)

            except AttributeError:
                msg = "Gosh darnit! You screwed it up again.\n\n Make sure you mention a user so I know " \
                    "where to send this thing."
                sc.api_call(
                    'chat.postMessage',
                    channel=channel,
                    as_user=True,
                    text=msg
                )

        elif self.options[0]:  # short
            self.ticket = Bugz().returnTicket(True, ticket_num)
            channel_reply = sc.find_channel(channel)

            if channel_reply:
                channel_reply.send_message(self.ticket)

        else:  # regular
            self.ticket = Bugz().returnTicket(False, ticket_num)
            channel_reply = sc.find_channel(channel)

            if channel_reply:
                channel_reply.send_message(self.ticket)
Ejemplo n.º 22
0
def command_pipe(command):
    return Popen(shlex.split(command),
                 stdin=PIPE,
                 stdout=PIPE,
                 stderr=STDOUT,
                 env=myenv)
Ejemplo n.º 23
0
    def parse(command_string):

        def f(cmd):
            if not cmd:
                return None, []
            if len(cmd) == 1:
                return cmd[0], []
            return cmd[0], cmd[1:]

        if not command_string:
            return [None, []]
        cmds = []
        cmd = []
        parts = ushlex.split(command_string)
        parts.reverse()
        special = False
        while len(parts) > 0:
            part = parts.pop()
            if part in CommandParser._specials:
                if part == '>':
                    special = True
                    filename = None
                    if len(parts) >= 1:
                        filename = parts.pop()
                    cmds.append(('rstdout', ['-f', filename, '-m', 'ab']))
                    cmds.append((f(cmd)))
                    cmds.append(('rstdout', ['-r']))
                    cmd = []
                elif part == '>>':
                    special = True
                    filename = None
                    if len(parts) >= 1:
                        filename = parts.pop()
                    cmds.append(('rstdout', ['-f', filename, '-m', 'wb']))
                    cmds.append((f(cmd)))
                    cmds.append(('rstdout', ['-r']))
                    cmd = []
                elif part == '&':
                    special = True
                    cmds.append(('var', ['-n', 'pyco_unconditional_execution', '-v', 'True']))
                    cmds.append((f(cmd)))
                    cmds.append(('var', ['-u', 'pyco_unconditional_execution']))
                    cmd = []
                elif part == '&&':
                    special = True
                    cmds.append((f(cmd)))
                    cmd = []
                elif part == '|':
                    special = True
                    cmds.append(('var', ['-n', 'pyco_unconditional_execution', '-v', 'True']))
                    cmds.append(('rstdout', ['-b']))
                    cmds.append((f(cmd)))
                    cmds.append(('rstdout', ['-r']))
                    cmds.append(('var', ['-u', 'pyco_unconditional_execution']))
                    cmd = []
                elif part == '||':
                    special = True
                    cmds.append(('rstdout', ['-b']))
                    cmds.append((f(cmd)))
                    cmds.append(('rstdout', ['-r']))
                    cmd = []
                else:
                    cmds.append((f(cmd)))
                    cmd = []
            else:
                cmd.append(part)
        if cmd:
            cmds.append((f(cmd)))
        cmds = [(cmd_name, cmd_opts) for cmd_name, cmd_opts in cmds if cmd_name is not None]

        # HACK to compute duration on the entire set of commands but I really do not like this!
        # if special and GlobalVariables.get_instance().get('timeit_commands') == 'True':
        #     c = [('var', ['-n', 'timeit_commands', '-v', 'False']),
        #          ('time', ['--start']), ]
        #     c += cmds
        #     c.extend([
        #         ('time', ['--stop']),
        #         ('var', ['-n', 'timeit_commands', '-v', 'True']), ]
        #     )
        #     return c
        return cmds
Ejemplo n.º 24
0
def stream_media():
    @after_this_request
    def add_header(resp):
        if 'X-Sendfile' in resp.headers:
            app.logger.debug('Using X-Sendfile or X-Accel-Redirect')
            resp.headers['X-Accel-Charset'] = 'utf-8'
            resp.headers['X-Accel-Redirect'] = resp.headers['X-Sendfile']

        return resp

    def transcode(process):
        try:
            for chunk in iter(process.stdout.readline, ''):
                yield chunk
            process.wait()
        except:
            traceback.print_exc()
            process.terminate()
            process.wait()

    status, res = get_entity(request, Track)

    if not status:
        return res

    maxBitRate, format, timeOffset, size, estimateContentLength, client = map(
        request.args.get, [
            'maxBitRate', 'format', 'timeOffset', 'size',
            'estimateContentLength', 'c'
        ])
    if format:
        format = format.lower()

    do_transcoding = False
    src_suffix = res.suffix()
    dst_suffix = src_suffix
    dst_bitrate = res.bitrate
    dst_mimetype = mimetypes.guess_type('a.' + src_suffix)

    if maxBitRate:
        try:
            maxBitRate = int(maxBitRate)
        except:
            return request.error_formatter(0, 'Invalid bitrate value')

        if dst_bitrate > maxBitRate and maxBitRate != 0:
            do_transcoding = True
            dst_bitrate = maxBitRate

    if format and format != 'raw' and format != src_suffix:
        do_transcoding = True
        dst_suffix = format
        dst_mimetype = mimetypes.guess_type(dst_suffix)

    if client:
        prefs = session.query(ClientPrefs).get((request.user.id, client))
        if not prefs:
            prefs = ClientPrefs(user_id=request.user.id, client_name=client)
            session.add(prefs)

        if prefs.format:
            dst_suffix = prefs.format
        if prefs.bitrate and prefs.bitrate < dst_bitrate:
            dst_bitrate = prefs.bitrate

    if not format and src_suffix == 'flac':
        dst_suffix = 'ogg'
        dst_bitrate = 320
        dst_mimetype = 'audio/ogg'
        do_transcoding = True

    duration = mutagen.File(res.path).info.length

    if do_transcoding:
        transcoder = config.get(
            'transcoding', 'transcoder_{}_{}'.format(src_suffix, dst_suffix))

        decoder = config.get('transcoding',
                             'decoder_' + src_suffix) or config.get(
                                 'transcoding', 'decoder')
        encoder = config.get('transcoding',
                             'encoder_' + dst_suffix) or config.get(
                                 'transcoding', 'encoder')

        if not transcoder and (not decoder or not encoder):
            transcoder = config.get('transcoding', 'transcoder')
            if not transcoder:
                return request.error_formatter(
                    0, 'No way to transcode from {} to {}'.format(
                        src_suffix, dst_suffix))

        transcoder, decoder, encoder = map(
            lambda x: prepare_transcoding_cmdline(x, res.path, src_suffix,
                                                  dst_suffix, dst_bitrate),
            [transcoder, decoder, encoder])

        if '|' in transcoder:
            pipe_index = transcoder.index('|')
            decoder = transcoder[:pipe_index]
            encoder = transcoder[pipe_index + 1:]
            transcoder = None

        try:
            if not transcoder:
                decoder = map(lambda s: s.decode('UTF8'),
                              shlex.split(decoder.encode('utf8')))
                encoder = map(lambda s: s.decode('UTF8'),
                              shlex.split(encoder.encode('utf8')))
                dec_proc = subprocess.Popen(decoder,
                                            stdout=subprocess.PIPE,
                                            shell=False)
                proc = subprocess.Popen(encoder,
                                        stdin=dec_proc.stdout,
                                        stdout=subprocess.PIPE,
                                        shell=False)
            else:
                transcoder = map(lambda s: s.decode('UTF8'),
                                 shlex.split(transcoder.encode('utf8')))
                proc = subprocess.Popen(transcoder,
                                        stdout=subprocess.PIPE,
                                        shell=False)

            response = Response(transcode(proc), 200, {
                'Content-Type': dst_mimetype,
                'X-Content-Duration': str(duration)
            })
        except:
            traceback.print_exc()
            return request.error_formatter(
                0, 'Error while running the transcoding process: {}'.format(
                    sys.exc_info()[1]))

    else:
        response = send_file(res.path.encode('utf-8'))
        response.headers['Content-Type'] = dst_mimetype
        response.headers['Accept-Ranges'] = 'bytes'
        response.headers['X-Content-Duration'] = str(duration)

    res.play_count = res.play_count + 1
    res.last_play = now()
    request.user.last_play = res
    request.user.last_play_date = now()
    session.commit()

    return response