Skip to content

Commit 8e8185e

Browse files
authored
Merge pull request #336 from kjnh10/feature/muti-includepath_for_bundling_c++
Add multi include path feature for bundling c++ code
2 parents 320233e + 7089fac commit 8e8185e

File tree

9 files changed

+23
-18
lines changed

9 files changed

+23
-18
lines changed

onlinejudge_bundle/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def main(args: Optional[List[str]] = None) -> None:
2222
# parse command-line arguments
2323
parser = argparse.ArgumentParser()
2424
parser.add_argument('path', type=pathlib.Path)
25-
parser.add_argument('-I', metavar='dir', type=pathlib.Path, dest='iquote', default=pathlib.Path.cwd(), help='add the directory dir to the list of directories to be searched for header files during preprocessing (default: ".")')
25+
parser.add_argument('-I', metavar='dir', action='append', type=pathlib.Path, dest='iquote', default=[pathlib.Path.cwd()], help='add the directory dir to the list of directories to be searched for header files during preprocessing (default: ".")')
2626
parsed = parser.parse_args(args)
2727

2828
language = onlinejudge_verify.languages.list.get(parsed.path)
2929
assert language is not None
30-
sys.stdout.buffer.write(language.bundle(parsed.path, basedir=parsed.iquote))
30+
sys.stdout.buffer.write(language.bundle(parsed.path, basedir=parsed.iquote[0], options={'include_paths': parsed.iquote}))
3131

3232

3333
if __name__ == "__main__":

onlinejudge_verify/documentation/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _render_source_code_stat(stat: SourceCodeStat, *, basedir: pathlib.Path) ->
6868
try:
6969
language = onlinejudge_verify.languages.list.get(stat.path)
7070
assert language is not None
71-
bundled_code = language.bundle(stat.path, basedir=basedir).decode()
71+
bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
7272
except Exception:
7373
logger.warning("failed to bundle: %s", str(stat.path))
7474
bundled_code = traceback.format_exc()

onlinejudge_verify/languages/cplusplus.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ def list_dependencies(self, path: pathlib.Path, *, basedir: pathlib.Path) -> Lis
186186
joined_CXXFLAGS = ' '.join(map(shlex.quote, [*env.CXXFLAGS, '-I', str(basedir)]))
187187
return _cplusplus_list_depending_files(path.resolve(), CXX=env.CXX, joined_CXXFLAGS=joined_CXXFLAGS)
188188

189-
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path) -> bytes:
190-
bundler = Bundler(iquotes=[basedir])
189+
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path = pathlib.Path.cwd(), options: Dict[str, Any]) -> bytes:
190+
include_paths: List[pathlib.Path] = options['include_paths']
191+
assert isinstance(include_paths, list)
192+
bundler = Bundler(iquotes=include_paths)
191193
bundler.update(path)
192194
return bundler.get()
193195

onlinejudge_verify/languages/csharpscript.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def list_attributes(self, path: pathlib.Path, *, basedir: pathlib.Path) -> Dict[
109109
def list_dependencies(self, path: pathlib.Path, *, basedir: pathlib.Path) -> List[pathlib.Path]:
110110
return list(_get_csx_dependencies(path.resolve()))
111111

112-
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path) -> bytes:
112+
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path, options: Dict[str, Any]) -> bytes:
113113
raise NotImplementedError
114114

115115
def list_environments(self, path: pathlib.Path, *, basedir: pathlib.Path) -> Sequence[CSharpScriptLanguageEnvironment]:

onlinejudge_verify/languages/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def list_dependencies(self, path: pathlib.Path, *, basedir: pathlib.Path) -> Lis
4040
raise NotImplementedError
4141

4242
@abc.abstractmethod
43-
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path) -> bytes:
43+
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path, options: Dict[str, Any]) -> bytes:
4444
"""
4545
:throws Exception:
4646
:throws NotImplementedError:

onlinejudge_verify/languages/nim.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def list_dependencies(self, path: pathlib.Path, *, basedir: pathlib.Path) -> Lis
8282
stk.append(child)
8383
return list(set(dependencies))
8484

85-
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path) -> bytes:
85+
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path, options: Dict[str, Any]) -> bytes:
8686
raise NotImplementedError
8787

8888
def is_verification_file(self, path: pathlib.Path, *, basedir: pathlib.Path) -> bool:

onlinejudge_verify/languages/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import textwrap
66
from logging import getLogger
7-
from typing import List, Sequence, Tuple
7+
from typing import Any, Dict, List, Sequence, Tuple
88

99
import importlab.environment
1010
import importlab.fs
@@ -78,7 +78,7 @@ class PythonLanguage(Language):
7878
def list_dependencies(self, path: pathlib.Path, *, basedir: pathlib.Path) -> List[pathlib.Path]:
7979
return _python_list_depending_files(path.resolve(), basedir)
8080

81-
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path) -> bytes:
81+
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path, options: Dict[str, Any]) -> bytes:
8282
"""
8383
:throws NotImplementedError:
8484
"""

onlinejudge_verify/languages/user_defined.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def list_dependencies(self, path: pathlib.Path, *, basedir: pathlib.Path) -> Lis
6363
dependencies.append(pathlib.Path(line.decode()))
6464
return dependencies
6565

66-
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path) -> bytes:
66+
def bundle(self, path: pathlib.Path, *, basedir: pathlib.Path, options: Dict[str, Any]) -> bytes:
6767
assert 'bundle' in self.config
6868
command = self.config['bundle'].format(path=str(path), basedir=str(basedir))
6969
logger.info('$ %s', command)

tests/test_bundle.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_smoke(self) -> None:
116116
self.assertEqual(subprocess.check_output([str(pathlib.Path('a.out').resolve())]), ('Hello World' + os.linesep).encode())
117117

118118
def test_complicated(self) -> None:
119-
library_files = {
119+
library_files_1 = {
120120
pathlib.Path('library', 'macro.hpp'): textwrap.dedent("""\
121121
#pragma once
122122
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
@@ -125,6 +125,8 @@ def test_complicated(self) -> None:
125125
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
126126
#define ALL(x) std::begin(x), std::end(x)
127127
""").encode(),
128+
}
129+
library_files_2 = {
128130
pathlib.Path('library', 'fibonacci.hpp'): textwrap.dedent("""\
129131
#pragma once
130132
#include <cstdint>
@@ -161,12 +163,13 @@ def test_complicated(self) -> None:
161163
with tempfile.TemporaryDirectory() as tempdir_dst_:
162164
tempdir_dst = pathlib.Path(tempdir_dst_)
163165

164-
with tests.utils.load_files_pathlib(library_files) as tempdir_src_library:
165-
with tests.utils.load_files_pathlib(test_files) as tempdir_src_test:
166-
with open(tempdir_dst / 'main.bundled.cpp', 'w') as fh:
167-
with contextlib.redirect_stdout(fh):
168-
args = ['-I', str(tempdir_src_library), str(tempdir_src_test / 'test' / 'main.cpp')]
169-
onlinejudge_bundle.main.main(args=args)
166+
with tests.utils.load_files_pathlib(library_files_1) as tempdir_src_library_1:
167+
with tests.utils.load_files_pathlib(library_files_2) as tempdir_src_library_2:
168+
with tests.utils.load_files_pathlib(test_files) as tempdir_src_test:
169+
with open(tempdir_dst / 'main.bundled.cpp', 'w') as fh:
170+
with contextlib.redirect_stdout(fh):
171+
args = ['-I', str(tempdir_src_library_1), '-I', str(tempdir_src_library_2), str(tempdir_src_test / 'test' / 'main.cpp')]
172+
onlinejudge_bundle.main.main(args=args)
170173

171174
# compile
172175
subprocess.check_call(['g++', '-o', str(tempdir_dst / 'a.out'), str(tempdir_dst / 'main.bundled.cpp')], stderr=sys.stderr)

0 commit comments

Comments
 (0)