14 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
15 stdout, stderr = p.communicate()
17 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
18 stdout, stderr = p.communicate(input=data)
20 return stdout.decode(
'utf-8'), stderr.decode(
'utf-8'), p.returncode
24 cmd = [cxx,
'-E',
'-x',
'c++',
'-v',
'/dev/null']
25 stdout, stderr, retCode =
runCmd(cmd)
27 m = re.findall(
'\n (/.*)', stderr)
32 if -1 != x.find(
'(framework directory)'):
35 includes.append(
'-isystem%s' %(x))
41 fileName = match.group(2)
43 cpp =
'<!-- source:%s.cpp -->\n' %(fileName)
45 cpp += open(
'examples/%s.cpp' %(fileName),
'r').read().strip()
47 cpp +=
'<!-- source-end:%s.cpp -->' %(fileName)
55 print(
'Replacing 20 by 2a')
69 return(
'https://cppinsights.io/lnk?code=%s&insightsOptions=%s&rev=1.0' %(base64.b64encode(code).decode(
'utf-8'), options))
73 cppFileName = match.group(2) +
'.cpp'
75 insightsPath = args[
'insights']
76 remainingArgs = args[
'args']
77 defaultCppStd =
'-std=%s'% (args[
'std'])
80 cpp =
'<!-- transformed:%s -->\n' %(cppFileName)
81 cpp +=
'Here is the transformed code:\n'
84 cmd = [insightsPath,
'examples/%s' %(cppFileName),
'--', defaultCppStd,
'-m64']
85 stdout, stderr, retCode =
runCmd(cmd)
89 cppData = open(
'examples/%s' %(cppFileName),
'r').read().strip().encode(
'utf-8')
94 cpp +=
'<!-- transformed-end:%s -->' %(cppFileName)
100 parser = argparse.ArgumentParser(description=
'Description of your program')
101 parser.add_argument(
'--insights', help=
'C++ Insights binary', required=
True)
102 parser.add_argument(
'--cxx', help=
'C++ compiler to used', default=
'/usr/local/clang-current/bin/clang++')
103 parser.add_argument(
'--std', help=
'C++ Standard to used', default=
'c++17')
104 parser.add_argument(
'args', nargs=argparse.REMAINDER)
105 args = vars(parser.parse_args())
107 insightsPath = args[
'insights']
108 remainingArgs = args[
'args']
109 defaultCppStd =
'-std=%s'% (args[
'std'])
114 for f
in os.listdir(
'.'):
115 if not f.startswith(
'opt-')
or not f.endswith(
'.md'):
118 optionName = os.path.splitext(f)[0][4:].strip()
120 data = open(f,
'r').read()
122 cppFileName =
'cmdl-examples/%s.cpp' %(optionName)
124 cpp = open(cppFileName,
'r').read().strip()
126 data = data.replace(
'%s-source' %(optionName), cpp)
128 cmd = [insightsPath, cppFileName,
'--%s' %(optionName),
'--', defaultCppStd,
'-m64']
129 stdout, stderr, retCode =
runCmd(cmd)
131 data = data.replace(
'%s-transformed' %(optionName), stdout)
133 open(f,
'w').write(data)
135 regEx = re.compile(
'(<!-- source:(.*?).cpp -->(.*?)<!-- source-end:(.*?) -->)', re.DOTALL)
136 regExIns = re.compile(
'(<!-- transformed:(.*?).cpp -->(.*?)<!-- transformed-end:(.*?) -->)', re.DOTALL)
138 for f
in os.listdir(
'examples'):
139 if not f.endswith(
'.md'):
142 exampleName = os.path.splitext(f)[0].strip()
144 mdFileName = os.path.join(
'examples',
'%s.md' %(exampleName))
146 mdData = open(mdFileName,
'r').read()
148 mdData = regEx.sub(replaceSource, mdData)
151 mdData = regExIns.sub(rpl, mdData)
153 open(mdFileName,
'w').write(mdData)
def runCmd(cmd, data=None)
def getDefaultIncludeDirs(cxx)
def cppinsightsLink(code, std='2a', options='')
def replaceInsights(match, parser, args)