Skip to content

Commit

Permalink
[IO] Added custom library paths
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 committed Nov 11, 2018
1 parent 84b7d68 commit f6333f2
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 102 deletions.
2 changes: 0 additions & 2 deletions include/occa/io/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace occa {
namespace io {
bool isCached(const std::string &filename);

std::string getLibraryName(const std::string &filename);

std::string hashDir(const hash_t &hash);

std::string hashDir(const std::string &filename,
Expand Down
7 changes: 7 additions & 0 deletions include/occa/io/fileOpener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define OCCA_IO_FILEOPENER_HEADER

#include <iostream>
#include <map>
#include <vector>

namespace occa {
Expand All @@ -10,6 +11,7 @@ namespace occa {
}

namespace io {
typedef std::map<std::string, std::string> libraryPathMap_t;
class fileOpener {
friend class occa::env::envInitializer_t;

Expand Down Expand Up @@ -47,6 +49,11 @@ namespace occa {
bool handles(const std::string &filename);
std::string expand(const std::string &filename);
};

libraryPathMap_t &getLibraryPathMap();

void addLibraryPath(const std::string &library,
const std::string &path);
//==================================
}
}
Expand Down
41 changes: 3 additions & 38 deletions src/io/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,7 @@ namespace occa {

// File is already cached
const std::string &cPath = cachePath();
if (startsWith(expFilename, cPath)) {
return true;
}

std::string occaLibName = getLibraryName(expFilename);
if (occaLibName.size() == 0) {
return false;
}

// File is already cached in the library cache
const std::string lpath = libraryPath() + occaLibName + "/cache/";
return startsWith(expFilename, lpath);
}

std::string getLibraryName(const std::string &filename) {
std::string expFilename = io::filename(filename);
const std::string cacheLibraryPath = env::OCCA_CACHE_DIR + "libraries/";

if (!startsWith(expFilename, cacheLibraryPath)) {
return "";
}

const char *start = expFilename.c_str() + cacheLibraryPath.size();
const char *end = start;
lex::skipTo(end, '/');

return expFilename.substr(start - expFilename.c_str(),
end - start);
return startsWith(expFilename, cPath);
}

std::string hashDir(const hash_t &hash) {
Expand All @@ -59,16 +32,8 @@ namespace occa {

const std::string &cPath = cachePath();
std::string cacheDir = cPath;
bool useHash = true;

// Check cached locations first
if (filename.size() && fileIsCached) {
useHash = false;
// Cached in a library
if (!startsWith(filename, cPath)) {
cacheDir = libraryPath() + getLibraryName(filename) + "/cache/";
}
}

const bool useHash = !filename.size() || !fileIsCached;

// Regular file, use hash
if (useHash) {
Expand Down
51 changes: 48 additions & 3 deletions src/io/fileOpener.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <occa/defines.hpp>
#include <occa/io/fileOpener.hpp>
#include <occa/io/utils.hpp>
#include <occa/tools/string.hpp>
#include <occa/tools/sys.hpp>

namespace occa {
namespace io {
Expand Down Expand Up @@ -53,10 +55,53 @@ namespace occa {
}

std::string occaFileOpener::expand(const std::string &filename) {
if (filename.size() == 7) {
return cachePath();
const std::string path = filename.substr(7);
const size_t firstSlash = path.find('/');

if ((firstSlash == 0) ||
(firstSlash == std::string::npos)) {
return "";
}

const std::string library = path.substr(0, firstSlash);
const std::string relativePath = path.substr(firstSlash);

libraryPathMap_t libraryPaths = getLibraryPathMap();
libraryPathMap_t::const_iterator it = libraryPaths.find(library);
if (it == libraryPaths.end()) {
return "";
}

return it->second + relativePath;
}

libraryPathMap_t &getLibraryPathMap() {
static libraryPathMap_t libraryPaths;
return libraryPaths;
}

void addLibraryPath(const std::string &library,
const std::string &path) {
std::string safeLibrary = library;
if (endsWith(safeLibrary, "/")) {
safeLibrary = safeLibrary.substr(0, safeLibrary.size() - 1);
}
return (libraryPath() + filename.substr(7));
OCCA_ERROR("Library name cannot be empty",
safeLibrary.size());
OCCA_ERROR("Library name cannot have / characters",
safeLibrary.find('/') == std::string::npos);

std::string safePath = path;
// Remove the trailing /
if (endsWith(safePath, "/")) {
safePath = safePath.substr(0, safePath.size() - 1);
}
if (!safePath.size()) {
return;
}

libraryPathMap_t &libraryPaths = getLibraryPathMap();
libraryPaths[safeLibrary] = safePath;
}
//==================================
}
Expand Down
8 changes: 0 additions & 8 deletions src/io/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,6 @@ namespace occa {
return filename;
}

const std::string &lPath = libraryPath();
if (startsWith(expFilename, lPath)) {
std::string libName = getLibraryName(expFilename);
std::string theRest = expFilename.substr(lPath.size() + libName.size() + 1);

return ("occa://" + libName + "/" + theRest);
}

const std::string &cPath = cachePath();
return expFilename.substr(cPath.size());
}
Expand Down
17 changes: 1 addition & 16 deletions src/tools/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,22 +391,7 @@ namespace occa {
std::string getFieldFrom(const std::string &command,
const std::string &field) {
#if (OCCA_OS & OCCA_LINUX_OS)
std::string shellToolsFile = io::filename("occa://occa/scripts/shellTools.sh");

if (!io::isFile(shellToolsFile)) {
mkpath(io::dirname(shellToolsFile));
std::string localFile = env::OCCA_DIR + "include/occa/scripts/shellTools.sh";

std::ifstream src(localFile.c_str(),
std::ios::binary);
std::ofstream dest(shellToolsFile.c_str(),
std::ios::binary);

dest << src.rdbuf();

src.close();
dest.close();
}
std::string shellToolsFile = env::OCCA_DIR + "include/occa/scripts/shellTools.sh";

std::stringstream ss;

Expand Down
28 changes: 0 additions & 28 deletions tests/src/io/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,16 @@ void testCacheInfoMethods() {
ASSERT_TRUE(occa::io::isCached(
occa::env::OCCA_CACHE_DIR + "cache/foo.okl"
));
ASSERT_FALSE(occa::io::isCached(
occa::env::OCCA_CACHE_DIR + "libraries/foo.okl"
));
ASSERT_TRUE(occa::io::isCached(
occa::env::OCCA_CACHE_DIR + "libraries/lib/cache/foo.okl"
));

// getLibraryName
ASSERT_EQ(occa::io::getLibraryName(""),
"");
ASSERT_EQ(occa::io::getLibraryName("foo.okl"),
"");
ASSERT_EQ(occa::io::getLibraryName("occa://lib/foo.okl"),
"lib");
ASSERT_EQ(occa::io::getLibraryName(
occa::env::OCCA_CACHE_DIR + "libraries/lib/cache/foo.okl"
),
"lib");
}

void testHashDir() {
occa::hash_t hash = occa::hash(occa::toString(rand()));
const std::string cacheDir = (
occa::io::cachePath() + hash.toString() + "/"
);
const std::string cacheLibDir = (
occa::io::libraryPath() + "lib/cache/" + hash.toString() + "/"
);
const std::string manualCacheDir = (
occa::io::cachePath() + "1234/"
);
const std::string manualCacheLibDir = (
occa::io::libraryPath() + "lib/cache/1234/"
);

// Default
ASSERT_EQ(occa::io::hashDir(hash),
Expand All @@ -93,10 +69,6 @@ void testHashDir() {
manualCacheDir);
ASSERT_EQ(occa::io::hashDir(manualCacheDir + "dir/foo.okl", hash),
manualCacheDir);
ASSERT_EQ(occa::io::hashDir(manualCacheLibDir + "foo.okl", hash),
manualCacheLibDir);
ASSERT_EQ(occa::io::hashDir(manualCacheLibDir + "dir/foo.okl", hash),
manualCacheLibDir);
}

void testBuild() {
Expand Down
40 changes: 35 additions & 5 deletions tests/src/io/fileOpener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
#include <occa/tools/env.hpp>
#include <occa/tools/testing.hpp>

void testFileOpeners();
void testDefaultFileOpener();
void testOccaFileOpener();

int main(const int argc, const char **argv) {
occa::env::OCCA_CACHE_DIR = occa::io::dirname(__FILE__);

testFileOpeners();
testDefaultFileOpener();
testOccaFileOpener();

return 0;
}

void testFileOpeners() {
void testDefaultFileOpener() {
occa::io::defaultFileOpener defaultOpener;
ASSERT_TRUE(defaultOpener.handles(""));
ASSERT_TRUE(defaultOpener.handles("foo.okl"));
Expand All @@ -24,15 +26,43 @@ void testFileOpeners() {
"foo.okl");
ASSERT_EQ(defaultOpener.expand("occa://foo.okl"),
"occa://foo.okl");
}

void testOccaFileOpener() {
occa::io::occaFileOpener occaOpener;
ASSERT_FALSE(occaOpener.handles(""));
ASSERT_FALSE(occaOpener.handles("foo.okl"));
ASSERT_TRUE(occaOpener.handles("occa://"));
ASSERT_TRUE(occaOpener.handles("occa://foo.okl"));

ASSERT_EQ(occaOpener.expand("occa://"),
occa::io::cachePath());
"");
ASSERT_EQ(occaOpener.expand("occa://foo.okl"),
occa::io::libraryPath() + "foo.okl");
"");

ASSERT_EQ(occaOpener.expand("occa://a/"),
"");
ASSERT_EQ(occaOpener.expand("occa://a/foo.okl"),
"");

occa::io::addLibraryPath("a", "foo");

ASSERT_EQ(occaOpener.expand("occa://a/"),
"foo/");
ASSERT_EQ(occaOpener.expand("occa://a/foo.okl"),
"foo/foo.okl");

occa::io::addLibraryPath("a", "foo/");

ASSERT_EQ(occaOpener.expand("occa://a/"),
"foo/");
ASSERT_EQ(occaOpener.expand("occa://a/foo.okl"),
"foo/foo.okl");

ASSERT_THROW(
occa::io::addLibraryPath("", "foobar");
);
ASSERT_THROW(
occa::io::addLibraryPath("a/b/c", "foo/");
);
}
2 changes: 0 additions & 2 deletions tests/src/io/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ void testPathMethods() {
"foo.okl");
ASSERT_EQ(occa::io::shortname(occa::io::cachePath() + "hash/foo.okl"),
"hash/foo.okl");
ASSERT_EQ(occa::io::shortname(occa::io::libraryPath() + "lib/foo.okl"),
"occa://lib/foo.okl");
}

void testDirMethods() {
Expand Down

0 comments on commit f6333f2

Please sign in to comment.