Exemplo n.º 1
0
 def upgrade_environment(self, db):
     # borrowed from trac.env
     dao = WikiSyncDao(self.env)
     v = self._get_db_version(db) or 0
     cursor = db.cursor()
     for i in range(v + 1, DB_VERSION + 1):
         name = "version%i" % i
         try:
             schema = __import__("schema", globals(), locals(), [name])
             script = getattr(schema, name)
         except AttributeError:
             raise TracError("Invalid schema version %s" % i)
         script.do_upgrade(self.env, i, cursor)
         cursor.execute("""
             UPDATE system SET value=%s
             WHERE name='wikisync.schema.version'
         """, (i,))
         if cursor.rowcount < 1:
             cursor.execute("""
                 INSERT INTO system(name, value)
                 VALUES('wikisync.schema.version', %s)
             """, (i,))
         db.commit()
         self.log.info("Upgraded wikisync schema to version %s" % i)
     dao.sync_wiki_data()
     if not self._get_config("ignorelist"):
         self._set_config("ignorelist", DEFAULT_IGNORELIST)
         self._save_config()
Exemplo n.º 2
0
 def _process_action(self, req):
     dao = WikiSyncDao(self.env)
     action = req.args.get("action")
     assert len(action), "'action' required"
     names = req.args.get("name", [])
     if isinstance(names, basestring):
         if names:
             names = [names]
     else:
         names = [name for name in names if name]
     error = None
     wc = None
     try:
         wc = self._get_web_client()
         if action == "refresh":
             if names:
                 for name in names:
                     info = wc.get_remote_version(name)
                     item = dao.find(name)
                     if not item:
                         wiki = WikiPage(self.env, name)
                         if wiki.exists:
                             item = dao.factory(
                                 name=name,
                                 local_version=wiki.version,
                                 sync_time=time.time()
                             )
                             item = dao.create(item)
                             self.log.debug("Created '%s' wikisync" % name)
                     if info:
                         info = info[0]
                         info["sync_time"] = time.time()
                         if item:
                             item = dao.update(item.replace(**info))
                         else:
                             item = dao.create(dao.factory(**info))
                         self.log.debug("Updated '%s' wikisync info %s" % \
                             (name, info))
                     elif item:
                         item = dao.update(
                             item.replace(sync_time=time.time())
                         )
             else:
                 # update local and remote data
                 dao.sync_wiki_data()
                 ignore_filter = RegExpFilter(
                     self._get_config("ignorelist")
                 )
                 results = wc.get_remote_list()
                 dao.sync_remote_data(results, ignore_filter)
         elif action in ("pull", "push", "resolve"):
             items = []
             for name in names:
                 item = dao.find(name)
                 if not item:
                     raise ValueError("Missing wiki '%s'" % name)
                 if action == "pull":
                     author = get_reporter_id(req)
                     addr = req.remote_addr
                     wiki = WikiPage(self.env, item.name)
                     wiki.text = wc.pull(item.name,
                         item.remote_version)
                     if not len(wiki.text) and not wiki.version:
                         # BUGFIX: account for empy remote wiki Page,
                         # which throws a Page not modified exception when
                         # saving for the first time (default wiki.text = '')
                         wiki.text = " "
                     try:
                         wiki.save(author, DEFAULT_SIGNATURE, addr)
                     except TracError, e:
                         if wiki.text != wiki.old_text:
                             raise e
                         else:
                             self.log.debug("Content has not changed, "
                                            "skipping '%s'" % item.name)
                     item = item.replace(
                         local_version=wiki.version
                     ).synchronized()
                     dao.update(item)
                     self.log.debug("Pulled wiki '%s'" % item.name)
                 elif action == "push":
                     wiki = WikiPage(self.env, item.name)
                     author = get_reporter_id(req) or "wikisync"
                     assert wiki.version > 0, "Cannot find wiki '%s'" % item.name
                     item = item.replace(
                         **wc.push(
                             item.name,
                             wiki.text,
                             wiki.comment,
                             author
                         )
                     ).synchronized()
                     dao.update(item)
                     self.log.debug("Pushed wiki '%s'" % item.name)
                 elif action == "resolve":
                     status = req.args.get("status")
                     if status == "ignore":
                         item = item.replace(ignore=1)
                     elif status == "unignore":
                         item = item.replace(ignore=None)
                     elif status == "modified":
                         item = item.replace(
                             sync_remote_version=item.remote_version
                         )
                     elif status == "outdated":
                         item = item.replace(
                             sync_local_version=item.local_version
                         )
                     else:
                         raise RuntimeError(
                             "Unsupported resolution: '%s'" % status
                         )
                     dao.update(item)
                     self.log.debug("Resolved wiki '%s' as %s" % \
                         (item.name, status))
Exemplo n.º 3
0
class WikiSyncModelTestCase(unittest.TestCase):

    def setUp(self):
        self.env = EnvironmentStub()
        self.plugin = WikiSyncEnvironment(self.env)
        self.plugin.upgrade_environment(self.env.get_db_cnx())
        self.dao = WikiSyncDao(self.env)
        file = resource_filename(__name__, "data.sql")
        @self.env.with_transaction()
        def do_save(db):
            cursor = db.cursor()
            with open(file, "rb") as f:
                for sql in f:
                    if sql:
                        cursor.execute(sql)
    
    def test_sync_wiki_data(self):
        self.dao.sync_wiki_data()
        for name in ["CamelCase", "InterMapTxt", "NewPage", "WikiRestructuredTextLinks"]:
            self.assertTrue(self.dao.find(name) is not None)
            
    def test_all(self):
        results = [item for item in self.dao.all()]
        self.assertEqual(len(results), 3)

    def test_validate(self):
        item = self.dao.factory()
        self.assertRaises(AssertionError, item.validate)
        item.replace(name="Test").validate()

    def test_read(self):
        item = self.dao.find("Test1")
        self.assertEqual(self.dao.find("Unknown"), None)
        
    def test_create(self):
        self.dao.create(
            self.dao.factory(
                name="Test2", 
                remote_version=2
            )
        )
        db = self.env.get_read_db()
        cursor = db.cursor()
        cursor.execute("""
            SELECT name, remote_version FROM wikisync
            WHERE name='Test2'
        """)
        results = cursor.fetchall()
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0], (u'Test2', 2))

    def test_delete(self):
        item = self.dao.find("Test1")
        self.assertTrue(item is not None)
        self.dao.delete(item)
        self.assertTrue(self.dao.find("Test1") is None)
        
    def test_update(self):
        item = self.dao.find("Test1")
        item = item.replace(sync_remote_version=1, remote_version=11)
        self.dao.update(item)
        db = self.env.get_read_db()
        cursor = db.cursor()
        cursor.execute("""
            SELECT sync_remote_version, remote_version FROM wikisync
            WHERE name='Test1'
        """)
        results = cursor.fetchall()
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0], (1, 11))