Skip to content

Commit

Permalink
Use requirements.txt for dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Nov 3, 2023
1 parent 63fe6c8 commit 94aefc2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
18 changes: 7 additions & 11 deletions gitbark/commands/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ..core import get_bark_rules, BARK_RULES, BARK_RULES_BRANCH
from ..core import get_bark_rules, BARK_RULES, BARK_RULES_BRANCH, BARK_REQUIREMENTS
from ..objects import BarkRules, BranchRuleData, RuleData
from ..git import Commit, COMMIT_RULES, BARK_CONFIG
from ..project import Project
Expand Down Expand Up @@ -264,12 +264,8 @@ def add_modules_interactive(project: Project) -> None:
if curr_branch != BARK_RULES_BRANCH:
checkout_or_orphan(project, BARK_RULES_BRANCH)

try:
bark_rules = get_bark_rules(project)
except ValueError:
raise CliFail("'bark_modules.yaml' configuration is invalid!")

click.echo("Define what Bark Modules to add!\n")
requirements = []
while True:
module = click_prompt(
prompt="Module to add (leave blank to skip)",
Expand All @@ -279,15 +275,15 @@ def add_modules_interactive(project: Project) -> None:
if not module:
break

project.install_bark_module(module)
bark_rules.modules.append(module)
requirements.append(module)

_confirm_bark_rules(bark_rules)
content = "\n".join(requirements)
project.install_modules(content.encode())

dump_and_stage(
project=project,
file=f"{project.path}/{BARK_RULES}",
content=yaml.safe_dump(asdict(bark_rules), sort_keys=False),
file=f"{project.path}/{BARK_REQUIREMENTS}",
content=content,
)


Expand Down
32 changes: 14 additions & 18 deletions gitbark/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
from .project import Cache, Project
from .rule import RuleViolation, CommitRule, AllCommitRule, BranchRule
from .objects import BranchRuleData, BarkRules, RuleData
from functools import partial
from typing import Callable, Optional
import yaml

BARK_RULES_BRANCH = "bark_rules"
BARK_RULES = f"{BARK_CONFIG}/bark_rules.yaml"
BARK_REQUIREMENTS = f"{BARK_CONFIG}/requirements.txt"


def get_commit_rule(commit: Commit, project: Project) -> CommitRule:
Expand Down Expand Up @@ -105,17 +105,6 @@ def validate_commit(
raise violation


def update_modules(project: Project, branch: str, commit: Commit) -> None:
bark_modules = get_bark_rules(project, commit).modules
prev_bark_modules = [
set(get_bark_rules(project, p).modules) for p in commit.parents
]

for module in bark_modules:
if not prev_bark_modules or any(module not in p for p in prev_bark_modules):
project.install_bark_module(module)


def validate_branch_rules(
project: Project, head: Commit, branch: str, branch_rule: BranchRuleData
) -> None:
Expand All @@ -134,7 +123,9 @@ def validate_commit_rules(
on_valid: Callable[[Commit], None] = lambda commit: None
if branch == BARK_RULES_BRANCH:
# Need to update modules on each validated commit
on_valid = partial(update_modules, project, branch)
def on_valid(commit: Commit) -> None:
requirements = commit.read_file(BARK_REQUIREMENTS)
project.install_modules(requirements)

try:
validate_commit(head, bootstrap, project, on_valid)
Expand All @@ -145,15 +136,20 @@ def validate_commit_rules(
raise RuleViolation(error_message, [e])


def get_bark_rules_commit(project: Project) -> Optional[Commit]:
"""Gets the latest commit on bark_rules."""
bark_rules_branch = project.repo.lookup_branch(BARK_RULES_BRANCH)
if not bark_rules_branch:
return None
return Commit(bark_rules_branch.target.raw, project.repo)


def get_bark_rules(project: Project, commit: Optional[Commit] = None) -> BarkRules:
"""Returns the latest branch_rules"""

commit = commit or get_bark_rules_commit(project)
if not commit:
bark_rules_branch = project.repo.lookup_branch(BARK_RULES_BRANCH)
if not bark_rules_branch:
return BarkRules([], [])

commit = Commit(bark_rules_branch.target.raw, project.repo)
return BarkRules([], [])

try:
bark_rules_blob = commit.read_file(BARK_RULES)
Expand Down
5 changes: 1 addition & 4 deletions gitbark/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,12 @@ def branches(self, repo: Repository) -> list[str]:
@dataclass
class BarkRules:
branches: list[BranchRuleData]
modules: list[str]

@classmethod
def parse(cls, bark_rules: dict) -> "BarkRules":
try:
branches = [BranchRuleData.parse(rule) for rule in bark_rules["branches"]]
modules = bark_rules.get("modules", [])
except Exception:
raise
raise ValueError("Cannot parse bark_modules.yaml!")

return cls(branches=branches, modules=modules)
return cls(branches=branches)
18 changes: 15 additions & 3 deletions gitbark/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,21 @@ def create_db(self) -> None:
"""
)

def install_bark_module(self, bark_module: str) -> None:
pip_path = os.path.join(self.env_path, "bin", "pip")
cmd(pip_path, "install", bark_module)
def install_modules(self, requirements: bytes) -> None:
r_file = os.path.join(self.bark_directory, "requirements.txt")

if os.path.exists(r_file):
with open(r_file, "rb") as f:
old_reqs = f.read()
else:
old_reqs = b""

if requirements != old_reqs:
with open(r_file, "wb") as f:
f.write(requirements)

pip_path = os.path.join(self.env_path, "bin", "pip")
cmd(pip_path, "install", "-r", r_file)

def get_env_site_packages(self) -> str:
exec_path = os.path.join(self.env_path, "bin", "python")
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def _env_initialized_state(
branch_rule = BranchRuleData(
pattern="main", bootstrap=bootstrap_main.hash.hex(), rules=[]
)
bark_rules = BarkRules(branches=[branch_rule], modules=[test_bark_module])
env.repo.add_bark_rules(bark_rules)
bark_rules = BarkRules(branches=[branch_rule])
env.repo.add_bark_rules(bark_rules, test_bark_module)

dump_path = tmp_path_factory.mktemp("dump")
env.dump(dump_path)
Expand Down
11 changes: 9 additions & 2 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from gitbark.util import cmd
from gitbark.objects import BarkRules
from gitbark.commands.install import make_executable
from gitbark.core import BARK_RULES, BARK_RULES_BRANCH
from gitbark.core import BARK_RULES, BARK_RULES_BRANCH, BARK_REQUIREMENTS
from gitbark.git import Commit, BARK_CONFIG, COMMIT_RULES

from typing import Optional
Expand Down Expand Up @@ -129,7 +129,9 @@ def _add_bark_files(self, file: str, content: str) -> None:
with open(file, "w") as f:
f.write(content)

def add_bark_rules(self, bark_rules: BarkRules) -> None:
def add_bark_rules(
self, bark_rules: BarkRules, requirements: Optional[str] = None
) -> None:
curr_branch = cmd("git", "symbolic-ref", "--short", "HEAD", cwd=self.repo_dir)[
0
]
Expand All @@ -138,6 +140,11 @@ def add_bark_rules(self, bark_rules: BarkRules) -> None:
file=f"{self.repo_dir}/{BARK_RULES}",
content=yaml.safe_dump(asdict(bark_rules), sort_keys=False),
)
if requirements:
self._add_bark_files(
file=f"{self.repo_dir}/{BARK_REQUIREMENTS}",
content=requirements,
)
self.commit("Add bark rules.")
self.checkout(curr_branch)

Expand Down

0 comments on commit 94aefc2

Please sign in to comment.