Commit ca36c09b authored by Manuel Christlieb's avatar Manuel Christlieb

refactor(3): Move the functionality of the __init__ to a get_url() method...

refactor(3): Move the functionality of the __init__ to a get_url() method which saves the result to a class property on the first call
parent 0252d30f
......@@ -50,7 +50,7 @@ def main():
if os.listdir(full_path):
raise B5ExecutionError('Cannot init an existing directory if not empty')
_run_cmd(['git', 'clone', skeleton.url, full_path], 'Could not clone skeleton repository, see above')
_run_cmd(['git', 'clone', skeleton.get_url(), full_path], 'Could not clone skeleton repository, see above')
os.chdir(full_path)
if not branch is None:
_run_cmd(['git', 'checkout', branch], 'Could not checkout required branch, see above')
......@@ -69,7 +69,7 @@ def main():
shutil.rmtree(init_path)
# _run_cmd(['git', 'add', '-A'])
termcolor.cprint('Successful initialized {path}'.format(path=path), 'green')
termcolor.cprint(' skeleton used: {skeleton_url}'.format(skeleton_url=skeleton.url), 'green')
termcolor.cprint(' skeleton used: {skeleton_url}'.format(skeleton_url=skeleton.get_url()), 'green')
termcolor.cprint(' project path: {full_path}'.format(full_path=full_path), 'green')
except B5ExecutionError as error:
termcolor.cprint(str(error), 'red')
......
import os
import re
import urllib.request
from urllib.error import URLError
NON_URL_SKELETON = re.compile('^[A-Za-z0-9_-]+$')
class Skeleton:
def __init__(self, skeleton):
self._skeleton = skeleton
if NON_URL_SKELETON.match(skeleton):
self._url = 'https://git.team23.de/build/b5-skel-{skeleton}.git'.format(skeleton=skeleton)
'''if it's not a public repository, clone using ssh in order to allow ssh key file auth'''
if not self.__is_public_repository():
self._url = 'git@git.team23.de:build/b5-skel-{skeleton}.git'.format(skeleton=skeleton)
else:
self._url = skeleton
def get_url(self):
if not self._url:
non_url_skeleton = re.compile('^[A-Za-z0-9_-]+$')
if non_url_skeleton.match(self._skeleton):
self._url = 'https://git.team23.de/build/b5-skel-{skeleton}.git'.format(skeleton=self._skeleton)
'''if it's not a public repository, clone using ssh in order to allow ssh key file auth'''
if not self.__is_public_repository(self._url):
self._url = 'git@git.team23.de:build/b5-skel-{skeleton}.git'.format(skeleton=self._skeleton)
else:
self._url = self._skeleton
return self._url
def __is_public_repository(self):
req = urllib.request.urlopen(self._url)
req_url = req.geturl()
def __is_public_repository(self, url):
request = urllib.request.urlopen(self._url)
request_url = request.geturl()
if self._url == req_url or os.path.splitext(self._url)[0] == req_url :
if self._url == request_url or os.path.splitext(self._url)[0] == request_url:
try:
if req.getcode() == 200 :
if request.getcode() == 200:
return True
except Exception:
return False
except URLError:
pass
return False
@property
def url(self):
return self._url
@property
def skeleton(self):
return self._skeleton
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment