Commit d005d633 authored by David Danier's avatar David Danier

Merge branch 'issue/3' into 'master'

[3] Allow b5-init of a private repositories skeleton using ssh instead of https

Closes #3

See merge request build/b5!8
parents 3889190c 13a7ba5e
import os
import re
import shutil
import subprocess
import sys
import termcolor
from .lib.skeleton import Skeleton
from .lib.argumentparser import InitArgumentParser
from .exceptions import B5ExecutionError
NON_URL_SKELETON = re.compile('^[A-Za-z0-9_-]+$')
def _run_cmd(cmd, error='Command execution failed, see above'):
......@@ -30,22 +27,17 @@ def main():
args = parser.parse()
skeleton = args.skeleton
skeleton = Skeleton(args.skeleton)
branch = args.branch
path = args.path
if NON_URL_SKELETON.match(skeleton):
skeleton_url = '{skeleton}.git'.format(skeleton=skeleton)
skeleton_url = skeleton
full_path = os.path.realpath(os.path.join(os.getcwd(), path))
os.makedirs(full_path, exist_ok=True)
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')
if not branch is None:
_run_cmd(['git', 'checkout', branch], 'Could not checkout required branch, see above')
......@@ -64,7 +56,7 @@ def main():
# _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
class Skeleton:
NON_URL_SKELETON = re.compile('^[A-Za-z0-9_-]+$')
def __init__(self, skeleton_identified):
self._skeleton_identified = skeleton_identified
def get_url(self):
return self._url
except AttributeError:
skeleton_url = self._skeleton_identified
if self.NON_URL_SKELETON.match(skeleton_url):
skeleton_url = '{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 = '{skeleton}.git'.format(skeleton=self._skeleton_identified)
self._url = skeleton_url
return self._url
def __is_public_repository(self, url):
request = urllib.request.urlopen(url)
request_url = request.geturl()
if url == request_url or url.rsplit('.', 1)[0] == request_url:
if request.getcode() == 200:
return True
except URLError:
return False
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment