Ejemplo n.º 1
0
        def __init__(self):
            debug('instancing dbus') 
            self.loop = DBusGMainLoop(set_as_default=True)
            self.session_bus = dbus.SessionBus()
            self.bus_name = dbus.service.BusName(BUS_NAME, bus=self.session_bus)

            self.objects = {} #name: instance
            self.events = {} #name: instance
Ejemplo n.º 2
0
 def print_more(self):
     res = ''
     for v in self.nodes.values():
         res += '%s:' % str(v)
         for e in self.get_adiacent_edge(v):
             res += '%s (' % str(e.tuple()[1])
             for key,value in e.labels.items():
                 res += '%s=%s' % (key,value)
             res += '),'
         res += '\n'
     debug(res)
     return res
Ejemplo n.º 3
0
 def do_debug(self, arg=None):
     if self.device:
         output = self.device.shell("ps |grep %s" % arg)
         try:
             pid = output.split(" ")[4]
             print(output)
             debugger.debug(pid)
         except IndexError:
             print("[-] Failed to run debugging. Make sure the app is running and the package name is correct.")
         
     else:
         print("[-] Connect to a device first.")
Ejemplo n.º 4
0
 def test_fix_country(self):
     """Tests the fix_country method"""
     original = pd.Series(["  gErMaNy ", "   uNiTeD sTaTeS    "])
     spark_df = self.spark.createDataFrame(
         pd.DataFrame({"Country": original}))
     spark_df = spark_df.withColumn(
         "Country", self.transformer.fix_country(F.col("Country")))
     fixed = spark_df.toPandas()
     try:
         result = sorted(fixed["Country"])
         assert result == ["Germany", "United States"]
     except Exception as e:
         raise type(e)(''.join(debug(original))) from e
Ejemplo n.º 5
0
    def from_drawing(self, draw):
        SCALE=35
        CIRCLE_SIZE=10
        colors = [QtCore.Qt.yellow, QtCore.Qt.red, QtCore.Qt.black,
                QtCore.Qt.blue, QtCore.Qt.green]
        i=0
        for poly in draw.lines:
            #draw line
            #dx = random.randrange(-9,9)
            #dy = random.randrange(-9,9)
            dx = 0
            dy = 0
            i+=1
            line_col =colors[i%5]
            for line in poly.lines:
                lineitem = QtGui.QGraphicsLineItem(
                        SCALE*line.start.x+dx+CIRCLE_SIZE/2, -SCALE*line.start.y+dy+CIRCLE_SIZE/2,
                        SCALE*line.end.x+dx+CIRCLE_SIZE/2, -SCALE*line.end.y+dy+CIRCLE_SIZE/2)
                lineitem.setPen(line_col)
                self.addItem(lineitem)
        for n, p in draw.positions.items():
            #draw point
            debug('drawing node %s at (%d,%d)' % (n, p.x, p.y))
            circle = QtGui.QGraphicsEllipseItem(
                SCALE*p.x, -SCALE*p.y, CIRCLE_SIZE, CIRCLE_SIZE)
            circle.setToolTip(n)
            circle.setBrush(QtGui.QBrush(QtCore.Qt.yellow))
            circle.setZValue(1) #they're on top of lines
            self.addItem(circle)
            label = QtGui.QGraphicsTextItem(n)
            label.setPos(SCALE*p.x + CIRCLE_SIZE, -SCALE*p.y)
            label.setZValue(2) #they're on top of everythin
            self.addItem(label)

        for x in range(-5,5):
            c = QtGui.QGraphicsTextItem(str(x))
            c.setPos(SCALE*x, SCALE*1)
            c.setZValue(3)
            self.addItem(c)
Ejemplo n.º 6
0
    def test_remove_lenny_face(self):
        """Tests the remove_lenny_face method.

        Objective: Convert an incoming string into a format that can be casted to a FloatType by Spark.
        Spark is smart enough to convert "69.420" to 69.420 but <69.420> will be casted to a null.
        To keep the exercise simple (no regex required), you'll only need to handle the Lenny face.
        """
        original = pd.Series([
            "( ͡° ͜ʖ ͡°)4.384( ͡° ͜ʖ ͡°)", "-", "?", "#",
            "( ͡° ͜ʖ ͡°)1.53( ͡° ͜ʖ ͡°)"
        ])
        try:
            result = original.map(self.transformer.remove_lenny_face)
            assert result.to_list() == ["4.384", "-", "?", "#",
                                        "1.53"]  # make valid floats parsable
            spark_df = self.spark.createDataFrame(pd.DataFrame({"lol":
                                                                result}))
            spark_df = spark_df.withColumn(
                "lmao",
                F.col("lol").cast(FloatType()))  # automatic casting with spark
            assert spark_df.filter(F.col("lmao").isNull()).count() == 3
            assert spark_df.filter(F.col("lmao").isNotNull()).count() == 2
        except Exception as e:
            raise type(e)(''.join(debug(original))) from e
Ejemplo n.º 7
0
    def st_graph(self, s):
        '''return a DiTree that is an st-graph'''
        assert s in self.nodes.values()
        dg = DiGraph()
        queue = [(None, s)]
        #s['auxvis'] = True
        count = 1
        while queue:
            fath, n_old= queue.pop()
            if n_old['auxvis']:
                continue
            n = Node(n_old.id())
            debug('%s %s' % (str(n), str([(str(v), v['auxvis']) for v in self.get_adiacents(n_old)])))
            dg.add_node(n)
            if fath:
                debug('FATH %s %s' % (str(fath), str(n)))
                dg.add_edge(fath, n)
            n['dfn'] = count #Depth-First Numbering
            n['fath'] = fath
            n['auxvis'] = True
            n_old['auxvis'] = True
            assert n.adiacent == []
            for c in self.get_adiacents(n_old):
                if c['auxvis'] and fath.id() != c.id(): #Already visited
                    new_edge = dg.add_edge_by_id(n.id(), c.id()) #Arco all'indietro!!
                    new_edge['back'] = True
            new = [(n, c) for c in self.get_adiacents(n_old) if (not c['auxvis'])]
            new.reverse()
            queue += new
            count += 1

        s_t = [e for e in dg.get_adiacent_edge(dg.get_node(s.id())) if not e['back']][0]
        dg.t = dg.get_node(s.id())
        dg.s = s_t.tuple()[1]
        dg.print_more()
        assert dg.get_edge(dg.t, dg.s) is not None
        debug('S %s T %s' % (str(dg.s), str(dg.t)))
        return dg
Ejemplo n.º 8
0
    def draw(self):
        g = self.graph
        allocated_col = [] #int TODO: do we really need it?
        pending_edges = {} #node_from.id():[col1, col2]
        avail_sides = {}
        nodes = g.nodes.values()

        # Initialize avail_sides
        for n in nodes:
            #-1 is left, 0 is down, 1 is right, 2 is up
            avail_sides[n.id()] = [-1, 0, 1, 2]

        #### Routine definitions
        def get_position(node):
            return self.positions[node.id()]
        def set_position(node, column, line):
            '''Put a node somewhere'''
            self.positions[node.id()] = Point(column, line)
            return self.positions[node.id()]
        def allocate_column(node, column=None):
            #If none, uses himself column
            if column is None:
                column = get_position(node).x
            allocated_col.append(column)
            if node.id() in pending_edges:
                pending_edges[node.id()].append(column)
            else:
                pending_edges[node.id()] = [column]
        allocate_column.leftish_col = 0
        allocate_column.rightish_col = 3
        def allocate_column_left(node):
            allocate_column.leftish_col -= 1
            allocate_column(node, allocate_column.leftish_col)
        def allocate_column_right(node):
            allocate_column.rightish_col += 1
            allocate_column(node, allocate_column.rightish_col)

        def stn(node):
            return node['stn']
        def connect_points(a, b, col):
            if avail_sides[b.id()] == [2]: #Last, 4-degree node
                pl = Polyline.hvh(get_position(a), get_position(b)+(0,1), col)
                pl.add_line(Line(get_position(b)+(0,1), get_position(b)))
            else:
                pl = Polyline.hvh(get_position(a), get_position(b), col)
                if get_position(b).x < col:
                    avail_sides[b.id()].remove(1)
                elif get_position(b).x == col:
                    avail_sides[b.id()].remove(0)
                else:
                    avail_sides[b.id()].remove(-1)

            self.lines.append(pl)
            if get_position(a).x < col:
                avail_sides[a.id()].remove(1)
            elif get_position(a).x == col:
                avail_sides[a.id()].remove(2)
            else:
                avail_sides[a.id()].remove(-1)

            pending_edges[a.id()].remove(col)

            return pl

        ### End routine definition

        #Sorting by ST-Numbering is the way!
        nodes.sort(key=stn)
        debug(str([(n.id(), n['stn']) for n in  nodes]))

        #Draw 1, 2 and the edge between them
        v1 = nodes.pop(0)
        v2 = nodes.pop(0)
        set_position(v1, 0, 0) #The center of drawing is v1
        set_position(v2, 3, 0)
        v1_v2_line = Polyline()
        v1_v2_line.add_line(Line((0,0), (0, -1)))
        v1_v2_line.add_line(Line((0,-1), (3, -1)))
        v1_v2_line.add_line(Line((3,-1), (3, 0)))
        self.lines.append(v1_v2_line)
        #So ugly
        pending_edges[v1.id()] = []
        pending_edges[v2.id()] = []
        allocate_column(v1)
        if len(g.get_adiacents(v1)) > 2:
            allocate_column(v1, 1)
        if len(g.get_adiacents(v1)) > 3:
            allocate_column_left(v1)
        allocate_column(v2)
        if len(g.get_adiacents(v2)) > 2:
            allocate_column(v2, 2)
        if len(g.get_adiacents(v2)) > 3:
            allocate_column_right(v2)

        line = 1
        v = nodes.pop(0)
        while len(nodes) >= 0:
            debug('now on %s' % v.name)
            debug(str(pending_edges))
            #Choose column
            available_cols = []
            for x in g.get_adiacents(v):
                if x.id() in pending_edges:
                    for edge in pending_edges[x.id()]:
                        available_cols.append((edge, x.id()))
            if not available_cols:
                raise Exception('sth went wrong: no available columns!')
            #col is the chosen column
            degree = len([x for x in g.get_adiacents(v) if x['stn'] < v['stn']])
            debug('Choosing col for %s from %s' % (v.id(), str(available_cols)))
            col = available_cols[len(available_cols)/2]
            chosen_col = col_choose.column_choose(available_cols)
            col = chosen_col[(len(chosen_col)-1)/2][0]
            set_position(v, col, line)
            debug('Chosen: %d' % col)
            for column in chosen_col:
                debug('Connect %s to %s through %d' % (column[1], v.id(), column[0]))
                connect_points(g.nodes[column[1]], v, column[0])

            out_degree = len(g.get_adiacents(v)) - 4 + len(avail_sides[v.id()])
            debug(str(avail_sides[v.id()]))
            debug('%s has %d out_degree' % (v.id(), out_degree))
            allocate_column(v)
            if out_degree > 1:
                if -1 in avail_sides[v.id()]:
                    allocate_column_left(v)
                    if out_degree > 2:
                        assert 1 in avail_sides[v.id()]
                        allocate_column_right(v)
                else:
                    assert out_degree == 2
                    allocate_column_right(v)

            line += 1
            try:
                v = nodes.pop(0)
            except:
                break
        info(str(self.positions))
Ejemplo n.º 9
0
 def print_graph(self):
     res = ''
     for v in self.nodes.values():
         res +=  '%s %s\n' % (str(v), str([str(ad) for ad in self.get_adiacents(v)]))
     debug(res)
     return res
Ejemplo n.º 10
0
        def path(v):
            self.s['path_mark'] = True
            self.t['path_mark'] = True
            self.get_edge_by_id(self.t.id(), self.s.id())['path_mark'] = True

            #Caso1: c'e' un arco di riporto non marcato {v,w}: viene marcato l'arco e ritornato vw
            for adiac in self.get_adiacent_edge(v):
                if adiac['back'] and not adiac['path_mark']:
                    debug('CASO 1: arco di riporto non marcato (v,w)')
                    adiac['path_mark'] = True
                    return [v, adiac.tuple()[1]]

            #Caso 2: esiste un arco dell'albero non marcato (v,w)
            for adiac in self.get_adiacent_edge(v):
                if (not adiac['back']) and (not adiac['path_mark']):
                    debug('CASO 2: arco dell albero non marcato')
                    ret = [v]
                    w = wi = adiac.tuple()[1]
                    adiac['path_mark'] = True
                    while True:
                        debug(str(wi) + 'ret now' + str([str(n) for n in ret]))
                        wi['path_mark'] = True
                        ret.append( wi)
                        for backedge in self.get_adiacent_edge(wi):
                            if not backedge['back']: # or backedge['path_mark']:
                                continue
                            u = backedge.tuple()[1]
                            #mah...
                            if u['dfn'] != w['low']:
                                continue
                            if u['path_mark']:
                                backedge['path_mark'] = True
                                u['path_mark'] = True
                                ret.append(u)
                                v['path_mark'] = True
                                return ret

                            #u_v = self.get_edge(u, v)
                            #if u_v and not u_v['back']:
                            #    u['path_mark'] = True
                            #    #u_v['path_mark'] = True
                            #    ret.append(u)
                            #    return ret
                        for treeedge in self.get_adiacent_edge(wi):
                            if treeedge['back']:
                                continue
                            if treeedge.tuple()[1]['low'] == w['low']:
                                wi = treeedge.tuple()[1]
                                wi['path_mark'] = True
                                treeedge['path_mark'] = True
                                break
                        else: #Nothing found
                            raise Exception('Where do we go now?')

            #Caso 3: Esiste un arco di riporto non marcato
            for adiac in self.get_incident_edge(v):
                if adiac['back'] and not adiac['path_mark']:
                    debug('CASO 3: Arco di riporto non marcato (w,v)')
                    assert adiac.tuple()[0]['dfn'] > adiac.tuple()[1]['dfn']
                    #Risaliamo l'albero seguendo FATH
                    ret = [v]
                    wi = adiac.tuple()[0]
                    v['path_mark'] = True
                    adiac['path_mark'] = True
                    edge = adiac
                    #while wi != v:
                    while not wi['path_mark']:
                        wi['path_mark'] = True
                        edge['path_mark'] = True
                        ret.append(wi)
                        edge = self.get_edge(wi['fath'], wi)
                        wi = wi['fath']
                    wi['path_mark'] = True
                    edge['path_mark'] = True
                    ret.append(wi)
                    return ret
            #Caso 4: tutti gli archi incidenti a v sono marcati
            if False not in [adiac['path_mark'] for adiac in self.get_incident_edge(v)]:
                debug('Caso 4: tutti gli archi incidenti a v sono marcati')
                v['path_mark'] = True
                return []

            raise Exception('Per %s Nessun caso va bene!!' % str(v))
Ejemplo n.º 11
0
    def st(self):
        def low(n):
            if n['low']:
                return n['low']
            min = n['dfn']
            for x in [edge.tuple()[1] for edge in self.get_adiacent_edge(n) if not edge['back']]:
                low_x = low(x)
                assert low_x is not None
                if low_x < min:
                    min = x['low']
            for w in [edge.tuple()[1] for edge in self.get_adiacent_edge(n) if edge['back']]:
                if w['dfn'] < min:
                    min = w['dfn']
            n['low'] = min
            return min
        def path(v):
            self.s['path_mark'] = True
            self.t['path_mark'] = True
            self.get_edge_by_id(self.t.id(), self.s.id())['path_mark'] = True

            #Caso1: c'e' un arco di riporto non marcato {v,w}: viene marcato l'arco e ritornato vw
            for adiac in self.get_adiacent_edge(v):
                if adiac['back'] and not adiac['path_mark']:
                    debug('CASO 1: arco di riporto non marcato (v,w)')
                    adiac['path_mark'] = True
                    return [v, adiac.tuple()[1]]

            #Caso 2: esiste un arco dell'albero non marcato (v,w)
            for adiac in self.get_adiacent_edge(v):
                if (not adiac['back']) and (not adiac['path_mark']):
                    debug('CASO 2: arco dell albero non marcato')
                    ret = [v]
                    w = wi = adiac.tuple()[1]
                    adiac['path_mark'] = True
                    while True:
                        debug(str(wi) + 'ret now' + str([str(n) for n in ret]))
                        wi['path_mark'] = True
                        ret.append( wi)
                        for backedge in self.get_adiacent_edge(wi):
                            if not backedge['back']: # or backedge['path_mark']:
                                continue
                            u = backedge.tuple()[1]
                            #mah...
                            if u['dfn'] != w['low']:
                                continue
                            if u['path_mark']:
                                backedge['path_mark'] = True
                                u['path_mark'] = True
                                ret.append(u)
                                v['path_mark'] = True
                                return ret

                            #u_v = self.get_edge(u, v)
                            #if u_v and not u_v['back']:
                            #    u['path_mark'] = True
                            #    #u_v['path_mark'] = True
                            #    ret.append(u)
                            #    return ret
                        for treeedge in self.get_adiacent_edge(wi):
                            if treeedge['back']:
                                continue
                            if treeedge.tuple()[1]['low'] == w['low']:
                                wi = treeedge.tuple()[1]
                                wi['path_mark'] = True
                                treeedge['path_mark'] = True
                                break
                        else: #Nothing found
                            raise Exception('Where do we go now?')

            #Caso 3: Esiste un arco di riporto non marcato
            for adiac in self.get_incident_edge(v):
                if adiac['back'] and not adiac['path_mark']:
                    debug('CASO 3: Arco di riporto non marcato (w,v)')
                    assert adiac.tuple()[0]['dfn'] > adiac.tuple()[1]['dfn']
                    #Risaliamo l'albero seguendo FATH
                    ret = [v]
                    wi = adiac.tuple()[0]
                    v['path_mark'] = True
                    adiac['path_mark'] = True
                    edge = adiac
                    #while wi != v:
                    while not wi['path_mark']:
                        wi['path_mark'] = True
                        edge['path_mark'] = True
                        ret.append(wi)
                        edge = self.get_edge(wi['fath'], wi)
                        wi = wi['fath']
                    wi['path_mark'] = True
                    edge['path_mark'] = True
                    ret.append(wi)
                    return ret
            #Caso 4: tutti gli archi incidenti a v sono marcati
            if False not in [adiac['path_mark'] for adiac in self.get_incident_edge(v)]:
                debug('Caso 4: tutti gli archi incidenti a v sono marcati')
                v['path_mark'] = True
                return []

            raise Exception('Per %s Nessun caso va bene!!' % str(v))

        for v in self.nodes.values():
            low(v)
        with open('lastgraph.dot', 'w') as buf:
            buf.write('digraph G {\n%s}\n' % self.to_graphviz())
        #Its just a test, the real algo is a bit more complex
        stack = [self.t, self.s]
        self.s['vis'] = True
        self.t['vis'] = True
        self.get_edge(self.t, self.s)['vis'] = True
        cont = 1
        v = stack.pop() #s
        while v != self.t:
            res = path(v)
            debug('path(%s) = %s' % (str(v), [str(e) for e in res]))
            if res == [] and not v['stn']:
                debug('Assigned %s[STN] = %d' % (v.name, cont))
                v['stn'] = cont
                cont += 1
                with open('lastgraph.dot.%d' % cont, 'w') as buf:
                    buf.write('digraph G {\n%s}\n' % self.to_graphviz())
            else:
                res.reverse()
                stack.extend(res[1:])
            v = stack.pop()

        self.t['stn'] = cont
        for n in self.nodes.values():
            debug('%s has STN: %d' % (str(n), n['stn']))
Ejemplo n.º 12
0
except:
    traceback.print_exc()
    print('Press Enter to close...')
    try:
        raw_input()
    except NameError:
        input()
    sys.exit(1)


#=======================================================================================================================
# 1. Debugger port to connect to.
# 2. GUID for the debug session.
# 3. Startup script name.
#=======================================================================================================================
port_num = int(sys.argv[1])
debug_id = sys.argv[2]
del sys.argv[1:3]

# filename = sys.argv[0]

sys.path[0] = ''

current_pid = os.getpid()

del sys, os

print(current_pid)

debugger.debug(port_num, debug_id, current_pid)
Ejemplo n.º 13
0
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the LICENSE file in
# the root directory of this source tree.
'''There is no shebang for this file: it must be run as an argument to `python`.
'''

if __name__ == '__main__':
    '''This should be run via `python main.py <unix.sock> <prog.py> <arg1> ...`.
    '''
    import debugger
    import sys
    path_to_socket = sys.argv[1]
    del sys.argv[0]  # Hide "main.py" from argument list.
    del sys.argv[0]  # Hide path_to_socket from argument list.
    debugger.debug(path_to_socket)
Ejemplo n.º 14
0
    os.chdir(sys.argv[1])

    # fix sys.path to be our real starting dir, not this one
    sys.path[0] = sys.argv[1]
    port_num = int(sys.argv[2])
    debug_id = sys.argv[3]
    del sys.argv[0:4]

    wait_on_exception = False
    redirect_output = False
    if len(sys.argv) >= 1 and sys.argv[0] == '--wait-on-exception':
        wait_on_exception = True
        del sys.argv[0]

    if len(sys.argv) >= 1 and sys.argv[0] == '--redirect-output':
        redirect_output = True
        del sys.argv[0]

    # set file appropriately, fix up sys.argv...
    __file__ = sys.argv[0]

    # remove all state we imported
    del sys, os

    # and start debugging
    debugger.debug(__file__, port_num, debug_id, globals(), locals(),
                   wait_on_exception, redirect_output)
    #input()
except:
    #input()
    pass
Ejemplo n.º 15
0
 def on_testrandom(self):
     global start_time
     start_time = time.time()
     graph = st.build_graph_random(self.spin_box.value())
     self.draw(graph)
     debug(graph.generating_code())
Ejemplo n.º 16
0
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the LICENSE file in
# the root directory of this source tree.

'''There is no shebang for this file: it must be run as an argument to `python`.
'''

if __name__ == '__main__':
    '''This should be run via `python main.py <unix.sock> <prog.py> <arg1> ...`.
    '''
    import debugger
    import sys
    path_to_socket = sys.argv[1]
    del sys.argv[0]  # Hide "main.py" from argument list.
    del sys.argv[0]  # Hide path_to_socket from argument list.
    debugger.debug(path_to_socket)