def exec_txl(cls, txlprog, filepath, txlargs=None): directory, filename = os.path.split(filepath) directory = directory and directory or "." txlbin = DelphiCompiler.get_txl_binary(relative_to=directory) grammar_path = DelphiCompiler.get_txl_grammar_path(relative_to=directory) txlprog = os.path.join(grammar_path, txlprog) if not io.platform_is_linux(): txlprog = io.path_cygwin_to_win(txlprog) txlargs = txlargs and txlargs or [] return io.invoke([txlbin] + txlargs + [txlprog, filename], cwd=directory, return_all=True)
def do_dfm_conversion(transform, graph): forms = find_forms(graph) for filepath_dfm in forms: directory = os.path.dirname(filepath_dfm) filepath_dfmbin = filepath_dfm + ".bin" filepath_dfmtxt = filepath_dfm + ".txt" filename_dfm = os.path.basename(filepath_dfm) filename_dfmbin = os.path.basename(filepath_dfmbin) dfmconvert = DelphiCompiler.get_dfmconvert(relative_to=directory) io.run_win32app( "Convert %s to binary dfm format" % filename_dfm, directory, [dfmconvert, "--to-binary", filename_dfm, "--output", filename_dfmbin], ) if os.path.exists(filepath_dfmbin): shutil.copymode(filepath_dfm, filepath_dfmbin) # make backup, file will be overwritten shutil.copyfile(filepath_dfm, filepath_dfmtxt) shutil.copymode(filepath_dfm, filepath_dfmtxt) # rewrite original filename os.unlink(filepath_dfm) os.rename(filepath_dfmbin, filepath_dfm) transform[filepath_dfm] = filepath_dfmtxt
def do_compile(transform, abspath, relpath, filename, includepath, target_dir=None): filepath = os.path.join(abspath, relpath, filename) directory = os.path.dirname(filepath) handle_cfg(transform, filepath) # keep codebase clean, write output to tempdir tmpdir = io.get_tmpdir() if io.platform_is_cygwin(): tmpdir = io.path_cygwin_to_win(tmpdir) target_dir = target_dir and target_dir or tmpdir outputdirs = [ '-E"%s"' % target_dir, # OutputDir '-N"%s"' % tmpdir, # UnitOutputDir '-LE"%s"' % tmpdir, # PackageDLLOutputDir '-LN"%s"' % tmpdir, # PackageDCPOutputDir ] dcc32 = DelphiCompiler.get_dcc32(relative_to=directory) exitcode = io.run_win32app( "Building %s" % filename, directory, [ dcc32, # '--depends', '-u"%s"' % includepath, ] + outputdirs + [filename], ) return exitcode
def build(filepath, use_graph=None, target_dir=None): transform = {} if not DelphiGraph.filepath_is_graph(filepath): fpabs = os.path.abspath(filepath) abspath = os.path.dirname(fpabs) relpath = "." filename = os.path.basename(fpabs) stdlibpath = DelphiCompiler.get_stdlibpath(libonly=True, relative_to=abspath) includepath = ";".join(stdlibpath) if use_graph: dg = DelphiGraph.from_file(use_graph) df = dg.rootnode includepath = prepare_path(dg, use_abspath=abspath) return do_compile(transform, abspath, relpath, filename, includepath, target_dir=target_dir) else: # load from file dg = DelphiGraph.from_file(filepath) df = dg.rootnode targets = df.collect_nodes(lambda n: n.filetype in (FileTypes.Program, FileTypes.Library, FileTypes.Package)) target = targets[0] includepath = prepare_path(dg) try: # XXX seems to be redundant at times, not understood when do_dfm_conversion(transform, dg) exitcode = do_compile( transform, dg.abspath, target.path, target.filename, includepath, target_dir=target_dir ) finally: for (orig, new) in transform.items(): io.rename(new, orig) return exitcode
def get_txl_prog(cls): grammar_path = DelphiCompiler.get_txl_grammar_path(relative_to=directory) txlprog = os.path.join(grammar_path, txlprog) return txlprog
def trace(self, filepath, maxdepth=None, projview=False): def trace_deps(filepath, searchpath, stdlibpath, unitaliases, depth=0, maxdepth=None): # don't prevent calls on filepaths already in index so as to resolve # cycles properly, but catch these here to return a valid object # for a filepath already known if self.index_has(filepath): return self.index_get(filepath) io.write_next_action("Tracing %s" % os.path.basename(filepath), indent=depth) filepath, exists, filetype = classify_file(filepath, searchpath, stdlibpath) delphifile = self.new_delphifile(filepath, (filepath, exists, filetype)) if exists: searchpath = collect_searchpath(searchpath, delphifile, filepath) unitaliases = collect_unitaliases(unitaliases, delphifile, filepath) finder_function = finder_dispatch.get(filetype) if finder_function: fps = finder_function(open(filepath).read(), parent=delphifile) io.write_result('Found [%s]' % ', '.join(fps), indent=depth) fps = process_filepaths(delphifile.path, unitaliases, fps) if not maxdepth or maxdepth >= depth+1: for fp in fps: delphifile.add_node(trace_deps(fp, searchpath, stdlibpath, unitaliases, depth=depth+1, maxdepth=maxdepth)) return delphifile path, filename = os.path.split(filepath) abspath = os.path.abspath(path) searchpath = [] stdlibpath = DelphiCompiler.get_stdlibpath(relative_to=(path or '.')) finder_dispatch = { FileTypes.DelphiProjectGroup: findProjects, FileTypes.DelphiProject: group(findMainSource, findDelphiCompilerFlags), FileTypes.Program: group(findUses, findIncludes, findResources), FileTypes.Library: group(findUses, findIncludes, findResources), FileTypes.Package: group(findContains, findIncludes, findResources), FileTypes.Unit: group(findUses, findIncludes, findResources), FileTypes.FileInclude: group(findUses, findIncludes, findResources), } if projview: finder_dispatch = { FileTypes.DelphiProjectGroup: findProjects, FileTypes.DelphiProject: findMainSource, } # change cwd to where the file is so that no matter what . is at # runtime the paths of files in the graph will be constant oldcwd = os.getcwd() try: io.safechdir(path) self.index_clear() df = trace_deps(filename, searchpath, stdlibpath, {}, maxdepth=maxdepth) finally: io.safechdir(oldcwd) return DelphiGraph(df, abspath, searchpath, stdlibpath)