Skip to content
Snippets Groups Projects
Commit ca690398 authored by tuhe's avatar tuhe
Browse files

Fixed windows wexpect issue

parent 0c2e62db
No related branches found
No related tags found
No related merge requests found
wexpect # wexpect
git+https://github.com/raczben/wexpect.git@dev # Changing to dev bc of venv incompatibility stuff. (th juli 2023: problem with 4.0.0).
pexpect pexpect
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# Use: pipreqs.exe slider --no-pin --force for requirements_pip.txt # Use: pipreqs.exe slider --no-pin --force for requirements_pip.txt
# https://packaging.python.org/tutorials/packaging-projects/ # https://packaging.python.org/tutorials/packaging-projects/
# py -m build && twine upload dist/* # py -m build && twine upload dist/*
# Linux> python -m build && twine upload dist/* # Linux> python -m build && python -m twine upload dist/*
# Local install: sudo pip install -e ./ # Local install: sudo pip install -e ./
import setuptools import setuptools
......
Metadata-Version: 2.1 Metadata-Version: 2.1
Name: codesnipper Name: codesnipper
Version: 0.1.17 Version: 0.1.18.5
Summary: A lightweight framework for censoring student solutions files and extracting code + output Summary: A lightweight framework for censoring student solutions files and extracting code + output
Home-page: https://lab.compute.dtu.dk/tuhe/snipper Home-page: https://lab.compute.dtu.dk/tuhe/snipper
Author: Tue Herlau Author: Tue Herlau
......
...@@ -22,3 +22,4 @@ src/snipper/snip_dir.py ...@@ -22,3 +22,4 @@ src/snipper/snip_dir.py
src/snipper/snipper_main.py src/snipper/snipper_main.py
src/snipper/test.py src/snipper/test.py
src/snipper/version.py src/snipper/version.py
tests/test_python.py
\ No newline at end of file
...@@ -121,7 +121,7 @@ def strip_tag(lines, tag): ...@@ -121,7 +121,7 @@ def strip_tag(lines, tag):
lines2 = [] lines2 = []
for l in lines: for l in lines:
dx = l.find(tag) dx = l.find(tag)
if dx > 0: if dx >= 0:
l = l[:dx] l = l[:dx]
if len(l.strip()) == 0: if len(l.strip()) == 0:
l = None l = None
......
...@@ -66,7 +66,7 @@ def fix_f(lines, debug, keep=False): ...@@ -66,7 +66,7 @@ def fix_f(lines, debug, keep=False):
return lines2 return lines2
# stats = {'n': 0} # stats = {'n': 0}
def _block_fun(lines, start_extra, end_extra, keep=False, permute=False, questionmarks=False, halfquestionmarks=False, silent=False, nolines=False): def _block_fun(lines, start_extra, end_extra, keep=False, permute=False, questionmarks=False, halfquestionmarks=False, silent=False, nolines=False, noerror=False):
methods = {'remove': 0} methods = {'remove': 0}
# if method not in ['remove', 'permute', 'questionmark', 'halfquestionmark']: # if method not in ['remove', 'permute', 'questionmark', 'halfquestionmark']:
# assert False # assert False
...@@ -122,6 +122,7 @@ def _block_fun(lines, start_extra, end_extra, keep=False, permute=False, questio ...@@ -122,6 +122,7 @@ def _block_fun(lines, start_extra, end_extra, keep=False, permute=False, questio
lines = [id + todo] lines = [id + todo]
else: else:
lines = msg + lines lines = msg + lines
if not noerror:
lines += [id + f'raise NotImplementedError("{ee}")'] lines += [id + f'raise NotImplementedError("{ee}")']
l2 = ([id + start_extra] if len(start_extra) > 0 else []) + lines # [id + f"# TODO: {cc} lines missing.", l2 = ([id + start_extra] if len(start_extra) > 0 else []) + lines # [id + f"# TODO: {cc} lines missing.",
# id + f'raise NotImplementedError("{ee}")'] # id + f'raise NotImplementedError("{ee}")']
......
...@@ -23,7 +23,11 @@ def rsession(analyzer, lines, extra): ...@@ -23,7 +23,11 @@ def rsession(analyzer, lines, extra):
import time import time
an = we.spawn("python", encoding="utf-8", timeout=20) an = we.spawn("python", encoding="utf-8", timeout=20)
try:
an.setwinsize(400, 400) # set window size to avoid truncated output or input. an.setwinsize(400, 400) # set window size to avoid truncated output or input.
except AttributeError as e:
print("> Mulble pexpect('pyhon',...) does not support setwinsize on this system (windows?). Ignoring")
an.expect([">>>"]) an.expect([">>>"])
l3 = """ l3 = """
2 + 4 # Add two integers 2 + 4 # Add two integers
...@@ -130,7 +134,11 @@ def run_i(lines, file, output): ...@@ -130,7 +134,11 @@ def run_i(lines, file, output):
if extra['python'] is None: if extra['python'] is None:
an = we.spawn("python", encoding="utf-8", timeout=20) an = we.spawn("python", encoding="utf-8", timeout=20)
try:
an.setwinsize(400, 400) # set window size to avoid truncated output or input. an.setwinsize(400, 400) # set window size to avoid truncated output or input.
except AttributeError as e:
print("> Mulble pexpect('pyhon',...) does not support setwinsize on this system (windows?). Ignoring")
an.expect([">>>"]) an.expect([">>>"])
extra['python'] = an extra['python'] = an
......
import functools import functools
import os import os
from snipper.legacy import block_process
from snipper.block_parsing import indent from snipper.block_parsing import indent
import sys
import subprocess
def run_o(lines, file, output): def o_block_funlines(lines, art, output):
def block_fun(lines, start_extra, end_extra, art, output, **kwargs):
id = indent(lines[0]) id = indent(lines[0])
outf = output + ("_" + art if art is not None else "") + ".txt" if not os.path.isdir(os.path.dirname(output)):
os.makedirs(os.path.dirname(output))
# art = name
outf = output + ("_" + art if art is not None and art != "" else "") + ".txt"
l2 = [] l2 = []
l2 += [id + "import sys", id + f"sys.stdout = open('{outf}', 'w')"] l2 += [id + "import sys", id + f"sys.stdout = open('{outf}', 'w')"]
l2 += lines l2 += lines
l2 += [indent(lines[-1]) + "sys.stdout = sys.__stdout__"] l2 += [indent(lines[-1]) + "sys.stdout = sys.__stdout__"]
return l2, None return l2
pass
def run_o(lines, file, output,package_base_dir=None):
# def block_fun(lines, start_extra, end_extra, art, output, **kwargs):
# id = indent(lines[0])
# outf = output + ("_" + art if art is not None else "") + ".txt"
# l2 = []
# l2 += [id + "import sys", id + f"sys.stdout = open('{outf}', 'w')"]
# l2 += lines
# l2 += [indent(lines[-1]) + "sys.stdout = sys.__stdout__"]
#
#
# return l2, None
try: try:
lines2, didfind, extra, _ = block_process(lines, tag="#!o", block_fun=functools.partial(block_fun, output=output) ) from snipper.block_parsing import block_split, block_join
except Exception as e: # args = {k: v for k, v in b['start_tag_args'].items() if len(k) > 0}
print("Bad file: ", file) # cutout.append(b['block'])
print("I was cutting the #!o tag") # b['block'], dn = _block_fun(b['block'], start_extra=b['arg1'], end_extra=b['arg2'], **args, keep=keep)
print("\n".join( lines) ) # # cutout += b['block']
raise(e) # # method = b['start_tag_args'].get('', 'remove')
# # b['block'], dn = _block_fun(b['block'], start_extra=b['arg1'], end_extra=b['arg1'], **args, keep=keep)
# lines = block_join(b)
if didfind: while True:
b = block_split(lines, tag="#!o")
if b is None:
break
# ex = b['name']
# o_block_fun(b['block'], None, )
l2 = o_block_funlines( b['block'], b['name'], output)
lines2 = b['first'] + l2 + b['last']
lines = b['first'] + b['block'] + b['last']
fp, ex = os.path.splitext(file) fp, ex = os.path.splitext(file)
file_run = fp + "_RUN_OUTPUT_CAPTURE" + ex file_run = fp + "_RUN_OUTPUT_CAPTURE" + ex
# lines = lines2
if os.path.exists(file_run): if os.path.exists(file_run):
print("file found mumble...") print("file found mumble...")
else: else:
with open(file_run, 'w', encoding="utf-8") as f: with open(file_run, 'w', encoding="utf-8") as f:
f.write("\n".join(lines2)) f.write("\n".join(lines2))
cmd = "python " + file_run python = sys.executable
import subprocess if package_base_dir is None:
cmd = f"cd {os.path.dirname(file_run)} && {python} {os.path.basename(file_run)}"
else:
# cmd = f"cd {os.path.dirname(package_base_dir)} && {python} {os.path.basename(file_run)}"
rp = os.path.relpath(file_run, package_base_dir).replace("\\", "/").replace("/", ".")[:-3]
cmd = f"cd {package_base_dir} && {python} -m {rp}"
print(cmd)
s = subprocess.check_output(cmd, shell=True) s = subprocess.check_output(cmd, shell=True)
os.remove(file_run) os.remove(file_run)
# lines2, didfind, extra, _ = block_process(lines, tag="#!o", block_fun=functools.partial(block_fun, output=output) )
except Exception as e:
print("Bad file: ", file)
print("I was cutting the #!o tag")
print("\n".join( lines) )
raise(e)
# if didfind:
# fp, ex = os.path.splitext(file)
# file_run = fp + "_RUN_OUTPUT_CAPTURE" +ex
# if os.path.exists(file_run):
# print("file found mumble...")
# else:
# with open(file_run, 'w', encoding="utf-8") as f:
# f.write("\n".join(lines2) )
# cmd = "python " + file_run
# import subprocess
# s = subprocess.check_output(cmd, shell=True)
# os.remove(file_run)
\ No newline at end of file
...@@ -18,8 +18,9 @@ def snip_dir(source_dir, # Sources ...@@ -18,8 +18,9 @@ def snip_dir(source_dir, # Sources
license_head=None, license_head=None,
censor_files=True, censor_files=True,
verbose=True, verbose=True,
package_base_dir=None, # When running files, this will be treated as the base of the package the file is run from.
): ):
if verbose:
print(f"Snipper fixing {source_dir} {cut_files=}, {censor_files=}, {output_dir=}") print(f"Snipper fixing {source_dir} {cut_files=}, {censor_files=}, {output_dir=}")
if dest_dir == None: if dest_dir == None:
dest_dir = tempfile.mkdtemp() dest_dir = tempfile.mkdtemp()
...@@ -84,6 +85,7 @@ def snip_dir(source_dir, # Sources ...@@ -84,6 +85,7 @@ def snip_dir(source_dir, # Sources
references=references, references=references,
license_head=license_head, license_head=license_head,
censor_files=censor_files, censor_files=censor_files,
package_base_dir=package_base_dir,
**kwargs) **kwargs)
if nrem > 0 and verbose: if nrem > 0 and verbose:
print(f"{nrem}> {f}") print(f"{nrem}> {f}")
......
...@@ -33,10 +33,11 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True, ...@@ -33,10 +33,11 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True,
base_path=None, base_path=None,
strict=True, strict=True,
references=None, references=None,
license_head=None): license_head=None,
package_base_dir=None):
if str(file).endswith("rst"): if str(file).endswith("rst"):
assert not run_files and not cut_files and not censor_files assert not run_files and not cut_files and not censor_files
print(file) # print(file)
# if str(file).endswith("md"): # if str(file).endswith("md"):
# assert not run_files and not cut_files # assert not run_files and not cut_files
...@@ -71,7 +72,7 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True, ...@@ -71,7 +72,7 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True,
ofiles[0] = ofiles[0].replace("\\", "/") ofiles[0] = ofiles[0].replace("\\", "/")
if run_files: if run_files:
run_o(lines, file=file, output=ofiles[0]) run_o(lines, file=file, output=ofiles[0], package_base_dir=package_base_dir)
run_i(lines, file=file, output=ofiles[0]) run_i(lines, file=file, output=ofiles[0])
if cut_files: if cut_files:
save_s(lines, file_path=os.path.relpath(file, base_path), output_dir=run_out_dirs) save_s(lines, file_path=os.path.relpath(file, base_path), output_dir=run_out_dirs)
...@@ -79,8 +80,6 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True, ...@@ -79,8 +80,6 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True,
if censor_files: if censor_files:
lines = fix_f(lines, dbug) lines = fix_f(lines, dbug)
# if "pacman" in str(file):
# print("sdaf")
lines, nB, cut = fix_b(lines) lines, nB, cut = fix_b(lines)
else: else:
nB = 0 nB = 0
......
__version__ = "0.1.17" __version__ = "0.1.18.5"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment