1- """
2- @header binpython.py
3- @author xingyujie https://github.com/xingyujie
4- @abstract BINPython main file
5- """
6-
71#BINPython By:XINGYUJIE AGPL-V3.0 LICENSE Release
82#Please follow the LICENSE AGPL-V3
93#full version
2822#from xxxx import xxxx
2923#xxxxxxxxxxxxxx
3024####################################
25+
3126#BINPython function and variable START
3227
3328class binpythoninfo :
@@ -96,12 +91,13 @@ def optreadfile():
9691 import urllib
9792 import wget
9893 import shutil
99-
94+ import json
10095#fix for exit()
10196 from sys import exit
10297#import for http_server
10398 import http .server
10499 import socketserver
100+ import flask
105101#except ImportError:
106102except (Exception , BaseException ) as error :
107103 print ("Unable to use any library, the program does not work properly, please rebuild" )
@@ -154,7 +150,7 @@ def importpygame():
154150#main BINPython
155151def binpython_welcome_text ():
156152 print ("BINPython " + ver + "-" + releases_ver + " (Python Version:" + platform .python_version () + ") By: Edward Hsing(Xing Yu Jie) https://github.com/xingyujie/binpython[Running on " + platform .platform () + " " + platform .version () + "]" )
157- print ('Type "about", "help", "copyright", "credits" or "license" for more information.' )
153+ print ('Type "about", "help", "copyright", "credits" or "license" for more information. Type "binpython_cmd" to enter BINPython CMD ' )
158154def binpython_shell ():
159155 while True :
160156 try :
@@ -187,6 +183,8 @@ def binpython_shell():
187183 """ )
188184 elif pycmd == 'license' :
189185 print ("Type license() to see the full license text" )
186+ elif pycmd == 'binpython_cmd' :
187+ binpython_cmd ()
190188 else :
191189 exec (pycmd )
192190 except KeyboardInterrupt :
@@ -198,9 +196,6 @@ def binpython_shell():
198196 optreadfile ()
199197except :
200198 pass
201- def execpyfile (filename ):
202- f = open (filename )
203- exec (f .read ())
204199def cloudruncli ():
205200
206201 print ("Welcome to CloudRun CLI. Let your script run in the cloud with BINPython" )
@@ -305,6 +300,10 @@ def editsource(url):
305300#cloudrun functions end
306301
307302#cmd start
303+ def get_resource_path (relative_path ):
304+ if hasattr (sys , '_MEIPASS' ):
305+ return os .path .join (sys ._MEIPASS , relative_path )
306+ return os .path .join (os .path .abspath ("." ), relative_path )
308307def listfiles ():
309308 import os
310309 dirs = os .listdir ("./" )
@@ -344,14 +343,133 @@ def unzip(path, folder_abs):
344343 zip_file .extract (f , folder_abs )
345344
346345 zip_file .close ()
346+ def mkbpfs ():
347+ print ("Welcome to bpfs[BINPython File System] making tool (mkfs.bpfs). Type help to list commands and help." )
348+ while True :
349+ cmd = input ("(BPFS CLI) " )
350+ if cmd == 'mkfs' :
351+ print ("Make a new bpfs filesystem" )
352+ fspath = input ("Enter the bpfs save path of the file system: " )
353+ try :
354+ os .mkdir (fspath )
355+ except :
356+ pass
357+ try :
358+ os .chdir (fspath )
359+ print ("[*] Done." )
360+ except :
361+ print ("[E] path does not exist!" )
362+ print ("[*] Make base file system" )
363+ try :
364+ os .makedirs ("binpython_files/apps" )
365+ os .makedirs ("binpython_files/cmd" )
366+ os .makedirs ("binpython_files/hostname" )
367+ os .makedirs ("binpython_files/userdata" )
368+ open ("binpython_files/cmd/cmd.py" , "w" )
369+ except (Exception , BaseException ) as error :
370+ print ("make dirs and files Error." + error )
371+ try :
372+ hostnameyn = input ("do you want to create hostname(y/n): " )
373+ if hostnameyn == 'y' :
374+ hostnamecontent = input ("enter hostname: " )
375+ with open ("binpython_files/hostname/hostname" , "w" ) as sethostname :
376+ sethostname .write (hostnamecontent )
377+ print ("[*] Done." )
378+ else :
379+ pass
380+ except (Exception , BaseException ) as error :
381+ print ("makehostname Error." + error )
382+ try :
383+ usernameyn = input ("do you want to set default login user(y/n): " )
384+ if usernameyn == 'y' :
385+ usernamecontent = input ("enter default login username: " )
386+ with open ("binpython_files/userdata/defaultloginuser" , "w" ) as setusername :
387+ setusername .write (usernamecontent )
388+ os .makedirs (f"binpython_files/userdata/home/{ usernamecontent } " )
389+ print ("[*] Done." )
390+ else :
391+ pass
392+ except (Exception , BaseException ) as error :
393+ print ("Set default username Error. " + error )
394+ if cmd == 'help' :
395+ print ("""
396+ List commands:
397+ mkfs -- Make a new bpfs filesystem
398+ help -- This help
399+ exit -- exit mkbpfs
400+ """ )
401+ if cmd == 'exit' :
402+ binpython_cmd ()
403+ def getfsfile (path ):
404+ try :
405+ os .mkdir ("binpython_files" )
406+ except :
407+ pass
408+ print ("[*] Unzip File System BPFS(BINPython File System) from file" )
409+ unzip (path , runpath + "/binpython_files" )
410+ print ("\n " )
411+ print ("[*] Done!" )
412+ binpython_cmd ()
413+ def downfs (bpfsurl ):
414+ try :
415+ os .mkdir ("binpython_files" )
416+ except :
417+ pass
418+ print ("[*] Download File System BPFS(BINPython File System)" )
419+ wget .download (bpfsurl , runpath + "/binpython_files" )
420+ unzip (runpath + "/binpython_files/officialbpfs.bpfs" , runpath + "/binpython_files" )
421+ print ("\n " )
422+ print ("[*] Done!" )
423+ binpython_cmd ()
347424def binpython_cmd ():
348425 global runpath
349426 runpath = os .path .dirname (os .path .realpath (sys .argv [0 ]))
427+ try :
428+ os .chdir (runpath + f"/binpython_files/userdata/" )
429+ os .chdir (runpath + f"/binpython_files/cmd" )
430+ except :
431+ whichmethod = input ("""
432+ Welcome to BINPython CMD! Choose a method to install BINPython CMD:
433+ 1. WEB graphical interactive installation (recommended)
434+ 2. Interactive text interface (also for non-graphical devices)
435+ Please enter a number (1/2):
436+ """ )
437+ if whichmethod == '1' :
438+ getwebui = open (get_resource_path ('webui.py' ))
439+ webui = getwebui .read ()
440+ exec (webui )
441+ if whichmethod == '2' :
442+ print ('''
443+ The file system cannot be found or there is an incomplete file system.
444+ type "getfs" to download a file system;
445+ type "getfsurl" to download a filesystem via a custom url;
446+ Type "getfsfile" to unzip the filesystem via file;
447+ type "mkbpfs" to make a new file system;
448+ type "exit" to exit;
449+ type "shell" to force entry into the shell.
450+ * After forcing into the shell, you can create bpfs via "mkbpfs" command or use "adduser", "setdefaultuser" and other commands to build the filesystem step by step. But we don't recommend it, it may be more problematic and more complex
451+ ''' )
452+ while True :
453+ initcmd = input ("(InstallationENV) " )
454+ if initcmd == 'exit' :
455+ exit ()
456+ if initcmd == 'shell' :
457+ break
458+ if initcmd == 'mkbpfs' :
459+ mkbpfs ()
460+ if initcmd == 'getfs' :
461+ downfs ('https://raw.githubusercontent.com/xingyujie/binpython-repository/main/officialbpfsbase.bpfs' )
462+ if initcmd == 'getfsurl' :
463+ fsurl = input ("Please enter a download link (like: http://url.com/bpfs/bpfsbase.bpfs)" )
464+ downfs (fsurl )
465+ if initcmd == 'getfsfile' :
466+ filepath = input ("Please enter a (*.bpfs) file path: " )
467+ getfsfile (filepath )
350468 try :
351469 global cmd_username
352- defaultprofile = open (" binpython_files/userdata/defaultloginuser" , "r" )
470+ defaultprofile = open (runpath + "/ binpython_files/userdata/defaultloginuser" , "r" )
353471 cmd_username = defaultprofile .read ()
354- os .chdir (f" binpython_files/userdata/home/{ cmd_username } " )
472+ os .chdir (runpath + f"/ binpython_files/userdata/home/{ cmd_username } " )
355473 except :
356474 cmd_username = 'user'
357475 print ('Unable to switch to BINPython userprofile: Default user not found. To use a temporary directory user, use "adduser" and "setdefaultuser <username>" to create a user and set default user' )
@@ -367,7 +485,7 @@ def binpython_cmd():
367485 pass
368486 class cmdshell (cmd .Cmd ):
369487 intro = 'Welcome to BINPython Shell. Type help or ? to list commands and help.\n '
370- prompt = cmd_username + '@' + cmd_hostname + ':# '
488+ prompt = cmd_username + '@' + cmd_hostname + ':~ # '
371489 file = None
372490 try :
373491 os .makedirs (runpath + f"/binpython_files/cmd/" )
@@ -517,6 +635,29 @@ def do_install(self, arg):
517635 print ('Unzip the package failed, please see the log "install_error.log" for details' )
518636 f = open ("install_error.log" , "a" )
519637 f .write ('Unzip package Error: ' + time .strftime ('%m-%d-%Y %H:%M:%S' ,time .localtime (time .time ())) + ' ' + str (error ) + '\n ' )
638+ try :
639+ f = open (runpath + f"/binpython_files/apps/{ cmd_username } /{ arg } /package.json" )
640+ pkginfo = json .loads (f .read ())
641+ print (f"""
642+ Package information:
643+ Name: { pkginfo ['name' ]}
644+ Version: { pkginfo ['version' ]}
645+ Summary: { pkginfo ['summary' ]}
646+ Homepage: { pkginfo ['homepage' ]}
647+ Author: { pkginfo ['author' ]}
648+ Email: { pkginfo ['email' ]}
649+ License: { pkginfo ['license' ]}
650+ """ )
651+ f .close ()
652+ yn = input ("Do you want to continue?(y/n): " )
653+ if yn == 'y' :
654+ pass
655+ else :
656+ sys .exit (0 )
657+ except (Exception , BaseException ) as error :
658+ print ('[W] Warning: Could not find package configuration information file, please see the log "install_warning.log" for details' )
659+ f = open ("install_warning.log" , "a" )
660+ f .write ('Run package information Warning: ' + time .strftime ('%m-%d-%Y %H:%M:%S' ,time .localtime (time .time ())) + ' ' + str (error ) + '\n ' )
520661 try :
521662 print ("[*] configure package" )
522663 f = open (runpath + f"/binpython_files/apps/{ cmd_username } /{ arg } /config.py" )
@@ -538,6 +679,7 @@ def do_installfile(self, arg):
538679 if arg == '' :
539680 print ("Please use install <package file name> missing options <package file name>" )
540681 exit ()
682+ nobpkgarg = arg .replace ('.bpkg' , '' )
541683 try :
542684 os .makedirs (runpath + f"/binpython_files/apps/{ cmd_username } " )
543685 except :
@@ -548,14 +690,36 @@ def do_installfile(self, arg):
548690 pass
549691 try :
550692 print ("[*] Unzip the package" )
551- unzip (arg , runpath + f"/binpython_files/apps/{ cmd_username } /{ arg } " )
693+ unzip (arg , runpath + f"/binpython_files/apps/{ cmd_username } /{ nobpkgarg } " )
552694 except (Exception , BaseException ) as error :
553695 print ('Unzip the package failed, please see the log "install_error.log" for details' )
554696 f = open ("install_error.log" , "a" )
555697 f .write ('Unzip package Error: ' + time .strftime ('%m-%d-%Y %H:%M:%S' ,time .localtime (time .time ())) + ' ' + str (error ) + '\n ' )
698+ try :
699+ f = open (runpath + f"/binpython_files/apps/{ cmd_username } /{ nobpkgarg } /package.json" )
700+ pkginfo = json .loads (f .read ())
701+ print (f"""
702+ Package information:
703+ Name: { pkginfo ['name' ]}
704+ Version: { pkginfo ['version' ]}
705+ Summary: { pkginfo ['summary' ]}
706+ Homepage: { pkginfo ['homepage' ]}
707+ Author: { pkginfo ['author' ]}
708+ Email: { pkginfo ['email' ]}
709+ License: { pkginfo ['license' ]}
710+ """ )
711+ yn = input ("Do you want to continue?(y/n): " )
712+ if yn == 'y' :
713+ pass
714+ else :
715+ binpython_cmd ()
716+ except (Exception , BaseException ) as error :
717+ print ('[W] Warning: Could not find package configuration information file, please see the log "install_warning.log" for details' )
718+ f = open ("install_warning.log" , "a" )
719+ f .write ('Run package information Warning: ' + time .strftime ('%m-%d-%Y %H:%M:%S' ,time .localtime (time .time ())) + ' ' + str (error ) + '\n ' )
556720 try :
557721 print ("[*] configure package" )
558- f = open (runpath + f"/binpython_files/apps/{ cmd_username } /{ arg } /config.py" )
722+ f = open (runpath + f"/binpython_files/apps/{ cmd_username } /{ nobpkgarg } /config.py" )
559723 exec (f .read ())
560724 except (Exception , BaseException ) as error :
561725 print ('[W]config file no configuration or configuration error, please see the log "install_warning.log" for details' )
@@ -573,8 +737,7 @@ def do_switchappdir(self, arg):
573737 def do_runapp (self , arg ):
574738 'To run a BINPython bpkg program, first pass install <app name> or installfile <app package path> Usage: runapp <appname>. '
575739 try :
576- f = open (runpath + f"/binpython_files/apps/{ cmd_username } /{ arg } /main.py" )
577- exec (f .read ())
740+ execpyfile (runpath + f"/binpython_files/apps/{ cmd_username } /{ arg } /main.py" )
578741 except (Exception , BaseException ) as error :
579742 print ('App not exits or failed, please see the log "binpython_pkg_error.log" for details' )
580743 f = open ("binpython_pkg_error.log" , "a" )
@@ -598,7 +761,7 @@ def do_listapps(self, arg):
598761 def do_editsource (self , arg ):
599762 'To change the software source, usage: editsouce <souceurl>. Please pay attention to the software source specification, otherwise you will get an error. <souceurl> like this: http://xxx.com/'
600763 f = open (runpath + f"/binpython_files/apps/source.config" , "w" )
601- f .write (arg )
764+ f .write ('http://' + arg + '/' )
602765 def do_tempuser (self , arg ):
603766 'Create a temporary user, logging out will destroy the user space'
604767 print ("You are trying to create a temporary user, this user space is only used for demonstration, testing and learning. When this user is logged out, all user data will also be deleted" )
@@ -642,8 +805,9 @@ def do_initcmd(self, arg):
642805 print ('Can not initcmd, please see the log "binpython_cmd_error.log" for details' )
643806 f = open ("binpython_cmd_error.log" , "a" )
644807 f .write ('Init CMD Error: ' + time .strftime ('%m-%d-%Y %H:%M:%S' ,time .localtime (time .time ())) + ' ' + str (error ) + '\n ' )
645-
646-
808+ def do_mkbpfs (self , arg ):
809+ 'Make BINPython File System'
810+ mkbpfs ()
647811 if __name__ == '__main__' :
648812 cmdshell ().cmdloop ()
649813#cmd end
@@ -687,6 +851,9 @@ def outputfullhelp():
687851 sys .exit ()
688852#get every option and run
689853for opt_name ,opt_value in opts :
854+ def execpyfile (filename ):
855+ f = open (filename )
856+ exec (f .read ())
690857 if opt_name in ('-h' ,'--help' ):
691858#-h show full help function
692859 outputfullhelp ()
@@ -1085,6 +1252,4 @@ def loadmain(key):
10851252 startupcode = open (filename .read (),encoding = "utf-8" )
10861253 exec (startupcode .read ())
10871254except :
1088- pass
1089- #go shell
1090- binpython_shell ()
1255+ binpython_shell ()
0 commit comments