<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using NuGet.Common;

namespace NuGet.Commands {
    [Command(typeof(NuGetResources), "push", "PushCommandDescription",
        MinArgs = 1, MaxArgs = 2, UsageDescriptionResourceName = "PushCommandUsageDescription",
        UsageSummaryResourceName = "PushCommandUsageSummary")]
    public class PushCommand : Command {
        [Option(typeof(NuGetResources), "PushCommandCreateOnlyDescription", AltName = "co")]
        public bool CreateOnly { get; set; }

        [Option(typeof(NuGetResources), "PushCommandSourceDescription", AltName = "src")]
        public string Source { get; set; }

        public override void ExecuteCommand() {
            // Frist argument should be the package
            string packagePath = Arguments[0];

            // Don't push symbols by default
            bool pushSymbols = false;
            string source = null;

            if (!String.IsNullOrEmpty(Source)) {
                CommandLineUtility.ValidateSource(Source);
                source = Source;
            }
            else {
                if (packagePath.EndsWith(PackCommand.SymbolsExtension, StringComparison.OrdinalIgnoreCase)) {
                    source = GalleryServer.DefaultSymbolServerUrl;
                }
                else {
                    source = GalleryServer.DefaultGalleryServerUrl;
                    pushSymbols = true;
                }
            }

            PushPackage(packagePath, source);

            if (pushSymbols) {
                // Get the symbol package for this package
                string symbolPackagePath = GetSymbolsPath(packagePath);

                // Push the symbols package if it exists
                if (File.Exists(symbolPackagePath)) {
                    source = GalleryServer.DefaultSymbolServerUrl;

                    // See if the api key exists
                    string apiKey = GetApiKey(source, configOnly: true, throwIfNotFound: false);

                    if (String.IsNullOrEmpty(apiKey)) {
                        Console.WriteWarning(NuGetResources.Warning_SymbolServerNotConfigured, Path.GetFileName(symbolPackagePath), NuGetResources.DefaultSymbolServer);
                    }
                    else {
                        PushPackage(symbolPackagePath, source, apiKey);
                    }
                }
            }
        }

        /// <summary>
        /// Get the symbols package from the original package. Removes the .nupkg and adds .symbols.nupkg
        /// </summary>
        private string GetSymbolsPath(string packagePath) {
            string symbolPath = Path.GetFileNameWithoutExtension(packagePath) + PackCommand.SymbolsExtension;
            string packageDir = Path.GetDirectoryName(packagePath);
            return Path.Combine(packageDir, symbolPath);
        }

        private void PushPackage(string packagePath, string source, string apiKey = null) {
            var gallery = new GalleryServer(source);

            // Use the specified api key or fall back to default behavior
            apiKey = apiKey ?? GetApiKey(source);

            // Push the package to the server
            var package = new ZipPackage(packagePath);

            Console.WriteLine(NuGetResources.PushCommandPushingPackage, package.GetFullName(), CommandLineUtility.GetSourceDisplayName(source));

            using (Stream stream = package.GetStream()) {
                gallery.CreatePackage(apiKey, stream);
            }

            // Publish the package on the server
            if (!CreateOnly) {
                var cmd = new PublishCommand();
                cmd.Console = Console;
                cmd.Source = source;
                cmd.Arguments = new List<string> { package.Id, package.Version.ToString(), apiKey };
                cmd.Execute();
            }
            else {
                Console.WriteLine(NuGetResources.PushCommandPackageCreated, source);
            }
        }

        private string GetApiKey(string source, bool configOnly = false, bool throwIfNotFound = true) {
            string apiKey = null;

            if (!configOnly) {
                // Second argument, if present, should be the API Key
                if (Arguments.Count > 1) {
                    apiKey = Arguments[1];
                }
            }
<commit_msg>Use specified API key for symbol server. Work items: 959<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using NuGet.Common;

namespace NuGet.Commands {
    [Command(typeof(NuGetResources), "push", "PushCommandDescription",
        MinArgs = 1, MaxArgs = 2, UsageDescriptionResourceName = "PushCommandUsageDescription",
        UsageSummaryResourceName = "PushCommandUsageSummary")]
    public class PushCommand : Command {
        [Option(typeof(NuGetResources), "PushCommandCreateOnlyDescription", AltName = "co")]
        public bool CreateOnly { get; set; }

        [Option(typeof(NuGetResources), "PushCommandSourceDescription", AltName = "src")]
        public string Source { get; set; }

        public override void ExecuteCommand() {
            // Frist argument should be the package
            string packagePath = Arguments[0];

            // Don't push symbols by default
            bool pushSymbols = false;
            string source = null;

            if (!String.IsNullOrEmpty(Source)) {
                CommandLineUtility.ValidateSource(Source);
                source = Source;
            }
            else {
                if (packagePath.EndsWith(PackCommand.SymbolsExtension, StringComparison.OrdinalIgnoreCase)) {
                    source = GalleryServer.DefaultSymbolServerUrl;
                }
                else {
                    source = GalleryServer.DefaultGalleryServerUrl;
                    pushSymbols = true;
                }
            }

            PushPackage(packagePath, source);

            if (pushSymbols) {
                // Get the symbol package for this package
                string symbolPackagePath = GetSymbolsPath(packagePath);

                // Push the symbols package if it exists
                if (File.Exists(symbolPackagePath)) {
                    source = GalleryServer.DefaultSymbolServerUrl;

                    // See if the api key exists
                    string apiKey = GetApiKey(source, throwIfNotFound: false);

                    if (String.IsNullOrEmpty(apiKey)) {
                        Console.WriteWarning(NuGetResources.Warning_SymbolServerNotConfigured, Path.GetFileName(symbolPackagePath), NuGetResources.DefaultSymbolServer);
                    }
                    else {
                        PushPackage(symbolPackagePath, source, apiKey);
                    }
                }
            }
        }

        /// <summary>
        /// Get the symbols package from the original package. Removes the .nupkg and adds .symbols.nupkg
        /// </summary>
        private string GetSymbolsPath(string packagePath) {
            string symbolPath = Path.GetFileNameWithoutExtension(packagePath) + PackCommand.SymbolsExtension;
            string packageDir = Path.GetDirectoryName(packagePath);
            return Path.Combine(packageDir, symbolPath);
        }

        private void PushPackage(string packagePath, string source, string apiKey = null) {
            var gallery = new GalleryServer(source);

            // Use the specified api key or fall back to default behavior
            apiKey = apiKey ?? GetApiKey(source);

            // Push the package to the server
            var package = new ZipPackage(packagePath);

            Console.WriteLine(NuGetResources.PushCommandPushingPackage, package.GetFullName(), CommandLineUtility.GetSourceDisplayName(source));

            using (Stream stream = package.GetStream()) {
                gallery.CreatePackage(apiKey, stream);
            }

            // Publish the package on the server
            if (!CreateOnly) {
                var cmd = new PublishCommand();
                cmd.Console = Console;
                cmd.Source = source;
                cmd.Arguments = new List<string> { package.Id, package.Version.ToString(), apiKey };
                cmd.Execute();
            }
            else {
                Console.WriteLine(NuGetResources.PushCommandPackageCreated, source);
            }
        }

        private string GetApiKey(string source, bool throwIfNotFound = true) {
            string apiKey = null;

            // Second argument, if present, should be the API Key
            if (Arguments.Count > 1) {
                apiKey = Arguments[1];
            }

{'fraction_non_alphanumeric': 0.07958079004952205, 'fraction_numerical': 0.0014971783945640907, 'mean_word_length': 1.8351289585373816, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6187961', 'n_tokens_mistral': 2076, 'n_tokens_neox': 1952, 'n_words': 524}