Skip to content

Commit 018616a

Browse files
using plugin in path that is set after instantiation of a data mode fails. (#875)
Ensure that Python storages are reloaded if the Python storage plugin path has changed. --------- Co-authored-by: Jesper Friis <[email protected]>
1 parent 3ce8434 commit 018616a

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed

examples/storage_plugin/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
# Set search path to our user-defined storage plugin
77
thisdir = Path(__file__).resolve().parent
8+
#dlite.python_storage_plugin_path.append(thisdir / "plugins")
9+
10+
dlite.storage_path.append(thisdir / "entities" / "TempProfile.json")
11+
DataModel = dlite.get_instance("http://onto-ns.com/meta/0.1/TempProfile")
812
dlite.python_storage_plugin_path.append(thisdir / "plugins")
13+
#DataModel = dlite.get_instance("http://onto-ns.com/meta/0.1/TempProfile")
914

1015
# Create instance from dataset
1116
inst = dlite.Instance.from_location("tempprofile", thisdir / "dataset.txt",

src/dlite-storage-plugins.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct _DLiteStoragePluginIter {
3131
/* Global variables for dlite-storage-plugins */
3232
typedef struct {
3333
PluginInfo *storage_plugin_info; /* reference to storage plugin info */
34-
unsigned char storage_plugin_path_hash[32]; /* Sha256 hash of plugin paths */
34+
unsigned char storage_plugin_path_hash[32]; /* Sha3 hash of plugin paths */
3535
} Globals;
3636

3737

@@ -119,7 +119,7 @@ const DLiteStoragePlugin *dlite_storage_plugin_get(const char *name)
119119

120120
/* ...otherwise, if any plugin path has changed, reload all plugins
121121
and try again */
122-
if (pathshash(hash, sizeof(hash), &info->paths) == 0) {
122+
if (pathshash(hash, sizeof(hash), &info->paths, DSL_EXT) == 0) {
123123

124124
if (memcmp(g->storage_plugin_path_hash, hash, sizeof(hash)) != 0) {
125125
plugin_load_all(info);

src/pathshash.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,30 @@
66
#include "pathshash.h"
77

88
/*
9-
Calculate sha3 hash of `paths` and stores it in `hash`.
9+
Calculate sha3 hash of all files in `paths` and stores it in `hash`.
1010
`hashsize` is the size of `hash` in bytes. Should be 32, 48 or 64.
11+
12+
If `pattern` is given, it should be a glob pattern for selecting what
13+
files to include.
14+
1115
Returns non-zero on error.
1216
*/
13-
int pathshash(unsigned char *hash, int hashsize, const FUPaths *paths)
17+
int pathshash(unsigned char *hash, int hashsize, const FUPaths *paths,
18+
const char *pattern)
1419
{
1520
sha3_context c;
1621
FUIter *iter;
1722
unsigned bitsize = hashsize * 8;
1823
const unsigned char *buf;
1924
const char *path;
20-
if (!(iter = fu_pathsiter_init(paths, NULL))) return 1;
25+
if (!(iter = fu_startmatch(pattern, paths)))
26+
return err(1, "cannot initiate paths iterator (%d)", (paths) ? (int)paths->n : -1);
2127
if (sha3_Init(&c, bitsize))
2228
return err(1, "invalid hash size: %d bytes", hashsize);
23-
while ((path = fu_pathsiter_next(iter)))
29+
while ((path = fu_nextmatch(iter)))
2430
sha3_Update(&c, path, strlen(path));
2531
buf = sha3_Finalize(&c);
26-
fu_pathsiter_deinit(iter);
32+
fu_endmatch(iter);
2733
memcpy(hash, buf, hashsize);
2834
return 0;
2935
}

src/pathshash.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
#include "utils/fileutils.h"
55

66
/**
7-
Calculate sha3 hash of `paths` and stores it in `hash`.
7+
Calculate sha3 hash of all files in `paths` and stores it in `hash`.
88
`hashsize` is the size of `hash` in bytes. Should be 32, 48 or 64.
9+
10+
If `pattern` is given, it should be a glob pattern for selecting what
11+
files to include.
12+
913
Returns non-zero on error.
1014
*/
11-
int pathshash(unsigned char *hash, int hashsize, const FUPaths *paths);
15+
int pathshash(unsigned char *hash, int hashsize, const FUPaths *paths,
16+
const char *pattern);
1217

1318

1419
#endif /* _PATHSHASH_H */

src/pyembed/dlite-python-storage.c

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "config-paths.h"
1212

1313
#include "utils/strutils.h"
14+
#include "pathshash.h"
1415
#include "dlite.h"
1516
#include "dlite-macros.h"
1617
#include "dlite-misc.h"
@@ -28,13 +29,13 @@ typedef PyObject *(*InstanceConverter)(DLiteInstance *inst);
2829

2930
/* Global state for this module */
3031
typedef struct {
31-
FUPaths paths; /* Python storage paths */
32-
int initialised; /* Whether `paths` is initiated */
33-
int modified; /* Whether `paths` is modified */
34-
PyObject *loaded_storages; /* Cache with all loaded python storage plugins */
35-
char **failed_paths; /* NULL-terminated array of paths to storages
36-
that fail to load. */
37-
size_t failed_len; /* Allocated length of `failed_paths`. */
32+
FUPaths paths; /* Python storage paths */
33+
int initialised; /* Whether `paths` is initiated */
34+
unsigned char paths_hash[32]; /* Sha3 hash of plugin paths */
35+
PyObject *loaded_storages; /* Cache with all loaded python storage plugins */
36+
char **failed_paths; /* NULL-terminated array of paths to storages
37+
that fail to load. */
38+
size_t failed_len; /* Allocated length of `failed_paths`. */
3839
} PythonStorageGlobals;
3940

4041

@@ -102,7 +103,6 @@ FUPaths *dlite_python_storage_paths(void)
102103
if (s < 0) return dlite_err(1, "error initialising dlite python storage "
103104
"plugin dirs"), NULL;
104105
g->initialised = 1;
105-
g->modified = 0;
106106

107107
/* Make sure that dlite DLLs are added to the library search path */
108108
dlite_add_dll_path();
@@ -119,7 +119,6 @@ void dlite_python_storage_paths_clear(void)
119119
if (g->initialised) {
120120
fu_paths_deinit(&g->paths);
121121
g->initialised = 0;
122-
g->modified = 0;
123122
}
124123
}
125124

@@ -131,14 +130,9 @@ void dlite_python_storage_paths_clear(void)
131130
*/
132131
int dlite_python_storage_paths_insert(const char *path, int n)
133132
{
134-
int stat;
135133
const FUPaths *paths;
136134
if (!(paths = dlite_python_storage_paths())) return -1;
137-
if ((stat = fu_paths_insert((FUPaths *)paths, path, n))) {
138-
PythonStorageGlobals *g = get_globals();
139-
g->modified = 1;
140-
}
141-
return stat;
135+
return fu_paths_insert((FUPaths *)paths, path, n);
142136
}
143137

144138
/*
@@ -147,14 +141,9 @@ int dlite_python_storage_paths_insert(const char *path, int n)
147141
*/
148142
int dlite_python_storage_paths_append(const char *path)
149143
{
150-
int stat;
151144
const FUPaths *paths;
152145
if (!(paths = dlite_python_storage_paths())) return -1;
153-
if ((stat = fu_paths_append((FUPaths *)paths, path))) {
154-
PythonStorageGlobals *g = get_globals();
155-
g->modified = 1;
156-
}
157-
return stat;
146+
return fu_paths_append((FUPaths *)paths, path);
158147
}
159148

160149
/*
@@ -163,14 +152,9 @@ int dlite_python_storage_paths_append(const char *path)
163152
*/
164153
int dlite_python_storage_paths_remove_index(int index)
165154
{
166-
int stat;
167155
const FUPaths *paths;
168156
if (!(paths = dlite_python_storage_paths())) return -1;
169-
if ((stat = fu_paths_remove_index((FUPaths *)paths, index))) {
170-
PythonStorageGlobals *g = get_globals();
171-
g->modified = 1;
172-
}
173-
return stat;
157+
return fu_paths_remove_index((FUPaths *)paths, index);
174158
}
175159

176160
/*
@@ -194,15 +178,17 @@ const char **dlite_python_storage_paths_get(void)
194178
void *dlite_python_storage_load(void)
195179
{
196180
PyObject *storagebase;
181+
unsigned char hash[32];
182+
const FUPaths *paths;
197183
PythonStorageGlobals *g = get_globals();
198184

199185
if (!(storagebase = dlite_python_storage_base())) return NULL;
186+
if (!(paths = dlite_python_storage_paths())) return NULL;
187+
if (pathshash(hash, sizeof(hash), paths, "*.py")) return NULL;
200188

201-
if (!g->loaded_storages || g->modified) {
202-
const FUPaths *paths;
189+
if (!g->loaded_storages || memcmp(g->paths_hash, hash, sizeof(hash)) != 0) {
190+
memcpy(g->paths_hash, hash, sizeof(hash));
203191
if (g->loaded_storages) dlite_python_storage_unload();
204-
if (!(paths = dlite_python_storage_paths())) return NULL;
205-
206192
g->loaded_storages = dlite_pyembed_load_plugins((FUPaths *)paths,
207193
storagebase,
208194
&g->failed_paths,

0 commit comments

Comments
 (0)