skeleton.py 1.28 KB
Newer Older
1 2 3
import os
import re
import urllib.request
4
from urllib.error import URLError
5 6 7


class Skeleton:
8
    NON_URL_SKELETON = re.compile('^[A-Za-z0-9_-]+$')
9

10 11
    def __init__(self, skeleton_identified):
        self._skeleton_identified = skeleton_identified
12

13
    def get_url(self):
14 15 16 17 18 19 20 21 22 23 24
        try:
            return self._url
        except AttributeError:
            skeleton_url = self._skeleton_identified
            if self.NON_URL_SKELETON.match(skeleton_url):
                skeleton_url = 'https://git.team23.de/build/b5-skel-{skeleton}.git'.format(skeleton=self._skeleton_identified)
                # If it's not a public repository, clone using ssh in order to allow ssh key file auth
                if not self.__is_public_repository(skeleton_url):
                    skeleton_url = 'git@git.team23.de:build/b5-skel-{skeleton}.git'.format(skeleton=self._skeleton_identified)
            self._url = skeleton_url
            return self._url
25

26
    def __is_public_repository(self, url):
27
        request = urllib.request.urlopen(url)
28
        request_url = request.geturl()
29

30
        if url == request_url or url.rsplit('.', 1)[0] == request_url:
31
            try:
32
                if request.getcode() == 200:
33
                    return True
34 35
            except URLError:
                pass
36
        return False