public static void SendHeartbeat(string windowTitle)
        {
            PythonCliParameters.Key    = ApiKey;
            PythonCliParameters.Entity = windowTitle;
            PythonCliParameters.Plugin = $"{WakaTimeConstants.PluginName}/{_version}";

            var pythonBinary = PythonManager.GetPython();

            if (pythonBinary != null)
            {
                var process = new RunProcess(pythonBinary, PythonCliParameters.ToArray());
                if (Debug)
                {
                    Logger.Debug($"[\"{pythonBinary}\", \"{string.Join("\", \"", PythonCliParameters.ToArray(true))}\"]");
                    process.Run();
                    Logger.Debug($"CLI STDOUT: {process.Output}");
                    Logger.Debug($"CLI STDERR: {process.Error}");
                }
                else
                {
                    process.RunInBackground();
                }

                if (!process.Success)
                {
                    Logger.Error($"Could not send heartbeat: {process.Error}");
                }
            }
            else
            {
                Logger.Error("Could not send heartbeat because python is not installed");
            }
        }
示例#2
0
        static string GetEmbeddedPath()
        {
            var appDataPath = GetAppDataDirectory();
            var path        = Path.Combine(appDataPath, "python");

            path = Path.Combine(path, "pythonw");

            try
            {
                var process = new RunProcess(path, "--version");

                process.Run();

                if (!process.Success)
                {
                    return(null);
                }

                Logger.Debug(string.Format("Python found from embedded location: {0}", path));

                return(path);
            }
            catch (Exception ex)
            {
                Logger.Error("GetEmbeddedPath:", ex);
                return(null);
            }
        }
        public static void SendHeartbeat(string windowTitle)
        {
            PythonCliParameters.Key = ApiKey;
            PythonCliParameters.Entity = windowTitle;
            PythonCliParameters.Plugin = $"{WakaTimeConstants.PluginName}/{_version}";

            var pythonBinary = PythonManager.GetPython();
            if (pythonBinary != null)
            {
                var process = new RunProcess(pythonBinary, PythonCliParameters.ToArray());
                if (Debug)
                {
                    Logger.Debug($"[\"{pythonBinary}\", \"{string.Join("\", \"", PythonCliParameters.ToArray(true))}\"]");
                    process.Run();
                    Logger.Debug($"CLI STDOUT: {process.Output}");
                    Logger.Debug($"CLI STDERR: {process.Error}");
                }
                else
                    process.RunInBackground();

                if (!process.Success)
                    Logger.Error($"Could not send heartbeat: {process.Error}");
            }
            else
                Logger.Error("Could not send heartbeat because python is not installed");
        }
        static bool IsCliLatestVersion()
        {
            var process = new RunProcess(PythonManager.GetPython(), PythonCliParameters.Cli, "--version");

            process.Run();

            if (process.Success)
            {
                var currentVersion = process.Error.Trim();
                Logger.Info(string.Format("Current wakatime-cli version is {0}", currentVersion));

                Logger.Info("Checking for updates to wakatime-cli...");
                var latestVersion = WakaTimeConstants.LatestWakaTimeCliVersion();

                if (currentVersion.Equals(latestVersion))
                {
                    Logger.Info("wakatime-cli is up to date.");
                    return(true);
                }
                else
                {
                    Logger.Info(string.Format("Found an updated wakatime-cli v{0}", latestVersion));
                }
            }
            return(false);
        }
示例#5
0
        internal static string GetPythonPathFromFixedPath()
        {
            List <string> locations = new List <string>();

            for (int i = 26; i <= 50; i++)
            {
                locations.Add(Path.Combine("\\python" + i, "pythonw"));
                locations.Add(Path.Combine("\\Python" + i, "pythonw"));
            }

            foreach (var location in locations)
            {
                try {
                    var process = new RunProcess(location, "--version");
                    process.Run();

                    if (!process.Success)
                    {
                        continue;
                    }
                } catch { /*ignored*/ }

                Logger.Debug(string.Format("Python found by Fixed Path: {0}", location));

                return(location);
            }

            return(null);
        }
示例#6
0
        internal static string GetEmbeddedPath()
        {
            var path = Path.Combine(WakaTimeConstants.PluginConfigDir, "python", "pythonw");

            try
            {
                var process = new RunProcess(path, "--version");

                process.Run();

                if (!process.Success)
                {
                    return(null);
                }

                Logger.Debug(string.Format("Python found from embedded location: {0}", path));

                return(path);
            }
            catch (Exception ex)
            {
                Logger.Error("GetEmbeddedPath:", ex);
                return(null);
            }
        }
        static string GetPathFromMicrosoftRegistry() {
            try {
                var regex = new Regex(@"""([^""]*)\\([^""\\]+(?:\.[^"".\\]+))""");
                var pythonKey = Registry.ClassesRoot.OpenSubKey(@"Python.File\shell\open\command");
                if (pythonKey == null) {
                    Logger.Log("Couldn't find python's path through Microsft Registry. Please try repairing your Python installation.");
                    return null;
                }
                var python = pythonKey.GetValue(null).ToString();
                var match = regex.Match(python);

                if (!match.Success) return null;

                var directory = match.Groups[1].Value;
                var fullPath = Path.Combine(directory, "pythonw");
                var process = new RunProcess(fullPath, "--version");

                process.Run();

                if (!process.Success)
                    return null;

                Logger.Log(string.Format("Python found from Microsoft Registry: {0}", fullPath));

                return fullPath;
            } catch (Exception ex) {
                Logger.Error("GetPathFromMicrosoftRegistry:", ex);
                return null;
            }
        }
        static string GetPathFromMicrosoftRegister()
        {
            try
            {
                var regex     = new Regex(@"""([^""]*)\\([^""\\]+(?:\.[^"".\\]+))""");
                var pythonKey = Registry.ClassesRoot.OpenSubKey(@"Python.File\shell\open\command");
                var python    = pythonKey.GetValue(null).ToString();
                var match     = regex.Match(python);

                if (!match.Success)
                {
                    return(null);
                }

                var directory = match.Groups[1].Value;
                var fullPath  = Path.Combine(directory, "pythonw");
                var process   = new RunProcess(fullPath, "--version");

                process.Run();

                if (!process.Success)
                {
                    return(null);
                }

                Logger.Debug($"Python found by Microsoft Register: {fullPath}");

                return(fullPath);
            }
            catch (Exception ex)
            {
                Logger.Error("GetPathFromMicrosoftRegister:", ex);
                return(null);
            }
        }
        public static void SendHeartbeat(string fileName, bool isWrite)
        {
            PythonCliParameters.Key = ApiKey;
            PythonCliParameters.File = fileName;
            PythonCliParameters.Plugin = string.Format("{0}/{1} {2}/{3}", WakaTimeConstants.EditorName, _editorVersion, WakaTimeConstants.PluginName, _version);
            PythonCliParameters.IsWrite = isWrite;
            PythonCliParameters.Project = GetProjectName();

            var pythonBinary = PythonManager.GetPython();
            if (pythonBinary != null)
            {
                var process = new RunProcess(pythonBinary, PythonCliParameters.ToArray());
                if (Debug)
                {
                    Logger.Debug(string.Format("[\"{0}\", \"{1}\"]", pythonBinary, string.Join("\", \"", PythonCliParameters.ToArray(true))));
                    process.Run();
                    Logger.Debug(string.Format("CLI STDOUT: {0}", process.Output));
                    Logger.Debug(string.Format("CLI STDERR: {0}", process.Error));
                }
                else
                    process.RunInBackground();

                if(!process.Success)
                    Logger.Error(string.Format("Could not send heartbeat: {0}", process.Error));
            }
            else
                Logger.Error("Could not send heartbeat because python is not installed");
        }
示例#10
0
        public static void SendHeartbeat(PythonCliParameters cliParameters)
        {
            var pythonBinary = PythonManager.GetPython();

            if (pythonBinary != null)
            {
                var process = new RunProcess(pythonBinary, cliParameters.ToArray());
                if (WakaTimeConfigFile.Debug)
                {
                    Logger.Debug(string.Format("[\"{0}\", \"{1}\"]", pythonBinary, string.Join("\", \"", cliParameters.ToArray(true))));
                    process.Run();
                    Logger.Debug(string.Format("CLI STDOUT: {0}", process.Output));
                    Logger.Debug(string.Format("CLI STDERR: {0}", process.Error));
                }
                else
                {
                    process.RunInBackground();
                }

                if (!process.Success)
                {
                    Logger.Error(string.Format("Could not send heartbeat: {0}", process.Error));
                }
            }
            else
            {
                Logger.Error("Could not send heartbeat because python is not installed");
            }
        }
        public static void SendHeartbeat(string fileName, bool isWrite)
        {
            PythonCliParameters.Key     = ApiKey;
            PythonCliParameters.File    = fileName;
            PythonCliParameters.Plugin  = string.Format("{0}/{1} {2}/{3}", WakaTimeConstants.EditorName, WakaTimeConstants.EditorVersion, WakaTimeConstants.PluginName, WakaTimeConstants.PluginVersion);
            PythonCliParameters.IsWrite = isWrite;
            PythonCliParameters.Project = GetProjectName();

            var pythonBinary = PythonManager.GetPython();

            if (pythonBinary != null)
            {
                var process = new RunProcess(pythonBinary, PythonCliParameters.ToArray());
                if (Debug)
                {
                    Logger.Debug(string.Format("[\"{0}\", \"{1}\"]", pythonBinary, string.Join("\", \"", PythonCliParameters.ToArray(true))));
                    process.Run();
                    Logger.Debug(string.Format("CLI STDOUT: {0}", process.Output));
                    Logger.Debug(string.Format("CLI STDERR: {0}", process.Error));
                }
                else
                {
                    process.RunInBackground();
                }

                if (!process.Success)
                {
                    Logger.Error(string.Format("Could not send heartbeat: {0}", process.Error));
                }
            }
            else
            {
                Logger.Error("Could not send heartbeat because python is not installed");
            }
        }
示例#12
0
        static bool IsCliLatestVersion()
        {
            var process = new RunProcess(PythonManager.GetPython(), GetCliPath(), "--version");
            process.Run();

            var version = GetLatestWakaTimeCliVersion();
            return process.Success && process.Error.Equals(version.ToString());
        }
示例#13
0
        static bool IsCliLatestVersion()
        {
            var process = new RunProcess(PythonManager.GetPython(), PythonCliParameters.Cli, "--version");

            process.Run();

            var wakatimeVersion = WakaTimeConstants.CurrentWakaTimeCliVersion();

            return(process.Success && process.Error.Equals(wakatimeVersion));
        }
示例#14
0
        static bool IsCliLatestVersion()
        {
            var process = new RunProcess(PythonManager.GetPython(), GetCliPath(), "--version");

            process.Run();

            var version = GetLatestWakaTimeCliVersion();

            return(process.Success && process.Error.Equals(version.ToString()));
        }
示例#15
0
        static string GetPathFromMicrosoftRegistry()
        {
#if !NETSTANDARD
            try
            {
                var regex     = new Regex(@"""([^""]*)\\([^""\\]+(?:\.[^"".\\]+))""");
                var pythonKey = Registry.ClassesRoot.OpenSubKey(@"Python.File\shell\open\command");
                if (pythonKey == null)
                {
                    Logger.Debug("Couldn't find python's path through Microsoft Registry. Please try repairing your Python installation.");
                    return(null);
                }
                var python = pythonKey.GetValue(null).ToString();
                var match  = regex.Match(python);

                if (!match.Success)
                {
                    return(null);
                }

                var directory = match.Groups[1].Value;
                var fullPath  = Path.Combine(directory, "pythonw");
                var process   = new RunProcess(fullPath, "--version");

                process.Run();

                if (!process.Success)
                {
                    return(null);
                }

                Logger.Debug(string.Format("Python found from Microsoft Registry: {0}", fullPath));

                return(fullPath);
            }
            catch (Exception ex)
            {
                Logger.Error("GetPathFromMicrosoftRegistry:", ex);
                return(null);
            }
#else
            return(null);
#endif
        }
示例#16
0
        internal static string GetEmbeddedPythonPath()
        {
            var path = Path.Combine(AppDataDirectory, "python", "pythonw");
            try
            {
                var process = new RunProcess(path, "--version");
                process.Run();

                if (!process.Success)
                    return null;

                Logger.Debug(string.Format("Python found from embedded location: {0}", path));

                return path;
            }
            catch (Exception ex)
            {
                Logger.Error("GetEmbeddedPath:", ex);
                return null;
            }
        }
        internal static string GetEmbeddedPath()
        {
            var path = Path.Combine(WakaTimeConstants.PluginConfigDir, "python", "pythonw");
            try
            {
                var process = new RunProcess(path, "--version");

                process.Run();

                if (!process.Success)
                    return null;

                Logger.Debug(string.Format("Python found from embedded location: {0}", path));

                return path;
            }
            catch (Exception ex)
            {
                Logger.Error("GetEmbeddedPath:", ex);
                return null;
            }
        }
示例#18
0
        internal static bool IsCliUpToDate()
        {
            var process = new RunProcess(Dependencies.GetPython(), CliLocation, "--version");
            process.Run();

            if (process.Success)
            {
                var currentVersion = process.Error.Trim();
                Logger.Info(string.Format("Current wakatime-cli version is {0}", currentVersion));

                Logger.Info("Checking for updates to wakatime-cli...");
                var latestVersion = Constants.LatestWakaTimeCliVersion();

                if (currentVersion.Equals(latestVersion))
                {
                    Logger.Info("wakatime-cli is up to date.");
                    return true;
                }

                Logger.Info(string.Format("Found an updated wakatime-cli v{0}", latestVersion));
            }
            return false;
        }
示例#19
0
        public static void SendHeartbeat(PythonCliParameters cliParameters)
        {
            var pythonBinary = PythonManager.GetPython();
            if (pythonBinary != null)
            {
                var process = new RunProcess(pythonBinary, cliParameters.ToArray());
                if (WakaTimeConfigFile.Debug)
                {
                    Logger.Debug(string.Format("[\"{0}\", \"{1}\"]", pythonBinary, string.Join("\", \"", cliParameters.ToArray(true))));
                    process.Run();
                    Logger.Debug(string.Format("CLI STDOUT: {0}", process.Output));
                    Logger.Debug(string.Format("CLI STDERR: {0}", process.Error));
                }
                else
                    process.RunInBackground();

                if (!process.Success)
                    Logger.Error(string.Format("Could not send heartbeat: {0}", process.Error));
            }
            else
            {
                Logger.Error("Could not send heartbeat because python is not installed");
            }
        }
        private static bool isCliLatestVersion() {
            var process = new RunProcess(PythonManager.GetPython(), PythonCliParameters.Cli, "--version");
            process.Run();

            if (process.Success) {
                var currentVersion = process.Error.Trim();
                Logger.Log(string.Format("Current wakatime-cli version is {0}", currentVersion));

                Logger.Log("Checking for updates to wakatime-cli...");
                var latestVersion = WakaTimeConstants.LatestWakaTimeCliVersion();

                if (currentVersion.Equals(latestVersion)) {
                    Logger.Log("wakatime-cli is up to date.");
                    return true;
                } else {
                    Logger.Log(string.Format("Found an updated wakatime-cli v{0}", latestVersion));
                }

            }
            return false;
        }
        static bool IsCliLatestVersion()
        {
            var process = new RunProcess(PythonManager.GetPython(), PythonCliParameters.Cli, "--version");
            process.Run();

            var wakatimeVersion = WakaTimeConstants.CurrentWakaTimeCliVersion();

            return process.Success && process.Error.Equals(wakatimeVersion);
        }
示例#22
0
        private static void ProcessHeartbeats()
        {
            var pythonBinary = PythonManager.GetPython();

#if NET35
            if (pythonBinary == null || string.IsNullOrEmpty(pythonBinary.Trim()))
#else
            if (string.IsNullOrWhiteSpace(pythonBinary))
#endif
            {
                Logger.Error("Could not send heartbeat because python is not installed");
                return;
            }

            // get first heartbeat from queue
            var gotOne = heartbeatQueue.TryDequeue(out Heartbeat heartbeat);
            if (!gotOne)
            {
                return;
            }

            // remove all extra heartbeats from queue
            var extraHeartbeats = new List <Heartbeat>();
            while (heartbeatQueue.TryDequeue(out Heartbeat hbOut))
            {
                extraHeartbeats.Add(hbOut.Clone());
            }

            var hasExtraHeartbeats = extraHeartbeats.Any();
            var cliParams          = new PythonCliParameters
            {
                Key                = WakaTimeConfigFile.ApiKey,
                Plugin             = string.Format("{0}/{1} {2}/{3}", editorInfo.Name, editorInfo.Version, editorInfo.PluginKey, editorInfo.PluginVersion),
                File               = heartbeat.FileName,
                Time               = heartbeat.Timestamp,
                IsWrite            = heartbeat.IsWrite,
                HasExtraHeartbeats = hasExtraHeartbeats,
            };

            string extraHeartbeatsJSON = null;
            if (hasExtraHeartbeats)
            {
#if NET45
                var serializer = new JavaScriptSerializer();
                serializer.RegisterConverters(new JavaScriptConverter[] { new DataContractJavaScriptConverter(true) });
                extraHeartbeatsJSON = serializer.Serialize(extraHeartbeats);
#else
                extraHeartbeatsJSON = JsonConvert.SerializeObject(extraHeartbeats, Formatting.None);
#endif
            }

            var process = new RunProcess(pythonBinary, cliParams.ToArray());
            if (WakaTimeConfigFile.Debug)
            {
                Logger.Debug(string.Format("[\"{0}\", \"{1}\"]", pythonBinary, string.Join("\", \"", cliParams.ToArray(true))));
                process.Run(extraHeartbeatsJSON);
                if (process.Output != null && process.Output != "")
                {
                    Logger.Debug(process.Output);
                }
                if (process.Error != null && process.Error != "")
                {
                    Logger.Debug(process.Error);
                }
            }
            else
            {
                process.RunInBackground(extraHeartbeatsJSON);
            }

            if (!process.Success)
            {
                Logger.Error("Could not send heartbeat.");
                if (process.Output != null && process.Output != "")
                {
                    Logger.Error(process.Output);
                }
                if (process.Error != null && process.Error != "")
                {
                    Logger.Error(process.Error);
                }
            }
        }
        static string GetPathFromFixedPath()
        {
            string[] locations =
            {
                "pythonw",
                "python",
                "\\Python37\\pythonw",
                "\\Python36\\pythonw",
                "\\Python35\\pythonw",
                "\\Python34\\pythonw",
                "\\Python33\\pythonw",
                "\\Python32\\pythonw",
                "\\Python31\\pythonw",
                "\\Python30\\pythonw",
                "\\Python27\\pythonw",
                "\\Python26\\pythonw",
                "\\python37\\pythonw",
                "\\python36\\pythonw",
                "\\python35\\pythonw",
                "\\python34\\pythonw",
                "\\python33\\pythonw",
                "\\python32\\pythonw",
                "\\python31\\pythonw",
                "\\python30\\pythonw",
                "\\python27\\pythonw",
                "\\python26\\pythonw",
                "\\Python37\\python",
                "\\Python36\\python",
                "\\Python35\\python",
                "\\Python34\\python",
                "\\Python33\\python",
                "\\Python32\\python",
                "\\Python31\\python",
                "\\Python30\\python",
                "\\Python27\\python",
                "\\Python26\\python",
                "\\python37\\python",
                "\\python36\\python",
                "\\python35\\python",
                "\\python34\\python",
                "\\python33\\python",
                "\\python32\\python",
                "\\python31\\python",
                "\\python30\\python",
                "\\python27\\python",
                "\\python26\\python",
            };

            foreach (var location in locations)
            {
                try
                {
                    var process = new RunProcess(location, "--version");
                    process.Run();

                    if (!process.Success)
                    {
                        continue;
                    }
                }
                catch { /*ignored*/ }

                Logger.Debug($"Python found by Fixed Path: {location}");

                return(location);
            }

            return(null);
        }
        private static void ProcessHeartbeats()
        {
            var pythonBinary = Dependencies.GetPython();

            if (pythonBinary != null)
            {
                // get first heartbeat from queue
                Heartbeat heartbeat;
                bool      gotOne = heartbeatQueue.TryDequeue(out heartbeat);
                if (!gotOne)
                {
                    return;
                }

                // remove all extra heartbeats from queue
                ArrayList extraHeartbeats = new ArrayList();
                Heartbeat h;
                while (heartbeatQueue.TryDequeue(out h))
                {
                    extraHeartbeats.Add(new Heartbeat(h));
                }
                bool hasExtraHeartbeats = extraHeartbeats.Count > 0;

                PythonCliParameters.Key                = ApiKey;
                PythonCliParameters.Plugin             = string.Format("{0}/{1} {2}/{3}", Constants.EditorName, Constants.EditorVersion, Constants.PluginKey, Constants.PluginVersion);
                PythonCliParameters.File               = heartbeat.entity;
                PythonCliParameters.Time               = heartbeat.timestamp;
                PythonCliParameters.IsWrite            = heartbeat.is_write;
                PythonCliParameters.HasExtraHeartbeats = hasExtraHeartbeats;

                string extraHeartbeatsJSON = null;
                if (hasExtraHeartbeats)
                {
                    extraHeartbeatsJSON = new JavaScriptSerializer().Serialize(extraHeartbeats);
                }

                var process = new RunProcess(pythonBinary, PythonCliParameters.ToArray());
                if (Debug)
                {
                    Logger.Debug(string.Format("[\"{0}\", \"{1}\"]", pythonBinary, string.Join("\", \"", PythonCliParameters.ToArray(true))));
                    process.Run(extraHeartbeatsJSON);
                    if (process.Output != null && process.Output != "")
                    {
                        Logger.Debug(process.Output);
                    }
                    if (process.Error != null && process.Error != "")
                    {
                        Logger.Debug(process.Error);
                    }
                }
                else
                {
                    process.RunInBackground(extraHeartbeatsJSON);
                }

                if (!process.Success)
                {
                    Logger.Error("Could not send heartbeat.");
                    if (process.Output != null && process.Output != "")
                    {
                        Logger.Error(process.Output);
                    }
                    if (process.Error != null && process.Error != "")
                    {
                        Logger.Error(process.Error);
                    }
                }
            }
            else
            {
                Logger.Error("Could not send heartbeat because python is not installed");
            }
        }
        static string GetPathFromFixedPath() {
            string[] locations = {
                "pythonw",
                "python",
                "\\Python37\\pythonw",
                "\\Python36\\pythonw",
                "\\Python35\\pythonw",
                "\\Python34\\pythonw",
                "\\Python33\\pythonw",
                "\\Python32\\pythonw",
                "\\Python31\\pythonw",
                "\\Python30\\pythonw",
                "\\Python27\\pythonw",
                "\\Python26\\pythonw",
                "\\python37\\pythonw",
                "\\python36\\pythonw",
                "\\python35\\pythonw",
                "\\python34\\pythonw",
                "\\python33\\pythonw",
                "\\python32\\pythonw",
                "\\python31\\pythonw",
                "\\python30\\pythonw",
                "\\python27\\pythonw",
                "\\python26\\pythonw",
                "\\Python37\\python",
                "\\Python36\\python",
                "\\Python35\\python",
                "\\Python34\\python",
                "\\Python33\\python",
                "\\Python32\\python",
                "\\Python31\\python",
                "\\Python30\\python",
                "\\Python27\\python",
                "\\Python26\\python",
                "\\python37\\python",
                "\\python36\\python",
                "\\python35\\python",
                "\\python34\\python",
                "\\python33\\python",
                "\\python32\\python",
                "\\python31\\python",
                "\\python30\\python",
                "\\python27\\python",
                "\\python26\\python",
            };

            foreach (var location in locations) {
                try {
                    var process = new RunProcess(location, "--version");
                    process.Run();

                    if (!process.Success) continue;
                } catch { /*ignored*/ }

                Logger.Log(string.Format("Python found by Fixed Path: {0}", location));

                return location;
            }

            return null;
        }
        static string GetPathFromFixedPath()
        {
            string[] locations = {
                    "pythonw",
                    "python",
                    "\\Python37\\pythonw",
                    "\\Python36\\pythonw",
                    "\\Python35\\pythonw",
                    "\\Python34\\pythonw",
                    "\\Python33\\pythonw",
                    "\\Python32\\pythonw",
                    "\\Python31\\pythonw",
                    "\\Python30\\pythonw",
                    "\\Python27\\pythonw",
                    "\\Python26\\pythonw",
                    "\\python37\\pythonw",
                    "\\python36\\pythonw",
                    "\\python35\\pythonw",
                    "\\python34\\pythonw",
                    "\\python33\\pythonw",
                    "\\python32\\pythonw",
                    "\\python31\\pythonw",
                    "\\python30\\pythonw",
                    "\\python27\\pythonw",
                    "\\python26\\pythonw",
                    "\\Python37\\python",
                    "\\Python36\\python",
                    "\\Python35\\python",
                    "\\Python34\\python",
                    "\\Python33\\python",
                    "\\Python32\\python",
                    "\\Python31\\python",
                    "\\Python30\\python",
                    "\\Python27\\python",
                    "\\Python26\\python",
                    "\\python37\\python",
                    "\\python36\\python",
                    "\\python35\\python",
                    "\\python34\\python",
                    "\\python33\\python",
                    "\\python32\\python",
                    "\\python31\\python",
                    "\\python30\\python",
                    "\\python27\\python",
                    "\\python26\\python",
                };

            foreach (var location in locations)
            {
                var process = new RunProcess(location, "--version");

                process.Run();

                if (!process.Success) continue;

                Logger.Instance.Info("Found python in standard location at: " + location.ToString());

                return location;
            }

            return null;
        }
        static string GetPathFromMicrosoftRegister()
        {
            try
            {
                var regex = new Regex(@"""([^""]*)\\([^""\\]+(?:\.[^"".\\]+))""");
                var pythonKey = Registry.ClassesRoot.OpenSubKey(@"Python.File\shell\open\command");
                var python = pythonKey.GetValue(null).ToString();
                var match = regex.Match(python);

                if (!match.Success) return null;

                var directory = match.Groups[1].Value;
                var fullPath = Path.Combine(directory, "pythonw");
                var process = new RunProcess(fullPath, "--version");

                process.Run();

                if (!process.Success)
                    return null;

                Logger.Debug("Python found by Microsoft Register: " + fullPath.ToString());

                return fullPath;
            }
            catch (Exception ex)
            {
                Logger.Error("GetPathFromMicrosoftRegister:", ex);
                return null;
            }
        }
        private void ProcessHeartbeats()
        {
            var pythonBinary = Dependencies.GetPython();
            if (pythonBinary != null)
            {
                // get first heartbeat from queue
                Heartbeat heartbeat;
                bool gotOne = heartbeatQueue.TryDequeue(out heartbeat);
                if (!gotOne)
                    return;

                // remove all extra heartbeats from queue
                ArrayList extraHeartbeats = new ArrayList();
                Heartbeat h;
                while (heartbeatQueue.TryDequeue(out h))
                    extraHeartbeats.Add(new Heartbeat(h));
                bool hasExtraHeartbeats = extraHeartbeats.Count > 0;

                PythonCliParameters.Key = ApiKey;
                PythonCliParameters.Plugin = string.Format("{0}/{1} {2}/{3}", Constants.EditorName, Constants.EditorVersion, Constants.PluginName, Constants.PluginVersion);
                PythonCliParameters.File = heartbeat.entity;
                PythonCliParameters.Time = heartbeat.timestamp;
                PythonCliParameters.IsWrite = heartbeat.is_write;
                PythonCliParameters.Project = heartbeat.project;
                PythonCliParameters.HasExtraHeartbeats = hasExtraHeartbeats;

                string extraHeartbeatsJSON = null;
                if (hasExtraHeartbeats)
                    extraHeartbeatsJSON = new JavaScriptSerializer().Serialize(extraHeartbeats);

                var process = new RunProcess(pythonBinary, PythonCliParameters.ToArray());
                if (Debug)
                {
                    Logger.Debug(string.Format("[\"{0}\", \"{1}\"]", pythonBinary, string.Join("\", \"", PythonCliParameters.ToArray(true))));
                    process.Run(extraHeartbeatsJSON);
                    if (process.Output != null && process.Output != "")
                        Logger.Debug(process.Output);
                    if (process.Error != null && process.Error != "")
                        Logger.Debug(process.Error);
                }
                else
                    process.RunInBackground(extraHeartbeatsJSON);

                if (!process.Success)
                {
                    Logger.Error("Could not send heartbeat.");
                    if (process.Output != null && process.Output != "")
                        Logger.Error(process.Output);
                    if (process.Error != null && process.Error != "")
                        Logger.Error(process.Error);
                }
            }
            else
                Logger.Error("Could not send heartbeat because python is not installed");
        }
        static bool IsCliLatestVersion()
        {
            var process = new RunProcess(PythonManager.GetPython(), GetCli(), "--version");
            process.Run();

            return process.Success && process.Error.Equals(WakaTimeConstants.CurrentWakaTimeCliVersion);
        }
        static string GetPathFromFixedPath()
        {
            string[] locations = {
                "pythonw",
                "python",
                "\\Python37\\pythonw",
                "\\Python36\\pythonw",
                "\\Python35\\pythonw",
                "\\Python34\\pythonw",
                "\\Python33\\pythonw",
                "\\Python32\\pythonw",
                "\\Python31\\pythonw",
                "\\Python30\\pythonw",
                "\\Python27\\pythonw",
                "\\Python26\\pythonw",
                "\\python37\\pythonw",
                "\\python36\\pythonw",
                "\\python35\\pythonw",
                "\\python34\\pythonw",
                "\\python33\\pythonw",
                "\\python32\\pythonw",
                "\\python31\\pythonw",
                "\\python30\\pythonw",
                "\\python27\\pythonw",
                "\\python26\\pythonw",
                "\\Python37\\python",
                "\\Python36\\python",
                "\\Python35\\python",
                "\\Python34\\python",
                "\\Python33\\python",
                "\\Python32\\python",
                "\\Python31\\python",
                "\\Python30\\python",
                "\\Python27\\python",
                "\\Python26\\python",
                "\\python37\\python",
                "\\python36\\python",
                "\\python35\\python",
                "\\python34\\python",
                "\\python33\\python",
                "\\python32\\python",
                "\\python31\\python",
                "\\python30\\python",
                "\\python27\\python",
                "\\python26\\python",
            };

            foreach (var location in locations)
            {
                try
                {
                    var process = new RunProcess(location, "--version");
                    process.Run();
                    if (!process.Success) continue;
                }
                catch
                {
                }

                Logger.Debug("Python found by Fixed Path: " + location.ToString());

                return location;
            }

            return null;
        }
示例#31
0
        private static void ProcessHeartbeats()
        {
            var pythonBinary = Dependencies.GetPython();

            if (pythonBinary != null)
            {
                // get first heartbeat from queue
                var gotOne = HeartbeatQueue.TryDequeue(out var heartbeat);
                if (!gotOne)
                {
                    return;
                }

                // remove all extra heartbeats from queue
                var extraHeartbeats = new ArrayList();
                while (HeartbeatQueue.TryDequeue(out var h))
                {
                    extraHeartbeats.Add(new Heartbeat(h));
                }
                var hasExtraHeartbeats = extraHeartbeats.Count > 0;

                PythonCliParameters.Key    = Config.ApiKey;
                PythonCliParameters.Plugin =
                    $"{Constants.EditorName}/{Constants.EditorVersion} {Constants.PluginName}/{Constants.PluginVersion}";
                PythonCliParameters.File               = heartbeat.entity;
                PythonCliParameters.Time               = heartbeat.timestamp;
                PythonCliParameters.IsWrite            = heartbeat.is_write;
                PythonCliParameters.Project            = heartbeat.project;
                PythonCliParameters.HasExtraHeartbeats = hasExtraHeartbeats;

                string extraHeartbeatsJson = null;
                if (hasExtraHeartbeats)
                {
                    extraHeartbeatsJson = new JavaScriptSerializer().Serialize(extraHeartbeats);
                }

                var process = new RunProcess(pythonBinary, PythonCliParameters.ToArray());
                if (Config.Debug)
                {
                    Logger.Debug(
                        $"[\"{pythonBinary}\", \"{string.Join("\", \"", PythonCliParameters.ToArray(true))}\"]");
                    process.Run(extraHeartbeatsJson);
                    if (!string.IsNullOrEmpty(process.Output))
                    {
                        Logger.Debug(process.Output);
                    }
                    if (!string.IsNullOrEmpty(process.Error))
                    {
                        Logger.Debug(process.Error);
                    }
                }
                else
                {
                    process.RunInBackground(extraHeartbeatsJson);
                }

                if (!process.Success)
                {
                    Logger.Error("Could not send heartbeat.");
                    if (!string.IsNullOrEmpty(process.Output))
                    {
                        Logger.Error(process.Output);
                    }
                    if (!string.IsNullOrEmpty(process.Error))
                    {
                        Logger.Error(process.Error);
                    }
                }
            }
            else
            {
                Logger.Error("Could not send heartbeat because python is not installed");
            }
        }
        public static void SendHeartbeat(string fileName, bool isWrite)
        {
            var arguments = new List<string>
            {
                GetCli(),
                "--key",
                ApiKey,
                "--file",
                fileName,
                "--plugin",
                WakaTimeConstants.EditorName + "/" + _editorVersion + " " + WakaTimeConstants.PluginName + "/" + _version
            };

            if (isWrite)
                arguments.Add("--write");

            var projectName = GetProjectName();
            if (!string.IsNullOrEmpty(projectName))
            {
                arguments.Add("--project");
                arguments.Add(projectName);
            }

            var pythonBinary = PythonManager.GetPython();
            if (pythonBinary != null)
            {

                var process = new RunProcess(pythonBinary, arguments.ToArray());
                if (DEBUG)
                {
                    Logger.Instance.Info("[\"" + pythonBinary + "\", \"" + string.Join("\", ", arguments) + "\"]");
                    process.Run();
                    Logger.Instance.Info("WakaTime CLI STDOUT:" + process.Output);
                    Logger.Instance.Info("WakaTime CLI STDERR:" + process.Error);
                }
                else
                {
                    process.RunInBackground();
                }

            }
            else
            {
                Logger.Instance.Error("Could not send heartbeat because python is not installed.");
            }
        }
        internal static bool IsCliUpToDate()
        {
            var process = new RunProcess(Dependencies.GetPython(), CliLocation, "--version");
            process.Run();

            if (process.Success)
            {
                var currentVersion = process.Error.Trim();
                Logger.Info(string.Format("Current wakatime-cli version is {0}", currentVersion));

                Logger.Info("Checking for updates to wakatime-cli...");
                var latestVersion = Constants.LatestWakaTimeCliVersion();

                if (currentVersion.Equals(latestVersion))
                {
                    Logger.Info("wakatime-cli is up to date.");
                    return true;
                }

                Logger.Info(string.Format("Found an updated wakatime-cli v{0}", latestVersion));
            }
            return false;
        }
        internal static string GetPythonPathFromFixedPath()
        {
            List<string> locations = new List<string>();
            for (int i = 26; i <= 50; i++)
            {
                locations.Add(Path.Combine("\\python" + i, "pythonw"));
                locations.Add(Path.Combine("\\Python" + i, "pythonw"));
            }

            foreach (var location in locations)
            {
                try
                {
                    var process = new RunProcess(location, "--version");
                    process.Run();

                    if (!process.Success) continue;
                }
                catch { /*ignored*/ }

                Logger.Debug(string.Format("Python found by Fixed Path: {0}", location));

                return location;
            }

            return null;
        }
        static string GetEmbeddedPath()
        {
            var localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var path = Path.Combine(localAppDataPath, "python", "pythonw");
            try
            {
                var process = new RunProcess(path, "--version");

                process.Run();

                if (!process.Success)
                    return null;

                Logger.Debug(string.Format("Python found from embedded location: {0}", path));

                return path;
            }
            catch (Exception ex)
            {
                Logger.Error("GetEmbeddedPath:", ex);
                return null;
            }
        }