Skip to content

Commit

Permalink
Init command (#194)
Browse files Browse the repository at this point in the history
* Add init command

* Tests for init command

* init file

* Add git to workers

* Update git clone (as per @A-dead-pixel suggestion)
Cleanup used tmpdir after command finishes

* Remove restrictions on taskid

* Fix different /tmp filesystem error
  • Loading branch information
adespawn authored Feb 21, 2024
1 parent f8abf6f commit 29300a0
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Arch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Prepare system
run: |
sysctl kernel.perf_event_paranoid=-1
pacman -Syu --noconfirm diffutils time gcc dpkg ghostscript texlive-latexextra
pacman -Syu --noconfirm diffutils time gcc dpkg ghostscript texlive-latexextra git
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/Ubuntu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Prepare system
run: |
apt update
apt install -y sqlite3 sqlite3-doc build-essential dpkg texlive-latex-extra ghostscript
apt install -y sqlite3 sqlite3-doc build-essential dpkg texlive-latex-extra ghostscript git
sysctl kernel.perf_event_paranoid=-1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
Expand Down
76 changes: 76 additions & 0 deletions src/sinol_make/commands/init/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import argparse
import os
import shutil
import subprocess
import tempfile

from sinol_make import util
from sinol_make.interfaces.BaseCommand import BaseCommand


class Command(BaseCommand):
"""
Class for "init"
"""

def get_name(self):
return "init"

def configure_subparser(self, subparser: argparse.ArgumentParser):
parser = subparser.add_parser(
self.get_name(),
help='Create package from the template',
description='Create package from predefined template with given id.'
)
parser.add_argument('task_id', type=str, help='Id of the task to create')

def download_template(self):
repo = 'https://github.com/sio2project/sinol-make.git'
package_dir = 'sinol-make/example_package'
self.used_tmpdir = tempfile.TemporaryDirectory()
tmp_dir = self.used_tmpdir.name
ret = subprocess.run(['git', 'clone', '-q', '--depth', '1', repo], cwd=tmp_dir)
if ret.returncode != 0:
util.exit_with_error("Could not access repository. Please try again.")
return os.path.join(tmp_dir, package_dir)

def move_folder(self):
mapping = {}
mapping[self.template_dir] = os.getcwd()
for root, dirs, files in os.walk(self.template_dir):
for directory in dirs:
mapping[os.path.join(root, directory)] = os.path.join(mapping[root], directory)
os.mkdir(os.path.join(mapping[root], directory))
for file in files:
dest_filename = file
if file[:3] == 'abc':
dest_filename = self.task_id + file[3:]
shutil.move(os.path.join(root, file), os.path.join(mapping[root], dest_filename))

def update_config(self):
with open(os.path.join(os.getcwd(), 'config.yml')) as config:
config_data = config.read()
config_data = config_data.replace('sinol_task_id: abc', f'sinol_task_id: {self.task_id}')

with open(os.path.join(os.getcwd(), 'config.yml'), 'w') as config:
config.write(config_data)

def run(self, args: argparse.Namespace):
self.task_id = args.task_id
destination = os.path.join(os.getcwd(), args.task_id)
if os.path.isdir(destination) or os.path.isfile(destination):
util.exit_with_error(f"Cannot create task {args.task_id}, this name is already used in current directory. "
f"Remove the file/folder with this name or choose another id.")

self.template_dir = self.download_template()

os.mkdir(os.path.join(os.getcwd(), self.task_id))
os.chdir(os.path.join(os.getcwd(), self.task_id))

self.move_folder()
self.update_config()

self.used_tmpdir.cleanup()

print(util.info(f'Successfully created task "{self.task_id}"'))

Empty file added tests/commands/init/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions tests/commands/init/test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import pytest

from sinol_make import configure_parsers
from sinol_make.commands.init import Command
from tests.fixtures import temp_workdir


@pytest.mark.parametrize("temp_workdir", [''], indirect=True)
def test_simple(capsys, temp_workdir):
"""
Test `init` command.
"""
parser = configure_parsers()
args = parser.parse_args(["init", "xyz"])
command = Command()
command.run(args)
out = capsys.readouterr().out
assert 'Successfully created task "xyz"' in out

# Check presence of some files:
expected_files = ['config.yml', 'prog/xyz.cpp', 'prog/oi.h']

for file in expected_files:
assert os.path.isfile(os.path.join(os.getcwd(), file))

# Check if task id is correctly set
with open(os.path.join(os.getcwd(), 'config.yml')) as config_file:
config_file_data = config_file.read()
assert "sinol_task_id: xyz" in config_file_data
assert "sinol_task_id: abc" not in config_file_data
9 changes: 9 additions & 0 deletions tests/commands/init/test_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

from sinol_make.commands.init import Command


def test_if_download_successful():
command = Command()
tmp_dir = command.download_template()
assert os.path.isfile(os.path.join(tmp_dir,'config.yml'))
14 changes: 14 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ def create_package(request):
yield package_path

tmpdir.cleanup()


@pytest.fixture
def temp_workdir():
"""
Fixture to change the current working directory to the temporary directory.
"""
tmpdir = tempfile.TemporaryDirectory()
print(tmpdir)
os.chdir(tmpdir.name)

yield tmpdir.name

tmpdir.cleanup()

0 comments on commit 29300a0

Please sign in to comment.