Ejemplo n.º 1
0
func ProcessFileList(pt *test01.Parse2Type, inList []string, outFn string) (err error) {

	var fp *os.File

	if outFn != "" {
		fp, err = com.Fopen(outFn, "w")
		if err != nil {
			fmt.Fprintf(os.Stderr, "Fatal: Unable to create output file\n")
			err = fmt.Errorf("Fatal: Unable to create output file")
			return
		}
		defer fp.Close()
	} else {
		fp = os.Stdout
	}

	for _, fn := range inList {
		if !com.Exists(fn) {
			fmt.Fprintf(os.Stderr, "Fatal: Missing input file %s\n", fn)
			err = fmt.Errorf("Fatal: Missing input file %s", fn)
			return
		}
	}

	go func() {
		r := pbread.NewPbRead()
		for _, fn := range inList {
			r.OpenFile(fn)
		}
		pt.Lex.MatcherLexieTable(r, "S_Init")
	}()

	// ------------------------------------------------------ process tokens --------------------------------------------------------------
	// Generate a parse tree and print out.
	xpt := pt.GenParseTree(0)
	pt.TheTree = xpt
	xpt.DumpMtType(test01.Dbf, 0, 0)
	pt.ExecuteFunctions(0)
	if false {
		fmt.Fprintf(test01.Dbf, "----------------------------------- debug output ----------------------------------------------------\n")
		fmt.Fprintf(test01.Dbf, "%s\n", com.SVarI(xpt))
	}
	fmt.Fprintf(test01.Dbf, "----------------------------------- errors ----------------------------------------------------\n")
	pp := pt.CollectErrorNodes(0)
	for ii, vv := range pp {
		fmt.Fprintf(test01.Dbf, "Error [%3d]: msg=%s\n", ii, vv.ErrorMsg)
	}
	fmt.Fprintf(test01.Dbf, "----------------------------------- final template results  ----------------------------------------------------\n")
	pt.OutputTree(test01.Dbf, 0)

	pt.OutputTree(fp, 0)

	return
}
Ejemplo n.º 2
0
func CopyInAssets(optsSiteName string, BaseAssets string, SiteAssets string, optsOutput string, User string, Theme string, forcedCpFlag bool) {

	// ---------------------------------------------------------------------------------------------------------------------------------
	data := make(map[string]string)
	data["site_name"] = optsSiteName
	data["user"] = User
	data["theme"] = Theme

	//if opts.Input == "" && optsSiteName != "" {
	//	opts.Input = com.Qt("./site/%{site_name%}/", data2)
	//}

	if optsOutput == "" && optsSiteName != "" {
		optsOutput = com.Qt("./www/%{site_name%}/", data)
	}

	// Generate list of top directories to search
	top := make([]string, 0, 10)
	topDirs := com.Qt(SiteAssets+"/%{user%}/%{theme%}/", data) // ./site_assets/%{site_name%} ->  ./site_assets/%{site_name%}/%{user%}/%{theme%}/
	if com.Exists(topDirs) {
		top = append(top, topDirs)
	}

	data["user"] = ""
	// data["theme"] = "A-Theme"
	topDirs = com.Qt(SiteAssets+"/%{user%}/%{theme%}/", data) // ./site_assets/%{site_name%} ->  ./site_assets/%{site_name%}/%{user%}/%{theme%}/
	if com.Exists(topDirs) {
		top = append(top, topDirs)
	}

	data["user"] = User
	// data["user"] = ""
	data["theme"] = ""
	topDirs = com.Qt(SiteAssets+"/%{user%}/%{theme%}/", data) // ./site_assets/%{site_name%} ->  ./site_assets/%{site_name%}/%{user%}/%{theme%}/
	if com.Exists(topDirs) {
		top = append(top, topDirs)
	}

	top = append(top, BaseAssets)
	// top has array in order of top level dirs to copy from.

	var infn, outfn, dirs []string

	cpList2 := make(map[string]string)

	for ii := range top {
		jj := (len(top) - 1) - ii
		dir := top[jj]

		t_fns, t_dirs, err := com.GetFilenamesRecrusive(dir)
		if err != nil {
			fmt.Printf("Error: %s\n", err)
		} else {

			// if base-fiel-name is not in fns, then add it
			infn = append(infn, t_fns...)
			t_fns = com.ReplaceEach(t_fns, opts.Input, optsOutput)
			outfn = append(outfn, t_fns...)
			outfn = com.ReplaceEach(outfn, dir, optsOutput)

			for kk, in := range infn {
				// fmt.Printf("Loop %2d: in=>%s<- dir=%s\n", kk, in, dir)
				if com.Exists(in) {
					// xyzzy - compare time stamps and size
					if InputModified(in, outfn[kk], forcedCpFlag) || FileSizeDiffers(in, outfn[kk]) {
						cpList2[outfn[kk]] = in
					}
				}
			}

			// if base-dir-name is not in dirs then add base dir name
			t_dirs = com.ReplaceEach(t_dirs, dir, optsOutput)
			for _, x := range t_dirs {
				if !com.InArray(x, dirs) {
					dirs = append(dirs, x)
				}
			}

			if db_debug4 {
				fmt.Printf("\tinfn=\n")
				debugPrintStrinSlice(infn, "\t\t")
				fmt.Printf("\toutfn=\n")
				debugPrintStrinSlice(outfn, "\t\t")
				fmt.Printf("\tdirs 0=\n")
				debugPrintStrinSlice(dirs, "\t\t")
			}
		}
	}
	MkDirArray(dirs)
	if db_debug4 {
		fmt.Printf("cp: %s\n", com.SVarI(cpList2))
	}
	CopyFilesInHash(cpList2)

}
Ejemplo n.º 3
0
func (s *LexieTestSuite) TestLexie(c *C) {

	// return
	fmt.Fprintf(os.Stderr, "Test Parsing of REs, Test genration of NFAs %s\n", com.LF())

	com.DbOnFlags["db_NFA"] = true
	com.DbOnFlags["db_NFA_LnNo"] = true
	com.DbOnFlags["db_DumpPool"] = true
	com.DbOnFlags["parseExpression"] = true
	com.DbOnFlags["CalcLength"] = true

	// Add a test for any issue
	c.Check(42, Equals, 42)
	// c.Assert("nope", Matches, "hel.*there")
	fmt.Printf("**** In Test Issues\n")
	//x := test7GenDFA()
	//c.Check(x, Equals, 0)

	n_err := 0
	n_skip := 0

	for ii, vv := range Lexie01Data {
		if !vv.SkipTest {
			fmt.Printf("\n\n--- %d Test: %s -----------------------------------------------------------------------------\n\n", ii, vv.Test)

			Pool := NewNFA_Pool()
			Cur := Pool.GetNFA()
			Pool.InitState = Cur

			Pool.AddReInfo(vv.Re, "", 1, vv.Rv, InfoType{})
			Pool.Sigma = Pool.GenerateSigma()

			if false {
				com.DbPrintf("test7", "Pool=%s\n", com.SVarI(Pool))
			}
			Pool.DumpPool(false)
			Pool.DumpPoolJSON(os.Stdout, vv.Re, vv.Rv)

			fmt.Printf("Sigma: ->%s<-\n", Pool.Sigma)

			newFile := fmt.Sprintf("../ref/nfa_%s.tst", vv.Test)
			cmpFile := fmt.Sprintf("../ref/nfa_%s.ref", vv.Test)
			gvFile := fmt.Sprintf("../ref/nfa_%s.gv", vv.Test)
			svgFile := fmt.Sprintf("../ref/nfa_%s.svg", vv.Test)
			fp, _ := com.Fopen(newFile, "w")
			Pool.DumpPoolJSON(fp, vv.Re, vv.Rv)
			fp.Close()
			newData, err := ioutil.ReadFile(newFile)
			if err != nil {
				panic("unable to read file, " + cmpFile)
			}

			if com.Exists(cmpFile) {
				ref, err := ioutil.ReadFile(cmpFile)
				if err != nil {
					panic("unable to read file, " + cmpFile)
				}
				if string(ref) != string(newData) {
					c.Check(string(newData), Equals, string(ref))
					fmt.Printf("%sError%s: Test case %s failed to match\n", com.Red, com.Reset, vv.Test)
					n_err++
				}
			} else {
				n_skip++
			}

			gv, _ := com.Fopen(gvFile, "w")
			Pool.GenerateGVFile(gv, vv.Re, vv.Rv)
			gv.Close()

			out, err := exec.Command("/usr/local/bin/dot", "-Tsvg", "-o"+svgFile, gvFile).Output()
			if err != nil {
				fmt.Printf("Error from dot, %s, %s\n", err, com.LF())
				fmt.Printf("Output: %s\n", out)
			}
		}
	}
	if n_skip > 0 {
		fmt.Fprintf(os.Stderr, "%sSkipped, # of files without automated checks = %d%s\n", com.Yellow, n_skip, com.Reset)
		com.DbPrintf("debug", "\n\n%sSkipped, # of files without automated checks = %d%s\n", com.Yellow, n_skip, com.Reset)
	}
	if n_err > 0 {
		fmt.Fprintf(os.Stderr, "%sFailed, # of errors = %d%s\n", com.Red, n_err, com.Reset)
		com.DbPrintf("debug", "\n\n%sFailed, # of errors = %d%s\n", com.Red, n_err, com.Reset)
	} else {
		fmt.Fprintf(os.Stderr, "%sPASS%s\n", com.Green, com.Reset)
		com.DbPrintf("debug", "\n\n%sPASS%s\n", com.Green, com.Reset)
	}
}
Ejemplo n.º 4
0
func main() {

	// ------------------------------------------------------ cli processing --------------------------------------------------------------
	ifnList, err := flags.ParseArgs(&opts, os.Args)

	if err != nil {
		fmt.Printf("Invalid Command Line: %s\n", err)
		os.Exit(1)
	}

	test01.Dbf = os.Stdout
	if opts.Debug != "" {
		s := strings.Split(opts.Debug, ";")
		com.DbOnFlags[opts.Debug] = true
		for _, v := range s {
			if len(v) > 5 && v[0:4] == "out:" {
				test01.Dbf, _ = com.Fopen(v[4:], "w")
			} else {
				com.DbOnFlags[v] = true
			}
		}
	}

	fmt.Fprintf(test01.Dbf, "Test Matcher test from %s file, %s\n", opts.LexPat, com.LF())

	// ------------------------------------------------------ Options --------------------------------------------------------------
	// should be read in from a .json file!
	Options.MdExtensions = []string{".md", ".makrdown"}
	Options.ConvertMdToHtml = true
	Options.LeaveTmpFiles = false
	Options.TmpDir = "./tmp"

	if !com.Exists(Options.TmpDir) {
		os.Mkdir(Options.TmpDir, 0700)
	}

	// ------------------------------------------------------ setup Lexie --------------------------------------------------------------
	pt := test01.NewParse2Type()
	pt.Lex = dfa.NewLexie()
	pt.Lex.SetChanelOnOff(true) // Set for getting back stuff via Chanel

	// ------------------------------------------------------ input machine  --------------------------------------------------------------
	if opts.LexPat != "" {
		if !com.Exists(opts.LexPat) {
			fmt.Fprintf(os.Stderr, "Fatal: Must have -l <fn> lexical analyzer machine.  Missing file.\n")
			os.Exit(1)
		}
		pt.Lex.NewReadFile(opts.LexPat) // pstk.Lex.NewReadFile("../in/django3.lex")
	} else {
		fmt.Fprintf(os.Stderr, "Fatal: Must have -l <fn> lexical analyzer machine.\n")
		os.Exit(1)
	}

	// -------------------------------------------------- start scanning process  ----------------------------------------------------------
	tp := strings.Split(opts.TemplatePath, ";")
	for _, tps := range tp {
		pt.OpenLibraries(tps)
	}

	if opts.Recursive {

		CopyInAssets(opts.SiteName, opts.BaseAssets, opts.SiteAssets, opts.Output, opts.User, opts.Theme, opts.ForcedCpFlag)

		//fmt.Printf("After CopyInAssets: Not Implemented Yet\n")
		//os.Exit(1)

		data2 := make(map[string]string)
		data2["site_name"] = opts.SiteName

		if opts.Input == "" && opts.SiteName != "" {
			opts.Input = com.Qt("./site/%{site_name%}/", data2)
		}
		if opts.Output == "" && opts.SiteName != "" {
			opts.Output = com.Qt("./www/%{site_name%}/", data2)
		}

		// ---------------------------------------------------------------------------------------------------------------------------------

		// 1. Do the rsync copy ops
		// 2. Process the set of fiels from -i -> -o

		// -- Process the static files -----------------------------------------------------------------------------------------------------
		dp := make([]string, 0, 10)
		if opts.Input != "" {
			dp = append(dp, opts.Input)
		} else {
			for _, fn := range ifnList[1:] {
				dp = append(dp, fn)
			}
		}

		var fns, dirs []string

		for _, dir := range dp {
			if db_debug3 {
				fmt.Printf("Getting for %s\n", dir)
			}
			t_fns, t_dirs, err := com.GetFilenamesRecrusive(dir)
			if err != nil {
				if db_debug3 {
					fmt.Printf("Error: %s on %s\n", err, dir)
				}
			} else {
				fns = append(fns, t_fns...)
				dirs = append(dirs, t_dirs...)
			}
		}

		if db_debug3 {
			fmt.Printf("fns: %+v\n", fns)
			fmt.Printf("dirs: %+v\n", dirs)
		}

		mds := com.ReplaceEach(dirs, opts.Input, opts.Output)
		for _, aDir := range mds {
			if !com.Exists(aDir) {
				err := os.Mkdir(aDir, 0764)
				if err != nil {
					if db_debug3 {
						fmt.Printf("Error: Unable to create directory %s, error: %s\n", aDir, err)
					}
				}
			}
		}

		mf := com.ReplaceEach(fns, opts.Input, opts.Input+"/%{user%}/%{theme%}/")
		mO := com.ReplaceEach(fns, opts.Input, opts.Output)
		if db_debug3 {
			fmt.Printf("modded_files: %+v\n", mf)
		}

		final := make([]string, 0, len(mf))
		data := make(map[string]string)
		has_err := false

		for _, mff := range mf {
			data["user"] = opts.User
			data["theme"] = opts.Theme
			mfmod := com.Qt(mff, data)
			if com.Exists(mfmod) {
				final = append(final, mfmod)
			} else {

				data["user"] = ""
				// data["theme"] = "A-Theme"
				mfmod := com.Qt(mff, data)
				if com.Exists(mfmod) {
					final = append(final, mfmod)
				} else {

					data["user"] = opts.User
					// data["user"] = ""
					data["theme"] = ""
					mfmod := com.Qt(mff, data)
					if com.Exists(mfmod) {
						final = append(final, mfmod)
					} else {
						fmt.Printf("Error: File Missing %s\n", mfmod)
						has_err = true
					}
				}
			}
		}
		if has_err {
			fmt.Printf("Error occured...\n")
			os.Exit(1)
		}

		if db_debug3 {
			fmt.Printf("Final Files:%s\n", final)
		}
		tmpFiles := make([]string, 0, len(final))
		for ii, yy := range final {
			yyt := yy
			fmt.Printf("Process %s to %s\n", yy, mO[ii])
			ext := filepath.Ext(yy)
			if Options.ConvertMdToHtml && com.InArray(ext, Options.MdExtensions) { // if ext == ".md" || ext == ".markdown" {
				fmt.Printf("\t Convetting from MD to HTML\n")
				in := yy
				//yyt = "./tmp/" + com.Basename(yy) + ".html"	// old code - not using a Tempfile
				//err := ConvertMdToHtmlFile(in, yyt)
				yyt, err = ConvertMdToHtmlFileTmpFile(in)
				if err != nil {
					fmt.Printf("Error: In processing from markdown %s to HTML %s: %s\n", in, yyt, err)
					os.Exit(1)
				}
				mO[ii] = com.RmExt(mO[ii]) + ".html"
				tmpFiles = append(tmpFiles, yyt)
			}
			ProcessFileList(pt, []string{yyt}, mO[ii])
		}
		if !Options.LeaveTmpFiles {
			for _, fn := range tmpFiles {
				os.Remove(fn)
			}
		}

	} else {

		inList := make([]string, 0, 10)
		if opts.Input != "" {
			inList = append(inList, opts.Input)
		} else {
			inList = ifnList[1:]
		}

		ProcessFileList(pt, inList, opts.Output)

	}

}
Ejemplo n.º 5
0
func (s *NFA_Multi_Part_TestSuite) TestLexie(c *C) {

	// return
	fmt.Fprintf(os.Stderr, "Test NFA Multi-Part RE - NFA test %s\n", com.LF())
	n_err := 0
	n_skip := 0

	// ------------------------- ------------------------- --------------------------------------- ---------------------------------------
	// Test as sections
	// ------------------------- ------------------------- --------------------------------------- ---------------------------------------

	for ii, vv := range NFATest_02Data {
		fmt.Printf("\n\n--- %2d Test: %4s -------------------------------------------------------------------------------\n", ii, vv.Test)
		Pool := NewNFA_Pool()
		Cur := Pool.GetNFA()
		Pool.InitState = Cur
		for jj, ww := range vv.Data {
			fmt.Printf("\n\n--- %2d Test: %4s Part %2d ----------------------------------------------------------------------\n\n", ii, vv.Test, jj)
			// Add in components
			Pool.AddReInfo(ww.Re, "", 1, ww.Rv, InfoType{})
			// 		Dum out parsed REs
			// 		Dum out parsed NFAs along the way
		}
		Pool.FinializeNFA()  // Fnialize
		Pool.DumpPool(false) // Dump out NFA - check it.

		// ------------------------- --------------------------------------- ---------------------------------------
		// Test these also
		// 		func (nn *NFA_PoolType) DeleteRe(oldRe string) {
		// 		func (nn *NFA_PoolType) ChangeRe(oldRe string, newRe string) {
		// ------------------------- --------------------------------------- ---------------------------------------

		newFile := fmt.Sprintf("../ref/n2_%s.tst", vv.Test)
		cmpFile := fmt.Sprintf("../ref/n2_%s.ref", vv.Test)
		gvFile := fmt.Sprintf("../ref/n2_%s.gv", vv.Test)
		svgFile := fmt.Sprintf("../ref/n2_%s.svg", vv.Test)
		fp, _ := com.Fopen(newFile, "w")
		Pool.DumpPoolJSON(fp, vv.Test, 0)
		fp.Close()
		newData, err := ioutil.ReadFile(newFile)
		if err != nil {
			panic("unable to read file, " + cmpFile)
		}

		if com.Exists(cmpFile) {
			ref, err := ioutil.ReadFile(cmpFile)
			if err != nil {
				panic("unable to read file, " + cmpFile)
			}
			if string(ref) != string(newData) {
				c.Check(string(newData), Equals, string(ref))
				fmt.Printf("%sError%s: Test case %s failed to match\n", com.Red, com.Reset, vv.Test)
				n_err++
			}
		} else {
			n_skip++
		}

		gv, _ := com.Fopen(gvFile, "w")
		Pool.GenerateGVFile(gv, vv.Test, 0)
		gv.Close()

		_, err = exec.Command("/usr/local/bin/dot", "-Tsvg", "-o"+svgFile, gvFile).Output()
		if err != nil {
			fmt.Printf("Error from dot, %s\n", err)
		}
	}

	// ------------------------- ------------------------- --------------------------------------- ---------------------------------------
	// Test as a single machine
	// ------------------------- ------------------------- --------------------------------------- ---------------------------------------

	if n_skip > 0 {
		fmt.Fprintf(os.Stderr, "%sSkipped, # of files without automated checks = %d%s\n", com.Yellow, n_skip, com.Reset)
		com.DbPrintf("debug", "\n\n%sSkipped, # of files without automated checks = %d%s\n", com.Yellow, n_skip, com.Reset)
	}
	if n_err > 0 {
		fmt.Fprintf(os.Stderr, "%sFailed, # of errors = %d%s\n", com.Red, n_err, com.Reset)
		com.DbPrintf("debug", "\n\n%sFailed, # of errors = %d%s\n", com.Red, n_err, com.Reset)
	} else {
		fmt.Fprintf(os.Stderr, "%sPASS%s\n", com.Green, com.Reset)
		com.DbPrintf("debug", "\n\n%sPASS%s\n", com.Green, com.Reset)
	}

	_ = n_skip
}