Python on CLI

Advantages over bash

  • Libraries: system operations, reading files, listing directories, writing for loops, checking for exit codes…
  • Autocomplete with IDEs
  • Robust testing suite
  • iPython console
  • Python / Miniconda is available on most systems
  • Robust error checking with try and catch blocks
  • Python libraries deal with OS peculiarities under the hood, so it may run everywhere

Installation

  • Check if you have iPython installed

    1
    2
    which python
    which ipython
  • Install Miniconda with Python 3 and update

    1
    2
    3
    4
    5
    bash Miniconda3-latest-Linux-x86_64.sh
    conda update conda
    conda config --add channels conda-forge
    conda update -y --all
    conda install -y ipython

❗ Known issues installing

  • Avoid using the global area: use environments
  • Clean up everything in case of wrong installation
    1
    2
    conda clean --all
    conda update -y --all
  • Create Environments with Conda

    1
    2
    # Different projects should have their own isolated software environments.
    conda create -n my-project ipython package1 package2 package2

Python Libraries for System Administration

Handy Packages

  • os package. You can use it to list directories, check if files exist, check if symlinks exist, make directories, run system commands, get and set environmental variables, and more. It’s great!
  • subprocess module
  • shutil
  • pprint
  • pytest

Running code

  • Basic notions

    • Run script

      1
      python name-of-script.py
    • Help

      1
      help(os)
    • Import packages

      1
      2
      3
      4
      import os
      import subprocess
      import shutil
      from pprint import pprint
  • Common file and directory operations

    • Working directory

      1
      2
      3
      4
      # Get your current working directly
      # This returns a string
      my_cwd = os.getcwd()
      print(my_cwd)
    • List of directory contents

      1
      2
      3
      4
      5
      # List the contents of a directory
      # This returns a list
      dir_list = os.listdir()
      for item in dir_list:
      print(item)
    • Absolute path name of file

      1
      2
      # Get the Absolute Path name of a file (file + current working dir)
      os.path.abspath('some-file')
    • File basename

      1
      2
      #Get the basename - returns file
      os.path.basename('/path/to/file')
    • Split directory path

      1
      2
      3
      # Split a directory path - platform independent
      os.path.split(os.getcwd())
      # Out[17]: ('/Users', 'thali')
    • Check path existence

      1
      2
      3
      4
      # Check if a path exists
      os.path.exists('/path/on/filesystem')
      # Check if a path is a symlink
      os.path.islink()
  • Move files and directories

    • Copy directory

      1
      2
      3
      # Copy a directory
      # cp -rf
      shutil.copytree('src', 'dest')
    • Copy file

      1
      2
      3
      # Copy a file
      # cp -rf
      shutil.copyfile('file1', 'file2')
    • Move directory

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
        # Move a directory
      # mv
      shutil.move('src', 'dest')
      ````

      >❗ Not everything is going to be available through python libraries, such as installing system libraries
      ```python
      # Run an arbitrary system command
      command = "echo 'hello'"
      result = subprocess.run(command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      #Print the stdout and stderr
      print(result.stdout)
      print(result.stderr)
  • Write to files

    • Create a new file

      1
      2
      3
      4
      5
      # Write to a file (and create it if it doesn't exist)
      # echo "hello" > hello.txt
      f= open("hello.txt","w+")
      f.write("hello!")
      f.close()
    • Append to file

      1
      2
      3
      4
      5
      # Append to a file
      # echo "hello" >> hello.txt
      f = open("hello.txt", "a+")
      f.write("hello again!")
      f.close()

Tests

Tests mostly work by using a function called assert, which is essentially saying “make sure this is true and if not die loudly”.
Put this function in a file called test_my_code.py and run as pytest test_my_code.py.

1
2
3
4
5
def test_system_command():
"""Test the exit code of a system command"""
command = "echo 'hello'"
result = subprocess.run(command.split(' '), stdout=subprocess.PIPE)
assert result.returncode == 0