Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. import subprocess
  2. import gitlab
  3.  
  4.  
  5. REPOS = [
  6.     {'url': 'git@salsa.debian.org:ina-guest/logbook', 'branch': 'master', 'id': 26753},
  7.     #{'url': 'https://salsa.debian.org/debian/python-icecream', 'branch': 'master'},
  8. ]
  9. #DOCKER_IMAGE = 'debian:unstable'
  10. DOCKER_IMAGE = 'upstramer'
  11. UPSTREAM_BRANCH = 'upstream-update'
  12.  
  13. GITLAB_URL = 'https://salsa.debian.org'
  14. GITLAB_PRIVATE_TOKEN = ''
  15. BOT_REPO_ID = 26754
  16. SALSA = gitlab.Gitlab(GITLAB_URL, private_token=GITLAB_PRIVATE_TOKEN)
  17.  
  18.  
  19. class Container():
  20.     def __init__(self):
  21.         pass
  22.  
  23.     def start(self):
  24.         self.container_id = subprocess.check_output([
  25.             'docker', 'run', '--detach=true', '-w=/root/workspace',
  26.             DOCKER_IMAGE, 'sleep', 'infinity'
  27.         ]).decode('UTF-8').strip()
  28.  
  29.     def run(self, command, path='/root/workspace/repo'):
  30.         proc = subprocess.Popen(
  31.             self._(command, path),
  32.             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  33.         stdout, stderr = proc.communicate()
  34.         exit_code = proc.poll()
  35.         return exit_code, stdout, stderr
  36.  
  37.     def keys(self):
  38.         # Polemic.
  39.         # Como pushea sino?
  40.         subprocess.check_call(['docker', 'cp', '/home/ina/.ssh', '{}:/root/.ssh'.format(self.container_id)])
  41.         subprocess.check_call(['docker', 'exec', self.container_id, 'sh', '-c', 'chown -R root. /root/.ssh'])
  42.  
  43.     def stop(self):
  44.         subprocess.check_call(['docker', 'stop', self.container_id])
  45.         subprocess.check_call(['docker', 'rm', '-f', self.container_id])
  46.  
  47.     def _(self, command, path):
  48.         docker_exec = ['docker', 'exec', '-w', path, self.container_id, 'sh', '-c']
  49.         return docker_exec + [command]
  50.  
  51.  
  52. def create_mr(repo):
  53.     project = SALSA.projects.get(repo['id'])
  54.     project.mergerequests.create(
  55.         source_branch=UPSTREAM_BRANCH,
  56.         target_branch='master',
  57.         remove_source_branch=True,
  58.         title='New upstream release',
  59.         description='Please find tests output here',
  60.     )
  61.  
  62.  
  63. def notify_downstream(virt, repo):
  64.     virt.run('gbp dch'.format(**globals()))
  65.     _, stdout, stderr = virt.run('dpkg-parsechangelog -S Version')
  66.     branch_name = '{}_{}'.format(UPSTREAM_BRANCH, stdout.strip().decode('utf-8'))
  67.  
  68.     virt.run('git checkout -b {branch_name}'.format(**locals()))
  69.     virt.run('git commit -m "New upstream release" debian/changelog')
  70.     virt.run('gbp push --debian-branch={branch_name}'.format(**locals()))
  71.  
  72.     create_mr(repo)
  73.  
  74.  
  75. def check_repo(repo):
  76.     try:
  77.         virt = Container()
  78.         virt.start()
  79.         virt.keys()
  80.  
  81.         virt.run('gbp clone {} repo'.format(repo['url']), path='/root/workspace')
  82.         exit_code, _, __ = virt.run('gbp import-orig --uscan --no-interactive')
  83.  
  84.         if exit_code == 0:
  85.             notify_downstream(virt, repo)
  86.  
  87.     except Exception as e:
  88.         raise e
  89.  
  90.     finally:
  91.         virt.stop()
  92.  
  93.  
  94. def main():
  95.     for repo in REPOS:
  96.         check_repo(repo)
  97.  
  98. if __name__ == '__main__':
  99.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement