Skip to content

Commit

Permalink
Merge pull request #207 from hrntsm/feature/use_tunny_env_folder
Browse files Browse the repository at this point in the history
use tunny env folder
  • Loading branch information
hrntsm authored Dec 28, 2023
2 parents 525b76e + 76015b7 commit bfc2224
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "C:/Program Files/Rhino/System/Rhino.exe",
"program": "C:/Program Files/Rhino 8/System/Rhino.exe",
"args": ["/nosplash"],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
Expand Down
25 changes: 14 additions & 11 deletions Tunny/Handler/PythonInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using System.IO;
using System.IO.Compression;

using Tunny.Util;

namespace Tunny.Handler
{
public static class PythonInstaller
{
public static string Path { get; set; } = ".";
public static string ComponentFolderPath { get; set; }
public static void Run(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
Expand All @@ -20,18 +22,19 @@ public static void Run(object sender, DoWorkEventArgs e)

private static string[] UnzipLibraries()
{
if (Directory.Exists(Path + "/python"))
string envPath = TunnyVariables.TunnyEnvPath;
if (Directory.Exists(envPath + "/python"))
{
Directory.Delete(Path + "/python", true);
Directory.Delete(envPath + "/python", true);
}
ZipFile.ExtractToDirectory(Path + "/Lib/python.zip", Path + "/python");
ZipFile.ExtractToDirectory(ComponentFolderPath + "/Lib/python.zip", envPath + "/python");

if (Directory.Exists(Path + "/Lib/whl"))
if (Directory.Exists(envPath + "/Lib/whl"))
{
Directory.Delete(Path + "/Lib/whl", true);
Directory.Delete(envPath + "/Lib/whl", true);
}
ZipFile.ExtractToDirectory(Path + "/Lib/whl.zip", Path + "/Lib/whl");
return Directory.GetFiles(Path + "/Lib/whl");
ZipFile.ExtractToDirectory(ComponentFolderPath + "/Lib/whl.zip", envPath + "/Lib/whl");
return Directory.GetFiles(envPath + "/Lib/whl");
}

private static void InstallPackages(BackgroundWorker worker, string[] packageList)
Expand All @@ -40,11 +43,11 @@ private static void InstallPackages(BackgroundWorker worker, string[] packageLis
for (int i = 0; i < num; i++)
{
double progress = (double)i / num * 100d;
string packageName = System.IO.Path.GetFileName(packageList[i]).Split('-')[0];
string packageName = Path.GetFileName(packageList[i]).Split('-')[0];
worker.ReportProgress((int)progress, "Now installing " + packageName + "...");
var startInfo = new ProcessStartInfo
{
FileName = Path + "/python/python.exe",
FileName = TunnyVariables.TunnyEnvPath + "/python/python.exe",
Arguments = "-m pip install --no-deps " + packageList[i],
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
Expand All @@ -61,7 +64,7 @@ private static void InstallPackages(BackgroundWorker worker, string[] packageLis

internal static string GetEmbeddedPythonPath()
{
return Path + "/python";
return TunnyVariables.TunnyEnvPath + "/python";
}
}
}
2 changes: 1 addition & 1 deletion Tunny/Settings/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Tunny.Settings
{
public class Storage
{
public string Path { get; set; } = "/fish.log";
public string Path { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/fish.log";
public StorageType Type { get; set; } = StorageType.Journal;

public string GetOptunaStoragePath()
Expand Down
2 changes: 1 addition & 1 deletion Tunny/Settings/TunnySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TunnySettings
public string Version { get; set; } = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
public Optimize Optimize { get; set; } = new Optimize();
public Result Result { get; set; } = new Result();
public string StudyName { get; set; } = "study1";
public string StudyName { get; set; } = string.Empty;
public Storage Storage { get; set; } = new Storage();
public bool CheckPythonLibraries { get; set; } = true;

Expand Down
9 changes: 5 additions & 4 deletions Tunny/UI/OptimizationWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Tunny.Handler;
using Tunny.Settings;
using Tunny.Solver;
using Tunny.Util;

namespace Tunny.UI
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public OptimizationWindow(FishingComponent component)

private void RunPythonInstaller()
{
PythonInstaller.Path = _component.GhInOut.ComponentFolder;
PythonInstaller.ComponentFolderPath = _component.GhInOut.ComponentFolder;
if (_settings.CheckPythonLibraries)
{
var installer = new PythonInstallDialog()
Expand Down Expand Up @@ -82,7 +83,7 @@ public void BGDispose()

private void LoadSettingJson()
{
string settingsPath = _component.GhInOut.ComponentFolder + @"\Settings.json";
string settingsPath = TunnyVariables.OptimizeSettingsPath;
if (File.Exists(settingsPath))
{
_settings = TunnySettings.Deserialize(File.ReadAllText(settingsPath));
Expand All @@ -93,7 +94,7 @@ private void LoadSettingJson()
{
Storage = new Settings.Storage
{
Path = _component.GhInOut.ComponentFolder + @"\fish.log",
Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "fish.log"),
Type = StorageType.Journal
}
};
Expand Down Expand Up @@ -123,7 +124,7 @@ private void FormClosingXButton(object sender, FormClosingEventArgs e)
var ghCanvas = Owner as GH_DocumentEditor;
ghCanvas?.EnableUI();
GetUIValues();
_settings.Serialize(_component.GhInOut.ComponentFolder + @"\Settings.json");
_settings.Serialize(TunnyVariables.OptimizeSettingsPath);

//TODO: use cancelAsync to stop the background worker safely
optimizeBackgroundWorker?.Dispose();
Expand Down
3 changes: 2 additions & 1 deletion Tunny/UI/OptimizeWindowTab/FileTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Tunny.Settings;
using Tunny.Storage;
using Tunny.Util;

namespace Tunny.UI
{
Expand Down Expand Up @@ -59,7 +60,7 @@ private void OutputDebugLogButton_Click(object sender, EventArgs e)
using (var process = new Process())
{
process.StartInfo.FileName = "PowerShell.exe";
process.StartInfo.Arguments = $"tree {_component.GhInOut.ComponentFolder} /f > {_component.GhInOut.ComponentFolder}\\debug.log";
process.StartInfo.Arguments = $"tree {_component.GhInOut.ComponentFolder} /f > {TunnyVariables.TunnyEnvPath}\\debug.log";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
Expand Down
11 changes: 11 additions & 0 deletions Tunny/Util/TunnyVariables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.IO;

namespace Tunny.Util
{
public static class TunnyVariables
{
public static string TunnyEnvPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + ".tunny_env");
public static string OptimizeSettingsPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".tunny_env", "settings.json");
}
}

0 comments on commit bfc2224

Please sign in to comment.