public void Validate(Metadata metadata)
        {
            var mod = CkanModule.FromJson(metadata.Json().ToString());

            if (!mod.IsDLC)
            {
                var file = _http.DownloadModule(metadata);

                // Make sure this would actually generate an install.
                if (!_moduleService.HasInstallableFiles(mod, file))
                {
                    throw new Kraken(string.Format(
                                         "Module contains no files matching: {0}",
                                         mod.DescribeInstallStanzas()
                                         ));
                }

                // Make sure no paths include GameData other than at the start
                var gamedatas = _moduleService.FileDestinations(mod, file)
                                .Where(p => p.StartsWith("GameData") && p.LastIndexOf("/GameData/") > 0)
                                .ToList();
                if (gamedatas.Any())
                {
                    var badPaths = string.Join("\r\n", gamedatas);
                    throw new Kraken($"GameData directory found within GameData:\r\n{badPaths}");
                }
            }
        }
Beispiel #2
0
        public void Validate(Metadata metadata)
        {
            var mod = CkanModule.FromJson(metadata.Json().ToString());

            if (!mod.IsDLC)
            {
                var file = _http.DownloadModule(metadata);

                // Make sure this would actually generate an install.
                if (!_moduleService.HasInstallableFiles(mod, file))
                {
                    throw new Kraken(string.Format(
                                         "Module contains no files matching: {0}",
                                         mod.DescribeInstallStanzas(new KerbalSpaceProgram())
                                         ));
                }

                // Get the files the module will install
                var allFiles = _moduleService.FileDestinations(mod, file).Memoize();

                // Make sure no paths include GameData other than at the start
                var gamedatas = allFiles
                                .Where(p => p.StartsWith("GameData") && p.LastIndexOf("/GameData/") > 0)
                                .OrderBy(f => f)
                                .ToList();
                if (gamedatas.Any())
                {
                    var badPaths = string.Join("\r\n", gamedatas);
                    throw new Kraken($"GameData directory found within GameData:\r\n{badPaths}");
                }

                // Make sure we won't try to overwrite our own files
                var duplicates = allFiles
                                 .GroupBy(f => f)
                                 .SelectMany(grp => grp.Skip(1).OrderBy(f => f))
                                 .ToList();
                if (duplicates.Any())
                {
                    var badPaths = string.Join("\r\n", duplicates);
                    throw new Kraken($"Multiple files attempted to install to:\r\n{badPaths}");
                }
            }
        }
        public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts)
        {
            if (metadata.Download != null)
            {
                var json = metadata.Json();

                string file = _http.DownloadModule(metadata);

                var internalJson = _moduleService.GetInternalCkan(file);

                if (internalJson != null)
                {
                    Log.InfoFormat("Executing internal CKAN transformation with {0}", metadata.Kref);
                    Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                    foreach (var property in internalJson.Properties())
                    {
                        // We've already got the file, too late to tell us where it lives
                        if (property.Name == "$kref")
                        {
                            Log.DebugFormat("Skipping $kref property: {0}", property.Value);
                            continue;
                        }
                        json.SafeAdd(property.Name, property.Value);
                    }

                    json["spec_version"] = ModuleVersion.Max(metadata.SpecVersion, new Metadata(internalJson).SpecVersion)
                                           .ToSpecVersionJson();

                    json.SafeMerge("resources", internalJson["resources"]);

                    Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);
                }

                yield return(new Metadata(json));
            }
            else
            {
                yield return(metadata);
            }
        }
Beispiel #4
0
        public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts)
        {
            if (metadata.Download != null)
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing Download attribute transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                string file = _http.DownloadModule(metadata);

                if (file != null)
                {
                    Log.Debug("Calculating download size...");
                    json["download_size"] = _fileService.GetSizeBytes(file);

                    json["download_hash"] = new JObject();

                    var download_hashJson = (JObject)json["download_hash"];
                    Log.Debug("Calculating download SHA1...");
                    download_hashJson.SafeAdd("sha1", _fileService.GetFileHashSha1(file));
                    Log.Debug("Calculating download SHA256...");
                    download_hashJson.SafeAdd("sha256", _fileService.GetFileHashSha256(file));

                    Log.Debug("Calculating download MIME type...");
                    json["download_content_type"] = _fileService.GetMimetype(file);
                }

                Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);

                yield return(new Metadata(json));
            }
            else
            {
                yield return(metadata);
            }
        }
        public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts)
        {
            _vrefValidator.Validate(metadata);

            if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc")
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing internal AVC transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                var noVersion = metadata.Version == null;

                if (noVersion)
                {
                    json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw
                }

                var mod = CkanModule.FromJson(json.ToString());

                if (noVersion)
                {
                    json.Remove("version");
                }

                var file = _http.DownloadModule(metadata);
                var avc  = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id);

                if (avc != null)
                {
                    Log.Info("Found internal AVC version file");

                    var resourcesJson = (JObject)json["resources"];
                    var remoteUri     = resourcesJson?["remote-avc"] != null
                        ? new Uri((string)resourcesJson["remote-avc"])
                        : GetRemoteAvcUri(avc);

                    if (remoteUri != null)
                    {
                        if (resourcesJson == null)
                        {
                            json["resources"] = resourcesJson = new JObject();
                        }
                        resourcesJson.SafeAdd("remote-avc", remoteUri.OriginalString);

                        try
                        {
                            var remoteJson = _github?.DownloadText(remoteUri)
                                             ?? _http.DownloadText(remoteUri);
                            var remoteAvc = JsonConvert.DeserializeObject <AvcVersion>(remoteJson);

                            if (avc.version.CompareTo(remoteAvc.version) == 0)
                            {
                                // Local AVC and Remote AVC describe the same version, prefer
                                Log.Info("Remote AVC version file describes same version as local AVC version file, using it preferentially.");
                                avc = remoteAvc;
                            }
                        }
                        catch (JsonReaderException e)
                        {
                            Log.WarnFormat("Error parsing remote version file {0}: {1}",
                                           remoteUri, e.Message);
                            Log.Debug(e);
                        }
                        catch (Exception e)
                        {
                            Log.WarnFormat("Error fetching remote version file {0}: {1}", remoteUri, e.Message);
                            Log.Debug(e);
                        }
                    }

                    ApplyVersions(json, avc);

                    // It's cool if we don't have version info at all, it's optional in the AVC spec.

                    Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);
                }

                yield return(new Metadata(json));
            }
            else
            {
                yield return(metadata);
            }
        }