Example #1
0
    def test_load_index(self):
        directory = syncall.Directory('uuid', self.TEST_DIR)
        directory.update_index(save_index=True)

        del directory

        directory = syncall.Directory('uuid', self.TEST_DIR)

        for path in self.TEST_FILES:
            self.assertIn(path, directory._index)

            file_data = directory._index[path]
            self.assertIn('last_update', file_data)
            self.assertIn('last_update_location', file_data)
            self.assertEqual(file_data['last_update_location'], 'uuid')
            self.assertIn('hash', file_data)
            self.assertEqual(
                file_data['hash'],
                bintools.hash_file(self.TEST_DIR + '/' + path)
            )
            self.assertIn('sync_log', file_data)
            self.assertIn('uuid', file_data['sync_log'])
            self.assertEqual(
                file_data['sync_log']['uuid'],
                file_data['last_update']
            )
Example #2
0
    def _update_file_index(self, file_path, changes):
        relative_path = pathext.normalize(
            os.path.relpath(file_path, self.dir_path)
        )
        file_data = self._index.setdefault(relative_path, dict())

        if not file_data:
            # New file
            file_hash = bintools.hash_file(file_path)
            file_data['last_update'] = int(os.path.getmtime(file_path))
            file_data['hash'] = file_hash
            file_data['last_update_location'] = self.uuid

            sync_log = file_data.setdefault('sync_log', dict())
            sync_log[self.uuid] = file_data['last_update']

            changes.add(relative_path)

        elif int(os.path.getmtime(file_path)) > file_data['last_update']:
            # Check if file is actually changed or the system time is off
            file_hash = bintools.hash_file(file_path)

            if file_data['hash'] != file_hash:
                # File modified locally (since last sync)
                file_data['last_update'] = int(os.path.getmtime(file_path))
                file_data['hash'] = file_hash
                file_data['last_update_location'] = self.uuid

                sync_log = file_data.setdefault('sync_log', dict())
                sync_log[self.uuid] = file_data['last_update']

                changes.add(relative_path)

        if 'deleted' in file_data:
            file_data['last_update'] = datetime.now().timestamp()
            file_data['hash'] = bintools.hash_file(file_path)
            file_data['last_update_location'] = self.uuid

            sync_log = file_data.setdefault('sync_log', dict())
            sync_log[self.uuid] = file_data['last_update']

            changes.add(relative_path)

            del file_data['deleted']

        if 'not_found' in file_data:
            del file_data['not_found']
Example #3
0
    def _update_file_index(self, file_path, changes):
        relative_path = pathext.normalize(
            os.path.relpath(file_path, self.dir_path))
        file_data = self._index.setdefault(relative_path, dict())

        if not file_data:
            # New file
            file_hash = bintools.hash_file(file_path)
            file_data['last_update'] = int(os.path.getmtime(file_path))
            file_data['hash'] = file_hash
            file_data['last_update_location'] = self.uuid

            sync_log = file_data.setdefault('sync_log', dict())
            sync_log[self.uuid] = file_data['last_update']

            changes.add(relative_path)

        elif int(os.path.getmtime(file_path)) > file_data['last_update']:
            # Check if file is actually changed or the system time is off
            file_hash = bintools.hash_file(file_path)

            if file_data['hash'] != file_hash:
                # File modified locally (since last sync)
                file_data['last_update'] = int(os.path.getmtime(file_path))
                file_data['hash'] = file_hash
                file_data['last_update_location'] = self.uuid

                sync_log = file_data.setdefault('sync_log', dict())
                sync_log[self.uuid] = file_data['last_update']

                changes.add(relative_path)

        if 'deleted' in file_data:
            file_data['last_update'] = datetime.now().timestamp()
            file_data['hash'] = bintools.hash_file(file_path)
            file_data['last_update_location'] = self.uuid

            sync_log = file_data.setdefault('sync_log', dict())
            sync_log[self.uuid] = file_data['last_update']

            changes.add(relative_path)

            del file_data['deleted']

        if 'not_found' in file_data:
            del file_data['not_found']
Example #4
0
    def test_load_index(self):
        directory = syncall.Directory('uuid', self.TEST_DIR)
        directory.update_index(save_index=True)

        del directory

        directory = syncall.Directory('uuid', self.TEST_DIR)

        for path in self.TEST_FILES:
            self.assertIn(path, directory._index)

            file_data = directory._index[path]
            self.assertIn('last_update', file_data)
            self.assertIn('last_update_location', file_data)
            self.assertEqual(file_data['last_update_location'], 'uuid')
            self.assertIn('hash', file_data)
            self.assertEqual(file_data['hash'],
                             bintools.hash_file(self.TEST_DIR + '/' + path))
            self.assertIn('sync_log', file_data)
            self.assertIn('uuid', file_data['sync_log'])
            self.assertEqual(file_data['sync_log']['uuid'],
                             file_data['last_update'])
Example #5
0
    def test_hash_file(self):
        file_hash = bintools.hash_file(self.TEST_DIR + '/to_be_hashed')
        file_hash_known = bytes.fromhex('d2ff3650d8809f4622ef8eb1e920710b')

        self.assertEqual(file_hash, file_hash_known)
Example #6
0
    def test_hash_file(self):
        file_hash = bintools.hash_file(self.TEST_DIR + '/to_be_hashed')
        file_hash_known = bytes.fromhex('d2ff3650d8809f4622ef8eb1e920710b')

        self.assertEqual(file_hash, file_hash_known)