def chmodX (thefile):
"""
Convert script file to executable or add extract shebang to cmd line
@params:
`thefile`: the script file
@returns:
A list with or without the path of the interpreter as the first element and the script file as the last element
"""
thefile = path.realpath(thefile)
ret = [thefile]
try:
st = stat (thefile)
chmod (thefile, st.st_mode | S_IEXEC)
except Exception as e1:
try:
shebang = open (thefile).read().strip().splitlines()[0]
if not shebang.startswith("#!"): # pragma: no cover
raise
ret = shebang[2:].strip().split() + [thefile] # pragma: no cover
except Exception as e2:
raise Exception("Cannot change %s as executable or read the shebang from it:\n%s\n%s" % (thefile, e1, e2))
return ret
评论列表
文章目录