From 37f7035ab749d885efc6f2a0ec883697a6d6605f Mon Sep 17 00:00:00 2001 From: Justus Adam Date: Wed, 18 May 2016 23:42:21 +0200 Subject: [PATCH] Cleaned some code. Refactored all intermediate messages to go through a single method Changed git command to use -C rather than using shell=True and && --- GitAutoDeploy.py | 52 ++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/GitAutoDeploy.py b/GitAutoDeploy.py index 1e61cc6..cd28504 100755 --- a/GitAutoDeploy.py +++ b/GitAutoDeploy.py @@ -37,13 +37,11 @@ def getConfig(myClass): def do_POST(self): event = self.headers.getheader('X-Github-Event') if event == 'ping': - if not self.quiet: - print 'Ping event received' + self.report('Ping event received') self.respond(204) return if event != 'push': - if not self.quiet: - print 'We only handle ping and push events' + self.report('We only handle ping and push events') self.respond(304) return @@ -64,12 +62,12 @@ def parseRequest(self): return [payload['repository']['url']] def getMatchingPaths(self, repoUrl): - res = [] config = self.getConfig() - for repository in config['repositories']: - if(repository['url'] == repoUrl): - res.append(repository['path']) - return res + return [ + repository + for repository in config['repositories'] + if repository['url'] == repoUrl + ] def respond(self, code): self.send_response(code) @@ -77,39 +75,42 @@ def respond(self, code): self.end_headers() def fetch(self, path): - if(not self.quiet): - print "\nPost push request received" - print 'Updating ' + path - call(['cd "' + path + '" && git fetch'], shell=True) + self.report( + '\nPost push request received' + '\nUpdating ' + path + ) + call(['git', '-C', path, 'fetch']) def deploy(self, path): config = self.getConfig() for repository in config['repositories']: if(repository['path'] == path): if 'deploy' in repository: - branch = None - if 'branch' in repository: - branch = repository['branch'] + branch = repository.get('branch', None) if branch is None or branch == self.branch: - if(not self.quiet): - print 'Executing deploy command' + self.report('Executing deploy command') call(['cd "' + path + '" && ' + repository['deploy']], shell=True) - - elif not self.quiet: - print 'Push to different branch (%s != %s), not deploying' % (branch, self.branch) + + else: + self.report('Push to different branch (%s != %s), not deploying' % (branch, self.branch)) break + @classmethod + def report(self, message): + if not self.quiet: + print message + def main(): try: server = None - for arg in sys.argv: + for arg in sys.argv: if(arg == '-d' or arg == '--daemon-mode'): GitAutoDeploy.daemon = True GitAutoDeploy.quiet = True if(arg == '-q' or arg == '--quiet'): GitAutoDeploy.quiet = True - + if(GitAutoDeploy.daemon): pid = os.fork() if(pid != 0): @@ -120,7 +121,7 @@ def main(): print 'Github Autodeploy Service v0.2 started' else: print 'Github Autodeploy Service v 0.2 started in daemon mode' - + server = HTTPServer(('', GitAutoDeploy.getConfig()['port']), GitAutoDeploy) server.serve_forever() except (KeyboardInterrupt, SystemExit) as e: @@ -130,8 +131,7 @@ def main(): if(not server is None): server.socket.close() - if(not GitAutoDeploy.quiet): - print 'Goodbye' + GitAutoDeploy.report('Goodbye') if __name__ == '__main__': main()