Skip to content

Commit

Permalink
Som devel (#3)
Browse files Browse the repository at this point in the history
* added -o option; wait for somcpp fio open

* interim

* utilizing fio open

* py -o option

* py -o option

* py -o option
  • Loading branch information
sorgom authored Nov 7, 2024
1 parent c0719f9 commit 76d4f53
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .gitHooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ done

if [[ -n $txts ]]; then
echo cleaning text files
$py $sompy/cleanTxt.py $txts
$py $sompy/cleanTxt.py -l $txts
fi
for file in $txts; do git add $file; done
27 changes: 14 additions & 13 deletions code/Covbr2Html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <SOM/TraceMacros.h>

using py::repl;
using std::regex, std::regex_replace, std::regex_search;
using std::regex, std::regex_search;
using std::string;
using fpath = std::filesystem::path;

bool Covbr2Html::convert(const string& covbrTxt, const bool wb, const bool hc)
bool Covbr2Html::convert(const string& covbrTxt, const std::string& odir, const bool wb, const bool hc)
{
#define C_BEGIN "(?:^|(\\n))"
#define C_ECLIP "(?: +\\.\\.\\.\\n)?"
Expand Down Expand Up @@ -68,6 +68,11 @@ bool Covbr2Html::convert(const string& covbrTxt, const bool wb, const bool hc)
const bool ok = read(buff, covbrTxt);
if (ok and regex_search(buff, reFile))
{
const bool fWb = not odir.empty();
const auto opath = fWb ? fpath(odir) : fpath(covbrTxt).parent_path();
const auto fname = fpath(covbrTxt).filename().string();
std::ofstream os;

string rep;
{
TRACE_FLOW_TIME(clean txt)
Expand All @@ -76,16 +81,15 @@ bool Covbr2Html::convert(const string& covbrTxt, const bool wb, const bool hc)
// if anything left
if (regex_search(rep, reFile))
{
// write text file if changed
if (wb and rep != buff)
// write text file if changed or output directory specified
if (wb and (fWb or rep != buff))
{
TRACE_FLOW_TIME(re-write source)
std::ofstream os(covbrTxt);
if (checkos(os, covbrTxt))
if (open(os, opath / fname))
{
os << rep;
os.close();
}
os.close();
}
{
TRACE_FLOW_TIME(convert to html)
Expand All @@ -103,14 +107,12 @@ bool Covbr2Html::convert(const string& covbrTxt, const bool wb, const bool hc)
// write html file
{
TRACE_FLOW_TIME(write html)
const string ttl = repl(reExt, "", fpath(covbrTxt).filename().string());
const string covbrHtml = repl(reExt, ".html", covbrTxt);
std::ofstream os(covbrHtml);
if (checkos(os, covbrHtml))
const string ttl = repl(reExt, "", fname);
if (open(os, opath / repl(reExt, ".html", fname)))
{
os << cTtl << ttl << cHead << rep << cTail;
os.close();
}
os.close();
}
}
}
Expand Down Expand Up @@ -144,4 +146,3 @@ const CONST_C_STRING Covbr2Html::cHead =
"<p>";

const CONST_C_STRING Covbr2Html::cTail = "</p></body></html>\n";

3 changes: 1 addition & 2 deletions code/Covbr2Html.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Covbr2Html
// covbrTxt: source file name
// wb: write back cleaned sources
// hc: highlight covered parts
static bool convert(const std::string& covbrTxt, bool wb = false, bool hc = false);
static bool convert(const std::string& covbrTxt, const std::string& odir, bool wb = false, bool hc = false);

private:
static const CONST_C_STRING cTtl;
Expand All @@ -28,4 +28,3 @@ class Covbr2Html
};

#endif // _H

37 changes: 30 additions & 7 deletions code/CovbrGlobber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,47 @@
#include <Covbr2Html.h>
#include <SOM/TraceMacros.h>
#include <iostream>
#include <filesystem>

void CovbrGlobber::process(const CONST_C_STRING item)
{
mThreads.push_back(std::thread(&CovbrGlobber::threadFunc, this, std::string(item)));
if (mOk) mThreads.push_back(std::thread(&CovbrGlobber::threadFunc, this, std::string(item)));
}

bool CovbrGlobber::setOdir(const CONST_C_STRING odir)
{
mOk = true;
mOdir.clear();
try
{
std::filesystem::create_directories(odir);
mOdir = odir;
}
catch(const std::filesystem::filesystem_error&)
{
mOk = false;
mRet = 1;
std::cerr << "cannot create directory " << odir << '\n';
}
return mOk;
}

INT32 CovbrGlobber::ret()
{
TRACE_FUNC_TIME()
for (auto& th : mThreads) th.join();
mThreads.clear();
if (_ret != 0) std::cerr << _ret << " errors\n";
return _ret;
join();
if (mRet != 0) std::cerr << mRet << " errors\n";
return mRet;
}

void CovbrGlobber::threadFunc(const std::string&& file)
{
TRACE_FUNC_TIME()
if (not Covbr2Html::convert(file, mWb, mHc)) ++_ret;
if (not Covbr2Html::convert(file, mOdir, mWb, mHc)) ++mRet;
}

void CovbrGlobber::join()
{
TRACE_FUNC_TIME()
for (auto& th : mThreads) th.join();
mThreads.clear();
}
21 changes: 15 additions & 6 deletions code/CovbrGlobber.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,41 @@ class CovbrGlobber : public I_GlobProcessor
public:
CovbrGlobber() = default;

inline ~CovbrGlobber()
{
join();
}

// I_GlobProcessor
void process(CONST_C_STRING item) override;

// set write back input files
void setWb(bool wb = true)
inline void setWb(bool wb = true)
{
mWb = wb;
}
void setHc(bool hc = true)
// set highlight covered items
inline void setHc(bool hc = true)
{
mHc = hc;
}
// set output directory
bool setOdir(const CONST_C_STRING odir);

// wait for all threads to finish
// return: number of failed conversions
// return: number of failures
// -> end of main return
INT32 ret();
private:
std::vector<std::thread> mThreads;
std::atomic<INT32> _ret = 0;
std::atomic<INT32> mRet = 0;
bool mWb = false;
bool mHc = false;
std::string mOdir;
bool mOk = true;
void threadFunc(const std::string&& file);

void join();
NOCOPY(CovbrGlobber)
};

#endif // _H

14 changes: 9 additions & 5 deletions code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

const CONST_C_STRING cOpts =
"options:\n"
"-o <directory> output to directory\n"
"-c highlight covered items\n"
"-w write back cleaned covbr text files\n"
"-w write cleaned covbr text files\n"
"-h this help\n"
;

Expand All @@ -26,7 +27,7 @@ void help(const CONST_C_STRING arg)
INT32 main(const INT32 argc, const CONST_C_STRING* const argv)
{
TRACE_FUNC_TIME()
INT32 ret = 1;
auto ret = 1;
DocOpts opts;
if (opts.process(cOpts, argc, argv))
{
Expand All @@ -40,13 +41,16 @@ INT32 main(const INT32 argc, const CONST_C_STRING* const argv)
CovbrGlobber globber;
globber.setWb(opts.isSet('w'));
globber.setHc(opts.isSet('c'));
for (INT32 i = 0; i < opts.argc(); ++i)
CONST_C_STRING odir = nullptr;
if ((not opts.getValue(odir, 'o')) or globber.setOdir(odir))
{
fglob(opts.args()[i], globber);
for (auto i = 0; i < opts.argc(); ++i)
{
fglob(opts.args()[i], globber);
}
}
ret = globber.ret();
}
}
return ret;
}

67 changes: 39 additions & 28 deletions py/covbr2html.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""
cleans covbr reports from fully covered files and creates html reports
clean covbr reports from fully covered files and create html reports
Usage: this script [options] files
usage: this script [options] files
options:
-o <directory> output directory
-w re-write cleaned covbr text files
-c highlight covered items
-h help
"""

import re
from os.path import isfile, dirname, basename
from os.path import dirname, basename, join, isdir
from os import makedirs
from html import escape
import sompy
# due to usage of match case
Expand All @@ -18,12 +20,22 @@

class Covbr2html(object):
"""covbr cleaner and html converter"""
def __init__(self, wb:bool=False, hc=False) -> None:
template = dirname(__file__) + '/covbr_template.html'
def __init__(self, wb:bool=False, hc=False, odir=None) -> None:
template = join(dirname(__file__), 'covbr_template.html')
with open(template, 'r') as fh:
self.template = fh.read()
fh.close()
self.err = False
self.wb = wb
self.odir = odir
if odir:
if not isdir(odir):
try:
makedirs(odir)
except:
print('cannot create output directory:', odir)
self.err = True

if hc:
self.okb = '<i>'
self.oke = '</i>'
Expand All @@ -41,12 +53,16 @@ def __init__(self, wb:bool=False, hc=False) -> None:
self.rx_nok = re.compile(r'^( *)--&gt;(\w+)?( .*)?', re.M)
self.cnt = 0

def write(self, fp:str, oldc:str, newc:str):
if newc != oldc:
with open(fp, 'w') as fh:
fh.write(newc)
fh.close()
self.cnt += 1
def ok(self):
return not self.err

def write(self, fp:str, newc:str):
if self.odir:
fp = join(self.odir, basename(fp))
with open(fp, 'w') as fh:
fh.write(newc)
fh.close()
self.cnt += 1

# indication: not covered
def _replNok(self, mo):
Expand All @@ -71,7 +87,8 @@ def _replOk(self, mo):
return f'{self.okb}{ind}{tag}{line}{self.oke}'

def process(self, fp:str):
"""cleans the txt file writes the html file"""
"""clean txt file write html file"""
if self.err: return
with open(fp, 'r') as fh:
oldc = fh.read()
fh.close()
Expand All @@ -84,31 +101,25 @@ def process(self, fp:str):

if not self.rxFile.search(newc): return

if self.wb:
self.write(fp, oldc, newc)
if self.wb and (self.odir or newc != oldc):
self.write(fp, newc)

# create html
newc = self.rx_ok.sub(self._replOk,
self.rx_nok.sub(self._replNok,
self.rx_fp.sub(r'\n<em>\1</em>\n', escape(newc)))).strip()
nfp = re.sub(r'\.\w+$', '', fp)
ttl = basename(nfp)
nfp = nfp + '.html'
fp = re.sub(r'\.\w+$', '', fp)
ttl = basename(fp)
newc = self.template.replace('##TITLE', ttl, 1).replace('##CONTENT', newc, 1)
oldc = ''
if isfile(nfp):
with open(nfp, 'r') as fh:
oldc = fh.read()
fh.close()
self.write(nfp, oldc, newc)
self.write(fp + '.html', newc)

if __name__ == '__main__':
from docOpts import docOpts
from globify import globify

opts, args = docOpts(__doc__, reqArgs=True)
cb = Covbr2html(wb=opts.get('w', False), hc=opts.get('c', False))
for arg in globify(args):
cb.process(arg)
if cb.cnt: print('>', cb.cnt)

cb = Covbr2html(wb=opts.get('w', False), hc=opts.get('c', False), odir=opts.get('o'))
if cb.ok():
for arg in globify(args):
cb.process(arg)
if cb.cnt: print('>', cb.cnt)
2 changes: 1 addition & 1 deletion somcpp

0 comments on commit 76d4f53

Please sign in to comment.