pinokiod 1.0.47 → 1.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -95,13 +95,58 @@ class FS {
95
95
  // }
96
96
  // }
97
97
  //
98
+ // 2. pip
99
+ //
100
+ // 1.1. Share all pip
101
+ // {
102
+ // “method”: “fs.share”,
103
+ // “params”: {
104
+ // "venv": "app/env"
105
+ // }
106
+ // }
107
+ //
98
108
  /////////////////////////////////////////////////////////////////////////////
99
109
 
100
- console.log("fs.share", req.params)
101
-
102
110
  ondata({ raw: "\r\ncreating a shared drive:\r\n" + JSON.stringify(req.params, null, 2).replace(/\n/g, "\r\n") })
103
111
 
104
- if (req.params.drive) {
112
+ if (req.params.venv) {
113
+ // pip sharing
114
+ // 1. get all pip items
115
+ let p
116
+ if (kernel.platform === "win32") {
117
+ p = path.resolve(req.cwd, req.params.venv, "Lib", "site-packages")
118
+ } else {
119
+ p = path.resolve(req.cwd, req.params.venv, "lib", "python3.10", "site-packages")
120
+ }
121
+ const res = await kernel.python.call(
122
+ "pip",
123
+ path.resolve(__dirname, "../../python/pip.py"),
124
+ "list",
125
+ [p]
126
+ )
127
+ const d = {}
128
+ // symlink:
129
+ // from => :package_path
130
+ // to => /drives/packages/pip/:package_name/:package_version
131
+ for(let name in res) {
132
+ const package_path = res[name].path
133
+ const package_name = res[name].name
134
+ const version = res[name].version
135
+ d[`${package_name}/${version}`] = package_path
136
+ }
137
+ ondata({
138
+ raw: "\r\nLINKING DRIVES:\r\n" + JSON.stringify(d, null, 2).replace(/\n/g, "\r\n")
139
+ })
140
+ const drivePath = kernel.path("drive")
141
+ const drive = new Pdrive(drivePath)
142
+ await drive.create({
143
+ drive: d,
144
+ parent: "packages/pip"
145
+ })
146
+ ondata({
147
+ raw: "Done.\r\n"
148
+ })
149
+ } else if (req.params.drive) {
105
150
  const drivePath = kernel.path("drive")
106
151
  const drive = new Pdrive(drivePath)
107
152
  for(const route in req.params.drive) {
@@ -24,8 +24,6 @@ const fakeUa = require('fake-useragent');
24
24
  const fse = require('fs-extra')
25
25
 
26
26
 
27
-
28
-
29
27
  //const Puppet = require("./puppeteer")
30
28
  class Bin {
31
29
  constructor(kernel) {
@@ -437,7 +435,7 @@ class Bin {
437
435
  } else if (type === "pip") {
438
436
  const message = (install ? install : `pip install ${name} ${args}`)
439
437
  ondata({ html: `<b>${progress} Installing ${name}</b><br>${message}` }, "notify")
440
- await this.exec({ message }, ondata)
438
+ await this.exec({ message, conda: "base" }, ondata)
441
439
  } else if (type === "brew") {
442
440
  const message = (install ? install : `brew install ${name} ${args}`)
443
441
  ondata({ html: `<b>${progress} Installing ${name}</b><br>${message}` }, "notify")
package/kernel/index.js CHANGED
@@ -9,6 +9,7 @@ const portfinder = require('portfinder');
9
9
  const Loader = require("./loader")
10
10
  const Bin = require('./bin')
11
11
  const Api = require("./api")
12
+ const Python = require("./python")
12
13
  const Template = require('./template')
13
14
  const Shells = require("./shells")
14
15
  const Key = require("./key")
@@ -184,6 +185,7 @@ class Kernel {
184
185
  this.loader = new Loader()
185
186
  this.bin = new Bin(this)
186
187
  this.api = new Api(this)
188
+ this.python = new Python(this)
187
189
  this.shell = new Shells(this)
188
190
  this.system = system
189
191
  this.keys = {}
@@ -0,0 +1,49 @@
1
+ const path = require('path')
2
+ const nodecallspython = require("node-calls-python");
3
+ class Python {
4
+ constructor(kernel) {
5
+ this.kernel = kernel
6
+ }
7
+ async call(modname, modpath, method, args) {
8
+ // if this.proc doesn't exist, create
9
+ if (!this.proc) {
10
+ await this.kernel.exec({
11
+ message: [
12
+ "which pip",
13
+ "pip install fastapi importlib_metadata uvicorn[standard]",
14
+ ],
15
+ conda: "base"
16
+ }, (s) => {
17
+ })
18
+ await new Promise((resolve, reject) => {
19
+ this.kernel.exec({
20
+ message: "uvicorn --port 42001 server:app",
21
+ path: __dirname,
22
+ conda: "base"
23
+ }, (s) => {
24
+ // process.stdout.write(s)
25
+ if (/Uvicorn running on/i.test(s.cleaned)) {
26
+ resolve()
27
+ }
28
+ })
29
+ })
30
+ console.log("Server Started")
31
+ this.proc = true
32
+ }
33
+
34
+ let res = await fetch("http://127.0.0.1:42001/call", {
35
+ method: "POST",
36
+ headers: { "Content-Type": "application/json" },
37
+ body: JSON.stringify({
38
+ name: modname,
39
+ path: modpath,
40
+ method,
41
+ params: args
42
+ })
43
+ }).then((res) => {
44
+ return res.json()
45
+ })
46
+ return res
47
+ }
48
+ }
49
+ module.exports = Python
@@ -0,0 +1,79 @@
1
+ import pkg_resources
2
+ import platform
3
+ from pathlib import Path
4
+ import site
5
+ import sysconfig
6
+ import sys
7
+ import importlib
8
+ import importlib_metadata
9
+ import os
10
+
11
+ # Get all the pip packages for the base path
12
+ def list(sitepackages_base):
13
+ # print(f"LIST {sitepackages_base}")
14
+ sys.path=[sitepackages_base]
15
+ importlib.invalidate_caches()
16
+
17
+ # sys.path.insert(0, sitepackages_base)
18
+ print(f"sys.path={sys.path}")
19
+ print(f"sys.meta_path={sys.meta_path}")
20
+ print(f"sys.path_hooks={sys.path_hooks}")
21
+ #dists = importlib_metadata.packages_distributions()
22
+ dists = importlib_metadata.packages_distributions()
23
+ print(f"dists={dists}")
24
+ # print(f"DISTS {dists}")
25
+
26
+ # print(f"SYSPATH {sys.path}")
27
+ # for key in sys.modules:
28
+ # print(f"## {key}: {sys.modules[key]}")
29
+ # print(f"sys.modules={sys.modules}")
30
+ #print(f"BEFORE PATH: {sys.path}")
31
+ #print(f"AFTER PATH: {sys.path}")
32
+ # pkg_resources.working_set.add_entry(sitepackages_base)
33
+ sitepackages_path = sysconfig.get_paths()["purelib"]
34
+ dict = {}
35
+ # packages = importlib.resources.files(package)
36
+ for package_name in dists:
37
+ print(f"package_name={package_name}")
38
+ for dist_name in dists[package_name]:
39
+ # get package path
40
+ try:
41
+ meta = importlib_metadata.metadata(dist_name)
42
+ files = importlib_metadata.files(dist_name)
43
+
44
+ for file in files:
45
+
46
+ #if str(file).startswith("_"):
47
+ if str(file).startswith("__pycache__"):
48
+ continue
49
+ if str(file).startswith("."):
50
+ continue
51
+
52
+
53
+ # if the resolved path
54
+ abspath = os.path.join(sitepackages_base, file)
55
+ is_child = Path(abspath).is_relative_to(sitepackages_base) and (sitepackages_base in abspath)
56
+ if is_child:
57
+ relative = str(file)
58
+ chunks = relative.split(os.sep)
59
+ if len(chunks) > 1:
60
+ folder = chunks[0]
61
+ package_path = str(os.path.join(sitepackages_base, folder))
62
+ if not hasattr(dict, folder):
63
+ dict[folder] = {}
64
+
65
+ try:
66
+ dict[folder]["version"] = meta["Version"]
67
+ except:
68
+ print('no version')
69
+ dict[folder]["name"] = folder
70
+ dict[folder]["path"] = package_path
71
+ except Exception as error:
72
+ print(f"#ERR {error}")
73
+ # nothing
74
+
75
+ # get dist path (only if there's a version)
76
+
77
+ print(f"DICT={dict}")
78
+
79
+ return dict
@@ -0,0 +1,27 @@
1
+ from fastapi import FastAPI, Body
2
+ from typing import List
3
+ from pydantic import BaseModel
4
+ import sys
5
+ import os
6
+
7
+ import importlib
8
+ class Params(BaseModel):
9
+ values: List[str]
10
+
11
+ app = FastAPI()
12
+ #@app.get("/")
13
+ #async def root():
14
+ # return {"greeting":"Hello world"}
15
+
16
+ # { name: "pip.py", method: "list", params: [ ] }
17
+ @app.post("/call")
18
+ async def call(item: dict = Body(...)):
19
+ print(f"item={item}")
20
+ sys.path.insert(0, os.path.dirname(item["path"]))
21
+ #async def call(path: str, method: str, params: Params):
22
+ mod = importlib.import_module(item["name"], item["path"])
23
+ print(f"Mod = {mod}")
24
+ fun = getattr(mod, item["method"])
25
+ print(f"Fun = {fun}")
26
+ result = fun(*item["params"])
27
+ return result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinokiod",
3
- "version": "1.0.47",
3
+ "version": "1.0.49",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -40,6 +40,7 @@
40
40
  "marked": "^5.0.1",
41
41
  "mime-types": "^2.1.35",
42
42
  "multer": "^1.4.5-lts.1",
43
+ "node-calls-python": "^1.8.2",
43
44
  "node-downloader-helper": "^2.1.9",
44
45
  "node-pty-prebuilt-multiarch-cp": "^0.10.1-pre.48",
45
46
  "pdrive": "^0.0.14",