Skip to content

Commit

Permalink
Reworking the CMD_FN to allow more customised cmd execution
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkerz committed Oct 16, 2019
1 parent 9e91737 commit 8852846
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions pie.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,26 @@
WINDOWS=(os.name=='nt')
PY3=(sys.version_info>=(3,0))


# function for input (also so that we can mock it tests)
INPUT_FN=input if PY3 else raw_input
# function to execute a command - must emulate the subprocess call method and return an error code on failure
CMD_FN=subprocess.call


def DEFAULT_CMD_FN(c):
"""The default cmd function using Popen and passing stdout and stderr through"""
return subprocess.call(c,shell=True)
# p=subprocess.Popen(c,shell=True,stdout=sys.stdout,stderr=sys.stderr)
# # TODO: is this useful? ,universal_newlines=True
# p.wait()
# return p.returncode

def DRY_RUN_CMD_FN(c):
"""Only prints the command that would be run, doesn't actually run it"""
print(c)
return 0

# function to execute a command - can be overridden, must return an error code on failure
CMD_FN=DEFAULT_CMD_FN



Expand Down Expand Up @@ -233,7 +249,7 @@ def enter(cls,ctx):
def cmd(cls,c,i=None):
if i is None: i=len(cls.context)
if i>0: return cls.context[i-1].cmd(c)
errorcode=CMD_FN(c,shell=True)
errorcode=CMD_FN(c)
if errorcode!=0:
raise cls.CmdError(errorcode,c)

Expand Down Expand Up @@ -540,6 +556,9 @@ def parseArguments(args):
elif arg=='-m':
parsed.append(ModuleName(args[i+1]))
i+=1
elif arg=='-n':
global CMD_FN
CMD_FN=DRY_RUN_CMD_FN
elif arg=='-R':
parsed.append(CreatePieVenv())
parsed.append(UpdatePieVenv())
Expand Down

0 comments on commit 8852846

Please sign in to comment.