Skip to content

Commit

Permalink
added test and ready to release
Browse files Browse the repository at this point in the history
  • Loading branch information
kerim371 committed Sep 20, 2021
1 parent 6f4b278 commit 2038539
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 41 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include cppguts/tests/data/*.in
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# cppguts
If your C/C++ project depends on external C/C++ projects and
If your C/C++ project depends on some external C/C++ projects and
you want to make some changes in external functions/methods
and you would like to copy/paste these changes automatically
then this package may help you.
Expand All @@ -13,7 +13,7 @@ We will discuss `editcpp` as it is the objective tool.
**`editcpp` doesn't work with templates.**

## The idea behind `editcpp` tool
`editcpp` tool uses `libclang` to find function/method definition.
`editcpp` uses `libclang` to find function/method definition start/end lines in text file (.c, .h, .hpp, .cpp or whatever extension you use for your C/C++ project).
`libclang` parses each `dest.cpp` and `src.cpp` and everything that is
included by `#include` preprocessor directives. Then `editcpp` tool
selects all functions and methods defined in `dest.cpp` and `src.cpp`
Expand Down Expand Up @@ -127,10 +127,15 @@ namespace ns {
}
}
```
Run: `editcpp --source-file=src.h --dest-file=dest.h --oldfile-keep -std=c++17`
Run:

The `-std=c++17` is simply for illustration but you can pass any clang flag
for example `-I` to include directories that are required by the files.
`editcpp --src-file=src.h --dest-file=dest.h --oldfile-keep -std=c++03`

Another option is to run test:

`python -m unittest cppguts.tests.test_cppguts`

The `-std=c++03` tells the clang to parse the files as C++. Also you may need to use any other clang flags like `-I` to include directories that are required by the files.

`--oldfile-keep` is used to keep the original file (it will be renamed
by adding `_OLD_N` suffix). Otherwise use `--oldfile-delete` to delete the
Expand Down
12 changes: 7 additions & 5 deletions cppguts/editcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ def compare_method_argument_types(node_1: Cursor, node_2: Cursor) -> bool:

def main():
parser = argparse.ArgumentParser(description=
'Replace C++ function/method definitions in destination file '
'Replace C++ function/method definition in destination file '
'(but doesn`t work with templates). '
'One source file may contain several function/method definitions. '
'One source file may contain several functions/methods to replace. '
'After passing `editcpp` flags you are allowed to pass clang '
'commands like `-I` (to include dir), `-std=c++17` and other. '
'Dont pass a file without flag to clang! Use `--dest-file=` instead.')
parser.add_argument('--source-file', dest='srcfile', action='store',
parser.add_argument('--src-file', dest='srcfile', action='store',
type=type('string'), required=True, default=None,
help='file with new functions definitions')
parser.add_argument('--dest-file', dest='destfile', action='store',
Expand Down Expand Up @@ -218,12 +218,14 @@ def main():
method_def_nodes_src = []
find_method_def_nodes(tu_src.cursor, method_def_nodes_src, args.srcfile)
if not method_def_nodes_src:
parser.error("unable to find any method definition in source file:\t" + args.srcfile)
parser.error("unable to find any method definition in source file:\n" +
args.srcfile + "\nprobably you forgot to pass `-std=c++03` (or higher) flag?")

method_def_nodes_dest = []
find_method_def_nodes(tu_dest.cursor, method_def_nodes_dest, args.destfile)
if not method_def_nodes_dest:
parser.error("unable to find any function/method definition in destination file:\t" + args.destfile)
parser.error("unable to find any function/method definition in destination file:\n" +
args.destfile + "\nprobably you forgot to pass `-std=c++11` (or higher) flag?")

# read source file
with open(args.srcfile, mode='r') as file:
Expand Down
52 changes: 52 additions & 0 deletions cppguts/tests/data/tmp/dest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>

// helper class that is used by the target class `Src`
class SrcPrivate {
public:
SrcPrivate(){};

void add(int v){
val += v;
}

void substract(int v){
val -= v;
}

private:
int val;
}

// target class
class Src {
// method defined inside the class
void add(SrcPrivate p, int v){

p.add(v) + 10;

}

// method defined outside the class
void substract(SrcPrivate p, int v);

// we won't tuch this method
void untouched_print(int v){
std::cout << "The value is:\t" << v << std::endl;
}
}

void Src::substract(SrcPrivate p, int v){
p.substract(v) - 10;
}

// simple function
void foo(int &v){
v -= 10;
}

namespace ns {
// function in namespace
void bar(int &v){
v += 10;
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion cppguts/tests/data/src.h → cppguts/tests/data/tmp/src.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void Src::substract(SrcPrivate p, int v){
p.substract(v) - 10;
}

// NEW simple function definition
// NEW simple function definition
void foo(int &v){
v -= 10;
}
Expand Down
50 changes: 23 additions & 27 deletions cppguts/tests/test_cppguts.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
import unittest, pathlib
from pathlib import Path
import shutil
import subprocess, os, filecmp
import unittest


class test_basics(unittest.TestCase):
def setUp(self):
self.srcin = 'data/src.h.in'
self.destin = 'data/dest.h.in'
self.src = 'data/tmp/src.h'
self.dest = 'data/tmp/dest.h'

def tearDown(self):
h5File = self.seisContainer.getH5File()

seisContainer = None
p = h5geo.SeisParam()
FILE_NAME = None
SEIS_NAME1 = None
SEIS_NAME2 = None
this_dir = os.path.dirname(__file__)
data_dir = this_dir + '/data'
tmp_dir = data_dir + '/tmp'
src = tmp_dir + '/src.h'
dest = tmp_dir + '/dest.h'
srcin = data_dir + '/src.h.in'
destin = data_dir + '/dest.h.in'

def test_createContainer(self):
self.assertTrue(os.path.isfile(self.FILE_NAME))

def test_createSeisWithDifferentCreateFlags(self):
seis = self.seisContainer.createSeis(self.SEIS_NAME1, self.p, h5geo.CreationType.OPEN_OR_CREATE)
self.assertFalse(seis is None)
def setUp(self):
shutil.rmtree(self.tmp_dir, ignore_errors=True)
Path(self.tmp_dir).mkdir(parents=True, exist_ok=True)
shutil.copy(self.srcin, self.src)
shutil.copy(self.destin, self.dest)

seis = self.seisContainer.createSeis(self.SEIS_NAME1, self.p, h5geo.CreationType.CREATE_OR_OVERWRITE)
self.assertFalse(seis is None)
# def tearDown(self):
# shutil.rmtree(self.tmp_dir, ignore_errors=True)

seis = self.seisContainer.createSeis(self.SEIS_NAME1, self.p, h5geo.CreationType.CREATE_UNDER_NEW_NAME)
self.assertFalse(seis is None)
def test_basics(self):
subprocess.run(['editcpp', '--src-file', self.src, '--dest-file', self.dest, '--oldfile-keep', '-std=c++03'])

seis = self.seisContainer.createSeis(self.SEIS_NAME1, self.p, h5geo.CreationType.OPEN_OR_CREATE)
self.assertFalse(seis is None)
with open(self.dest) as f:
with open(self.destin) as fin:
self.assertTrue(f != fin)
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build-system]
requires = [
"setuptools>=42",
"wheel"
]
build-backend = "setuptools.build_meta"
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
24 changes: 21 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
from setuptools import setup
import setuptools

setup(
setuptools.setup(
name='cppguts',
version='0.1.0',
packages=['cppguts'],
packages=setuptools.find_packages(),
url='https://github.com/tierra-colada/cppguts',
license='MIT',
author='kerim khemrev',
author_email='[email protected]',
description='python package aimed at C++ source code correction',
download_url='https://github.com/tierra-colada/cppguts/archive/refs/tags/v0.1.0.tar.gz',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'Programming Language :: Python :: 3.6',
'Topic :: Software Development :: Code Generators',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
keywords='c cpp c-parser cpp-parser c-editor cpp-editor c-generator cpp-generator',
entry_points={
'console_scripts': ['editcpp=cppguts.editcpp:main',
'dumpcpp=cppguts.dumpcpp:main']
},
python_requires='>=3',
install_requires=[
'libclang',
],
include_package_data=True # important to copy MANIFEST.in files
)

0 comments on commit 2038539

Please sign in to comment.