Skip to content

Commit

Permalink
fix: fixed org, branch for apps mounted on disk (frappe#1293)
Browse files Browse the repository at this point in the history
* fix: fixed org, branch for apps mounted on disk

* fix: DRY-er code

* fix: fixed loading apps when use_ssh is true

* fix: removed flaky tests to test dependencies

* fix: removed exception if app not found in frappe or erpnext org

* fix: remote server

* test: Updated tests

fix: tests

* fix: raise when org is not found

* fix: setup requirements

* fix: falling back to original method of mounted app initialization if git repo isn't found
  • Loading branch information
Aradhya-Tripathi authored Apr 19, 2022
1 parent eb7e006 commit c8ef822
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
42 changes: 20 additions & 22 deletions bench/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# imports - third party imports
import click
from git import Repo

# imports - module imports
import bench
Expand Down Expand Up @@ -75,10 +76,10 @@ def setup_details(self):
if (
not self.to_clone
and hasattr(self, "bench")
and os.path.exists(os.path.join(self.bench.name, "apps", self.name))
and os.path.exists(self.mount_path)
):
self.from_apps = True
self._setup_details_from_installed_apps()
self._setup_details_from_mounted_disk()

# fetch meta for repo on mounted disk
elif os.path.exists(self.mount_path):
Expand All @@ -88,48 +89,45 @@ def setup_details(self):
# fetch meta for repo from remote git server - traditional get-app url
elif is_git_url(self.name):
self.is_url = True
if self.name.startswith("git@") or self.name.startswith("ssh://"):
self.use_ssh = True
self._setup_details_from_git_url()

# fetch meta from new styled name tags & first party apps on github
else:
self._setup_details_from_name_tag()

def _setup_details_from_mounted_disk(self):
self.org, self.repo, self.tag = os.path.split(self.mount_path)[-2:] + (self.branch,)
# If app is a git repo
self.git_repo = Repo(self.mount_path)
try:
self._setup_details_from_git_url(self.git_repo.remotes[0].url)
if not (self.branch or self.tag):
self.tag = self.branch = self.git_repo.active_branch.name
except IndexError:
self.org, self.repo, self.tag = os.path.split(self.mount_path)[-2:] + (self.branch,)

def _setup_details_from_name_tag(self):
self.org, self.repo, self.tag = fetch_details_from_tag(self.name)
self.tag = self.tag or self.branch

def _setup_details_from_installed_apps(self):
self.org, self.repo, self.tag = os.path.split(
os.path.join(self.bench.name, "apps", self.name)
)[-2:] + (self.branch,)
def _setup_details_from_git_url(self, url=None):
return self.__setup_details_from_git(url)

def _setup_details_from_git_url(self):
return self.__setup_details_from_git()

def __setup_details_from_git(self):
if self.use_ssh:
_first_part, _second_part = self.name.split(":")
def __setup_details_from_git(self, url=None):
name = url if url else self.name
if name.startswith("git@") or name.startswith("ssh://"):
self.use_ssh = True
_first_part, _second_part = name.split(":")
self.remote_server = _first_part.split("@")[-1]
self.org, _repo = _second_part.rsplit("/", 1)
else:
self.remote_server, self.org, _repo = self.name.rsplit("/", 2)
protocal = "https://" if "https://" in name else "http://"
self.remote_server, self.org, _repo = name.replace(protocal, "").rsplit("/", 2)

self.tag = self.branch
self.repo = _repo.split(".")[0]

@property
def url(self):
if self.from_apps:
return os.path.abspath(os.path.join("apps", self.name))

if self.on_disk:
return self.mount_path

if self.is_url:
return self.name

Expand Down
3 changes: 2 additions & 1 deletion bench/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ def requirements(self, apps=None):
print(f"Installing {len(apps)} applications...")

for app in apps:
App(app, bench=self.bench, to_clone=False).install(
path_to_app = os.path.join(self.bench.name, "apps", app)
App(path_to_app, bench=self.bench, to_clone=False).install(
skip_assets=True, restart_bench=False, ignore_resolution=True
)

Expand Down
3 changes: 2 additions & 1 deletion bench/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def test_utils(self):
def test_init(self, bench_name="test-bench", **kwargs):
self.init_bench(bench_name, **kwargs)
app = App("file:///tmp/frappe")
self.assertEqual(app.url, "/tmp/frappe")
self.assertEqual(app.mount_path, "/tmp/frappe")
self.assertEqual(app.url, "https://github.com/frappe/frappe.git")
self.assert_folders(bench_name)
self.assert_virtual_env(bench_name)
self.assert_config(bench_name)
Expand Down
9 changes: 0 additions & 9 deletions bench/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,3 @@ def test_app_states(self):
self.assertEqual("11.0", fake_bench.apps.states["frappe"]["version"])

shutil.rmtree(bench_dir)

def test_get_dependencies(self):
git_url = "https://github.com/frappe/healthcare"
branch = "develop"
fake_app = App(git_url, branch=branch)
self.assertIn("erpnext", fake_app._get_dependencies())
git_url = git_url.replace("healthcare", "erpnext")
fake_app = App(git_url)
self.assertTrue(len(fake_app._get_dependencies()) == 0)
2 changes: 1 addition & 1 deletion bench/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def find_org(org_repo):
if res.ok:
return org, org_repo

raise InvalidRemoteException(f"{org_repo} Not foung in frappe or erpnext")
raise InvalidRemoteException(f"{org_repo} not found in frappe or erpnext")


def fetch_details_from_tag(_tag: str) -> Tuple[str, str, str]:
Expand Down

0 comments on commit c8ef822

Please sign in to comment.