def test_GLOB(self): """ test Syntax.GLOB """ #test case of one file files = Syntax.GLOB("./*.cpp") self.assertEqual(files, "hello.cpp") #test case of more files and those files must in the lexicographical order module = self._module Function.RunCommand("touch %s/hello1.h" % module.root_path, ignore_stderr_when_ok = True) Function.RunCommand("touch %s/hello2.h" % module.root_path, ignore_stderr_when_ok = True) Function.RunCommand("touch %s/hello3.h" % module.root_path, ignore_stderr_when_ok = True) Function.RunCommand("touch %s/hello10.h" % module.root_path, ignore_stderr_when_ok = True) files = Syntax.GLOB("./*.h") self.assertEqual(files, "hello.h hello1.h hello10.h hello2.h hello3.h") #test case of files not in self module Function.RunCommand("touch %s/../README" % module.root_path, ignore_stderr_when_ok = True) flag = False try: Syntax.GLOB("../README") except Syntax.NotInSelfModuleError as e: flag = True self.assertTrue(flag) #test case of no such files flag = False try: Syntax.GLOB("./just_test.cpp") except Syntax.BrocArgumentIllegalError as e: flag = True self.assertTrue(flag)
def setUp(self): """ init """ sys.argv = ['NOT PLANISH'] module = BrocModule_pb2.Module() module.name = 'broc' module.module_cvspath = 'baidu/broc' module.broc_cvspath = 'baidu/broc/BROC' module.is_main = True module.repo_kind = BrocModule_pb2.Module.GIT module.revision = "1234" module.last_changed_rev = "1236" module.dep_level = 0 #get home dir home = os.environ['HOME'] module.workspace = '%s/unittest_broc/workspace' % home module.root_path = '%s/unittest_broc/workspace/baidu/broc' % home module.url = 'https://github.com/baidu/broc' module.br_kind = BrocModule_pb2.Module.BRANCH module.br_name = 'trunk' #module.commit_id = '5d9819900c2781873aa0ffce285d5d3e75b072a8' self._module = module Function.RunCommand("mkdir -p %s" % module.root_path, ignore_stderr_when_ok = True) Function.RunCommand("touch %s/hello.cpp" % module.root_path, ignore_stderr_when_ok = True) Function.RunCommand("touch %s/hello.h" % module.root_path, ignore_stderr_when_ok = True) self._env = Environment.Environment(module) Environment.SetCurrent(self._env)
def test_STATIC_LIBRARY(self): """ test Syntax.STATIC_LIBRARY """ #set local flags tag libs = Syntax.Libs("$OUT_ROOT/baidu/broc/libhello.a") cpptags = Syntax.CppFlags("-DBROC", "-DRELEASE") src = Syntax.Sources("hello.cpp") #an error name of application flag = False try: Syntax.STATIC_LIBRARY("^*^&*!*$^", src) except Syntax.BrocArgumentIllegalError as ex: flag = True self.assertTrue(flag) #an error args of application flag = False try: Syntax.STATIC_LIBRARY("hello", src, cpptags) except Syntax.BrocArgumentIllegalError as ex: flag = True self.assertTrue(flag) #Libs Syntax.STATIC_LIBRARY("hello", src, libs) library = self._env.Targets()[0] library.Action() self.assertEqual(library.tag_libs.V(), ["broc_out/baidu/broc/libhello.a"]) #two samename target flag = False try: Syntax.STATIC_LIBRARY("hello", src) except Syntax.BrocArgumentIllegalError as ex: flag = True self.assertTrue(flag) #library DoCopy Function.RunCommand("mkdir -p %s/lib" % self._module.root_path, \ ignore_stderr_when_ok = True) Function.RunCommand("touch %s/lib/libhello1.a" % self._module.root_path, \ ignore_stderr_when_ok = True) now_dir = os.getcwd() os.chdir(self._module.workspace) Syntax.STATIC_LIBRARY("hello1") lib_paths = os.path.join(self._module.workspace, "broc_out", \ self._module.module_cvspath, "output/lib/libhello1.a") self.assertTrue(os.path.exists(lib_paths)) os.chdir(now_dir)
def _download_modules(self): """ dnowload dependent module from repository Returns: download all modules successfully return True, otherwise return False """ for k, node in self.planished_nodes.iteritems(): if node.module.repo_kind == BrocModule_pb2.Module.SVN: # the infos of local code equal Node's info if os.path.exists(node.module.root_path): if BrocTree.BrocTree().SameSvnNode(node): continue else: dst = "%s-%f" % (node.module.root_path, time.time()) self.logger.LevPrint( "WARNING", "local code doesn't match BROC, \ reload it(%s)" % (node.module.origin_config)) Function.MoveFiles(node.module.root_path, dst) # generate command donwloading code from repository cmd = None url = node.module.url if node.module.revision: url = "%s -r %s --force" % (url, node.module.revision) else: url = "%s --force" % url cmd = "svn checkout %s %s" % (url, node.module.root_path) else: # for git cmd = "cd %s" % node.module.module_cvspath if node.module.tag_name: tag_name = node.module.tag_name cmd += " && (git checkout %s || (git fetch origin %s:%s && git checkout %s))" \ % (tag_name, tag_name, tag_name, tag_name) elif node.module.br_name: br_name = node.module.br_name if br_name != "master": cmd += " && (git checkout %s || (git fetch origin %s:%s && git checkout %s))" \ % (br_name, br_name, br_name, br_name) else: cmd += " && git checkout master" self.logger.LevPrint("MSG", "%s" % cmd) ret, msg = Function.RunCommand(cmd) if ret != 0: self.logger.LevPrint("ERROR", "%s failed(%s)" % (cmd, msg)) return False return True
def CalcHeaderFiles(self): """ calculate the header files that source file dependends Returns: { ret : True | False, headers : set(), msg : 'error message' } calculate successfully ret is True; otherwise ret is False and msg contains error message """ result = dict() result['ret'] = False result['headers'] = set() result['msg'] = '' retcode, msg = Function.RunCommand(self._header_cmd, ignore_stderr_when_ok=True) if retcode != 0: result['msg'] = '%s:%s' % (msg, self._header_cmd) return result files = msg.split() for f in files: if f.endswith(".h"): if self.workspace in f: result['headers'].add(f[len(self.workspace) + 1:]) else: result['headers'].add(f) result['ret'] = True return result
def Update(self): """ Update modify time and hash value of cache object """ # update modify time modify_time = None try: modify_time = os.stat(self.pathname).st_mtime except BaseException as err: Log.Log().LevPrint( "ERROR", "update cache(%s) failed: %s" % (self.pathname, err)) return if self.modify_time == modify_time: self.modified = False self.build = False return self.modify_time = modify_time # update hash _hash = Function.GetFileHash(self.pathname) # Log.Log().LevPrint("MSG", "update %s hash id(%s) %s --> %s" % (self.pathname, id(self), self.hash, _hash)) self.hash = _hash self.modified = False self.build = False
def IsModified(self): ''' to check whether object self changed Returns: -1 : the file that cacahe representing is missing 0 : not changed 1 : changed ''' if not os.path.exists(self.pathname): return -1 # check mtime modify_time = None try: modify_time = os.stat(self.pathname).st_mtime except BaseException: Log.Log().LevPrint('MSG', 'get %s modify_time failed' % self.pathname) self.modify_time = 0 return 1 if modify_time == self.modify_time: return 0 else: self.modify_time = modify_time ret = 0 # check hash _hash = Function.GetFileHash(self.pathname) if _hash != self.hash: self.hash = _hash ret = 1 # Log.Log().LevPrint('MSG', '%s content changed' % self.pathname) return ret
def DoCopy(self): """ if .a file has been built already before compile action, just copy it from code directory to output directory Returns: return True if copy success return False if fail to copy """ if len(self.tag_sources.V()): Log.Log().LevPrint( "ERROR", 'StaticLibrary(%s) can not copy because \ its source is not empty' % self.name) return False root, _ = os.path.splitext(self.name) frm = os.path.join(self.env.ModuleCVSPath(), 'lib', 'lib%s%s' % (root, '.a')) to = os.path.join("broc_out", self.env.ModuleCVSPath(), 'output/lib') cmd = "mkdir -p %(to)s && cp -Rp %(frm)s %(to)s" % (locals()) Log.Log().LevPrint('MSG', '[PreCopy] %s' % cmd) ret, msg = Function.RunCommand(cmd) if ret != 0: Log.Log().LevPrint("ERROR", "[ERROR] %s\n%s" % (cmd, msg)) return False else: return True
def test_PROTO_LIBRARY(self): """ test Syntax.PROTO_LIBRARY """ #make a new proto file Function.RunCommand("touch %s/hello.proto" % self._module.root_path, \ ignore_stderr_when_ok = True) #set local flags cpptags = Syntax.CppFlags("-DDEBUG_LOCAL", "-DRELEASE_LOCAL") cxxtags = Syntax.CxxFlags("-Wwrite-strings", "-Wswitch") incflag = Syntax.Include("") libflag = Syntax.Libs("$OUT_ROOT/baidu/broc/output/lib/libhello.a") now_dir = os.getcwd() os.chdir(self._module.workspace) protos = Syntax.PROTO_LIBRARY("hello", "*.proto", cpptags, cxxtags, incflag, libflag) proto_library = self._env.Targets()[0] src = proto_library.tag_sources.V()[0] proto_library.Action() os.chdir(now_dir) #check result proto_cmd = """mkdir -p broc_out/baidu/broc && protoc \ --cpp_out=broc_out/baidu/broc -I=baidu/broc \ -I=. baidu/broc/*.proto\n""" self.assertEqual(' '.join(protos.__str__().split()), ' '.join(proto_cmd.split())) self.assertEqual(src.cppflags, ["-DDEBUG_LOCAL"]) self.assertEqual(src.cxxflags, ["-Wwrite-strings"]) self.assertEqual(src.includes, [".", "broc_out", 'baidu/broc', u'broc_out/baidu/broc']) self.assertEqual(src.infile, "broc_out/baidu/broc/hello.pb.cc") self.assertEqual(proto_library.tag_libs.V(), \ ["broc_out/baidu/broc/output/lib/libhello.a"])
def STATIC_LIBRARY(name, *args): """ create one StaticLibrary object Args: name : the name of .a file args : the variable number of SyntagTag.TagSources, SyntaxTag.TagLibs object """ # to check name of result file if not Function.CheckName(name): raise BrocArgumentIllegalError( "name(%s) in STATIC_LIBRARY is illegal" % name) tag_libs = SyntaxTag.TagLibs() tag_sources = SyntaxTag.TagSources() for arg in args: if isinstance(arg, SyntaxTag.TagSources): tag_sources.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagLibs): tag_libs.AddSVs(arg.V()) else: raise BrocArgumentIllegalError("arguments (%s) in STATIC_LIBRARY is illegal" % \ type(arg)) env = Environment.GetCurrent() lib = Target.StaticLibrary(name, env, tag_sources, tag_libs) if len(tag_sources.V()): if not env.AppendTarget(lib): raise BrocArgumentIllegalError( "STATIC_LIBRARY(%s) exists already" % name) else: # .a file has been built already, just copy it from code directory to output directory lib.DoCopy()
def IsChanged(self, target=None): """ to check whether cache is changed Args: target : the target that compared to Returns: if file is modified, return True if file is not modified, return False """ # if build flag is True, means it has changed if self.build: return True # check mtime modify_time = None try: modify_time = os.stat(self.pathname).st_mtime except BaseException: self.build = True self.modify_time = 0 return True if modify_time == self.modify_time: return False # check hash _hash = Function.GetFileMd5(self.pathname) if _hash != self.hash: self.hash = _hash self.build = True return True return False
def UT_APPLICATION(name, sources, *args): """ create one UT Application object Args: name : the name of target sources : the SyntaxTag.TagSource object args : a variable number of SyntaxTag.TagLinkLDFlags, SyntaxTag.TagLibs, SyntaxTag.TagUTArgs """ # to check name of result file if not Function.CheckName(name): raise BrocArgumentIllegalError("name(%s) in UT_APPLICATION is illegal") tag_links = SyntaxTag.TagLDFlags() tag_libs = SyntaxTag.TagLibs() tag_utargs = SyntaxTag.TagUTArgs() for arg in args: if isinstance(arg, SyntaxTag.TagLDFlags): tag_links.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagLibs): tag_libs.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagUTArgs): tag_utargs.AddSVs(arg.V()) else: raise BrocArgumentIllegalError( "In UT_APPLICATION(%s) don't support %s" % (name, arg)) env = Environment.GetCurrent() app = Target.UTApplication(name, env, sources, tag_links, tag_libs, tag_utargs) if not env.AppendTarget(app): raise BrocArgumentIllegalError("UT_APPLICATION(%s) exists already" % name)
def _download_broc(self, node): """ download BROC file from repository Args: node : the BrocNode object Returns: return abs path of BROC file if download success return None if download failed """ broc_path = None cmd = None # for svn # Log.Log().LevPrint("MSG", 'download BROC %s' % node.module.url) if node.module.repo_kind == BrocModule_pb2.Module.SVN: hash_value = Function.CalcHash(node.module.url) broc_url = os.path.join(node.module.url, 'BROC') broc_path = os.path.join(self._broc_dir, "%s_BROC" % hash_value) if node.module.revision: broc_url = "%s -r %s" % (broc_url, node.module.revision) cmd = "svn export %s %s" % (broc_url, broc_path) else: # for GIT cmd = "" broc_path = os.path.join(node.module.workspace, node.module.module_cvspath, 'BROC') broc_dir = os.path.dirname(broc_path) if not os.path.exists(broc_path): cmd += "git clone %s %s" \ % ("%s.git" % node.module.url, "%s" % broc_dir) if node.module.br_name and node.module.br_name != 'master': br_name = node.module.br_name cmd += " && cd %s && (git checkout %s || (git fetch origin %s:%s && git checkout %s))" \ % (broc_dir, br_name, br_name, br_name, br_name) elif node.module.tag_name: tag_name = node.module.tag_name cmd += " && cd %s && (git checkout %s || (git fetch origin %s:%s && git checkout %s))" \ % (broc_dir, tag_name, tag_name, tag_name, tag_name) if cmd: Log.Log().LevPrint("MSG", "Getting BROC(%s) ..." % cmd) ret, msg = Function.RunCommand(cmd) if ret != 0: Log.Log().LevPrint("ERROR", msg) return None return broc_path
def test_GetHeaderFiles(self): """ test GetHeaderFiles """ now_dir = os.getcwd() obj = 'get_header_files.o' infile = 'get_header_files.cpp' include = ['/usr/include', '/usr/local/include'] compiler = '/usr/bin/g++' builder = Builder.ObjBuilder(obj, infile, include, None, compiler, now_dir) right_header_cmd = "/usr/bin/g++ \\\n\t-MM -MG\\\n\t-I/usr/include \ \\\n\t-I/usr/local/include \\\n\tget_header_files.cpp" self.assertEqual(right_header_cmd, builder.GetHeaderCmd()) with open('hello.h', 'wb') as f: f.write("#include <stdio.h>\n\ void hello()\n\ {\n\ print(\"hello - hello\");\n\ }\n") with open('world.h', 'wb') as f: f.write("#include <stdio.h>\n\ void world()\n\ {\n\ print(\"hello - world\");\n\ }\n") with open('get_header_files.cpp', 'wb') as f: f.write("#include <stdio.h>\n\ #include <pthread.h>\n\ #include \"hello.h\"\n\ #include \"world.h\"\n\ int main()\n\ {\n\ hello();\n\ world();\n\ return 0;\n\ }\n") ret = builder.CalcHeaderFiles() self.assertEqual(True, ret['ret']) self.assertEqual(sorted(["hello.h", 'world.h']), sorted(builder.GetHeaderFiles())) Function.DelFiles('get_header_files.cpp') Function.DelFiles('hello.h') Function.DelFiles('world.h')
def _download_broc(self, node): """ download BROC from node's repository Args: node : BrocNode object of module Returns: return the path of BROC file if download successfully; return None if fail to download BROC """ broc_path = None cmd = None # for svn if node.module.repo_kind == BrocModule_pb2.Module.SVN: _file = Function.CalcMd5(node.module.url) broc_url = os.path.join(node.module.url, 'BROC') broc_path = os.path.join(self._broc_dir, "%s_BROC" % _file) if node.module.revision: broc_url = "%s -r %s" % (broc_url, node.module.revision) cmd = "svn export %s %s" % (broc_url, broc_path) else: # for GIT broc_path = os.path.join(node.module.workspace, node.module.module_cvspath, 'BROC') cmd = "git clone %s %s && cd %s" \ % (node.module.url, node.module.module_cvspath, node.module.module_cvspath) if node.module.br_name: cmd += " && (git checkout %s || git fetch -all && git checkout %s)" \ % (node.module.br_name, node.module.br_name) elif node.module.tag_name: cmd += " && git fetch -all && git checkout %s " % node.module.tag_name else: self._logger.LevPrint("ERROR", "couldn't find node(%s) branch or tag name" \ % node.module.module_cvspath) return None self._logger.LevPrint("MSG", "run command %s" % cmd) ret, msg = Function.RunCommand(cmd) if ret != 0: self._logger.LevPrint("ERROR", "%s" % msg) self._need_broc_list.append(node.module.url) return None return broc_path
def DoPublish(self): """ do publish cmd """ for cmd in self._publish_cmd: ret, msg = Function.RunCommand(cmd) if ret != 0: return (False, msg) return (True, '')
def Initialize(self, target): """ to initialize LibCache Args: target : the Target.Target object """ if not self.initialized: try: self.hash = Function.GetFileHash(self.pathname) if self.hash: self.modify_time = os.stat(self.pathname.st_mtime) except BaseException: pass self.build_cmd = target.GetBuildCmd() self.initialized = True
def GetNodeHash(self, node): """ return the hash value of node node hash value = hash(module_cvspath) + hash(BROC) + hash(module tag/branch type) + hash(module tag/branch name) Args: node : the BrocNode object """ key = None if node.module.br_kind == BrocModule_pb2.Module.TAG: key = node.module.module_cvspath + str( node.module.br_kind) + node.module.tag_name else: key = node.module.module_cvspath + str( node.module.br_kind) + node.module.br_name return Function.CalcHash(key)
def Dump(self): """ save dependency relation of files """ dumped_file = os.path.join(self._root, ".BROC.FILE.DEPS") for pathname in self._cache: # the length of reverse deps is 0 means it is application or libs of main module if len(self._cache[pathname].reverse_deps) <= 0: self._dump(pathname, 0) try: dir_name = os.path.dirname(dumped_file) Function.Mkdir(dir_name) with open(dumped_file, "w") as f: f.write("" + self._dumped_str) except IOError as err: self._logger.LevPrint("ERROR", "save file dependency failed(%s)" % err)
def _save_cache(self): """ save cache objects into file and content of file is a list and its format is [ version, cache, cache, ...]. the first item is cache version, and the 2th, 3th ... item is cache object """ dir_name = os.path.dirname(self._cache_file) Function.Mkdir(dir_name) try: caches = [self._version] caches.extend(map(lambda x: self._cache[x], self._cache)) with open(self._cache_file, 'wb') as f: cPickle.dump(caches, f) except Exception as err: self._logger.LevPrint( "ERROR", "save cache(%s) failed(%s)" % (self._cache_file, str(err)))
def _check_broc(self, node): """ to check BROC file Args: node : the object of BrocNode Returns: return the abs path of BROC file if check successfully return None if fail to check BROC file """ broc_path = os.path.join(node.module.workspace, node.module.module_cvspath, 'BROC') # BROC exists in local file system if os.path.exists(broc_path): if node.module.repo_kind == BrocModule_pb2.Module.SVN: if self.SameSvnNode(node): return broc_path else: return self._download_broc(node) else: _broc_dir = os.path.join(node.module.workspace, node.module.module_cvspath) if node.module.is_main: return os.path.join(_broc_dir, "BROC") cmd = "cd %s " % _broc_dir if node.module.tag_name: cmd += " && (git checkout %s || (git fetch --all && git checkout %s))" \ % (node.module.tag_name, node.module.tag_name) else: cmd += " && (git checkout %s || (git fetch --all && git checkout %s))" \ % (node.module.br_name, node.module.br_name) # to run cmd # self._logger.LevPrint("MSG", "run cmd: %s ..." % cmd) ret, msg = Function.RunCommand(cmd) if ret != 0: self._logger.LevPrint( "ERROR", "fail to find BROC(%s) failed(%s)" % (cmd, msg)) # FIX ME, maybe return None is better raise BrocTreeError("%s\n%s" % (cmd, msg)) else: return broc_path # to dowonload BROC else: return self._download_broc(node)
def Run(self): """ thread entrence function """ while not self._queue.empty(): try: cmd = self._queue.get(True, 1) except Queue.Empty: break ret, msg = Function.RunCommand(cmd, True) if ret != 0: self._logger.LevPrint("ERROR", "run ut cmd(%s) failed: %s" % (cmd, msg)) self._errors.append(msg) else: self._logger.LevPrint("MSG", "run ut cmd(%s) OK\n%s" % (cmd, msg)) self._queue.task_done()
def DoBuild(self): """ to run build cmd Returns: return (True, '') if build successfully return (False, 'error msg') if fail to build """ result = dict() ret, msg = Function.RunCommand(self.build_cmd, ignore_stderr_when_ok=False) if ret != 0: result['ret'] = False else: self.build = False result['ret'] = True result['msg'] = self.build_cmd + '\n' + msg return result
def PreAction(self): """ parse proto flags and gernerate the command to handle proto file Returns: return (True, '') if deal with proto files successfully, otherwise return (False, 'error msg') """ proto_dirs = list() # find the first directory of all proto files # for example: a/b/c/util.proto, the first directory is a, to handle proto like this because # https://developers.google.com/protocol-buffers/docs/reference/python-generated#invocation proto_flags = " ".join(self._tag_protoflags.V()) # add the cvs path of directory of BROC self._tag_include.AddV(self.env.BrocCVSDir()) cvs_dirs = " ".join( map(lambda x: "-I=%s " % os.path.normpath(x), self._tag_include.V())) #protoc = os.path.join(os.environ['HOME'], "broc/protobuf/bin/protoc") protoc = 'protoc' for proto in self._protos.split(): normpath_proto = os.path.normpath(proto) protos = os.path.join(self.env.BrocCVSDir(), normpath_proto) out = os.path.normpath( os.path.join("broc_out", self.env.BrocCVSDir(), os.path.dirname(normpath_proto))) cpp_out = os.path.join('broc_out', self.env.BrocCVSDir()) pos = normpath_proto.find('/') if pos != -1: cpp_out = os.path.join('broc_out', self.env.BrocCVSDir(), normpath_proto[:pos]) # the current working directory is $WORKSPACE cmd = "mkdir -p %(out)s && %(protoc)s --cpp_out=%(cpp_out)s %(proto_flags)s %(cvs_dirs)s \ -I=. %(protos)s" % (locals()) self._proto_cmds.add(cmd) # run protoc for cmd in self._proto_cmds: Log.Log().LevPrint("MSG", "%s" % cmd) ret, msg = Function.RunCommand(cmd, True) if ret != 0: return (False, "%s%s" % (cmd, msg)) return (True, '')
def __init__(self, pathname, initialized=True): """ Args: pathname : the cvs path of object file initialized : to mark whether BrocObject need initialized, default is True, if initialized is False, create a empty BrocObject object """ self.pathname = pathname self.initialized = initialized # initialized flag self.deps = set() # dependent BrocObject self.reverse_deps = set() # reversed dependent BrocObject self.hash = None # hash value of content self.modify_time = 0 # the last modify time of BrocObject file self.build_cmd = "" # the commond of BrocObject to build if self.initialized: try: self.hash = Function.GetFileMd5(pathname) if self.hash: self.modify_time = os.stat(self.pathname.st_mtime) except BaseException: pass self.build = True # build flag, if build is True, the BrocObject need to compiled
def IsChanged(self, target=None): """ to check whether cache is changed Args: target : the target that compared to Returns: if file is modified, return True if file is not modified, return False """ # if build flag is True, means it has changed if self.build: #Log.Log().LevPrint('MSG', 'cache %s build mark is true' % self.pathname) return True # check mtime modify_time = None try: modify_time = os.stat(self.pathname).st_mtime except BaseException: Log.Log().LevPrint('MSG', 'get %s modify_time failed' % self.pathname) self.build = True self.modify_time = 0 return True if modify_time == self.modify_time: return False else: self.modify_time = modify_time ret = False # check hash _hash = Function.GetFileHash(self.pathname) if _hash != self.hash: self.hash = _hash # Log.Log().LevPrint('MSG', '%s content changed' % self.pathname) self.build = True ret = True return ret
def PreAction(self): """ parse proto flags and gernerate the command to handle proto file Returns: return (True, '') if deal with proto files successfully, otherwise return (False, 'error msg') """ proto_dirs = set() # find the cvs path of directory of all proto files for proto in self._protos.split(): proto_dirs.add( os.path.join(self.env.BrocCVSDir(), os.path.dirname(proto))) proto_flags = " ".join(self._tag_protoflags.V()) # add protobuf include set from PROTO_LIBRARY cvs_dirs = " ".join(map(lambda x: "-I=%s " % x, self._tag_include.V())) # add cvs path of directory of BROC cvs_dirs += "-I=%s " % self.env.BrocCVSDir() #protoc = os.path.join(os.environ['HOME'], "broc/protobuf/bin/protoc") protoc = 'protoc' for _dir in proto_dirs: # cvs_out_dir = os.path.normpath(os.path.join('broc_out', _dir)) cvs_out_dir = os.path.normpath( os.path.join('broc_out', self.env.BrocCVSDir())) protos = os.path.normpath("%(_dir)s/*.proto" % (locals())) cmd = "mkdir -p %(cvs_out_dir)s && %(protoc)s --cpp_out=%(cvs_out_dir)s %(proto_flags)s %(cvs_dirs)s \ -I=. %(protos)s" % (locals()) # 执行protoc的目录,在output下 self._proto_cmds.add(cmd) # run protoc for cmd in self._proto_cmds: Log.Log().LevPrint("MSG", "%s" % cmd) ret, msg = Function.RunCommand(cmd, True) if ret != 0: return (False, "%s%s" % (cmd, msg)) return (True, '')
def __del__(self): """ """ Function.DelFiles(self._broc_dir)
def PROTO_LIBRARY(name, files, *args): """ compile proto files into a static library .a when parse proto files failed, raise Exception BrocProtoError Args: name: the name of proto static library files: a string sperearated by blank character, representing the relative path of proto files args: a variable number of SyntaxTag.TagProtoFlags, SyntaxTag.TagInclude, SyntaxTag.CppFlags, SyntaxTag.CXXFlags, SyntaxTag.TagLibs """ # check validity of name if not Function.CheckName(name): raise BrocArgumentIllegalError("name(%s) PROTO_LIBRARY is illegal" % name) # check proto file whether belongs to module proto_files = files.split() env = Environment.GetCurrent() for _file in proto_files: abs_path = os.path.normpath(os.path.join(env.BrocDir(), _file)) if not env.ModulePath() in abs_path: raise NotInSelfModuleError(abs_path, env.ModulePath()) # to check args tag_protoflags = SyntaxTag.TagProtoFlags() tag_cppflags = SyntaxTag.TagCppFlags() tag_cxxflags = SyntaxTag.TagCxxFlags() tag_libs = SyntaxTag.TagLibs() tag_include = SyntaxTag.TagInclude() for arg in args: if isinstance(arg, SyntaxTag.TagProtoFlags): tag_protoflags.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagCppFlags): tag_cppflags.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagCxxFlags): tag_cxxflags.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagLibs): tag_libs.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagInclude): tag_include.AddSVs(arg.V()) else: raise BrocArgumentIllegalError("don't support tag(%s) in PROTO_LIBRARY in %s" \ % (str(arg), env.BrocPath())) include = set() source = set() for f in proto_files: root, _ = os.path.splitext(f) result_file = os.path.join(os.path.join('broc_out', env.ModuleCVSPath()), \ "%s.pb.cc" % root) include.add(os.path.dirname(result_file)) if os.path.dirname(f): tag_include.AddV( os.path.join(env.ModuleCVSPath(), os.path.dirname(f))) source.add(result_file) protolib = Target.ProtoLibrary(env, files, tag_include, tag_protoflags) ret, msg = protolib.PreAction() if not ret: raise BrocProtoError(msg) tag_include.AddSVs(include) broc_out = os.path.join("broc_out", env.BrocCVSDir()) if broc_out not in tag_include.V(): tag_include.AddV(os.path.join("broc_out", env.BrocCVSDir())) tag_sources = Sources(" ".join(list(source)), tag_include, tag_cppflags, tag_cxxflags) if not env.AppendTarget( Target.StaticLibrary(name, env, tag_sources, tag_libs)): raise BrocArgumentIllegalError( "PROTO_LIBRARY(%s) name exists already" % name) return protolib
def tearDown(self): """ teardown """ Function.RunCommand("rm -rf ~/unittest_broc", ignore_stderr_when_ok = True)