travis_run.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. # https://github.com/ahelal/travis-in-box
  3. import yaml
  4. import subprocess
  5. import sys
  6. import os.path
  7. class TravisExec(object):
  8. def __init__(self, filename="travis.yml"):
  9. self.fail = False
  10. stream = open(filename, 'r')
  11. yaml_file = yaml.load(stream)
  12. # language
  13. self.language = yaml_file.get("language", None)
  14. # Section
  15. self.section_before_install = yaml_file.get("before_install", None)
  16. self.section_install = yaml_file.get("install", None)
  17. self.section_before_script = yaml_file.get("before_script", None)
  18. self.section_script = yaml_file.get("script", None)
  19. #self.section_after_script = yaml_file.get("after_script", None)
  20. self.section_after_failure = yaml_file.get("after_failure", None)
  21. self.section_after_success = yaml_file.get("after_success", None)
  22. def _setup(self):
  23. if self.language == "python":
  24. print "********** Setup Python **********"
  25. print ""
  26. # Since we are not using container we have to install various lang our self
  27. # So this is probably not the best way to do it
  28. self._execute_command(["sudo apt-get install python-setuptools python-pip -y"])
  29. else:
  30. print "Errors unsupported language {}".format(self.language)
  31. exit(1)
  32. def life_cycle(self):
  33. # See http://docs.travis-ci.com/user/build-configuration/
  34. # 1. setup language
  35. self._setup()
  36. # 4. Run before_install commands
  37. self.run_command("before_install", self.section_before_install, self.section_after_failure)
  38. # 5. Run install commands
  39. self.run_command("install", self.section_install, self.section_after_failure)
  40. # 6. Run before_script commands
  41. self.run_command("before_script", self.section_before_script, self.section_after_failure)
  42. # 7. Run test script commands
  43. self.run_command("script", self.section_script, self.section_after_failure)
  44. # 8 . if we reach this point we made it run after_success
  45. self.run_command("after_success", self.section_after_success, None)
  46. @staticmethod
  47. def _execute_command(command):
  48. new_command = ["echo '> " + item.rstrip('\n') + "' && { " + item.rstrip('\n') + " ; }" for item in command]
  49. new_command = " && ".join(new_command)
  50. p = subprocess.Popen(new_command, shell=True, stderr=subprocess.PIPE)
  51. while True:
  52. out = p.stderr.read(1)
  53. if out == '' and p.poll() is not None:
  54. break
  55. if out != '':
  56. sys.stdout.write(out)
  57. sys.stdout.flush()
  58. print ""
  59. return p.returncode
  60. def run_command(self, section_name=None, command=None, execute_on_failure=None):
  61. if command:
  62. print ""
  63. print "********** Running '{}' **********".format(section_name)
  64. return_code = self._execute_command(command)
  65. if return_code != 0:
  66. print ""
  67. print "********** Failed in '{}' **********".format(section_name)
  68. if execute_on_failure:
  69. print ""
  70. print "********** Running after_failure **********".format(section_name)
  71. self._execute_command(execute_on_failure)
  72. exit(1)
  73. filename = None
  74. if len(sys.argv) == 1:
  75. filename = ".travis.yml"
  76. elif len(sys.argv) == 2:
  77. filename = sys.argv[1]
  78. else:
  79. print "Invalid number of arguments"
  80. exit(1)
  81. if os.path.exists(filename):
  82. TravisExec(filename).life_cycle()
  83. else:
  84. print "Could not file travis file '{}'".format(filename)
  85. exit(1)