Skip to content

Commit dc4cede

Browse files
committed
0.41
1 parent d5fd6ee commit dc4cede

File tree

1 file changed

+139
-18
lines changed

1 file changed

+139
-18
lines changed

binpythonfull.py

Lines changed: 139 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
####################################
1111
#build configure
1212

13-
ver = "0.40-build-full"
13+
ver = "0.41-build-full"
1414

1515
libs_warning = "1"
1616
#1 is ture 0 is false.
@@ -20,7 +20,7 @@
2020
releases_ver = "offical"
2121
importlibs = "os"
2222
cloudrunver = "1.03"
23-
cmdver = "0.02"
23+
cmdver = "0.04"
2424
#Imported library name, please use "importlibs="<library name>" instead of "import <library name>"
2525
#Please note: The "importlibs" function does not support loading functions (such as from xxxx import xxxx, if necessary, please write it in the following location. However, please note that this operation may have the risk of error reporting, please report issues or solve it yourself
2626
#xxxxxxxxxxxxxx
@@ -93,6 +93,11 @@ def optreadfile():
9393
import urllib.request
9494
import base64
9595
import cmd
96+
import zipfile
97+
import requests
98+
import urllib
99+
import wget
100+
96101
#fix for exit()
97102
from sys import exit
98103
#import for http_server
@@ -297,21 +302,48 @@ def listfiles():
297302
dirs = os.listdir("./")
298303
for file in dirs:
299304
print (file)
305+
def download(url,path):
306+
if not os.path.exists(path):
307+
os.mkdir(path)
308+
start = time.time()
309+
response = requests.get(url, stream=True)
310+
size = 0
311+
chunk_size = 1024
312+
content_size = int(response.headers['content-length'])
313+
try:
314+
if response.status_code == 200:
315+
print('Start download,[App size]:{size:.2f} MB'.format(size = content_size / chunk_size /1024))
316+
filepath = path+'\name.extension name'
317+
with open(filepath,'wb') as file:
318+
for data in response.iter_content(chunk_size = chunk_size):
319+
file.write(data)
320+
size +=len(data)
321+
print('\r'+'[Downloading]:%s%.2f%%' % ('>'*int(size*50/ content_size), float(size / content_size * 100)) ,end=' ')
322+
end = time.time()
323+
print('Download completed!,times: %.2f second' % (end - start))
324+
except:
325+
print("Download package failed!")
326+
def unzip(path, folder_abs):
327+
zip_file = zipfile.ZipFile(path)
328+
zip_list = zip_file.namelist()
300329

330+
for f in zip_list:
331+
zip_file.extract(f, folder_abs)
332+
333+
zip_file.close()
301334
def binpython_cmd():
302335
global runpath
303336
runpath = os.path.dirname(os.path.realpath(sys.argv[0]))
304337
try:
305338
global cmd_username
306-
defaultprofile = open("binpython_filesystem/userdata/defaultloginuser", "r")
339+
defaultprofile = open("binpython_files/userdata/defaultloginuser", "r")
307340
cmd_username = defaultprofile.read()
308-
os.chdir(f"binpython_filesystem/userdata/home/{cmd_username}")
341+
os.chdir(f"binpython_files/userdata/home/{cmd_username}")
309342
except:
310343
cmd_username = 'user'
311344
print('Unable to switch to BINPython userprofile: Default user not found. To use a temporary directory user, use "adddefaultuser" to create a default user')
312345
try:
313-
scriptpath = os.path.dirname(os.path.realpath(sys.argv[0]))
314-
gethostname = open(scriptpath + "/binpython_filesystem/hostname/hostname", "r")
346+
gethostname = open(runpath + "/binpython_files/hostname/hostname", "r")
315347
cmd_hostname = gethostname.read()
316348
except:
317349
cmd_hostname = "binpython"
@@ -344,7 +376,7 @@ def do_adduser(self, arg):
344376
username = input("Username: ")
345377
print("create user...")
346378
try:
347-
os.makedirs(runpath + f"/binpython_filesystem/userdata/home/{username}")
379+
os.makedirs(runpath + f"/binpython_files/userdata/home/{username}")
348380
except(Exception, BaseException) as error:
349381
print("User already exits or system error")
350382
print(error)
@@ -357,10 +389,10 @@ def do_adddefaultuser(self, arg):
357389
username = input("Default Username: ")
358390
print("create user...")
359391
try:
360-
os.makedirs(scriptpath + f"/binpython_filesystem/userdata/home/{username}")
392+
os.makedirs(scriptpath + f"/binpython_files/userdata/home/{username}")
361393
except:
362394
pass
363-
defaultprofile = open(scriptpath + "/binpython_filesystem/userdata/defaultloginuser", "w")
395+
defaultprofile = open(scriptpath + "/binpython_files/userdata/defaultloginuser", "w")
364396
defaultprofile.write(username)
365397
print("Done")
366398
def do_shell(self, arg):
@@ -374,25 +406,22 @@ def do_python(self, arg):
374406
exec(f.read())
375407
def do_seteditor(self, arg):
376408
'Set a default Python or Text file code editor usage: seteditor <editorname>. like: "seteditor code" (Open code via Visual Studio Code when using the "edit <filename>" command). "seteditor notepad" (Open the code through Notepad that comes with Windows)'
377-
scriptpath = os.path.dirname(os.path.realpath(sys.argv[0]))
378-
defaulteditor = open(runpath + f"/binpython_filesystem/userdata/home/{cmd_username}/defaulteditor.config", "w")
409+
defaulteditor = open(runpath + f"/binpython_files/userdata/home/{cmd_username}/defaulteditor.config", "w")
379410
defaulteditor.write(arg)
380411
def do_edit(self, arg):
381412
'Before using this command, you must use "seteditor <editorname>" to set the editor (see the usage of this parameter for details), otherwise it cannot be called up. edit usage: edit <filename>'
382-
scriptpath = os.path.dirname(os.path.realpath(sys.argv[0]))
383-
defaulteditor = open(runpath + f"/binpython_filesystem/userdata/home/{cmd_username}/defaulteditor.config", "r")
413+
defaulteditor = open(runpath + f"/binpython_files/userdata/home/{cmd_username}/defaulteditor.config", "r")
384414
os.system(defaulteditor.read() + ' ' + arg)
385415
def do_exit(self, arg):
386416
'exit shell'
387417
sys.exit()
388418
def do_sethostname(self, arg):
389419
'set up hostname. Usage sethostname <hostname>'
390-
scriptpath = os.path.dirname(os.path.realpath(sys.argv[0]))
391420
try:
392-
os.makedirs(scriptpath + "/binpython_filesystem/hostname/")
421+
os.makedirs(runpath + "/binpython_files/hostname/")
393422
except:
394423
pass
395-
hostnamefile = open(scriptpath + "/binpython_filesystem/hostname/hostname", "w")
424+
hostnamefile = open(runpath + "/binpython_files/hostname/hostname", "w")
396425
hostnamefile.write(arg)
397426
def do_rm(self, arg):
398427
'remove files Usage: rm <filename>'
@@ -420,8 +449,100 @@ def do_uname(self, arg):
420449
print(f"BINPython CMD By: Edward Hsing VER:{cmdver} ")
421450
def do_user(self, arg):
422451
'Change User'
423-
os.chdir(runpath + f"/binpython_filesystem/userdata/home/{arg}")
424-
452+
os.chdir(runpath + f"/binpython_files/userdata/home/{arg}")
453+
def do_install(self, arg):
454+
'Install package'
455+
try:
456+
global appsource
457+
f = open(runpath + f"/binpython_files/apps/source.config", "r")
458+
appsource = f.read()
459+
except:
460+
f = open(runpath + f"/binpython_files/apps/source.config", "w")
461+
f.write("https://raw.githubusercontent.com/xingyujie/binpython-repository/main/")
462+
appsource = "https://raw.githubusercontent.com/xingyujie/binpython-repository/main/"
463+
if arg == '':
464+
print("Please use install <package name> missing options <package name>")
465+
exit()
466+
try:
467+
os.makedirs(runpath + f"/binpython_files/apps/{cmd_username}")
468+
except:
469+
pass
470+
try:
471+
os.makedirs(runpath + f"/binpython_files/apps/{cmd_username}/installtemp")
472+
except:
473+
pass
474+
try:
475+
print("[*] download package")
476+
wget.download(f"{appsource}{arg}.bpkg", runpath + f"/binpython_files/apps/{cmd_username}/installtemp/{arg}.bpkg")
477+
print("\n")
478+
except(Exception, BaseException) as error:
479+
print('The package does not exist or system error, please see the log "install_error.log" for details')
480+
f = open("install_error.log", "a")
481+
f.write('Install Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
482+
try:
483+
print("[*] Unzip the package")
484+
unzip(runpath + f"/binpython_files/apps/{cmd_username}/installtemp/{arg}.bpkg", runpath + f"/binpython_files/apps/{cmd_username}/{arg}")
485+
except(Exception, BaseException) as error:
486+
print('Unzip the package failed, please see the log "install_error.log" for details')
487+
f = open("install_error.log", "a")
488+
f.write('Unzip package Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
489+
try:
490+
print("[*] configure package")
491+
f = open(runpath + f"/binpython_files/apps/{cmd_username}/{arg}/config.py")
492+
exec(f.read())
493+
except(Exception, BaseException) as error:
494+
print('[W] Warning: config file no configuration or configuration error, please see the log "install_warning.log" for details')
495+
f = open("install_warning.log", "a")
496+
f.write('Run package configuration file Warning: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
497+
try:
498+
print("[*] Clean up temporary files")
499+
os.remove(runpath + f"/binpython_files/apps/{cmd_username}/installtemp/{arg}.bpkg")
500+
except(Exception, BaseException) as error:
501+
print('Failed to clean up temporary files, please see the log "install_error.log" for details')
502+
f = open("install_error.log", "a")
503+
f.write('clean up temporary files Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
504+
print("[OK]Finished!")
505+
def do_installfile(self, arg):
506+
'Install package from local bpkg file'
507+
if arg == '':
508+
print("Please use install <package file name> missing options <package file name>")
509+
exit()
510+
try:
511+
os.makedirs(runpath + f"/binpython_files/apps/{cmd_username}")
512+
except:
513+
pass
514+
try:
515+
os.makedirs(runpath + f"/binpython_files/apps/{cmd_username}/installtemp")
516+
except:
517+
pass
518+
try:
519+
print("[*] Unzip the package")
520+
unzip(arg, runpath + f"/binpython_files/apps/{cmd_username}/{arg}")
521+
except(Exception, BaseException) as error:
522+
print('Unzip the package failed, please see the log "install_error.log" for details')
523+
f = open("install_error.log", "a")
524+
f.write('Unzip package Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
525+
try:
526+
print("[*] configure package")
527+
f = open(runpath + f"/binpython_files/apps/{cmd_username}/{arg}/config.py")
528+
exec(f.read())
529+
except(Exception, BaseException) as error:
530+
print('[W]config file no configuration or configuration error, please see the log "install_warning.log" for details')
531+
f = open("install_warning.log", "a")
532+
f.write('Run package configuration file Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
533+
print("[OK]Finished!")
534+
def do_switchappdir(self, arg):
535+
'Switch to App dir. Usage: switchappdir <appname>'
536+
os.chdir(runpath + f"/binpython_files/apps/{cmd_username}/{arg}")
537+
def do_runapp(self, arg):
538+
'To run a BINPython bpkg program, first pass install <app name> or installfile <app package path> Usage: runapp <appname>. '
539+
try:
540+
f = open(runpath + f"/binpython_files/apps/{cmd_username}/{arg}/main.py")
541+
exec(f.read())
542+
except(Exception, BaseException) as error:
543+
print('Runapp failed, please see the log "binpython_pkg_error.log" for details')
544+
f = open("binpython_pkg_error.log", "a")
545+
f.write('Run package Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
425546
if __name__ == '__main__':
426547
cmdshell().cmdloop()
427548
#cmd end

0 commit comments

Comments
 (0)