def write_new_spammers(): """ Synchronise all changes between GitHub and webapp Because we may have multiple pending pull requests, each changeset must be added to a new integration branch, which issues the pull request to origin/master TODO: tidy up remote integration branches """ errs = False # pull all branches from origin, and force-checkout master repo_checkout() # switch to a new integration branch newbranch = "integration_%s" % datetime.utcnow().strftime( "%Y_%b_%d_%H_%M_%S") git.checkout(b=newbranch) index = repo.index # re-generate spammers.txt try: output(filename="spammers.txt") except IOError as e: app.logger.error("Couldn't write spammers.txt. Err: %s" % e) repo.heads.master.checkout(f=True) git.branch(newbranch, D=True) errs = True # add spammers.txt to local integration branch try: index.add(['spammers.txt']) index.commit("Updating Spammers on %s" % now) # push local repo to webapp's remote our_remote.push(newbranch) except GitCommandError as e: errs = True app.logger.error("Couldn't push to staging remote. Err: %s" % e) # send pull request to main remote our_sha = "urschrei:%s" % newbranch their_sha = 'master' if not errs and pull_request(our_sha, their_sha): # delete our local integration branch, and reset counter counter = Counter.query.first() counter.count = 0 counter.timestamp = utcnow_() db.session.add(counter) db.session.commit() git.checkout("master") git.branch(newbranch, D=True) else: # register an error errs = True if errs: flash( "There was an error sending your updates to GitHub. We'll \ try again later, though, and they <em>have</em> been saved.", "text-error" )
def update_db(): """ Add any missing spammers to our app DB """ # pull all branches from origin, and force-checkout master repo_checkout() their_spammers = set(get_spammers()) our_spammers = set(addr.address.strip() for addr in Address.query.order_by('address').all()) to_update = list(their_spammers - our_spammers) if to_update: db.session.add_all([Address(address=new_addr) for new_addr in to_update]) # reset sync timestamp latest = UpdateCheck.query.first() or UpdateCheck() latest.timestamp = utcnow_() db.session.add(latest) db.session.commit()