Skip to content

Commit

Permalink
feat: update from local
Browse files Browse the repository at this point in the history
  • Loading branch information
lxl66566 committed Apr 8, 2024
1 parent 2581f13 commit 484047f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 43 deletions.
7 changes: 7 additions & 0 deletions bpm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ def value_in(value, in_list):
update_parser.add_argument(
"packages", nargs="*", help="Package names to update. Update all by default."
)
update_parser.add_argument(
"-l",
"--local",
nargs="?",
metavar="Archive",
help="update from local archive.",
)
update_parser.set_defaults(func=cli_update)

info_parser = subparsers.add_parser("info", help="Info package.")
Expand Down
92 changes: 53 additions & 39 deletions bpm/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@
from .utils.exceptions import RepoNotFoundError


def parse_name_or_url(name_or_url: str) -> tuple[str, bool]:
"""
Parse the name or url of a package.
`Returns`: a tuple with two elements: first is the true name, second is the flag of whether it is a url.
"""
test_parse = urlparse(name_or_url)
if test_parse.netloc == "github.com":
return RepoHandler.get_info_by_url(name_or_url)[1], True
return name_or_url, False


def download_and_install(args, repo: RepoHandler, rename=True):
try:
with TemporaryDirectory() as tmp_dir:
tmp_dir = Path(tmp_dir)
if args.local:
with Path(args.local).open("rb") as f:
main_path = extract(f, tmp_dir)
else:
main_path = download_and_extract(repo.asset, tmp_dir)
auto_install(repo, main_path, rename=rename)
except Exception as e:
raise e


def cli_install(args):
if args.interactive and args.quiet:
log.error("Cannot use both --interactive and --quiet.")
Expand All @@ -27,15 +52,14 @@ def cli_install(args):
)
exit(1)
for package in args.packages:
real_name = package
test_parse = urlparse(package)
if test_parse.netloc == "github.com":
real_name = RepoHandler.get_info_by_url(package)[1]
real_name, is_url = parse_name_or_url(package)
if not args.dry_run and repo_group.find_repo(real_name)[1]:
log.info(f"{real_name} is already installed.")
continue

# search
try:
if test_parse.netloc == "github.com":
if is_url:
repo = (
RepoHandler(
real_name,
Expand All @@ -55,41 +79,34 @@ def cli_install(args):
repo.ask(quiet=args.quiet, sort=args.sort)
if not args.local:
repo.get_asset(interactive=args.interactive)

except Exception as e:
log.error(f"Failed on searching `{package}`: {e}")
trace()
exit(1)
with TemporaryDirectory() as tmp_dir:
tmp_dir = Path(tmp_dir)
try:
if args.local:
with Path(args.local).open("rb") as f:
main_path = extract(f, tmp_dir)
else:
main_path = download_and_extract(repo.asset, tmp_dir)
auto_install(repo, main_path, rename=True)
log.info(f"Successfully installed `{repo.name}`.")
if WINDOWS:
bins = (
UFCS(repo.file_list)
.filter(lambda x: x.endswith(".lnk"))
.map(lambda x: Path(x).stem)
.map(lambda x: f"`{x}`")
)
log.info(
f"You can press `Win+r`, enter {', '.join(bins)} to start software, or execute in cmd."
)
if not args.dry_run:
repo_group.insert_repo(repo)

except Exception as e:
log.error(f"Failed to install `{repo.name}`: {e}")
trace()
log.error("Restoring...")
# rollback.
remove(repo.file_list)
error_exit("Files restored. Exiting...")
# install
try:
download_and_install(args, repo)
log.info(f"Successfully installed `{repo.name}`.")
if WINDOWS:
bins = (
UFCS(repo.file_list)
.filter(lambda x: x.endswith(".lnk"))
.map(lambda x: Path(x).stem)
.map(lambda x: f"`{x}`")
)
log.info(
f"You can press `Win+r`, enter {', '.join(bins)} to start software, or execute in cmd."
)
if not args.dry_run:
repo_group.insert_repo(repo)
except Exception as e:
log.error(f"Failed to install `{repo.name}`: {e}")
trace()
log.error("Restoring...")
# rollback.
remove(repo.file_list)
error_exit("Files restored. Exiting...")


def cli_remove(args):
Expand Down Expand Up @@ -131,10 +148,7 @@ def update(repo: RepoHandler):
log.info(
f"`{repo.name}` has an update: {result[0]} -> {result[1]}. Updating..."
)
with TemporaryDirectory() as tmp_dir:
tmp_dir = Path(tmp_dir)
main_path = download_and_extract(repo.asset, tmp_dir)
auto_install(repo, main_path, rename=False)
download_and_install(args, repo, rename=False)
log.info(f"`{repo.name}` updated successfully.")
else:
log.info(f"`{repo.name}` is the newest.")
Expand Down
8 changes: 5 additions & 3 deletions bpm/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def add_file_list(self, file):
self.installed_files.append(str(file))

@staticmethod
def get_info_by_fullname(fullname: str):
def get_info_by_fullname(fullname: str) -> tuple[str, str]:
"""
Returns: a tuple of (repo_owner, repo_name)
"""
Expand Down Expand Up @@ -309,13 +309,15 @@ def get_asset(self, interactive: bool = False):
log.info(f"selected asset: {self.asset}")
return self

def update_asset(self) -> Optional[tuple[str, str]]:
def update_asset(self) -> Optional[tuple[str, str] | tuple[None, None]]:
"""
update assets list.
update assets list. If a repo was installed locally, it will always return (None, None).
`Returns`: `None` if has no update, `(old_version, new_version)` if has update.
"""
old_version = self.version
if not old_version:
return None, None
self.get_asset()
if old_version == self.version:
return None
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "bin-package-manager"
version = "2.2.3"
version = "2.2.4"
description = "Bin package manager, a package manager based on Github release"
authors = ["lxl66566 <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 484047f

Please sign in to comment.