Skip to content

Commit

Permalink
Merge pull request #3 from philips-software/bugfix
Browse files Browse the repository at this point in the history
Bugfix
  • Loading branch information
BrijeshKrishnan committed Aug 12, 2020
2 parents 0f7478a + ad4b59a commit 7879705
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 140 deletions.
90 changes: 84 additions & 6 deletions README.md
Expand Up @@ -58,6 +58,24 @@ out_put = core_extractor.extractor (r"path_to_repo/code")
print(out_put)
```

- To exclude specific files from repository.

```sh
from functiondefextractor import core_extractor
out_put = core_extractor.extractor (r"path_to_repo/code",
regex_pattern=r'*\test\*, *.java')
print(out_put)
```

Sample regex patterns: (Note: replace # with *)

1. '#.java' => to exclude all java files in a repository.

2. '#/test/#' => to exclude test folder and files in it.

3. '#/src/#/*.cpp' => to exclude all cpp files in src and
it's sub directories

- To extract functions based on annotation.

```sh
Expand All @@ -81,10 +99,10 @@ print(out_put)
For example to search assert, suppress warnings patterns.

```sh
from functiondefextractor import core_extractor
from functiondefextractor import condition_checker
out_put = core_extractor.check_condition
("@SupressWarning", r"path_to_excelfile/dataframe", "(")
print(out_put)
print(out_put[0], out_put[1])
```

### Commandline
Expand All @@ -95,11 +113,18 @@ print(out_put)
>>>python -m functiondefextractor.extractor_cmd --p path/to/repo
```

- To ignore files from repo using regex pattern.

```sh
>>>python -m functiondefextractor.extractor_cmd --p path/to/repo
--i '*.java, *.cpp'
```

- To analyse various patterns in the code based on given condition.

```sh
>>>python -m functiondefextractor.extractor_cmd
--c "@Assert" --e path/to/excel/dataframe --s "("
--c "Assert" --e path/to/excel --s "("
```

- Help option can be found at,
Expand All @@ -108,6 +133,58 @@ print(out_put)
>>>python -m functiondefextractor.extractor_cmd -h
```

### Sample use cases

- To extract all functions from a repository

```sh
>>>python -m functiondefextractor.extractor_cmd --p path/to/repo
```

```sh
from functiondefextractor import core_extractor
out_put = core_extractor.extractor (r"path_to_repo/code")
print(out_put)
```

- To extract all functions with "@Test" annotation
excluding all ".cpp" files in the repository

```sh
>>>python -m functiondefextractor.extractor_cmd --p path/to/repo
--a "@Test" --i '*.cpp'
```

```sh
from functiondefextractor import core_extractor
out_put = core_extractor.extractor
(r"path_to_repo/code", annot="@Test", regex_pattern=r'*.cpp')
print(out_put)
```

Note:

1. functionstartwith argument can be used to specifically extract code
from required functions whose names starts with "test_" or what ever name
user is interested in.

2. delta and annot arguments together can be used to extract required number
of lines below and above the given annotation/keyword.

- To analyze various patterns present in extracted code

```sh
>>>python -m functiondefextractor.extractor_cmd
--c "Assert" --e path/to/excel --s "("
```

```sh
from functiondefextractor import condition_checker
out_put = core_extractor.check_condition
("@SupressWarning", r"path_to_excelfile/dataframe", "(")
print(out_put[0], out_put[1])
```

### Output

- Executing functiondefextractor to extract functions from
Expand All @@ -117,9 +194,10 @@ print(out_put)
- Using functiondefextractor to extract functions from code would return
a dataframe with same content as excel file.

- When functiondefextractor is executed to analyse patterns in code, an excel file
with multiple sheets would be generated which contains the requested patterns and
pivot table. Also an html file with pivot table of the same would be generated.
- When functiondefextractor is executed from script to analyse patterns in code,
a tuple with 2 data frames would be generated which contains the requested pattern
statements with their count in various functions and a pivot table of the
same respectively.

## Contact

Expand Down
2 changes: 1 addition & 1 deletion build_scripts/dependencies_static_analysis_test_cov.py
Expand Up @@ -90,7 +90,7 @@ def check_dead_code():
"""
checks the repo for dead code with minimum confidence 100
"""
call_subprocess("python3 -m vulture --min-confidence 60 "
call_subprocess("python -m vulture --min-confidence 60 "
"functiondefextractor test build_scripts whitelist.py")
print("Stage dead code detection -- COMPLETED & PASSED --")

Expand Down
28 changes: 16 additions & 12 deletions functiondefextractor/condition_checker.py
Expand Up @@ -21,18 +21,18 @@ def check_condition(condition, file_path_dataframe, splitter=None):
test_assert = condition
if ['Uniq ID'] not in data.columns.ravel():
return "Couldn't find Uniq ID column"
data = pd.DataFrame(data, columns=['Uniq ID', 'Code']).set_index("Uniq ID")
data = pd.DataFrame(data, columns=['Uniq ID', 'Code'])
specifier_column = []
spe_data = ""
for i in range(len(data)):
for line in str(data.iat[i, 0]).splitlines():
for line in str(data.iat[i, 1]).splitlines():
if test_assert.upper() in line.strip().upper():
spe_data = spe_data + line.strip() + os.linesep
specifier_column.append(spe_data)
spe_data = ""
data['Count of %s in function' % test_assert] = data["Code"].str.upper().str.count(test_assert.upper())
data["%s Statements" % test_assert] = specifier_column
get_pivot_table_result(data, test_assert, splitter, file_path_dataframe)
return get_pivot_table_result(data, test_assert, splitter, file_path_dataframe)


def get_pivot_table_result(data, test_assert, splitter, file_path):
Expand All @@ -47,16 +47,20 @@ def get_pivot_table_result(data, test_assert, splitter, file_path):
data["%s Statements" % test_assert] = data["%s Statements" % test_assert].apply(lambda x: x.split(splitter)[0])
data_table = data.groupby("%s Statements" % test_assert).count().iloc[:, 1]
data_table = data_table.to_frame()
data_table = data_table.rename({'Count of %s in function' % test_assert:
'Different %s pattern counts' % test_assert}, axis='columns')
data_table = data_table.rename({'Code': 'Different %s pattern counts' % test_assert}, axis='columns')
data_table = data_table.reset_index()
data_table["%s Statements" % test_assert] = data_table["%s Statements" % test_assert].str.wrap(200)
if data_table.iat[0, 0] == '': # pragma: no mutate
data_table = data_table.drop([data_table.index[0]])
html_file_path = os.path.join(os.path.dirname(file_path), 'Pivot_table_%s.html') % test_assert.strip("@")
writer = pd.ExcelWriter(os.path.join(os.path.dirname(file_path), 'Pattern_Result_%s.xlsx')
% test_assert.strip("@"), engine='xlsxwriter')
data.to_excel(writer, sheet_name='Data') # pragma: no mutate
data_table.to_excel(writer, sheet_name='Pivot Table') # pragma: no mutate
data_table.to_html(html_file_path)
writer.save()
if str(type(file_path)) != "<class 'pandas.core.frame.DataFrame'>":
html_file_path = os.path.join(os.path.dirname(file_path), 'Pivot_table_%s.html') % test_assert.strip("@")
writer = pd.ExcelWriter(os.path.join(os.path.dirname(file_path), 'Pattern_Result_%s.xlsx')
% test_assert.strip("@"), engine='xlsxwriter')
data.to_excel(writer, sheet_name='Data') # pragma: no mutate
data_table.to_excel(writer, sheet_name='Pivot Table') # pragma: no mutate
data_table.to_html(html_file_path)
writer.save()
ret_val = "Report files successfully generated at input path"
else:
ret_val = data, data_table
return ret_val

0 comments on commit 7879705

Please sign in to comment.