Example #1
0
    def do_pull(self):
        logging.info('Starting pull process')
        since = (utils.read_settings(self._conn, 'last_update')
                      .get('last_update', 0))
        cursor = self._conn.cursor()
        response = self.communicate({'ACTION': 'PULL',
                                     'SINCE': since})
        to_recv = parse.listify(parse.loads(response['CHANGES']))

        logging.info('Adding %d new files from server.' % len(to_recv))

        for x in to_recv:
            from_serv = parse.loads(x)
            sid = int(from_serv['ID'])
            logging.debug('Proccessing file update. SID: %d, type: %s'
                          % (sid, from_serv['type']))
            if from_serv['type'] == 'NEW':
                cursor.execute('SELECT 1 FROM files WHERE server_id=?', [sid])
                if cursor.fetchone():
                    logging.warning('Server returned a file I already have, '
                                    'ignoring and continuing pull process.')
                    continue

                file_path, file_hash = self.pull_remote(sid)

                fd = open(file_path, 'rb')
                our_hash = utils.hash_file(fd)
                if our_hash.digest() != file_hash:
                    raise Exception('MD5 digests did not match! Transmission '
                                    'error suspected.')

                it_path = self.add_to_itunes(file_path)
                os.remove(file_path)

                record = utils.generate_file_info(it_path)
                record['server_id'] = sid
                utils.insert_file_record(record, self._conn)

                logging.debug('Successfuly added file: %s' 
                              % (os.path.split(it_path)[-1],))

            elif from_serv['type'] == 'DELETE':
                cursor.execute('SELECT * FROM files WHERE server_id=?', [sid])
                record = cursor.fetchone()

                if not record:
                    logging.warning('Server sent delete directive on file I '
                                    'don\'t have. Ignoring.')
                    continue

                self.remove_from_itunes(sid)
                cursor.execute('DELETE FROM files WHERE server_id=?', [sid])

            self._conn.commit()

        logging.info('...finished pull process')
Example #2
0
    def test_loads(self):
        d = parse.loads('(ACTION LIST-HASHES)')
        assert 'ACTION' in d
        assert d['ACTION'] == 'LIST-HASHES'

        d = parse.loads('(ACTION SEHSAH-TSIL) (HASHES (1 2 3 4\\))')
        assert 'HASHES' in d
        assert d['HASHES'] == '(1 2 3 4)'
        
        d = parse.loads('(ACTION SEHSAH-TSIL) SHALOM (HASHES (1 2 3 4\\))')
        assert len(d.keys()) == 2
        assert 'SHALOM' not in d
Example #3
0
 def test_array_ints(self):
     string = '[1,2,3]'
     expected = json.loads(string)
     actual = loads(string)
     self.assertListEqual(expected, actual)
Example #4
0
 def test_array_strings(self):
     string = '["one","two","three"]'
     expected = json.loads(string)
     actual = loads(string)
     self.assertListEqual(expected, actual)
Example #5
0
 def test_array_empty(self):
     string = '[]'
     expected = json.loads(string)
     actual = loads(string)
     self.assertListEqual(expected, actual)
Example #6
0
 def test_hash_nested(self):
     string = '{"hash":{"inception":"win"}}'
     expected = json.loads(string)
     actual = loads(string)
     self.assertDictEqual(expected, actual)
Example #7
0
 def test_hash_with_int(self):
     string = '{"int":101}'
     expected = json.loads(string)
     actual = loads(string)
     self.assertDictEqual(expected, actual)
Example #8
0
 def test_simple_hash(self):
     string = '{"key":"value"}'
     expected = json.loads(string)
     actual = loads(string)
     self.assertDictEqual(expected, actual)
Example #9
0
 def test_empty_hash(self):
     expected = {}
     string = json.dumps(expected)
     actual = loads(string)
     self.assertDictEqual(expected, actual)
Example #10
0
 def test_array_strings(self):
     string = '["one","two","three"]'
     expected = json.loads(string)
     actual = loads(string)
     self.assertListEqual(expected, actual)
Example #11
0
 def test_array_ints(self):
     string = '[1,2,3]'
     expected = json.loads(string)
     actual = loads(string)
     self.assertListEqual(expected, actual)
Example #12
0
 def test_array_empty(self):
     string = '[]'
     expected = json.loads(string)
     actual = loads(string)
     self.assertListEqual(expected, actual)
Example #13
0
 def test_hash_nested(self):
     string = '{"hash":{"inception":"win"}}'
     expected = json.loads(string)
     actual = loads(string)
     self.assertDictEqual(expected, actual)
Example #14
0
 def test_hash_with_int(self):
     string = '{"int":101}'
     expected = json.loads(string)
     actual = loads(string)
     self.assertDictEqual(expected, actual)
Example #15
0
 def test_simple_hash(self):
     string = '{"key":"value"}'
     expected = json.loads(string)
     actual = loads(string)
     self.assertDictEqual(expected, actual)
Example #16
0
 def test_empty_hash(self):
     expected = {}
     string = json.dumps(expected)
     actual = loads(string)
     self.assertDictEqual(expected, actual)