Python Forum
Python cmd function call from script
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python cmd function call from script
#1
Dear all,

I found a function which is callable from command line. Now I need to add this function to my script. How can I do this?
So if I use CMD, call looks like this - 


sudo python fun_name1.py -t 'a' -r 2


How can I add the same call in another python script?

Thank you for your answer.
Reply
#2
You can use subprocessing library to do that. 

subprocess.Popen(you command)
Reply
#3
Hard to answer without looking at the code in fun_name1.py. If it is written adequately, you can import it as a Python module and call the functions it contains directly.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
(Feb-14-2017, 01:46 AM)Ofnuts Wrote: Hard to answer without looking at the code in fun_name1.py. If it is written adequately, you can import it as a Python module and call the functions it contains directly.

Function is stm32loader.py -> stm32
Reply
#5
(Feb-14-2017, 05:23 AM)John_O Wrote:
(Feb-14-2017, 01:46 AM)Ofnuts Wrote: Hard to answer without looking at the code in fun_name1.py. If it is written adequately, you can import it as a Python module and call the functions it contains directly.

Function is stm32loader.py -> stm32

Well it seems it is. You can try to import it and use the CommandInterface class directly.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
As peterkl suggested - use the subprocess method

Here's a link on how to run Linux commands from python.
Of the methods shown, the subprocess method is safer than the os.system
method.
see https://www.cyberciti.biz/faq/python-exe...-examples/
[url=https://www.cyberciti.biz/faq/python-execute-unix-linux-command-examples/][/url]
Reply
#7
Thank you for all your answer. I found one issue if I use subprocess function in combination with stm32loader. So, I have stm32loader.py script and additional file (sthdbg.bin) in mwork directory ->  path (/home/projects/mwork) on my RPi. 
If I use cmd and if I go in mwork directory I can use this command:
sudo python stm32loader.py -p '/dev/ttyAMA0' -e -w -v sthdbg.bin

If I use this command from another script which is also in mwork directory in combination with subprocess function then command looks like this:
subprocess.Popen(['sudo', 'python', 'stm32loader.py', '-p', "/dev/ttyAMA0", '-e', '-w', '-v', sthdbg.bin])
But I get error "NameError: name 'sthdbg' is not defined".
Do I need to use path?
Reply
#8
It looks like sthdbg.bin should be quoted like the rest of elements in the list supplied to Popen
Reply
#9
I missed that. Thanks. But in case if sthdbg.bin is on other location than script, how can I add the path? 
Do I need to add absolute path - in my case '/home/projects/mywork/sthdbg.bin'?
Reply
#10
yes, I think you need to specify the full path in this case

Also, looking at the script,  here is some code that should allow to use it to erase, write and verify (based on your cmd line)

NOTE THAT IT IS NOT TESTED SO USE IT ON YOUR OWN RISK, but should help you start and experiment

import smt32loader as smtl


# default address and baudrate, but in the script the defaults are set in if name == '_-main__'
# so we need to set up it here
address = 0x08000000
#baudrate = 115200


# default QUITE is 20, but if run the script from cmd and don't supply -V or -q, QUITE=5
# mdebug is commented right now
smtl.QUITE = 5 

port = '/dev/ttyAMA0'
bin_file = 'sthdbg.bin' # full path should be here

cmd = smtl.CommandInterface()
cmd.open(port)
# smtl.mdebug(10, "Open port %(port)s, baud %(baud)d" % {'port':port, 'baud':baud})
try:
   try:
       cmd.initChip()
   except:
       print "Can't init. Ensure that BOOT0 is enabled and reset device"
   bootversion = cmd.cmdGet()
   # smtl.mdebug(0, "Bootloader version %X" % bootversion)
   id = cmd.cmdGetID()
   # smtl.mdebug(0, "Chip id: 0x%x (%s)" % (id, smtl.chip_ids.get(id, "Unknown")))

# data
   data = map(lambda c: ord(c), file(bin_file, 'rb').read())

# erase
   cmd.cmdEraseMemory()

# write
   cmd.writeMemory(address, data)

# verify
   verify = cmd.readMemory(address, len(data))
   if(data == verify):
       print "Verification OK"
   else:
       print "Verification FAILED"
       print str(len(data)) + ' vs ' + str(len(verify))
       for i in xrange(0, len(data)):
           if data[i] != verify[i]:
               print hex(i) + ': ' + hex(data[i]) + ' vs ' + hex(verify[i])

finally:
   cmd.releaseChip()
there are at least two parts of the code, that I would write differently - line 31 and the loop in lines 42-44, but that's how it is in the original code
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 738 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,297 Jun-29-2023, 11:57 AM
Last Post: gologica
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 802 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Call a bash script from within a Python programme Pedroski55 6 2,480 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  string function doesn't work in script ClockPillow 3 2,426 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Passing flags to python script, through a function xbit 4 3,998 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  how to call an object in another function in Maya bstout 0 2,091 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,514 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 2,233 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 1,922 Oct-24-2020, 09:33 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020