Skip to content

Commit 60e5591

Browse files
committed
0.44
1 parent 4610e6a commit 60e5591

File tree

1 file changed

+79
-25
lines changed

1 file changed

+79
-25
lines changed

binpython.py

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

13-
ver = "0.43"
13+
ver = "0.44"
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.04"
23-
cmdver = "0.06"
23+
cmdver = "0.08"
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
@@ -213,7 +213,8 @@ def cloudruncli():
213213
editsource -- Set up custom sources and save via configuration files
214214
shell -- Go to BINPython Shell
215215
version -- CloudRun Version
216-
help -- show this help
216+
help -- show this help
217+
exit -- quit cloudrun
217218
""")
218219
if cloudruncli == 'get':
219220
print("Get apps/scripts in software repository")
@@ -241,6 +242,8 @@ def cloudruncli():
241242
binpython_shell()
242243
if cloudruncli == 'version':
243244
print(f"CloudRun-{cloudrunver} BINPython version By:Edward Hsing(Xing Yu Jie) AGPL-3.0 LICENSE")
245+
if cloudruncli == 'exit':
246+
exit()
244247
if cloudruncli == '':
245248
pass
246249
else:
@@ -349,16 +352,27 @@ def binpython_cmd():
349352
os.chdir(f"binpython_files/userdata/home/{cmd_username}")
350353
except:
351354
cmd_username = 'user'
352-
print('Unable to switch to BINPython userprofile: Default user not found. To use a temporary directory user, use "adddefaultuser" to create a default user')
355+
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')
353356
try:
354357
gethostname = open(runpath + "/binpython_files/hostname/hostname", "r")
355358
cmd_hostname = gethostname.read()
356359
except:
357360
cmd_hostname = "binpython"
361+
try:
362+
shutil.rmtree(runpath + f"/binpython_files/userdata/home/tempuser")
363+
shutil.rmtree(runpath + f"/binpython_files/apps/tempuser")
364+
except:
365+
pass
358366
class cmdshell(cmd.Cmd):
359367
intro = 'Welcome to BINPython Shell. Type help or ? to list commands and help.\n'
360-
prompt = cmd_username + '@' + cmd_hostname + ':' + '# '
368+
prompt = cmd_username + '@' + cmd_hostname + ':# '
361369
file = None
370+
try:
371+
f = open(runpath + f"/binpython_files/cmd/cmd.py", "r")
372+
exec(f.read())
373+
except(Exception, BaseException) as error:
374+
f = open(runpath + f"/binpython_files/cmd/cmd_ignore.txt", "a")
375+
f.write('Custom CMD ignore: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
362376
def do_cloudrunget(self, arg):
363377
'Get CloudRun Script from repository use:cloudrunget <scriptname>'
364378
cloudrun.get(arg)
@@ -389,19 +403,10 @@ def do_adduser(self, arg):
389403
print("User already exits or system error")
390404
print(error)
391405
print("Done")
392-
def do_adddefaultuser(self, arg):
393-
'Create a default user'
394-
global scriptpath
395-
scriptpath = os.path.dirname(os.path.realpath(sys.argv[0]))
396-
print("Create a default login user for BINPython")
397-
username = input("Default Username: ")
398-
print("create user...")
399-
try:
400-
os.makedirs(scriptpath + f"/binpython_files/userdata/home/{username}")
401-
except:
402-
pass
403-
defaultprofile = open(scriptpath + "/binpython_files/userdata/defaultloginuser", "w")
404-
defaultprofile.write(username)
406+
def do_setdefaultuser(self, arg):
407+
'Set a default user'
408+
defaultprofile = open(runpath + "/binpython_files/userdata/defaultloginuser", "w")
409+
defaultprofile.write(arg)
405410
print("Done")
406411
def do_shell(self, arg):
407412
'Go to Python interpreter'
@@ -425,6 +430,7 @@ def do_edit(self, arg):
425430
pass
426431
def do_exit(self, arg):
427432
'exit shell'
433+
print("logout")
428434
sys.exit()
429435
def do_sethostname(self, arg):
430436
'set up hostname. Usage sethostname <hostname>'
@@ -459,8 +465,15 @@ def do_uname(self, arg):
459465
'version of CMD'
460466
print(f"BINPython CMD By: Edward Hsing VER:{cmdver} ")
461467
def do_user(self, arg):
462-
'Change User'
468+
'Switch User Usage: user <username>'
463469
os.chdir(runpath + f"/binpython_files/userdata/home/{arg}")
470+
try:
471+
f = open(runpath + "/binpython_files/userdata/defaultloginuser", "w")
472+
f.write(arg)
473+
except(Exception, BaseException) as error:
474+
print('Switch User Error, please see the log "binpython_user_error.log" for details')
475+
f = open("binpython_user_error.log", "a")
476+
f.write('Switch User Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
464477
def do_install(self, arg):
465478
'Install package'
466479
try:
@@ -580,6 +593,50 @@ def do_editsouce(self, arg):
580593
'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/'
581594
f = open(runpath + f"/binpython_files/apps/source.config", "w")
582595
f.write(arg)
596+
def do_tempuser(self, arg):
597+
'Create a temporary user, logging out will destroy the user space'
598+
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")
599+
print("..........")
600+
time.sleep(0.3)
601+
print('Please note: the username of the temporary user is "tempuser" and the "tempuser" user is being created')
602+
try:
603+
os.makedirs(runpath + f"/binpython_files/apps/tempuser")
604+
os.makedirs(runpath + f"/binpython_files/userdata/home/tempuser")
605+
except(Exception, BaseException) as error:
606+
print('Can not create tempuser, please see the log "binpython_user_error.log" for details')
607+
f = open("binpython_user_error.log", "a")
608+
f.write('Switch tempuser Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
609+
os.chdir(runpath + f"/binpython_files/userdata/home/tempuser")
610+
print("Created successfully, has switched to the tempuser directory")
611+
def do_repolist(self, arg):
612+
'Change the BINPython source from the official list'
613+
cloudrun.load("https://raw.githubusercontent.com/xingyujie/binpython-repository/main/source.py")
614+
def do_cloudrunshell(self, arg):
615+
'Go to CloudRun Shell'
616+
cloudruncli()
617+
def do_whoami(self, arg):
618+
'Print Username'
619+
print(cmd_username)
620+
def do_hostname(self, arg):
621+
'Print Hostname'
622+
print(cmd_hostname)
623+
def do_rootpath(self, arg):
624+
'Print rootpath(runpath)'
625+
print(runpath)
626+
def do_initcmd(self, arg):
627+
'Initialize the custom CMD command file. After opening, you can use the "def do_cloudrunget(self, arg):" code to write your own commands through the "/binpython_files/cmd/cmd.py" file.'
628+
try:
629+
os.makedirs(runpath + f"/binpython_files/cmd")
630+
except:
631+
pass
632+
try:
633+
f = open(runpath + f"/binpython_files/cmd/{arg}.py", "w")
634+
f.write('#Initialize the custom CMD command file, you can write your own command through the "def do_cloudrunget(self, arg):" code, please refer to "https://docs.python.org/3/library/cmd.html" for details')
635+
except(Exception, BaseException) as error:
636+
print('Can not initcmd, please see the log "binpython_cmd_error.log" for details')
637+
f = open("binpython_cmd_error.log", "a")
638+
f.write('Init CMD Error: ' + time.strftime('%m-%d-%Y %H:%M:%S',time.localtime(time.time())) + ' ' + str(error) + '\n')
639+
583640
if __name__ == '__main__':
584641
cmdshell().cmdloop()
585642
#cmd end
@@ -598,8 +655,7 @@ def do_editsouce(self, arg):
598655
-i --idle Open BINPython IDLE Code Editor
599656
-p <port> --plus=<port> Open BINPython IDE Plus Code Editor(beta) with http web server
600657
-e --example Run various code examples through BINPython
601-
-c --cloudrun Run Python scripts in the cloud via CloudRun (into CloudRun CLI)
602-
-C --cmd Operations on BINPython (including Package Manager, CloudRun, etc.)
658+
-c --cmd Operations on BINPython (including Package Manager, CloudRun, etc.)
603659
-v --version View BINPython Version
604660
"""
605661
#base + plus, print full help
@@ -614,7 +670,7 @@ def outputfullhelp():
614670
#getopt
615671
try:
616672
#set options
617-
opts,args = getopt.getopt(sys.argv[1:],'-h-f:-s:-g-i-p:-e-c-C-v',['help','file=','server=','gui','idle','plus','example','cloudrun','cmd','version'])
673+
opts,args = getopt.getopt(sys.argv[1:],'-h-f:-s:-g-i-p:-e-c-v',['help','file=','server=','gui','idle','plus','example','cmd','version'])
618674
#set getopt error prompt
619675
except getopt.GetoptError as err:
620676
print("Please check help:")
@@ -948,9 +1004,7 @@ def examplerequests():
9481004
binpython_shell()
9491005
sys.exit()
9501006

951-
if opt_name in ('-c','--cloudrun'):
952-
cloudruncli()
953-
if opt_name in ('-C','--cmd'):
1007+
if opt_name in ('-c','--cmd'):
9541008
binpython_cmd()
9551009
#go main shell
9561010
#custom welcome script

0 commit comments

Comments
 (0)