Problem Compiling C++ Files with Python Script - c++

I'm working on a Python script which does the following:
Changes the names of two folders within the folder called src. Within these 2 folders there are C++ files which need to be compiled. The names of these two folders and the associated C++ files are: addition_calc with add.cpp and multiplication_calc with multiply.cpp.
Once the folders have had their names changed to addition and multiplication and the associated C++ files compiled, they will all appear in a folder called target.
Starting project structure:
main.py
src/
addition_calc/
add.cpp
multiplication_calc/
multiply.cpp
On running the application:
$ python3 main.py src target
Output project structure (which additionally includes the starting project): Note: I've included the C++ exe files as they are desired
target/
metadata.json
addition/
add.cpp
add
multiplication/
multiplication.cpp
multiplication
The two C++ files:
add.cpp
#include<iostream>
double add(double, double);
int main() {
double a, b;
std::cout<<"Enter two numbers: "; std::cin>>a>>b;
std::cout<<"The sum of these two numbers is: "<<add(a,b)<<std::endl;
}
double add(double a, double b) {
return a + b;
}
multiply.cpp
#include <iostream>
double multiply(double, double);
int main() {
double a, b;
std::cout<<"Enter two numbers: "; std::cin>>a>>b;
std::cout<<"Multiplication of those two numbers is "<<multiply(a,b)<<std::endl;
}
double multiply(double a, double b) {
return a * b;
}
The python script:
import json
import shutil
from subprocess import PIPE, run
import sys
CALC_DIR_PATTERN = "calc"
CALC_CODE_EXTENSION = ".cpp"
#Note: tested using Golang: CALC_COMPILE_COMMAND = ["go", "build"]
#CALC_COMPILE_COMMAND = ["g++", "add.cpp", "multiply.cpp", "-o", "main"]
#testing with one `C++` file:
CALC_COMPILE_COMMAND = ['g++', '-o', 'add.cpp', 'add']
def find_all_calc_paths(source):
calc_paths = []
for root, dirs, files in os.walk(source):
for directory in dirs:
if CALC_DIR_PATTERN in directory.lower():
path = os.path.join(source, directory)
calc_paths.append(path)
break
return calc_paths
def get_name_from_paths(paths, to_strip):
new_names = []
for path in paths:
_, dir_name = os.path.split(path)
new_dir_name = dir_name.replace(to_strip, "")
new_names.append(new_dir_name)
return new_names
def create_dir(path):
if not os.path.exists(path):
os.mkdir(path)
def copy_and_overwrite(source, dest):
if os.path.exists(dest):
shutil.rmtree(dest)
shutil.copytree(source, dest)
def make_json_metadata_file(path, calc_dirs):
data = {
"C++_Folder_Names": calc_dirs,
"Number_Of_Folders": len(calc_dirs)
}
with open(path, "w") as f:
json.dump(data, f)
def compile_calc_code(path):
code_file_name = None
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(CALC_CODE_EXTENSION):
code_file_name = file
break
break
if code_file_name is None:
return
command = CALC_COMPILE_COMMAND + [code_file_name]
run_command(command, path)
def run_command(command, path):
cwd = os.getcwd()
os.chdir(path)
result = run(command, stdout=PIPE, stdin=PIPE, universal_newlines=True)
print("compile result", result)
os.chdir(cwd)
def main(source, target):
cwd = os.getcwd()
source_path = os.path.join(cwd, source)
target_path = os.path.join(cwd, target)
calc_paths = find_all_calc_paths(source_path)
new_calc_dirs = get_name_from_paths(calc_paths, "_calc")
create_dir(target_path)
for src, dest in zip(calc_paths, new_calc_dirs):
dest_path = os.path.join(target_path, dest)
copy_and_overwrite(src, dest_path)
compile_calc_code(dest_path)
json_path = os.path.join(target_path, "metadata.json")
make_json_metadata_file(json_path, new_calc_dirs)
if __name__ == "__main__":
args = sys.argv
if len(args) != 3:
raise Exception("You must pass a source and target directory - only.")
source, target = args[1:]
main(source, target)
Errors generated
g++: error: multiply.cpp: No such file or directory
compile result CompletedProcess(args=['g++', 'add.cpp', 'multiply.cpp', '-o', 'main', 'add.cpp'], returncode=1, stdout='')
g++: error: add.cpp: No such file or directory
compile result CompletedProcess(args=['g++', 'add.cpp', 'multiply.cpp', '-o', 'main', 'multiply.cpp'], returncode=1, stdout='')
Note: contents of json file not included; all is fine with this.
Compiler: g++ 9.4.0.
Python: 3.8.10.
OS: Ubuntu 20.04.
So clearly a problem with the compiling stage of this but I can't see how to resolve it. Cheers.

Related

Is there a way to update the value of variable inside a dynamic library (in real time)?

I have a dynamic shared library built that contains a simple struct with variables inside of it. My main program is in C++ and this program changes the values of the struct in real-time.
I want a python script to make use of the updated values in real-time.
However, the current python script (using ctypes) does not get the updated values.
Is there a way to make this happen? Is it possible to do so?
Current python looks like this (script.py):
#!/usr/bin/env python3
import sys
import ctypes
# pulls library made from c++
lib = ctypes.cdll.LoadLibrary('./libsharedObject.so')
# init constructor
lib.object_new.argtypes = []
lib.object_new.restype = ctypes.c_void_p
# init do_something function
lib.object_do_something.argtypes = [ctypes.c_void_p]
lib.object_do_something.restype = ctypes.c_float
class Object:
def __init__(self):
self.obj = lib.object_new()
print("`Object` instance (as a `void *`): 0x{:016X}".format(self.obj))
def do_something(self):
return lib.object_do_something(self.obj)
def main():
print("Testing Library...")
obj = Object()
ret = obj.do_something()
print(ret)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
shared library file (sharedObject.h):
// Data Structure for Raspberry Pi
struct UAVTlm_t {
float distance; // distance
UAVTlm_t(){
distance = 7.12;
}
float do_something();
};
sharedObject.cpp
#include "sharedObject.h"
struct UAVTlm_t;
float UAVTlm_t::do_something() {
return distance;
}
extern "C" {
UAVTlm_t *object_new(){
return new UAVTlm_t;
}
float object_do_something(UAVTlm_t *pObj) {
return pObj->do_something();
}
}
The library is built with the following line:
$ g++ -shared -fPIC -o libsharedObject.so sharedObject.cpp -lstdc++

How to build static library from the Generated source files using Bazel Build

I am trying to make a following setup run with Bazel. With calling “bazel build” a Python script should generate unknown number of *.cc files with random names, and then compile these into single static library (.a file), all within one Bazel call. I have tried following: one generated file has fixed name, this one is referenced in outs of genrule() and srcs of cc_library rule. The problem is I need all generated files to be built as a library, not only the file with fixed name. Any ideas how to do this?
My BUILD file:
py_binary(
name = "sample_script",
srcs = ["sample_script.py"],
)
genrule(
name = "sample_genrule",
tools = [":sample_script"],
cmd = "$(location :sample_script)",
outs = ["cpp_output_fixed.cc"], #or shall also the files with random names be defined here?
)
cc_library(
name = "autolib",
srcs = ["cpp_output_fixed.cc"],
#srcs = glob([ #here should all generated .cc files be named
# "./*.cc",
# "./**/*.cc",
# ])+["cpp_output_fixed.cc"],
)
Python file sample_script.py:
#!/usr/bin/env python
import hashlib
import time
time_stamp = time.time()
time_1 = str(time_stamp)
time_2 = str(time_stamp + 1)
random_part_1 = hashlib.sha1(time_1).hexdigest()[-4:]
random_part_2 = hashlib.sha1(time_1).hexdigest()[-4:]
fixed_file = "cpp_output_fixed" + ".cc"
file_1 = "cpp_output_" + random_part_1 + ".cc"
file_2 = "cpp_output3_" + random_part_2 + ".cc"
with open(fixed_file, "w") as outfile:
outfile.write("#include <iostream>"
"int main() {"
" std::cout <<'''Hello_world''' <<std::endl;"
" return 0"
"}")
with open(file_1, "w") as outfile:
outfile.write("#include <iostream>"
"int main() {"
" std::cout <<'''Hello_world''' <<std::endl;"
" return 0"
"}")
with open(file_2, "w") as outfile_new:
outfile_new.write("#include <iostream>"
"int main() {"
" std::cout <<'''Hello_world''' <<std::endl;"
" return 0"
"}")
print ".cc generation DONE"
[big edit, since I found a way to make it work :)]
If you really need to emit files that are unknown at the analysis phase, your only way is what we internally call tree artifacts. You can think of it as a directory that contains files that will only be inspected at the execution phase. You can declare a tree artifact from Skylark using ctx.actions.declare_directory.
Here is a working example. Note 3 things:
we need to add ".cc" to the directory name to fool C++ rules that this is valid input
the generator needs to create the directory that bazel tells it to
you need to use bazel#HEAD (or bazel 0.11.0 and later)
genccs.bzl:
def _impl(ctx):
tree = ctx.actions.declare_directory(ctx.attr.name + ".cc")
ctx.actions.run(
inputs = [],
outputs = [ tree ],
arguments = [ tree.path ],
progress_message = "Generating cc files into '%s'" % tree.path,
executable = ctx.executable._tool,
)
return [ DefaultInfo(files = depset([ tree ])) ]
genccs = rule(
implementation = _impl,
attrs = {
"_tool": attr.label(
executable = True,
cfg = "host",
allow_files = True,
default = Label("//:genccs"),
)
}
)
BUILD:
load(":genccs.bzl", "genccs")
genccs(
name = "gen_tree",
)
cc_library(
name = "main",
srcs = [ "gen_tree" ]
)
cc_binary(
name = "genccs",
srcs = [ "genccs.cpp" ],
)
genccs.cpp
#include <fstream>
#include <sys/stat.h>
using namespace std;
int main (int argc, char *argv[]) {
mkdir(argv[1], S_IRWXU);
ofstream myfile;
myfile.open(string(argv[1]) + string("/foo.cpp"));
myfile << "int main() { return 42; }";
return 0;
}
1) List all output files.
2) Use the genrule as a dependency to the library.
genrule(
name = "sample_genrule",
tools = [":sample_script"],
cmd = "$(location :sample_script)",
outs = ["cpp_output_fixed.cc", "cpp_output_0.cc", ...]
)
cc_library(
name = "autolib",
srcs = [":sample_genrule"],
)

Search the last modified ".mxd" files in directory and sub-directory- Python Error

I try to find the last modified files which end with ".mxd" in directory and sub-directory and print the modified time, using this code:
import os
max_mtime = 0
for dirname,subdirs,files in os.walk(r"G:\desktop\Project"):
for fname in files:
if fname.endswith(".mxd"):
full_path = os.path.join(dirname, fname)
mtime = os.stat(full_path).st_mtime
if mtime > max_mtime:
max_mtime = mtime
max_dir = dirname
max_file = fname
print os.path.getatime(fname)
print max_dir, max_file
but when i run this code it raise an error and i don't understand what is my mistake:
WindowsError: [Error 2] : 'project.mxd'
I red How to get file creation & modification date/times in Python? but didn't found any way to solve my problem.
finaly, this code worked for well:
import os,time,datetime,glob
path = r"G:\desktop\Project"
for dirname,subdirs,files in os.walk(path):
max_mtime = 0
max_dir = ""
max_file =""
for fname in files:
mtime=0
if fname.endswith(".mxd"):
full_path = os.path.join(dirname, fname)
mtime = os.stat(full_path).st_mtime
if mtime > max_mtime:
max_mtime = mtime
max_dir = dirname
max_file = fname
print max_dir, max_file
print

how a program will take an input phrase and synthesise it with pauses according to the wav files

i have tried to take the input phrase and synthesise it with the .wav files given in the folder. if the input phrase has a comma, then it will start after 300ms, if we have a period or question mark, it will start after 400ms.
Started with this code, I don't have an idea how to initiate, join the phrase and play the wav files.
parser.add_argument('--play', '-a', action="store", default=False)
args = parser.parse_args()
class Synthesis(object):
def __init__(self, wav_folder):
self.phones = {}
self.get_wavs(wav_folder)
def get_wavs(self, wav_folder):
for root, dirs, files in os.walk(wav_folder, topdown=False):
for file in files:
filesplit = file.split('.')
filename = filesplit[0]
self.phones[filename] = AS.Audio()
self.phones[filename].load(root + '/' + file)
def punctuation(phrase):
concate_punc = []
for word in phrase:
phn_ex= re.match(r'[a-z]+\|,|[.]|!|[?]', word)
phone_exception = phn_ex.group(0)
print phone_exception
if __name__ == "__main__":
S = Synthesis(wav_folder=args.phones)
output = AS.Audio(rate=48000)

Dll load failed, in python 2.7 running on windows 8.1

I'am using Boneh-Lynn-Shacham Identity Based Signature scheme for my final year project for getting encryption keys
from charm.toolbox.pairinggroup import *
from charm.engine.util import *
debug = False
class IBSig():
def __init__(self, groupObj):
global group
group = groupObj
def dump(self, obj):
ser_a = serializeDict(obj, group)
return str(pickleObject(ser_a))
def keygen(self, secparam=None):
g, x = group.random(G2), group.random()
g_x = g ** x
pk = { 'g^x':g_x, 'g':g, 'identity':str(g_x), 'secparam':secparam }
sk = { 'x':x }
return (pk, sk)
def sign(self, x, message):
M = self.dump(message)
if debug: print("Message => '%s'" % M)
return group.hash(M, G1) ** x
def verify(self, pk, sig, message):
M = self.dump(message)
h = group.hash(M, G1)
if pair(sig, pk['g']) == pair(h, pk['g^x']):
return True
return False
def main():
groupObj = PairingGroup('../param/d224.param')
m = { 'a':"hello world!!!" , 'b':"test message" }
bls = IBSig(groupObj)
(pk, sk) = bls.keygen(0)
sig = bls.sign(sk['x'], m)
if debug: print("Message: '%s'" % m)
if debug: print("Signature: '%s'" % sig)
assert bls.verify(pk, sig, m)
if debug: print('SUCCESS!!!')
if __name__ == "__main__":
debug = True
main()
when I am implementing it in python the code was not able to find the module named pairing though I have added Charm module to my library.Getting error like
Traceback (most recent call last):
File "C:\Users\Sailesh\Desktop\bls.py", line 1, in <module>
from charm.toolbox.pairinggroup import *
File "C:\Python27\lib\charm\toolbox\pairinggroup.py", line 2, in <module>
from charm.core.math.pairing import serialize
ImportError: DLL load failed: The specified module could not be found.
I have taken the code from
Boneh-Lynn-Shacham Identity Based Signature code and downloaded the module charm from charm module link. Let me know where is the error or whether the
problem is with the module. I cant figure out what is the problem. Thanks in advance.
Try the 0.43 version directly fron github:
https://github.com/JHUISI/charm/releases