/* * Run scripts * Return non zero on failure */ int runScripts() { unsigned char *data; int rc = 0; TOC * ptoc = f_tocbuff; char msg[400]; VS("Running scripts\n"); /* Iterate through toc looking for scripts (type 's') */ while (ptoc < f_tocend) { if (ptoc->typcd == 's') { /* Get data out of the archive. */ data = extract(ptoc); /* Run it */ rc = PyRun_SimpleString(data); /* log errors and abort */ if (rc != 0) { sprintf(msg, " RC: %d from %s\n", rc, ptoc->name); VS(msg); return rc; } free(data); } ptoc = incrementTocPtr(ptoc); } return 0; }
/* * Import modules embedded in the archive - return 0 on success */ int importModules(ARCHIVE_STATUS *status) { PyObject *marshal; PyObject *marshaldict; PyObject *loadfunc; TOC *ptoc; PyObject *co; PyObject *mod; VS("importing modules from CArchive\n"); /* Get the Python function marshall.load * Here we collect some reference to PyObject that we don't dereference * Doesn't matter because the objects won't be going away anyway. */ marshal = PI_PyImport_ImportModule("marshal"); marshaldict = PI_PyModule_GetDict(marshal); loadfunc = PI_PyDict_GetItemString(marshaldict, "loads"); /* Iterate through toc looking for module entries (type 'm') * this is normally just bootstrap stuff (archive and iu) */ ptoc = status->tocbuff; while (ptoc < status->tocend) { if (ptoc->typcd == 'm' || ptoc->typcd == 'M') { unsigned char *modbuf = extract(status, ptoc); VS("extracted %s\n", ptoc->name); /* .pyc/.pyo files have 8 bytes header. Skip it and load marshalled * data form the right point. */ co = PI_PyObject_CallFunction(loadfunc, "s#", modbuf+8, ntohl(ptoc->ulen)-8); mod = PI_PyImport_ExecCodeModule(ptoc->name, co); /* Check for errors in loading */ if (mod == NULL) { FATALERROR("mod is NULL - %s", ptoc->name); } if (PI_PyErr_Occurred()) { PI_PyErr_Print(); PI_PyErr_Clear(); } free(modbuf); } ptoc = incrementTocPtr(status, ptoc); } return 0; }
/* Extract a file identifed by filename from the archive associated to status. */ static int extractDependencyFromArchive(ARCHIVE_STATUS *status, const char *filename) { TOC * ptoc = status->tocbuff; VS("Extracting dependencies from archive\n"); while (ptoc < status->tocend) { if (strcmp(ptoc->name, filename) == 0) if (extract2fs(status, ptoc)) return -1; ptoc = incrementTocPtr(status, ptoc); } return 0; }
/* * check if binaries need to be extracted. If not, this is probably a onedir solution, * and a child process will not be required on windows. */ int needToExtractBinaries(ARCHIVE_STATUS *status_list[]) { TOC * ptoc = status_list[SELF]->tocbuff; while (ptoc < status_list[SELF]->tocend) { if (ptoc->typcd == 'b' || ptoc->typcd == 'x' || ptoc->typcd == 'Z') return 1; if (ptoc->typcd == 'd') { return 1; } ptoc = incrementTocPtr(status_list[SELF], ptoc); } return 0; }
/* * A toc entry of type 'o' holds runtime options * toc->name is the arg * this is so you can freeze in command line args to Python */ int setRuntimeOptions(void) { int unbuffered = 0; TOC *ptoc = f_tocbuff; while (ptoc < f_tocend) { if (ptoc->typcd == 'o') { VS("%s\n", ptoc->name); switch (ptoc->name[0]) { case 'v': *PI_Py_VerboseFlag = 1; break; case 'u': unbuffered = 1; break; #ifdef HAVE_WARNINGS case 'W': if (ntohl(f_cookie.pyvers) >= 21) { PI_PySys_AddWarnOption(&ptoc->name[2]); } break; #endif case 's': *PI_Py_NoSiteFlag = 0; break; case 'O': *PI_Py_OptimizeFlag = 1; break; } } ptoc = incrementTocPtr(ptoc); } if (unbuffered) { #ifdef WIN32 _setmode(fileno(stdin), O_BINARY); _setmode(fileno(stdout), O_BINARY); #else fflush(stdout); fflush(stderr); #ifdef HAVE_SETVBUF setvbuf(stdin, (char *)NULL, _IONBF, 0); setvbuf(stdout, (char *)NULL, _IONBF, 0); setvbuf(stderr, (char *)NULL, _IONBF, 0); #else setbuf(stdin, (char *)NULL); setbuf(stdout, (char *)NULL); setbuf(stderr, (char *)NULL); #endif #endif } return 0; }
/* * extract all binaries (type 'b') and all data files (type 'x') to the filesystem */ int extractBinaries(char **workpath) { TOC * ptoc = f_tocbuff; workpath[0] = '\0'; VS("Extracting binaries\n"); while (ptoc < f_tocend) { if (ptoc->typcd == 'b' || ptoc->typcd == 'x' || ptoc->typcd == 'Z') if (extract2fs(ptoc)) return -1; ptoc = incrementTocPtr(ptoc); } *workpath = f_workpath; return 0; }
/* * check if binaries need to be extracted. If not, this is probably a onedir solution, * and a child process will not be required on windows. */ int needToExtractBinaries(ARCHIVE_STATUS *status_list[]) { TOC * ptoc = status_list[SELF]->tocbuff; while (ptoc < status_list[SELF]->tocend) { if (ptoc->typcd == ARCHIVE_ITEM_BINARY || ptoc->typcd == ARCHIVE_ITEM_DATA || ptoc->typcd == ARCHIVE_ITEM_ZIPFILE) return true; if (ptoc->typcd == ARCHIVE_ITEM_DEPENDENCY) { return true; } ptoc = incrementTocPtr(status_list[SELF], ptoc); } return false; }
/* * extract all binaries (type 'b') and all data files (type 'x') to the filesystem * and checks for dependencies (type 'd'). If dependencies are found, extract them. */ int extractBinaries(ARCHIVE_STATUS *status_list[]) { TOC * ptoc = status_list[SELF]->tocbuff; VS("Extracting binaries\n"); while (ptoc < status_list[SELF]->tocend) { if (ptoc->typcd == 'b' || ptoc->typcd == 'x' || ptoc->typcd == 'Z') if (extract2fs(status_list[SELF], ptoc)) return -1; if (ptoc->typcd == 'd') { if (extractDependency(status_list, ptoc->name) == -1) return -1; } ptoc = incrementTocPtr(status_list[SELF], ptoc); } return 0; }
/* * Install zlibs * Return non zero on failure */ int installZlibs(ARCHIVE_STATUS *status) { TOC * ptoc; VS("Installing import hooks\n"); /* Iterate through toc looking for zlibs (type 'z') */ ptoc = status->tocbuff; while (ptoc < status->tocend) { if (ptoc->typcd == 'z') { VS("%s\n", ptoc->name); installZlib(status, ptoc); } ptoc = incrementTocPtr(status, ptoc); } return 0; }
/* * Install zlibs * Return non zero on failure */ int installZlibs() { TOC * ptoc; VS("Installing import hooks\n"); /* Iterate through toc looking for zlibs (type 'z') */ ptoc = f_tocbuff; while (ptoc < f_tocend) { if (ptoc->typcd == 'z') { VS("%s\n", ptoc->name); installZlib(ptoc); } ptoc = incrementTocPtr(ptoc); } return 0; }
/* * extract all binaries (type 'b') and all data files (type 'x') to the filesystem * and checks for dependencies (type 'd'). If dependencies are found, extract them. */ int extractBinaries(ARCHIVE_STATUS *status_list[]) { TOC * ptoc = status_list[SELF]->tocbuff; VS("Extracting binaries\n"); while (ptoc < status_list[SELF]->tocend) { if (ptoc->typcd == ARCHIVE_ITEM_BINARY || ptoc->typcd == ARCHIVE_ITEM_DATA || ptoc->typcd == ARCHIVE_ITEM_ZIPFILE) if (extract2fs(status_list[SELF], ptoc)) return -1; if (ptoc->typcd == ARCHIVE_ITEM_DEPENDENCY) { if (extractDependency(status_list, ptoc->name) == -1) return -1; } ptoc = incrementTocPtr(status_list[SELF], ptoc); } return 0; }
/* * Run scripts * Return non zero on failure */ int runScripts() { unsigned char *data; char buf[_MAX_PATH]; int rc = 0; TOC * ptoc = f_tocbuff; PyObject *__main__ = PI_PyImport_AddModule("__main__"); PyObject *__file__; VS("Running scripts\n"); /* * Now that the startup is complete, we can reset the _MEIPASS2 env * so that if the program invokes another PyInstaller one-file program * as subprocess, this subprocess will not fooled into thinking that it * is already unpacked. */ unsetenv("_MEIPASS2"); /* Iterate through toc looking for scripts (type 's') */ while (ptoc < f_tocend) { if (ptoc->typcd == 's') { /* Get data out of the archive. */ data = extract(ptoc); /* Set the __file__ attribute within the __main__ module, for full compatibility with normal execution. */ strcpy(buf, ptoc->name); strcat(buf, ".py"); __file__ = PI_PyString_FromStringAndSize(buf, strlen(buf)); PI_PyObject_SetAttrString(__main__, "__file__", __file__); Py_DECREF(__file__); /* Run it */ rc = PI_PyRun_SimpleString(data); /* log errors and abort */ if (rc != 0) { VS("RC: %d from %s\n", rc, ptoc->name); return rc; } free(data); } ptoc = incrementTocPtr(ptoc); } return 0; }
/* * A toc entry of type 'o' holds runtime options * toc->name is the arg * this is so you can freeze in command line args to Python */ int setRuntimeOptions(ARCHIVE_STATUS *status) { int unbuffered = 0; TOC *ptoc = status->tocbuff; while (ptoc < status->tocend) { if (ptoc->typcd == 'o') { VS("%s\n", ptoc->name); switch (ptoc->name[0]) { case 'v': *PI_Py_VerboseFlag = 1; break; case 'u': unbuffered = 1; break; case 'W': PI_PySys_AddWarnOption(&ptoc->name[2]); break; case 's': *PI_Py_NoSiteFlag = 0; break; case 'O': *PI_Py_OptimizeFlag = 1; break; } } ptoc = incrementTocPtr(status, ptoc); } if (unbuffered) { #ifdef WIN32 _setmode(fileno(stdin), O_BINARY); _setmode(fileno(stdout), O_BINARY); #else fflush(stdout); fflush(stderr); setbuf(stdin, (char *)NULL); setbuf(stdout, (char *)NULL); setbuf(stderr, (char *)NULL); #endif } return 0; }
/* * Run scripts * Return non zero on failure */ int runScripts(ARCHIVE_STATUS *status) { unsigned char *data; char buf[_MAX_PATH]; int rc = 0; TOC * ptoc = status->tocbuff; PyObject *__main__ = PI_PyImport_AddModule("__main__"); PyObject *__file__; VS("Running scripts\n"); /* Iterate through toc looking for scripts (type 's') */ while (ptoc < status->tocend) { if (ptoc->typcd == 's') { /* Get data out of the archive. */ data = extract(status, ptoc); /* Set the __file__ attribute within the __main__ module, for full compatibility with normal execution. */ strcpy(buf, ptoc->name); strcat(buf, ".py"); __file__ = PI_PyString_FromStringAndSize(buf, strlen(buf)); PI_PyObject_SetAttrString(__main__, "__file__", __file__); Py_DECREF(__file__); /* Run it */ rc = PI_PyRun_SimpleString(data); /* log errors and abort */ if (rc != 0) { VS("RC: %d from %s\n", rc, ptoc->name); return rc; } free(data); } ptoc = incrementTocPtr(status, ptoc); } return 0; }
/* * Import modules embedded in the archive - return 0 on success */ int importModules() { PyObject *marshal; PyObject *marshaldict; PyObject *loadfunc; PyObject *pyfile; TOC *ptoc; PyObject *co; PyObject *mod; PyObject *res; char buf[32]; VS("importing modules from CArchive\n"); /* Get the Python function marshall.load * Here we collect some reference to PyObject that we don't dereference * Doesn't matter because the objects won't be going away anyway. */ marshal = PyImport_ImportModule("marshal"); marshaldict = PyModule_GetDict(marshal); loadfunc = PyDict_GetItemString(marshaldict, "loads"); /* Iterate through toc looking for module entries (type 'm') * this is normally just bootstrap stuff (archive and iu) */ ptoc = f_tocbuff; while (ptoc < f_tocend) { if (ptoc->typcd == 'm' || ptoc->typcd == 'M') { unsigned char *modbuf = extract(ptoc); /* .pyc/.pyo files have 8 bytes header. Skip it and get a Python * string directly pointing at the marshalled code. */ PyObject *mods = PyString_FromStringAndSize(modbuf + 8, ntohl(ptoc->ulen) - 8); VS(ptoc->name); VS("\n"); co = PyObject_CallFunction(loadfunc, "O", mods); mod = PyImport_ExecCodeModule(ptoc->name, co); /* Check for errors in loading */ if (mod == NULL) { FATALERROR("mod is NULL - "); FATALERROR(ptoc->name); } if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); } Py_DECREF(mods); free(modbuf); } ptoc = incrementTocPtr(ptoc); } return 0; }