private static void downloadPackage(string prefix, ResourceType resourceType, string resourceName, string version) { try { // check that resource is on server var availableResources = NetworkUtils.getAvailableResources(prefix, resourceType); if (!availableResources.resourceDescriptions.ContainsKey(resourceName)) { CLIInterface.logError($"Could not find {resourceType.ToString()} resource with name {resourceName} on server"); return; } // check that resource on server has specified version var versionInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName); if (!versionInfo.versions.ContainsKey(version)) { CLIInterface.logError( $"Could not find version {version} on server. These are the version(s) available: {string.Join(", ", versionInfo.versions.Keys.ToList())}" ); return; } using (FileStream resourceFileStream = FSOps.createResourceFile(resourceType, resourceName)) using (StreamWriter versionFileStream = FSOps.createOrOverwriteResourceVersionFile(resourceType, resourceName)) { Task resourceFileTask = NetworkUtils.downloadResource(prefix, resourceType, resourceName, version, resourceFileStream); Task resourceVersionTask = Task.Factory.StartNew(() => { versionFileStream.WriteLine(version); }); Task.WaitAll(resourceFileTask, resourceVersionTask); } CLIInterface.logLine($"{resourceType} resource {resourceName} added"); } catch (NetworkUtils.NetworkUtilsException ex) { CLIInterface.logError($"Network Error: {ex.Message}"); } catch (FSOps.FSOpsException ex) { CLIInterface.logError($"File System Error: " + ex.Message); } }
public static void publishResource(string prefix, ResourceIdentifier resourceDescription, IEnumerable <ResourceIdentifier> deps) { try { PublishDepsInfoContainer publishDepsInfo = new PublishDepsInfoContainer(); foreach (ResourceIdentifier depDescription in deps) { if (depDescription.version == null) { CLIInterface.logError( "The versions of dependencies must be supplied. For example, " + "\"dependency.csv\" does not include version, \"[email protected]\" does." ); return; } var publishDepDescription = new PublishDepsInfoContainer.PublishDepDescription(depDescription.version); if (depDescription.resourceType == ResourceType.Code) { publishDepsInfo.codeDeps[depDescription.resourceName] = publishDepDescription; } else if (depDescription.resourceType == ResourceType.Data) { publishDepsInfo.dataDeps[depDescription.resourceName] = publishDepDescription; } else if (depDescription.resourceType == ResourceType.Model) { publishDepsInfo.modelDeps[depDescription.resourceName] = publishDepDescription; } } string resourceName = resourceDescription.resourceName; ResourceType resourceType = resourceDescription.resourceType; string publishVersion = resourceDescription.version; // check that user has provided version to publish file as if (publishVersion == null) { publishVersion = "1.0"; CLIInterface.logLine($"Using default version {publishVersion}"); } if (!FSOps.hasNecessaryDirs()) { CLIInterface.logError($"Could not find nyoka resource folders in current directory. Try running {ConstStrings.APPLICATION_ALIAS} {CLIParserNS.InitOptions.description.name}?"); return; } // If a file to publish with the given name can't be found if (!FSOps.resourceFileExists(resourceType, resourceName)) { CLIInterface.logError($"Resource with name {resourceName} not found."); return; } var resourcesOnServer = NetworkUtils.getAvailableResources(prefix, resourceType); // If this resource already exists on server if (resourcesOnServer.resourceDescriptions.ContainsKey(resourceName)) { ResourceVersionsInfoContainer serverVersionsInfo = NetworkUtils.getResourceVersions(prefix, resourceType, resourceName); // If this resource exists with the same version on server if (serverVersionsInfo.versions.ContainsKey(publishVersion)) { bool continueAnyways = CLIInterface.askYesOrNo( $"Version {publishVersion} of {resourceType.ToString()} resource " + $"{resourceName} already exists on server. Overwrite?" ); if (!continueAnyways) { CLIInterface.logLine("Aborting publish."); return; } else { CLIInterface.logLine("Overwriting resource on server."); } } } CLIInterface.logLine("Opening file."); FileStream fileStream = FSOps.readResourceFile(resourceType, resourceName); CLIInterface.logLine("Uploading file."); NetworkUtils.publishResource( prefix, fileStream, resourceType, resourceName, publishVersion, publishDepsInfo ); // create or overwrite version file locally for this resource to be the publishVersion using (var versionFileStream = FSOps.createOrOverwriteResourceVersionFile(resourceType, resourceName)) { versionFileStream.WriteLine(publishVersion); } } catch (FSOps.FSOpsException ex) { CLIInterface.logError($"File System Error: " + ex.Message); } catch (NetworkUtils.NetworkUtilsException ex) { CLIInterface.logError($"Network Error: {ex.Message}"); } }