From d90453ecd936a6c33c633175d3c19cc39b2fc1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=AB=8B=E5=B8=AE?= <3294713004@qq.com> Date: Thu, 17 Apr 2025 15:57:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E5=9C=A8=E7=BA=BF=E7=89=88ampy?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E6=97=B6=E9=A6=96=E5=85=88?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=B7=A5=E4=BD=9C=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/modules/mixly-modules/web/ampy.js | 18 ++++++++++++++---- .../modules/mixly-modules/web/burn-upload.js | 5 +++-- common/templates/python/cwd.py | 6 ++++++ common/templates/python/ls.py | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 common/templates/python/cwd.py diff --git a/common/modules/mixly-modules/web/ampy.js b/common/modules/mixly-modules/web/ampy.js index fc3e8a02..5277af3e 100644 --- a/common/modules/mixly-modules/web/ampy.js +++ b/common/modules/mixly-modules/web/ampy.js @@ -27,6 +27,7 @@ class AmpyExt extends Ampy { this.RM = goog.get(path.join(Env.templatePath, 'python/rm.py')); this.RMDIR = goog.get(path.join(Env.templatePath, 'python/rmdir.py')); this.GET = goog.get(path.join(Env.templatePath, 'python/get.py')); + this.CWD = goog.get(path.join(Env.templatePath, 'python/cwd.py')); } #device_ = null; @@ -262,7 +263,7 @@ class AmpyExt extends Ampy { } const { data, dataError } = await this.exec(code, timeout); if (dataError) { - return '[]'; + return []; } return JSON.parse(data.replaceAll('\'', '\"')); } @@ -297,7 +298,6 @@ class AmpyExt extends Ampy { oldPath: oldname, newPath: newname }); - const result = await this.exec(code, timeout); const { dataError } = await this.exec(code, timeout); return !dataError; } @@ -310,7 +310,6 @@ class AmpyExt extends Ampy { path: filename }); await this.exec(code); - const result = await this.exec(code, timeout); const { dataError } = await this.exec(code, timeout); return !dataError; } @@ -322,11 +321,22 @@ class AmpyExt extends Ampy { const code = Mustache.render(AmpyExt.RMDIR, { path: directory }); - const result = await this.exec(code, timeout); const { dataError } = await this.exec(code, timeout); return !dataError; } + async cwd(timeout = 5000) { + if (!this.isActive()) { + throw new Error(Msg.Lang['ampy.portIsNotOpen']); + } + const code = Mustache.render(AmpyExt.CWD, {}); + const { data, dataError } = await this.exec(code, timeout); + if (dataError) { + return '/'; + } + return data; + } + getDevice() { return this.#device_; } diff --git a/common/modules/mixly-modules/web/burn-upload.js b/common/modules/mixly-modules/web/burn-upload.js index 98dbdfb7..c2abbbd2 100644 --- a/common/modules/mixly-modules/web/burn-upload.js +++ b/common/modules/mixly-modules/web/burn-upload.js @@ -701,14 +701,15 @@ BU.uploadWithAmpy = (portName) => { statusBarTerminal.addValue('Writing main.py '); await ampy.put('main.py', code); statusBarTerminal.addValue('Done!\n'); - /*const rootInfo = await ampy.ls('/'); + /*const cwd = await ampy.cwd(); + const rootInfo = await ampy.ls(cwd); let rootMap = {}; for (let item of rootInfo) { rootMap[item[0]] = item[1]; } if (libraries && libraries instanceof Object) { for (let key in libraries) { - if (rootMap[`/${key}`] !== undefined && rootMap[`/${key}`] === libraries[key].size) { + if (rootMap[`${cwd}/${key}`] !== undefined && rootMap[`${cwd}/${key}`] === libraries[key].size) { statusBarTerminal.addValue(`Skip ${key}\n`); continue; } diff --git a/common/templates/python/cwd.py b/common/templates/python/cwd.py new file mode 100644 index 00000000..c588360f --- /dev/null +++ b/common/templates/python/cwd.py @@ -0,0 +1,6 @@ +try: + import os +except ImportError: + import uos as os + +print(os.getcwd(), end='') \ No newline at end of file diff --git a/common/templates/python/ls.py b/common/templates/python/ls.py index 2fa2b75b..e7c0dd19 100644 --- a/common/templates/python/ls.py +++ b/common/templates/python/ls.py @@ -20,7 +20,7 @@ def check_path(path): return 'file' else: return 'special file' - except OSError: + except Exception: return 'none' def listdir(directory):