Skip to content

Commit

Permalink
util: check_dir(): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Obijuan committed Mar 8, 2024
1 parent 32a7bf1 commit 8b350a5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
9 changes: 4 additions & 5 deletions apio/managers/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def copy_example_dir(self, example, project_dir, sayno):
if util.check_package(
self.name, self.version, self.spec_version, self.examples_dir
):
project_dir = util.check_dir(project_dir)
example_path = str(Path(project_dir) / example)
project_dir = Path(util.check_dir(project_dir))
example_path = str(project_dir / example)
local_example_path = str(Path(self.examples_dir) / example)

if isdir(local_example_path):
Expand Down Expand Up @@ -121,13 +121,12 @@ def copy_example_files(self, example, project_dir, sayno):
if util.check_package(
self.name, self.version, self.spec_version, self.examples_dir
):
project_dir = util.check_dir(project_dir)
example_path = project_dir
example_path = Path(util.check_dir(project_dir))
local_example_path = str(Path(self.examples_dir) / example)

if isdir(local_example_path):
self._copy_files(
example, local_example_path, example_path, sayno
example, local_example_path, str(example_path), sayno
)
else:
click.secho(EXAMPLE_NOT_FOUND_MSG, fg="yellow")
Expand Down
8 changes: 4 additions & 4 deletions apio/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def create_sconstruct(self, project_dir="", arch=None, sayyes=False):
def create_ini(self, board, top_module, project_dir="", sayyes=False):
"""Creates a new apio project file"""

project_dir = util.check_dir(project_dir)
project_dir = Path(util.check_dir(project_dir))

# -- Build the filename
ini_path = str(Path(project_dir) / PROJECT_FILENAME)
ini_path = str(project_dir / PROJECT_FILENAME)

# Check board
boards = Resources().boards
Expand Down Expand Up @@ -108,10 +108,10 @@ def create_ini(self, board, top_module, project_dir="", sayyes=False):
def update_ini(self, top_module, project_dir):
"""Update the current init file with the given top-module"""

project_dir = util.check_dir(project_dir)
project_dir = Path(util.check_dir(project_dir))

# -- Build the filename
ini_path = str(Path(project_dir) / PROJECT_FILENAME)
ini_path = str(project_dir / PROJECT_FILENAME)

# -- Check if the apio.ini file exists
if not isfile(ini_path):
Expand Down
4 changes: 2 additions & 2 deletions apio/managers/scons.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def __init__(self, project_dir=""):
if project_dir is not None:
# Check if it is a correct folder
# (or create a new one)
project_dir = util.check_dir(project_dir)
project_dir = Path(util.check_dir(project_dir))

# Change to that folder
os.chdir(project_dir)
os.chdir(str(project_dir))

# W0703: Catching too general exception Exception (broad-except)
# pylint: disable=W0703
Expand Down
15 changes: 8 additions & 7 deletions apio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import platform
import subprocess
from threading import Thread
from os.path import isdir, isfile, dirname, exists
from os.path import isdir, dirname, exists
from pathlib import Path

import click
Expand Down Expand Up @@ -662,16 +662,17 @@ def mkdir(path):
pass


def check_dir(_dir):
"""Check if the given path is a folder. If no path is given
the current path is used"""
def check_dir(_dir: Path) -> str:
"""Check if the given path is a folder"""

# -- If no path is given, get the current working directory
if _dir is None:
_dir = os.getcwd()
if _dir:
_dir = Path(_dir)
else:
_dir = Path.cwd()

# -- Check if the path is a file or a folder
if isfile(_dir):
if _dir.is_file():
# -- It is a file! Error! Exit!
click.secho(
f"Error: project directory is already a file: {_dir}", fg="red"
Expand Down

0 comments on commit 8b350a5

Please sign in to comment.