Skip to content

04 What is cp

Austin edited this page Dec 16, 2017 · 3 revisions

What is cp?

We need a clear understanding of what's really going on here and why these modules even exist.

cp is a wrapper around subprocess.call().

terminal is a wrapper around child_process.spawn().

terminal creates the parent process (the shell) and cp creates a child process (your script). When a command key such as F5 or F6 is pressed, it passes the command to terminal, which passes it to cp.

cp is a way to control the process that it's running in. Optional flags are used to tell cp how to handle a system call before the sub-process runs.

Because cp is a CLI tool, it's easier to understand how it works by using it as a CLI tool. Outside of this scope, I would highly suggest just doing it the normal way; to just make the call it self.

Continuing off from the Getting Started - Start with cp section. We can see that we would get the same result as this.

$ python ~/Documents/Python/hello.py
hello, world!

The dilemma came from the desire to keep the terminal window open. When using child_process.spawn on it's own, it would create the process, execute the script, and close.

Instead, spawn is used to create cp, cp executes the users script, which then allows the user to toggle pausing. This would keep the window open and allow the user to choose whether or not the window should stay open.

$ ll
total 16K
drwxrwxr-x 2 th3ros th3ros 4.0K Sep 30 03:15 cp/
-rw-rw-r-- 1 th3ros th3ros 2.0K Sep 30 02:32 main.py
$ pwd
/home/th3ros/Documents/Github/atom-python-run/cp
$ python main.py -p python ~/Documents/Python/hello.py
hello, world!

Process returned 0 (0x0)	execution time : 0.008 s
Press [ENTER] to continue...

We can always ask for help from cp to get a bit of information on whats going on.

$ python main.py -h
usage: main.py [-h] [-f FILE] [-p] repl args [args ...]

Handles arbitrary arguments to be executed.

positional arguments:
  repl                  the interpreter used to execute 'args'
  args                  the arugments that are passed to 'repl'

optional arguments:
  -h, --help            show this help message and exit
  -f FILE, --file FILE  pipes 'stdout' and 'stderr' to the given file
  -p, --pause           prompt client for 'stdin'

For more information, see How does cp work?.

Clone this wiki locally