Beispiel #1
0
    def list_network_conf_files(self):
        '''
        Builds a list of tuples with  configuration files for wpa_supplicant or other
        strategies, that have been stored due to previous connections. 
        The structure of the tuple is (autonumeric, dir, filename)
        '''
        from conf import Conf
        import os, logging, util
        logger = logging.getLogger(Conf.LOGGER_NAME)

        network_index = 1
        stored_networks = list()
        networks_dir = self.conf_files[Conf.PATH_NETWORKS_DIR]
        
        if Conf.DEBUG_MODE:
            logger.info("Scanning " + networks_dir)

        #candidate_lists will be a lists of lists because it returns one list per subdirectory to walk
        candidate_lists = [conf_file for root,dirs,conf_file in
                           os.walk(networks_dir)]
        not_ser = lambda x: not util.is_net_serialization_filename(x)

        #traverse all lists in lists return all files in the hierarchy
        for clist in candidate_lists:
            for conf_file in filter(not_ser, clist):
                order = util.number_to_string(network_index,2)
                stored_networks.append((order, networks_dir, conf_file))
                network_index += 1

        return stored_networks
Beispiel #2
0
    def test_serializer(self):
        '''
        This test checks:
          * the serialization and storage of the network for later use. For each test network
          we write to a file (pickle), and then we read from that file, checking that it
          is identical to the original network
          * several other auxiliary functions related with storage and retrieval of network
          configuration files
        '''
        net_1_conf = self.cman.get_network_conf_file(self.net_1)
        net_2_conf = self.cman.get_network_conf_file(self.net_2)
        net_3_conf = self.cman.get_network_conf_file(self.net_3)
        net_4_conf = self.cman.get_network_conf_file(self.net_4)
        net_5_conf = self.cman.get_network_conf_file(self.net_5)

        self.net_1_ser = util.build_net_serialization_filename(net_1_conf)
        self.net_2_ser = util.build_net_serialization_filename(net_2_conf)
        self.net_3_ser = util.build_net_serialization_filename(net_3_conf)
        self.net_4_ser = util.build_net_serialization_filename(net_4_conf)
        self.net_5_ser = util.build_net_serialization_filename(net_5_conf)
        
        self.assertTrue(util.is_net_serialization_filename(self.net_1_ser))
        self.assertTrue(util.is_net_serialization_filename(self.net_2_ser))
        self.assertTrue(util.is_net_serialization_filename(self.net_3_ser))
        self.assertTrue(util.is_net_serialization_filename(self.net_4_ser))
        self.assertTrue(util.is_net_serialization_filename(self.net_5_ser))

        #calls to serialize_net_to_file have the side effect of creating a pickle file
        self.assertEqual(
            util.serialize_net_to_file(net_1_conf, self.net_1),
            self.net_1_ser
        )
        self.assertEqual(
            util.serialize_net_to_file(net_2_conf, self.net_2),
            self.net_2_ser
        )
        self.assertEqual(
            util.serialize_net_to_file(net_3_conf, self.net_3),
            self.net_3_ser
        )
        self.assertEqual(
            util.serialize_net_to_file(net_4_conf, self.net_4),
            self.net_4_ser
        )
        self.assertEqual(
            util.serialize_net_to_file(net_5_conf, self.net_5),
            self.net_5_ser
        )
        
        #test that we have the same networks after deserialization
        self.assertEqual(
            util.deserialize_net_from_file(net_1_conf),
            self.net_1
        )
        self.assertEqual(
            util.deserialize_net_from_file(net_2_conf),
            self.net_2
        )
        self.assertEqual(
            util.deserialize_net_from_file(net_3_conf),
            self.net_3
        )
        self.assertEqual(
            util.deserialize_net_from_file(net_4_conf),
            self.net_4
        )
        self.assertEqual(
            util.deserialize_net_from_file(net_5_conf),
            self.net_5
        )
        
        for n in [self.net_1_ser, self.net_2_ser, self.net_3_ser, self.net_4_ser,
                  self.net_5_ser]:
            try:
                remove(n)
            except OSError:
                pass