-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from psyinfra/rf-init
NF/TST: add `repo(init=bool)`, and assorted test modernization
- Loading branch information
Showing
10 changed files
with
465 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,28 @@ | ||
import logging | ||
import sys | ||
import shutil | ||
from pathlib import Path | ||
import git | ||
from onyo import Repo | ||
|
||
logging.basicConfig() | ||
log = logging.getLogger('onyo') | ||
|
||
|
||
def get_skel_dir(): | ||
""" | ||
Return the path of the skel/ dir in the onyo module directory. | ||
""" | ||
onyo_module_dir = Path(__file__).resolve().parent.parent | ||
skel = Path(onyo_module_dir, 'skel') | ||
return skel | ||
|
||
|
||
def sanitize_dir(directory, opdir): | ||
""" | ||
Check the directory for viability as an init target. | ||
Returns the absolute path on success. | ||
""" | ||
full_dir = Path(opdir) | ||
if directory: | ||
full_dir = Path(opdir, directory) | ||
|
||
# sanity checks | ||
# already an .onyo repo | ||
dot_onyo = full_dir.joinpath('.onyo') | ||
if dot_onyo.is_dir(): | ||
log.error(f"'{dot_onyo}' already exists. Exiting.") | ||
sys.exit(1) | ||
|
||
# target is a file, etc | ||
if full_dir.exists() and not full_dir.is_dir(): | ||
log.error(f"'{full_dir}' exists but is not a directory. Exiting.") | ||
sys.exit(1) | ||
|
||
# make sure parent exists | ||
if not full_dir.is_dir(): | ||
parent_dir = full_dir.parent | ||
if not parent_dir.is_dir(): | ||
log.error(f"'{parent_dir}' does not exist. Exiting.") | ||
sys.exit(1) | ||
|
||
abs_dir = str(full_dir.resolve()) | ||
return abs_dir | ||
|
||
|
||
def init(args, opdir): | ||
def init(args, opdir: str) -> None: | ||
""" | ||
Initialize an Onyo repository. The directory will be initialized as a git | ||
repository (if it is not one already), the ``.onyo/`` directory created | ||
(containing default config files, templates, etc), and everything committed. | ||
repository (if it is not one already), the ``.onyo/`` directory created and | ||
populated with config files, templates, etc. Everything will be committed. | ||
The current working directory will be initialized if neither ``directory`` | ||
nor the ``onyo -C <dir>`` option are specified. | ||
Running ``onyo init`` on an existing repository is safe. It will not | ||
overwrite anything; it will exit with an error. | ||
""" | ||
target_dir = sanitize_dir(args.directory, opdir) | ||
Path(target_dir).mkdir(exist_ok=True) | ||
target_dir = Path(opdir) | ||
if args.directory: | ||
if Path(args.directory).is_absolute(): | ||
target_dir = Path(args.directory) | ||
else: | ||
target_dir = Path(opdir, args.directory) | ||
|
||
try: | ||
repo = git.Repo(target_dir) | ||
log.info(target_dir + " has a git repository.") | ||
except git.exc.InvalidGitRepositoryError: | ||
repo = git.Repo.init(target_dir) | ||
|
||
# populate .onyo dir | ||
skel = get_skel_dir() | ||
dot_onyo = Path(target_dir, ".onyo") | ||
shutil.copytree(skel, dot_onyo) | ||
|
||
# add and commit | ||
repo.git.add('.onyo/') | ||
repo.git.commit(m='initialize onyo repository') | ||
|
||
# print success | ||
abs_dot_onyo = str(dot_onyo.resolve()) | ||
print(f'Initialized Onyo repository in {abs_dot_onyo}') | ||
Repo(target_dir, init=True) | ||
except (FileExistsError, FileNotFoundError): | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.