diff --git a/pykd/dbgpath.cpp b/pykd/dbgpath.cpp new file mode 100644 index 0000000..10be731 --- /dev/null +++ b/pykd/dbgpath.cpp @@ -0,0 +1,148 @@ +#include "stdafx.h" + +#include "dbgpath.h" + +#include + +/////////////////////////////////////////////////////////////////////////////// + + + +DbgPythonPath::DbgPythonPath() +{ + DWORD enviromentSize = 0; + + enviromentSize = GetEnvironmentVariableA( "PYTHONPATH", NULL, enviromentSize ); + + std::vector enviromentBuffer(enviromentSize); + + if (!enviromentBuffer.empty()) + { + GetEnvironmentVariableA( "PYTHONPATH", &enviromentBuffer[0], enviromentSize ); + + typedef boost::escaped_list_separator char_separator_t; + typedef boost::tokenizer< char_separator_t > char_tokenizer_t; + + std::string pytonPath( &enviromentBuffer[0] ); + + char_tokenizer_t token( pytonPath, char_separator_t( "", "; \t", "\"" ) ); + + for ( char_tokenizer_t::iterator it = token.begin(); it != token.end(); ++it ) + { + if ( *it != "" ) + m_pathList.push_back( *it ); + } + } +} + +/////////////////////////////////////////////////////////////////////////////// + +std::string +DbgPythonPath::getStr() const +{ + std::string str; + std::vector::const_iterator it = m_pathList.begin(); + + for ( ; it != m_pathList.end(); ++it ) + { + str += *it; + str += ";"; + } + + return str; +} + +/////////////////////////////////////////////////////////////////////////////// + +bool +DbgPythonPath::findPath( + const std::string &fileName, + std::string &fullFileName, + std::string &filePath ) const +{ + std::vector< std::string > extPathList; + + boost::python::object sys = boost::python::import( "sys"); + + boost::python::list pathList( sys.attr("path") ); + + boost::python::ssize_t n = boost::python::len(pathList); + for(boost::python::ssize_t i=0;i( pathList[i] ) ); + + bool pyExt = fileName.rfind( ".py" ) == fileName.length() - 3; + + // 1. Ищем в рабочей директории + DWORD bufSize = + SearchPathA( + NULL, + fileName.c_str(), + pyExt ? NULL : ".py", + 0, + NULL, + NULL ); + + if ( bufSize > 0 ) + { + bufSize += 1; + std::vector fullFileNameCStr(bufSize); + char *partFileNameCStr = NULL; + + SearchPathA( + NULL, + fileName.c_str(), + pyExt ? NULL : ".py", + bufSize, + &fullFileNameCStr[0], + &partFileNameCStr ); + + fullFileName = std::string( &fullFileNameCStr[0] ); + if ( !fullFileName.empty() ) + { + filePath = std::string( &fullFileNameCStr[0], partFileNameCStr ); + return true; + } + } + + // 2. Ищем во всех директориях, указанных в m_pathList + + std::vector::const_iterator it = extPathList.begin(); + + for ( ; it != extPathList.end(); ++it ) + { + DWORD bufSize = + SearchPathA( + (*it).c_str(), + fileName.c_str(), + pyExt ? NULL : ".py", + 0, + NULL, + NULL ); + + if ( bufSize > 0 ) + { + bufSize += 1; + std::vector fullFileNameCStr(bufSize); + char *partFileNameCStr = NULL; + + bufSize = SearchPathA( + (*it).c_str(), + fileName.c_str(), + pyExt ? NULL : ".py", + bufSize, + &fullFileNameCStr[0], + &partFileNameCStr ); + + fullFileName = std::string( &fullFileNameCStr[0] ); + if ( !fullFileName.empty() ) + { + filePath = std::string( &fullFileNameCStr[0], partFileNameCStr ); + return true; + } + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////