Ejemplo n.º 1
0
        /// <summary>
        /// This function does not fully validate the file against a Ckan schema.
        /// What it foes do is validate the format enough to be sure the file
        /// looks like std format Ckan file such as netkan produces.
        /// </summary>
        /// <remarks>
        /// There exist other json comaptible files (with strange 100% optional
        /// white space structures) that would validate as correct versus
        /// the json schemea that this code rejects, as it is too visually
        /// different and this code thus this code 'decides' that a human probably wont
        /// have properly validated the input file.
        /// </remarks>
        /// <returns>returns true if the file is sufficiently valid for our purposes.</returns>
        public bool validation(bool echo = false)
        {
            CkanLocaliserClass.DoingWhat.Push("Validation");
            // reset parser state.
            Level1Name.Clear();
            FileIsValid    = true;
            Level          = 0;
            AllowEOLs      = true;
            EOLMissedCount = 0;
            EOLExtraCount  = 0;

            // grammar:
            // <CkanFile>    := <CompoundValue> <EOF>
            Curs = TokFile.getCursor(echo);
            try
            {
                parseCompound();

                // grammar:
                // <EOF> := <EOL> >spaces tabs etc< / physical end of file.
                // <EOF> := >spaces tabs etc< / physical end of file.
                if (Curs.TokenObj.isCategory(TokenCategory.tokEOL))
                {   // alow 1 optional EOL then expect the EOF.
                    expectToken(TokenCategory.tokEOL);
                }
                expectToken(TokenCategory.tokEOF); // may have leading spaces.
            } catch (FormatException fe)
            {
                Console.WriteLine($"While Doing : {CkanLocaliserClass.WhatString()} ");
                Console.WriteLine(fe.Cause);
                FileIsValid = false;
                CkanLocaliserClass.DoingWhat.Pop();
                return(false);
            }
            CkanLocaliserClass.DoingWhat.Pop();
            return(true);
        }
Ejemplo n.º 2
0
        // static string[] NameFields = { "\"spec_version\"", "\"identifier\"", "\"name\"", "\"author\"", "\"version\"",
        //     "\"ksp_version_min\"", "\"ksp_version_max\"", "\"license\"", "\"provides\"", "\"conflicts\"" };
        /// <summary>
        /// This function validates the localises one specified input ckan File and generates/overwrites one output ckanfile
        /// CkanLocaliser Localise  AbsPathToMod SrcFile.ckan  DestFile.ckan [options].
        /// </summary>
        static public int Localise(string[] args)
        {
            CkanLocaliserClass.DoingWhat.Push("Localise");

            // Validate command line Parameters.
            if (args.Length < 4)
            {
                Usagesmsg();
                return(-1);
            }
            if (true)
            {
                using (StreamWriter sw = File.CreateText(args[3]))
                {   // This makes sure that if we can to avoid confusion the outfile does in no sense look valid
                    sw.WriteLine(" { Localise error } ");
                    sw.Close();
                }
            }


            if (!args[1].EndsWith(".zip"))
            {
                Console.Error.WriteLine("ModFile MUSTR be a zip File");
                Usagesmsg();
                return(-1);
            }
            DownloadPath = args[1];
            if (DoAWhiteBoxtest)
            {   // This allows test code to skip having an actual binary zip file.
                DownloadSize = 9999;
                SHA1         = "F550FBBEF92224DD32E1E0D045BAA8C95EF45343";
                SHA256       = "D51D322B8F68FCDE25FCF4334327C0B86AAE0A65E9B2B70B1B03ECC7761AFD2C";
            }
            else
            {
                FileInfo fi1 = new FileInfo(DownloadPath);
                if (!fi1.Exists)
                {
                    Console.Error.WriteLine("ModFile Must exist");
                    Usagesmsg();
                    return(-1);
                }
                if (!Path.IsPathRooted(DownloadPath))
                {
                    throw new FormatException($"Fatal Error: DownLoad path must be an absolute rooted path \"{DownloadPath}\" is not.", true);
                }
                DownloadSize = fi1.Length;

                SHA1   = GetFileHashSha1(DownloadPath);
                SHA256 = GetFileHashSha256(DownloadPath);
            }

            FileInfo fi2 = new FileInfo(args[2]);

            if (!fi2.Exists)
            {
                Console.Error.WriteLine("SrcFile Must exist");
                Console.Error.WriteLine(" PWD is :" + Directory.GetCurrentDirectory());
                Usagesmsg();
                return(-1);
            }
            // Validate Ckan File

            CkanTokeniser CT = new CkanTokeniser(args[2]);
            // CT.AllowSlashN = true;
            TokenFile Foo = new TokenFile(CT);

            Foo.parse();
            CKanFormat bar = new CKanFormat(Foo);

            bar.AllowEOLs = true;

            if (!bar.validation())
            {
                string DL = "\n====================================\n";
                Console.Error.WriteLine($"\n\n{DL}### Validation Error: Parsing Failure in File: <{args[2]}> \n Context in which That happened {DL}");
                bar.validation(true);
                return(-1);
            }
            // do The localisation and write the file
            try
            {   // First tag all three common human readable Fields to identify That we are localised
                CkanLocaliserClass.DoingWhat.Push("RewritingFields");
                parseToValueFor(bar, "\"identifier\"");
                OldIdentifier = bar.Curs.TokenObj.theToken;                                                 // Preserve what it was Called We will need that later
                bar.Curs.TokenObj.theToken = "\"" + Prefix + OldIdentifier.Substring(1);                    // prepend the Localising prefix
                                                                                                            //                Console.WriteLine(Curs.TokenObj.theToken);
                parseToValueFor(bar, "\"name\"");
                bar.Curs.TokenObj.theToken = "\"" + Prefix + ":" + bar.Curs.TokenObj.theToken.Substring(1); // prepend the Localising prefix

                parseToValueFor(bar, "\"abstract\"");
                bar.Curs.TokenObj.theToken = "\"" + Prefix + ":" + bar.Curs.TokenObj.theToken.Substring(1); // prepend the Localising prefix

                // Now Replace download
                parseToValueFor(bar, "\"download\"");
                System.Uri DldURI = new System.Uri(DownloadPath);              // Convert it to a URI
                bar.Curs.TokenObj.theToken = "\"" + DldURI.AbsoluteUri + "\""; // replace the URI

                // Now Replace download_Size
                parseToValueFor(bar, "\"download_size\"");
                bar.Curs.TokenObj.theToken = DownloadSize.ToString();

                // Now Replace download_Size
                if (bar.hasANameField("\"x_generated_by\"") == true)
                {
                    parseToValueFor(bar, "\"x_generated_by\"");
                    bar.Curs.TokenObj.theToken = "\"CkanLocaliser\"";
                }
                // Now for the not so easy bits  Values in CompoundTypes.
                CkanLocaliserClass.DoingWhat.Pop();
                CkanLocaliserClass.DoingWhat.Push("Wiping_Resoruces_Links");
                killResourcesLinks(bar);

                CkanLocaliserClass.DoingWhat.Pop();
                CkanLocaliserClass.DoingWhat.Push("Fixing_Hashes");
                fixHashValues(bar);
                // Now for the hard bits
                // done backwards through the file.. just to feel safer
                CkanLocaliserClass.DoingWhat.Pop();

                // Insert "Conflicts" preceded by "Provides" just after "license"
                InsertProvidesConflicts(bar);
                // Insert an author either makign the string into an array and/or prepending our Author string

                CkanLocaliserClass.DoingWhat.Push("Adding_Author");

                PieceOfPaper p = new PieceOfPaper();
                p.valueToAdd        = Stringify(Author);
                p.listToAddItTo     = "\"author\"";
                p.thingToAdditAfter = new List <string>();
                p.thingToAdditAfter.Add("\"abstract\"");
                p.EndProvidesLineNo  = 0;
                p.EndProvidesTokenNo = 0;
                fixValueInList(bar, ref p);

                CkanLocaliserClass.DoingWhat.Pop();
            }
            catch (FormatException fe)
            {
                if (fe.KnownReal == false)
                {   // EG: we know it is real if we try to localise ckan file and it has no "identifier" Name:Value pair   etc.
                    Console.WriteLine("CAVEAT: There is reasonably good chance th following is a code error.");
                    Console.WriteLine("CAVEAT: The code has been mainly/entirely checking things we thoguht we just checked already.");
                    Console.WriteLine("CAVEAT: ALL examples of ckan files (schema valid or not) that do this when localised gratefully appreciated.");
                }
                Console.WriteLine($"While Doing : {CkanLocaliserClass.WhatString()} ");
                Console.WriteLine(fe.Cause);

                return(-1);
            }
            if (true)
            {
                using (StreamWriter sw = File.CreateText(args[3]))
                {
                    Foo.writeTo(sw);
                    sw.Close();
                }
            }
            CkanLocaliserClass.DoingWhat.Pop();

            return(0);
        }