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

fix(hansbug): fix a bug of directory creation #3

Merged
merged 1 commit into from
Sep 16, 2022
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: 2 additions & 0 deletions igm/render/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def _run(self):

try:
rel_dstpath = os.path.relpath(abs_dstpath, start=dstdir)
if dstdir:
os.makedirs(dstdir, exist_ok=True)
os.chdir(dstdir)
# noinspection PyCallingNonCallable
script(rel_dstpath, **self.__extras)
Expand Down
14 changes: 14 additions & 0 deletions templates/test/template/nested/1/2/3/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cpus = {{ sys.cpu.num }}
mem_size = {{ sys.memory.total | str | potc }}
os = {{ sys.os.type | str | potc }}
python = {{ sys.python | str | potc }}
{% if sys.cuda %}
cuda_version = {{ sys.cuda.version | str | potc }}
gpu_num = {{ sys.gpu.num | potc }}
{% endif %}

print('This is your first try!')
print(f'UR running {python} on {os}, with a {cpus} core {mem_size} device.')
{% if sys.cuda %}
print(f'CUDA {cuda_version} is also detected, with {gpu_num} gpu(s).')
{% endif %}
28 changes: 28 additions & 0 deletions templates/test/template/nested/1/2/4/.script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os

import extras
from extras import wtf

from igm.render import igm_script


@igm_script
def run1(dst):
with open('script_1.ini', 'w') as f:
print('this is one', file=f)


@igm_script
def run2(dst):
with open('script_2.txt', 'w') as f:
print('this is two', file=f)
print(dst, file=f)
print(wtf(103, 279), file=f)
print(sorted(dir(extras)), file=f)
print(os.path.getsize('script_1.ini'), file=f)


@igm_script
def err(dst):
if os.environ.get('MAKE_ERROR'):
raise ValueError('This is the error')
15 changes: 15 additions & 0 deletions templates/test/template/nested/1/2/5/.d_unpacked.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

from igm.render import download

GITHUB_HOST = os.environ.get('GITHUB_HOST', 'github.com')


def _get_testfile_url(resource, branch='main') -> str:
if not os.environ.get('NO_GITHUB'):
return f'https://{GITHUB_HOST}/igm4ai/igm-testfile/raw/{branch}/{resource}'
else:
return f'https://gitee.com/hansbug/igm-testfile/raw/{branch}/{resource}'


download(_get_testfile_url('gztar_template-simple.tar.gz'))
37 changes: 35 additions & 2 deletions test/render/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ def test_task_test(self, config_2, silent, text_align_no_empty):
wtf=lambda x, y: f'wtf: {x} + {y} = {x + y}',
),
)
assert len(t) == 10
assert repr(t) == '<IGMRenderTask 10 jobs, srcdir: \'template\'>'
assert len(t) == 13
assert repr(t) == '<IGMRenderTask 13 jobs, srcdir: \'template\'>'
t.run(silent=silent)

text_align_no_empty.assert_equal(pathlib.Path('project/main.py').read_text(), [
Expand Down Expand Up @@ -229,6 +229,19 @@ def test_task_test(self, config_2, silent, text_align_no_empty):
'```'
])

text_align_no_empty.assert_equal(pathlib.Path('project/nested/1/2/3/main.py').read_text(), [
'cpus = 112',
"mem_size = '944.35 GiB'",
"os = 'macOS'",
"python = 'CPython 3.9.4'",
"cuda_version = '11.2'",
'gpu_num = 2',

"print('This is your first try!')",
"print(f'UR running {python} on {os}, with a {cpus} core {mem_size} device.')",
"print(f'CUDA {cuda_version} is also detected, with {gpu_num} gpu(s).')"
])

assert os.path.exists('project/raw.tar.gz')
assert os.path.isfile('project/raw.tar.gz')
assert pathlib.Path('project/raw.tar.gz').read_bytes() == \
Expand Down Expand Up @@ -262,3 +275,23 @@ def test_task_test(self, config_2, silent, text_align_no_empty):
assert line3 == 'wtf: 103 + 279 = 382'
assert line4 == '[\'template\', \'trepr\', \'wtf\']'
assert line5 == str(os.path.getsize('project/script_1.ini'))

assert os.path.exists('project/nested/1/2/4/script_1.ini')
assert os.path.isfile('project/nested/1/2/4/script_1.ini')
assert pathlib.Path('project/nested/1/2/4/script_1.ini').read_text().strip() == 'this is one'

assert os.path.exists('project/nested/1/2/4/script_2.txt')
assert os.path.isfile('project/nested/1/2/4/script_2.txt')
lines = text_align_no_empty.splitlines(
pathlib.Path('project/nested/1/2/4/script_2.txt').read_text())
line1, line2, line3, line4, line5 = lines
assert line1 == 'this is two'
assert_same_path(os.path.join('project', line2), 'project/script')
assert line3 == 'wtf: 103 + 279 = 382'
assert line4 == '[\'template\', \'trepr\', \'wtf\']'
assert line5 == str(os.path.getsize('project/nested/1/2/4/script_1.ini'))

assert os.path.exists('project/nested/1/2/5/d_unpacked')
assert os.path.isdir('project/nested/1/2/5/d_unpacked')
assert os.path.exists('project/nested/1/2/5/d_unpacked/README.md')
assert os.path.exists('project/nested/1/2/5/d_unpacked/meta.py')