Skip to content

Commit

Permalink
Try to use a preinstalled Python 2 when calling reposync
Browse files Browse the repository at this point in the history
  • Loading branch information
hjuutilainen committed Mar 16, 2022
1 parent 7a98592 commit 8fccec9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
65 changes: 63 additions & 2 deletions SUS Inspector/SIOperationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,65 @@ - (void)readReposadoInstanceContentsAsync:(SIReposadoInstanceMO *)instance force

- (void)runReposync:(SIReposadoInstanceMO *)instance
{
/*
Python 2 shenanigans
*/
BOOL python2found = NO;
NSString *python2Path;
NSURL *python2URL;

/*
First check if NSUserDefaults has a Python 2 path that exists. SUS Inspector default value is "/Library/Frameworks/Python.framework/Versions/2.7/bin/python2"
*/
NSString *python2PathUser = [[NSUserDefaults standardUserDefaults] stringForKey:@"python2Path"];
BOOL isdir;
if ([[NSFileManager defaultManager] fileExistsAtPath:python2PathUser isDirectory:&isdir]) {
if (!isdir) {
python2Path = python2PathUser;
python2found = YES;
NSLog(@"Python 2 found at NSUserDefaults path: %@", python2PathUser);
} else {
NSLog(@"Python 2 defined as: %@ but it is a directory", python2PathUser);
NSAlert *pythonAlert = [[NSAlert alloc] init];
pythonAlert.messageText = @"Error";
pythonAlert.informativeText = [NSString stringWithFormat:@"Python 2 path set to \"%@\" but it is a directory.", python2PathUser];
[pythonAlert addButtonWithTitle:@"OK"];

[pythonAlert runModal];
return;
}
} else {
/*
Check a few other known locations for existing Python 2 installation
*/
NSArray *python2PathsToCheck = @[
@"/usr/bin/python",
@"/Library/Frameworks/Python.framework/Versions/2.7/bin/python2",
@"/usr/local/munkireport/munkireport-python2"
];
for (NSString *path in python2PathsToCheck) {
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
python2Path = path;
python2found = YES;
NSLog(@"Python 2 found at path: %@", path);
break;
}
}
}

if (python2found) {
python2URL = [NSURL fileURLWithPath:python2Path];
} else {
NSAlert *pythonAlert = [[NSAlert alloc] init];
pythonAlert.messageText = @"Error";
pythonAlert.informativeText = [NSString stringWithFormat:@"Could not find a suitable Python 2 installation and SUS Inspector needs Python 2 to work.\n\nPython 2 can be downloaded from https://www.python.org/downloads/macos/"];
[pythonAlert addButtonWithTitle:@"OK"];

[pythonAlert runModal];
return;
}


[self willStartOperations];

[self deleteAllObjectsForEntityName:@"SIProduct"];
Expand All @@ -597,8 +656,10 @@ - (void)runReposync:(SIReposadoInstanceMO *)instance
self.currentOperationTitle = @"Refreshing catalogs...";

NSTask *task = [[NSTask alloc] init];
NSString *launchPath = instance.reposyncPath;
task.launchPath = launchPath;
task.executableURL = python2URL;

NSString *reposyncPath = instance.reposyncPath;
task.arguments = @[reposyncPath];

NSMutableDictionary *defaultEnv = [[NSMutableDictionary alloc] initWithDictionary:[[NSProcessInfo processInfo] environment]];
[defaultEnv setObject:@"YES" forKey:@"NSUnbufferedIO"] ;
Expand Down
2 changes: 2 additions & 0 deletions SUS Inspector/UserDefaults.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>python2Path</key>
<string>/Library/Frameworks/Python.framework/Versions/2.7/bin/python2</string>
<key>includeDeprecatedInProductGroups</key>
<false/>
<key>pkginfoPrefillVersion</key>
Expand Down

0 comments on commit 8fccec9

Please sign in to comment.