Python will now attempt (again) to create at least a minimal

preferences file if it is missing.
This commit is contained in:
Jack Jansen 1995-08-31 13:59:36 +00:00
parent 9119ccfadd
commit 41fa7ea719
3 changed files with 93 additions and 101 deletions

View File

@ -16,20 +16,20 @@
** shared ppc python, in the core dynamic library)
*/
/* The alert for "No Python directory, where is it?" */
#define NOPYTHON_ALERT 128
#define YES_ITEM 1
#define NO_ITEM 2
#define CURWD_ITEM 3
/* The alert for "No Python directory, where is it?" (OBSOLETE) */
#define NOPYTHON_ALERT 128
#define YES_ITEM 1
#define NO_ITEM 2
#define CURWD_ITEM 3
/* The alert for "this is an applet template" */
#define NOPYC_ALERT 129
#define NOPYC_ALERT 129
/* The dialog for our GetDirectory and PromptGetFile call */
#define GETDIR_ID 130 /* Resource ID for our "get directory" */
#define GETDIR_ID 130 /* Resource ID for our "get directory" */
#define GETFILEPROMPT_ID 132 /* Resource id for prompted get file */
#define PROMPT_ITEM 10 /* The prompt, at the top */
#define SELECTCUR_ITEM 11 /* "Select current directory" button */
#define PROMPT_ITEM 10 /* The prompt, at the top */
#define SELECTCUR_ITEM 11 /* "Select current directory" button */
/* The dialog for interactive options */
@ -42,6 +42,17 @@
#define OPT_UNBUFFERED 6
#define OPT_DEBUGGING 7
/* Dialog for 'No preferences directory' */
#define NOPREFDIR_ID 133
/* Dialog for 'Create preferences file?' */
#define NOPREFFILE_ID 134
#define NOPREFFILE_YES 1
#define NOPREFFILE_NO 2
/* Dialog for 'Bad preference file' */
#define BADPREFFILE_ID 135
/*
** The following are valid both in the binary (or shared library)
** and in the Preferences file

View File

@ -75,8 +75,8 @@ strdup(char *src)
/* Return FSSpec of current application */
static OSErr
current_process_location(FSSpec *applicationSpec)
OSErr
PyMac_process_location(FSSpec *applicationSpec)
{
ProcessSerialNumber currentPSN;
ProcessInfoRec info;
@ -155,7 +155,7 @@ get_application_name()
static char appname[256];
FSSpec appspec;
if (current_process_location(&appspec))
if (PyMac_process_location(&appspec))
return NULL;
if (get_full_path(&appspec, appname))
return NULL;

View File

@ -85,6 +85,57 @@ getpythonpath()
return pythonpath;
}
/*
** Open/create the Python Preferences file, return the handle
*/
short
PyMac_OpenPrefFile()
{
AliasHandle handle;
FSSpec dirspec;
short prefrh;
short prefdirRefNum;
long prefdirDirID;
short action;
if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
&prefdirDirID) != noErr ) {
/* Something wrong with preferences folder */
(void)StopAlert(NOPREFDIR_ID, NULL);
exit(1);
}
(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec);
prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
if ( prefrh < 0 ) {
action = CautionAlert(NOPREFFILE_ID, NULL);
if ( action == NOPREFFILE_NO )
exit(1);
FSpCreateResFile(&dirspec, 'PYTH', 'pref', 0);
prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
if ( prefrh == -1 ) {
/* This "cannot happen":-) */
printf("Cannot create preferences file!!\n");
exit(1);
}
if ( PyMac_process_location(&dirspec) != 0 ) {
printf("Cannot get FSSpec for application!!\n");
exit(1);
}
dirspec.name[0] = 0;
if (NewAlias(NULL, &dirspec, &handle) != 0 ) {
printf("Cannot make alias to application directory!!\n");
exit(1);
}
AddResource((Handle)handle, 'alis', PYTHONHOME_ID, "\p");
UpdateResFile(prefrh);
} else {
UseResFile(prefrh);
}
return prefrh;
}
/*
** Return the name of the Python directory
@ -92,111 +143,41 @@ getpythonpath()
char *
PyMac_GetPythonDir()
{
int item;
static char name[256];
AliasHandle handle;
FSSpec dirspec;
int ok = 0;
Boolean modified = 0, cannotmodify = 0;
Boolean modified = 0;
short oldrh, prefrh;
short prefdirRefNum;
long prefdirDirID;
/*
** Remember old resource file and try to open preferences file
** in the preferences folder. If it doesn't exist we try to create
** it. If anything fails here we limp on, but set cannotmodify so
** we don't try to store things later on.
** in the preferences folder.
*/
oldrh = CurResFile();
if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
&prefdirDirID) != noErr ) {
/* Something wrong with preferences folder */
cannotmodify = 1;
} else {
(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec);
prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
if ( prefrh == -1 ) {
#ifdef USE_MAC_MODPREFS
/* It doesn't exist. Try to create it */
FSpCreateResFile(&dirspec, 'PYTH', 'pref', 0);
prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
if ( prefrh == -1 ) {
/* This is strange, what should we do now? */
cannotmodify = 1;
} else {
UseResFile(prefrh);
}
#else
printf("Error: no Preferences file. Attempting to limp on...\n");
name[0] = 0;
getwd(name);
return name;
#endif
}
}
prefrh = PyMac_OpenPrefFile();
/* So, we've opened our preferences file, we hope. Look for the alias */
handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID);
if ( handle ) {
/* It exists. Resolve it (possibly updating it) */
if ( ResolveAlias(NULL, handle, &dirspec, &modified) == noErr ) {
ok = 1;
}
if ( handle == NULL ) {
(void)StopAlert(BADPREFFILE_ID, NULL);
exit(1);
}
if ( !ok ) {
#ifdef USE_MAC_MODPREFS
/* No luck, so far. ask the user for help */
item = Alert(NOPYTHON_ALERT, NULL);
if ( item == YES_ITEM ) {
/* The user wants to point us to a directory. Let her do so */
ok = PyMac_GetDirectory(&dirspec);
if ( ok )
modified = 1;
} else if ( item == CURWD_ITEM ) {
/* The user told us the current directory is fine. Build an FSSpec for it */
if ( getwd(name) ) {
if ( FSMakeFSSpec(0, 0, Pstring(name), &dirspec) == 0 ) {
ok = 1;
modified = 1;
}
}
}
if ( handle ) {
/* Set the (old, invalid) alias record to the new data */
UpdateAlias(NULL, &dirspec, handle, &modified);
}
#else
printf("Error: corrupted Preferences file. Attempting to limp on...\n");
name[0] = 0;
getwd(name);
return name;
#endif
/* It exists. Resolve it (possibly updating it) */
if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) {
(void)StopAlert(BADPREFFILE_ID, NULL);
exit(1);
}
#ifdef USE_MAC_MODPREFS
if ( ok && modified && !cannotmodify) {
/* We have a new, valid fsspec and we can update the preferences file. Do so. */
if ( !handle ) {
if (NewAlias(NULL, &dirspec, &handle) == 0 )
AddResource((Handle)handle, 'alis', PYTHONHOME_ID, "\p");
} else {
ChangedResource((Handle)handle);
}
if ( modified ) {
ChangedResource((Handle)handle);
UpdateResFile(prefrh);
}
#endif
if ( !cannotmodify ) {
/* This means we have the resfile open. Close it. */
CloseResFile(prefrh);
}
/* Back to the old resource file */
CloseResFile(prefrh);
UseResFile(oldrh);
/* Now turn the fsspec into a path to give back to our caller */
if ( ok ) {
ok = (nfullpath(&dirspec, name) == 0);
if ( ok ) strcat(name, ":");
}
if ( !ok ) {
/* If all fails, we return the current directory */
if ( nfullpath(&dirspec, name) == 0 ) {
strcat(name, ":");
} else {
/* If all fails, we return the current directory */
printf("Python home dir exists but I cannot find the pathname!!\n");
name[0] = 0;
(void)getwd(name);
}