|
|
@ -1,9 +1,10 @@ |
|
|
|
from pathlib import Path |
|
|
|
from pathlib import Path |
|
|
|
|
|
|
|
import re |
|
|
|
from shutil import rmtree |
|
|
|
from shutil import rmtree |
|
|
|
import string |
|
|
|
import string |
|
|
|
import random |
|
|
|
import random |
|
|
|
from werkzeug.utils import secure_filename |
|
|
|
from werkzeug.utils import secure_filename |
|
|
|
from git import Repo, InvalidGitRepositoryError, GitCommandError |
|
|
|
from git import Repo, InvalidGitRepositoryError, GitCommandError, cmd as git_cmd |
|
|
|
from flask import current_app as app |
|
|
|
from flask import current_app as app |
|
|
|
|
|
|
|
|
|
|
|
from biscd import config |
|
|
|
from biscd import config |
|
|
@ -56,36 +57,53 @@ class Project(YamlSerializable): |
|
|
|
repo = Repo(path) |
|
|
|
repo = Repo(path) |
|
|
|
app.logger.info('Already under git control') |
|
|
|
app.logger.info('Already under git control') |
|
|
|
except InvalidGitRepositoryError: |
|
|
|
except InvalidGitRepositoryError: |
|
|
|
app.logger.info(f"Cloning repo from {self.git_repo}") |
|
|
|
repo = self._clone_repo(path) |
|
|
|
repo = Repo.clone_from(self.git_repo, path) |
|
|
|
# If repo is not type Repo it must be an error and thus returned directly |
|
|
|
app.logger.info('Done!') |
|
|
|
if not isinstance(repo, Repo): |
|
|
|
|
|
|
|
return repo |
|
|
|
remotes = repo.remotes |
|
|
|
remotes = repo.remotes |
|
|
|
if not any(remotes): |
|
|
|
if not any(remotes): |
|
|
|
app.logger.error(f"Repo {path} doesn't have any remotes.") |
|
|
|
app.logger.error(f"Repo {path} doesn't have any remotes.") |
|
|
|
return ["Repo doesn't have any remotes. please fix manually", 'error'] |
|
|
|
return ["Repo doesn't have any remotes. please fix manually", 'error'] |
|
|
|
response = remotes[0].pull() |
|
|
|
response = remotes[0].pull() |
|
|
|
for item in response: |
|
|
|
for item in response: |
|
|
|
|
|
|
|
# TODO: is this nessecary? |
|
|
|
app.logger.debug(item) |
|
|
|
app.logger.debug(item) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response = self._checkout_branch(repo, self.branch) |
|
|
|
|
|
|
|
return response |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _clone_repo(self, path): |
|
|
|
|
|
|
|
app.logger.info("Cloning repo from %s", self.git_repo) |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
repo = Repo.clone_from(self.git_repo, path) |
|
|
|
|
|
|
|
except GitCommandError as gce: |
|
|
|
|
|
|
|
app.logger.error("Project %s's git repo: %s is invalid: %s", |
|
|
|
|
|
|
|
self.name, self.git_repo, gce.stderr.strip('\n ')) |
|
|
|
|
|
|
|
return ["Project does not have a valid git repository url", "error"] |
|
|
|
|
|
|
|
app.logger.info('Cloning repo (%s) succeeded!', self.git_repo) |
|
|
|
|
|
|
|
return repo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _checkout_branch(self, repo, branch): |
|
|
|
try: |
|
|
|
try: |
|
|
|
response = repo.git.checkout(self.branch) |
|
|
|
repo.git.checkout(branch) |
|
|
|
except AttributeError: |
|
|
|
except AttributeError: |
|
|
|
return [f"Branch is not set for {self.name}", 'error'] |
|
|
|
return [f"Branch is not set for {self.name}", 'error'] |
|
|
|
except GitCommandError: |
|
|
|
except GitCommandError: |
|
|
|
return [ |
|
|
|
return [ |
|
|
|
f"Repo '{self.git_repo}' does not have an existing branch called {self.branch}", |
|
|
|
f"Repo '{self.git_repo}' does not have an existing branch called {branch}", |
|
|
|
'error'] |
|
|
|
'error'] |
|
|
|
return None |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
def delete_files(self): |
|
|
|
def delete_files(self): |
|
|
|
if self._production_path_exists(): |
|
|
|
if self.production_path_exists(): |
|
|
|
path = self.absulute_path |
|
|
|
path = self.absulute_path |
|
|
|
app.logger.info(f"Deleting {path}") |
|
|
|
app.logger.info(f"Deleting {path}") |
|
|
|
rmtree(path) |
|
|
|
rmtree(path) |
|
|
|
self.relative_production_path = None |
|
|
|
self.relative_production_path = None |
|
|
|
self.save() |
|
|
|
self.save() |
|
|
|
|
|
|
|
|
|
|
|
def _production_path_exists(self): |
|
|
|
def production_path_exists(self): |
|
|
|
"""Return True if project's the production directory path exists""" |
|
|
|
"""Return True if project's the production directory path exists""" |
|
|
|
prod_path_dir = Path(config['production_path'].get()) |
|
|
|
prod_path_dir = Path(config['production_path'].get()) |
|
|
|
if hasattr(self, 'relative_production_path') and self.relative_production_path: |
|
|
|
if hasattr(self, 'relative_production_path') and self.relative_production_path: |
|
|
|