Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix non-existent file #28

Merged
merged 12 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "zshpower"
readme = "README.rst"
repository = "https://github.com/snakypy/zshpower"
version = "0.11.2"
version = "0.11.3"

classifiers = [
"Intended Audience :: Developers",
Expand Down
2 changes: 1 addition & 1 deletion snakypy/zshpower/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
HOME = str(Path.home())
__info__ = {
"name": "ZSHPower",
"version": "0.11.2",
"version": "0.11.3",
"description": "ZSHPower is a theme for ZSH with a manager.",
"pkg_name": "zshpower",
"executable": "zshpower",
Expand Down
28 changes: 18 additions & 10 deletions snakypy/zshpower/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""CLI - Command Line Interface"""
from sys import exit

from snakypy.helpers import FG, printer

from snakypy.zshpower.commands.cron import Cron

try:
Expand Down Expand Up @@ -75,13 +79,17 @@ def run_credits() -> None:
# @silent_errors
@only_linux
def main() -> None:
run_init()
run_config()
run_activate()
run_deactivate()
run_reset()
run_uninstall()
run_sync()
run_cron()
run_logs()
run_credits()
try:
run_init()
run_config()
run_activate()
run_deactivate()
run_reset()
run_uninstall()
run_sync()
run_cron()
run_logs()
run_credits()
except KeyboardInterrupt:
printer("Aborted by user.", foreground=FG().WARNING)
exit(0)
53 changes: 41 additions & 12 deletions snakypy/zshpower/commands/cron.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from os.path import exists
from os.path import exists, isdir
from pydoc import pager

from snakypy.helpers import FG, pick
Expand All @@ -11,34 +11,48 @@
from snakypy.zshpower.config.base import Base
from snakypy.zshpower.config.cron import cron_content, sync_content
from snakypy.zshpower.utils.check import checking_init
from snakypy.zshpower.utils.modifiers import command_superuser
from snakypy.zshpower.utils.modifiers import command_root
from snakypy.zshpower.utils.process import open_file_with_editor


class Cron(Base):
def __init__(self, home):
Base.__init__(self, home)

def manager(self, action=None):
def manager(self, action=None) -> bool:

if not isdir(self.cron_d_path):
printer(
f'It appears that your machine does not have Cron installed. {__info__["name"]} '
f"could not find the folder. Aborted!",
foreground=FG().WARNING,
)
return False

try:
if action == "create":

if exists(self.sync_path) or exists(self.cron_path):
printer(
f"There is already a task in Cron for {__info__['name']}. Aborted!",
foreground=FG().WARNING,
)
return False

printer(
f"Creating {__info__['name']} Task in Cron.",
foreground=FG().QUESTION,
)

cmd = f"""su -c 'echo "{sync_content}" > {self.sync_path}; chmod a+x {self.sync_path};
echo "{cron_content}" > {self.cron_path}; chmod a+x {self.cron_path}'
"""
command_superuser(cmd, logfile=self.logfile)
command_root(cmd, logfile=self.logfile)

printer(
f"{__info__['name']} Cron task created!", foreground=FG().FINISH
)

printer(
f"""
************************************ WARNING *********************************************
Expand All @@ -50,32 +64,41 @@ def manager(self, action=None):
foreground=FG().YELLOW,
)

return True

elif action == "remove":
cron_file = find_objects(
self.cron_d_path, files=(f"{__info__['pkg_name']}_task.sh",)
)
task_run = find_objects(
"/etc/local/bin/", files=(f"{__info__['pkg_name']}_sync.sh",)
)

if not cron_file["files"] and not task_run["files"]:
return printer(
"There is no configuration file in ZSHPower Cron. Aborted!",
printer(
f'There is no {__info__["name"]} task file in Cron to remove. Aborted!',
foreground=FG().WARNING,
)
return False

if cron_file["files"] or task_run["files"]:
title = f"Really want to remove {__info__['name']} task from Cron?"
options = ["Yes", "No"]
reply = pick(title, options, colorful=True, index=True)
cmd = f"""su -c 'rm -f {self.sync_path} {self.cron_path}';"""

if reply is None or reply[0] == 1:
printer("Canceled by user.", foreground=FG().WARNING)
return False
command_superuser(cmd, logfile=self.logfile)

command_root(cmd, logfile=self.logfile)

printer(
f"{__info__['name']} task has been removed from Cron!",
foreground=FG().FINISH,
)

return True
return False
except PermissionError as err:
Log(filename=self.logfile).record(
"No permission to write to directory /etc/crond.d or /usr/local/bin.",
Expand All @@ -98,7 +121,13 @@ def run(self, arguments):
self.config_file, file_common=self.cron_path, superuser=True
)
elif arguments["--view"]:
read_config = read_file(self.cron_path)
pager(read_config)
return True
return False
try:
read_config = read_file(self.cron_path)
pager(read_config)
return True
except FileNotFoundError:
printer(
f'ZSHPower task file does not exist in Cron. Use: "{__info__["executable"]} cron --create"',
foreground=FG().WARNING,
)
return False
6 changes: 3 additions & 3 deletions snakypy/zshpower/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def run(self, arguments, *, reload=False) -> None:
)
else:
printer(
"Wait a moment, creating initial settings...", foreground=FG().WARNING
"Wait a moment, creating initial settings...", foreground=FG().QUESTION
)
create_path(
self.config_root, self.database_root, self.cache_root, self.lib_root
Expand Down Expand Up @@ -86,11 +86,11 @@ def run(self, arguments, *, reload=False) -> None:
change_shell(self.logfile)
printer("Settings finished!", foreground=FG().FINISH)

printer("Generating database, wait...", foreground=FG().WARNING)
# printer("Generating database, wait...", foreground=FG().QUESTION)
# Create table in database if not exists
DAO().create_table(self.tbl_main)
# Insert registers in database
records("insert")
records("insert", "Generating database, wait ...", FG().QUESTION)
printer("Database generated!", foreground=FG().FINISH)

# Register logs
Expand Down
14 changes: 10 additions & 4 deletions snakypy/zshpower/commands/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from snakypy.helpers.os.removals import remove_objects

from snakypy.zshpower.config.base import Base
from snakypy.zshpower.utils.check import checking_init
from snakypy.zshpower.utils.check import checking_init, is_blank_file


class LogsCommand(Base):
Expand All @@ -22,6 +22,12 @@ def run(self, arguments) -> None:
except FileNotFoundError:
printer("Log files not found:", foreground=FG().WARNING)
elif arguments["--clean"]:
remove_objects(objects=(self.logfile,))
create_file("", self.logfile, force=True)
printer("Logs have been cleaned up.", foreground=FG().FINISH)
if is_blank_file(self.logfile):
printer(
"The logs have already been cleared. Nothing to do.",
foreground=FG().WARNING,
)
else:
remove_objects(objects=(self.logfile,))
create_file("", self.logfile, force=True)
printer("Logs have been cleaned up.", foreground=FG().FINISH)
13 changes: 1 addition & 12 deletions snakypy/zshpower/commands/reset.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from concurrent.futures import ThreadPoolExecutor

from snakypy.helpers import printer
from snakypy.helpers.ansi import FG
from snakypy.helpers.catches.generic import whoami
from snakypy.helpers.console import loading

from snakypy.zshpower.commands.utils.handle import records
from snakypy.zshpower.config.base import Base
Expand All @@ -29,15 +26,7 @@ def run(self, arguments) -> None:
reload_zsh()
elif arguments["--db"]:
DAO().create_table(self.tbl_main)
with ThreadPoolExecutor(max_workers=2) as executor:
executor.submit(
loading,
set_time=0.030,
bar=False,
header="ZSHPower Restoring the database ...",
foreground=FG().QUESTION,
)
executor.submit(records, action="insert")
records("insert", "ZSHPower Restoring the database ...", FG().QUESTION)
printer("Done!", foreground=FG().FINISH)
self.log.record(
f"User ({whoami()}) reset database.", colorize=True, level="info"
Expand Down
13 changes: 2 additions & 11 deletions snakypy/zshpower/commands/sync.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import sys
from concurrent.futures import ThreadPoolExecutor
from sqlite3 import OperationalError

from snakypy.helpers import FG
from snakypy.helpers.catches.generic import whoami
from snakypy.helpers.console import loading, printer
from snakypy.helpers.console import printer

from snakypy.zshpower.commands.utils.handle import records
from snakypy.zshpower.config.base import Base
Expand All @@ -18,15 +17,7 @@ def __init__(self, home):
def run(self) -> None:
try:
checking_init(self.HOME, self.logfile)
with ThreadPoolExecutor(max_workers=2) as executor:
executor.submit(
loading,
set_time=0.030,
bar=False,
header="Synchronizing versions with database ...",
foreground=FG().QUESTION,
)
executor.submit(records, action="update")
records("update", "Synchronizing versions with database ...", FG().QUESTION)
self.log.record(
f"User ({whoami()}) updated the database.",
colorize=True,
Expand Down
2 changes: 2 additions & 0 deletions snakypy/zshpower/commands/uninstall.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from snakypy.helpers import pick, printer
from snakypy.helpers.ansi import FG
from snakypy.helpers.decorators import silent_errors
from snakypy.helpers.files import backup_file, create_file
from snakypy.helpers.os import remove_objects

Expand Down Expand Up @@ -93,6 +94,7 @@ def orphan_zshpower(self):
reload_zsh(reload_zsh(sleep_timer=2, message=True))
return "ZSHPower removed!"

@silent_errors
def run(self) -> str:
if self.using_omz():
return self.zshpower_with_omz()
Expand Down
11 changes: 10 additions & 1 deletion snakypy/zshpower/commands/utils/handle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from concurrent.futures import ThreadPoolExecutor

from snakypy.helpers.console import loading

from snakypy.zshpower.prompt.sections.cmake import CMake
from snakypy.zshpower.prompt.sections.crystal import Crystal
from snakypy.zshpower.prompt.sections.dart import Dart
Expand All @@ -26,8 +28,15 @@
from snakypy.zshpower.prompt.sections.zig import Zig


def records(action) -> None:
def records(action, header, foreground, timer=0.090):
with ThreadPoolExecutor(max_workers=10) as executor:
executor.submit(
loading,
set_time=timer,
bar=False,
header=header,
foreground=foreground,
)
executor.submit(Dart().set_version, action=action)
executor.submit(Docker().set_version, action=action)
executor.submit(Dotnet().set_version, action=action)
Expand Down
12 changes: 10 additions & 2 deletions snakypy/zshpower/utils/check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from os.path import exists, join
from os.path import exists, getsize, isfile, join
from shutil import which
from sys import exit

Expand All @@ -9,10 +9,18 @@
from snakypy.zshpower import __info__


def is_blank_file(filepath):
"""
Checks if a file is blank, that is, it does not contain lines.
"""
return isfile(filepath) and getsize(filepath) == 0


@only_linux
def tools_requirements(*args) -> bool:
"""
Function that looks for the necessary tools, if it doesn't find them, it triggers a message and closes the program.
Function that looks for the necessary tools, if it doesn't find them,
it triggers a message and closes the program.
"""
for tool in args:
if which(tool) is None:
Expand Down
4 changes: 1 addition & 3 deletions snakypy/zshpower/utils/modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def create_zshrc(content, zshrc_path, logfile):
create_file(content, zshrc_path)


def command_superuser(
cmd, logfile=None, msg_header="Enter the machine superuser password"
):
def command_root(cmd, logfile=None, msg_header="Enter the machine root password"):
check = False
printer(f"[ {msg_header} ]", foreground=FG().WARNING)
try:
Expand Down
Loading