简体   繁体   中英

How can I call another python script, in a python script, based on a variable in a config file?

I have below py script to download the files from artifactory.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import tarfile
import urllib
from urllib import urlretrieve
import ConfigParser

Config = ConfigParser.ConfigParser()
Config.read('/vivek/release.conf')
code_version = Config.get('main', 'app_version')

os.chdir('/tmp/')
arti_st_url='http://repo.com/artifactory/libs-release-  local/com/name/tgz/abc.ear/{0}/abc.ear-{0}.tar.gz'.format(code_version)
arti_st_name='abc.ear-{0}.tar.gz'.format(code_version)
arti_sl_url='http://repo.com/artifactory/libs-release-  local/com/name/tgz/def.ear/{0}/def.ear-{0}.tar.gz'.format(code_version)
arti_sl_name='def.ear-{0}.tar.gz'.format(code_version)
urllib.urlretrieve(arti_st_url, arti_st_name)
urllib.urlretrieve(arti_sl_url, arti_sl_name)
oneEAR = 'abc.ear-{0}.tar.gz'.format(code_version)
twoEAR = 'def.ear-{0}.tar.gz'.format(code_version)
tar = tarfile.open(oneEAR)
tar.extractall()
tar.close()
tar1 = tarfile.open(twoEAR)
tar1.extractall()
tar1.close()
os.remove(oneEAR)
os.remove(twoEAR)

This script works perfectly, thanks to stackoverflow.

Here's the next question. There's a variable "protocol" in release.conf. If it's equal to "localcopy", there's an existing py script that does something. If the "protocol" is equal to "artifactory", above script should be called and executed. How can I achieve it?

Note: I am a beginner in Python, but my tasks are not. So, please help me out guys.

You could simply use:

import os

os.system("script_path")

to execute the script file. But there should be a line called shebang in the very top of that script file, you want to execute. If your python interpreter would be in /usr/bin/python this would be:

#!/usr/bin/python

Assuming you are a Linux user. In Windows shebang isn't supported. It determines what program to use running *.py file itself.

//Edit: To call that two scripts depending on a property config value you could just make another script called for example runthis.py which contains instruction like:

protocol = Config.get('main', 'protocol')
if protocol == 'localcopy':
  os.system('path_to_localcopy_script)
if protocol == 'antifactory':
  os.system('path_to_other_script')

Dont forgot to import needed modules in that new script.

Then you just run script you just made.

That is one way to do this.

If you dont want to create additional script, then put that code you wrote in a function, like:

def main():
  ...
  Your code
  ...

And on the very bottom of your script file write:

if __name__ = '__main__':
  protocol = Config.get('main', 'app_version')
  if protocol == 'localcopy':
    main()
  if protocol == 'antifactory':
    os.system('path_to_other_script')

if __name__ = '__main__' would execute only if you run that script by yourself (not by call from an other sctipt for example)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM