Skip to content

Commit

Permalink
Fix path to custom fixers for python migration (#11155)
Browse files Browse the repository at this point in the history
* fix path to custom fixers for python migration

* cleanup
  • Loading branch information
aparajit-pratap authored Sep 30, 2020
1 parent 0a42df0 commit 4314bdf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Dynamo.sln.DotSettings
*.sdf
*.backup
*.pdf
*.dgml

#ASM
extern/DynamoAsm/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Dynamo.PythonMigration.MigrationAssistant
internal static class ScriptMigrator
{
private const string INPUT_NAME = "code";
private const string PATH_NAME = "path_name";
private const string RETURN_NAME = "output";

/// <summary>
Expand All @@ -33,10 +34,17 @@ internal static string MigrateCode(string code)
using (Py.GIL())
{
string output;
var asm = Assembly.GetExecutingAssembly();

using (PyScope scope = Py.CreateScope())
{
scope.Set(INPUT_NAME, code.ToPython());
scope.Exec(Get2To3MigrationScript());

var path = Path.GetDirectoryName(asm.Location);

scope.Set(PATH_NAME, path.ToPython());
scope.Exec(Get2To3MigrationScript(asm));

output = scope.Contains(RETURN_NAME) ? scope.Get(RETURN_NAME).ToString() : string.Empty;
}

Expand All @@ -47,7 +55,7 @@ internal static string MigrateCode(string code)
using (PyScope scope = Py.CreateScope())
{
scope.Set(INPUT_NAME, output.ToPython());
scope.Exec(GetReindentationScript());
scope.Exec(GetReindentationScript(asm));
output = scope.Contains(RETURN_NAME) ? scope.Get(RETURN_NAME).ToString() : string.Empty;
}
}
Expand All @@ -62,19 +70,18 @@ internal static string MigrateCode(string code)
}
}

private static string Get2To3MigrationScript()
private static string Get2To3MigrationScript(Assembly asm)
{
return GetEmbeddedScript("Dynamo.PythonMigration.MigrationAssistant.migrate_2to3.py");
return GetEmbeddedScript(asm, "Dynamo.PythonMigration.MigrationAssistant.migrate_2to3.py");
}

private static string GetReindentationScript()
private static string GetReindentationScript(Assembly asm)
{
return GetEmbeddedScript("Dynamo.PythonMigration.MigrationAssistant.reindent.py");
return GetEmbeddedScript(asm, "Dynamo.PythonMigration.MigrationAssistant.reindent.py");
}

private static string GetEmbeddedScript(string resourceName)
private static string GetEmbeddedScript(Assembly asm, string resourceName)
{
Assembly asm = Assembly.GetExecutingAssembly();
string script;
using (var reader =
new StreamReader(asm.GetManifestResourceStream(resourceName)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def transform(source):
python_zip_folder = zipfile.ZipFile(os.path.join(python_zip_path, python_zip_file))
fixers = get_all_fixers_from_zipfolder(python_zip_folder)

dir_path = os.getcwd()
sys.path.append(os.path.join(dir_path, '.\\python_migration_fixers'))
sys.path.append(os.path.join(path_name, '.\\python_migration_fixers'))
fixers.extend(['fix_none'])

refactoring_tool = RefactoringTool(fixers)
Expand Down

0 comments on commit 4314bdf

Please sign in to comment.