Skip to content

Commit

Permalink
Merge pull request #257 from OpenPecha/fix-pass-branch-in-pecha-publish
Browse files Browse the repository at this point in the history
Fix pecha publish branch and base_names_list
  • Loading branch information
10zinten authored Mar 29, 2023
2 parents 8c6107c + 58e7135 commit 684cacc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
26 changes: 24 additions & 2 deletions openpecha/core/pecha.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
self._meta = self.__handle_old_metadata_attr(meta, metadata)
self._index = index
self.assets = assets if assets else {}
self._base_names_list = []
self._components = components if components else {}
self.current_base_order = 1

Expand Down Expand Up @@ -110,6 +111,19 @@ def index(self) -> Layer:
self._index = Layer.parse_obj(idxf)
return self._index

@property
def base_names_list(self) -> List[str]:
if self.bases:
self._base_names_list = list(self.bases.keys())

if "bases" in self.meta in self.meta.bases:
self._base_names_list = list(self.meta.bases.keys())

if not self._base_names_list:
self._base_names_list = self._read_base_names()

return self._base_names_list

@property
def components(self) -> Dict[str, List[LayerEnum]]:
if self._components:
Expand Down Expand Up @@ -296,6 +310,9 @@ def read_index_file(self) -> Dict:
if self.index_fn.is_file():
return load_yaml(self.index_fn)

def _read_base_names(self):
return [base.stem for base in self.base_path.iterdir()]

def _read_components(self):
res = {}
for vol_dir in self.layers_path.iterdir():
Expand Down Expand Up @@ -449,7 +466,9 @@ def opf_path(self) -> Path:
self._opf_path = self.pecha_path / f"{self.pecha_id}.opf"
return self._opf_path

def publish(self, asset_path: Path = None, asset_name: str = None):
def publish(
self, asset_path: Path = None, asset_name: str = None, branch: str = "main"
):
asset_paths = []
if not self.storage:
self.storage = GithubStorage()
Expand All @@ -458,7 +477,10 @@ def publish(self, asset_path: Path = None, asset_name: str = None):
commit_and_push(repo=local_repo, message="Pecha update")
else:
self.storage.add_dir(
path=self.pecha_path, description=self.about, is_private=self.is_private
path=self.pecha_path,
description=self.about,
is_private=self.is_private,
branch=branch,
)

# Publishing assets in release
Expand Down
8 changes: 4 additions & 4 deletions openpecha/storages.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import enum
import git
import os
import shutil
from pathlib import Path

import git
from git import Repo
from github import Github

Expand Down Expand Up @@ -130,15 +130,15 @@ def is_git_repo(self, path):
_ = git.Repo(path).git_dir
return True
except git.exc.InvalidGitRepositoryError:
return False
return False

def add_dir(self, path: Path, description: str, is_private: bool=False):
def add_dir(self, path: Path, description: str, is_private: bool = False, branch: str = "master"):
"""dir local dir to github."""
remote_repo_url = self._init_remote_repo(
path=path, description=description, is_private=is_private
)
local_repo = self._init_local_repo(path=path, remote_url=remote_repo_url)
commit_and_push(repo=local_repo, message="Initial commit")
commit_and_push(repo=local_repo, message="Initial commit", branch=branch)
return local_repo

def remove_dir_with_name(self, name: str):
Expand Down
5 changes: 5 additions & 0 deletions tests/core/test_pecha.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,8 @@ def test_multi_create_pecha():

assert len(pecha_02.bases) == 1
assert pecha_02.bases[pecha_02_base_name] == "pecha_02 base content"


def test_pecha_base_names_list(opf_path):
pecha = OpenPechaFS(path=opf_path)
assert pecha.base_names_list == ["v001"]

0 comments on commit 684cacc

Please sign in to comment.