Automation with Python

4 min read

Table of Contents:

Title Image for the Article

Python can take boring tasks away from you so you can continue to do other things. It is a scripting language and written in a way that is almost just like English. It is a powerful language that is easy to write. We will create an automation to grab a WordPress Theme from a dev directory, remove the node_modules folder, compress it, and put it on the desktop

Introduction

I have been working on a custom WordPress theme in a local environment that I keep copying and moving it to my desktop, removing the node_modules folder, and then compressing it. After awhile this got annoying to do. I thought to myself why not automate the process and make this just running a script.

Setup

For this to work, you must have a running instance of Python on your machine. I am using Python 3.9. Reading the documentation all the functions should work with 3.6 or above. If you need to install Python, please click here . I would also recommend only doing this on a machine that you have administrator access or own all the files you are modifying.

Once you have Python installed, create a Python file inside of a folder. I have my Python file in a folder on my desktop. You need to have it in a folder, so it does not conflict with other packages or folders. We will be creating a temporary file next to it, and you don't want something to get deleted with the same name.

This script can only be used on Linux/Mac devices that have "zip" installed on them. If you are on a Windows Machine look into command prompts ability to compress a file.

Imports

We will use core libraries inside of Python

import shutil, subprocess, os

Shutil is a library to help us run file and directory commands. It has abstracted away the file manipulations, so we do not have to worry about them.

Subprocess will also us to call terminal commands from Python. I am using this way because I want to use the power of the zip package. You can zip a file in Python; however, if it has directories, and sub-directories it requires nested for loops. I chose to take an easier way out and have a terminal command take care of the easy work!

The final library is OS. It is an abstracted layer between the operating system and Python. It will identify what operating system it is on and cater commands to that. While that is not needed in this project it is great to know it has that.

The Constants

For this project, I know exactly where all the files are going to go. I will setup all the file locations statically. Change these to fit what you need.

themeName = 'yourthemename'
wpThemeFolder = '/usr/local/var/www/wp-content/themes/' + themeName
destinationFolder = os.getcwd() + '/' + themeName
desktop = '/Users/user/Desktop/'

os.getcwd() will get the current directory of where the Python file is being executed.

The Code

try:
    shutil.copytree(wpThemeFolder, destinationFolder)    
except FileExistsError:
    shutil.rmtree(destinationFolder)
    shutil.copytree(wpThemeFolder, destinationFolder)

shutil.rmtree(destinationFolder + '/node_modules')

try:
    subprocess.call(['zip', '-r', themeName + '.zip', themeName])
except:
    shutil.rmtree(destinationFolder + '.zip')
    subprocess.call(['zip', '-r', themeName + '.zip', themeName])

shutil.rmtree(destinationFolder)
shutil.move(destinationFolder + '.zip', desktop)

This short block of code will get the theme make a copy of it, move it next to the Python folder, remove the node_modules folder, then compress it.

The shutil.copytree() will take the copy of the folder we need. It is different than copy because it will copy the entire directory.

The try-except blocks used for this code will see if the file/folder exist where we want to create our folder. If the file/folder exists it will throw an error. I have the try-except setup so if the file is there it will remove it.

The shutil.rmtree() will remove the directory and all files in it. When we remove the files in this way they do not go to the trash, they are removed from the computer. Use this function with care!

Subprocess.call()

This command breaks down the command into an array. Each option has to be its own separate string. So it will run this in the terminal:

zip -r themeName.zip themeName

This is an awesome library to run commands that can just be done easier with other programs. (Like zipping a directory with subdirectories!)

The Last Bit of Code

It cleans up what we have done. It will remove the copy of the directory and move the zip file to the desktop for ease of use.

shutil.rmtree(destinationFolder)
shutil.move(destinationFolder + '.zip', desktop)

Full Code

import shutil, subprocess, os



themeName = 'yourtheme'
wpThemeFolder = '/usr/local/var/www/wp-content/themes/' + themeName
destinationFolder = os.getcwd() + '/' + themeName
desktop = '/Users/user/Desktop/'


try:
    shutil.copytree(wpThemeFolder, destinationFolder)    
except FileExistsError:
    shutil.rmtree(destinationFolder)
    shutil.copytree(wpThemeFolder, destinationFolder)

shutil.rmtree(destinationFolder + '/node_modules')

try:
    subprocess.call(['zip', '-r', themeName + '.zip', themeName])
except:
    shutil.rmtree(destinationFolder + '.zip')
    subprocess.call(['zip', '-r', themeName + '.zip', themeName])

shutil.rmtree(destinationFolder)
shutil.move(destinationFolder + '.zip', desktop)

Conclusion

Python can be used to take simple but mundane tasks like this and make them automated. Simple commands to do work that we do not have to waste our time with. If you liked this article, please check out the other articles I have written. Keep learning!

Recent Articles

Push Weight with Python

Create a simple terminal application that can let you know how much weight you need to put on the ba...

Validate Forms in TypeScript

Relying on HTML to validate your forms is just one step in ensuring visitors enter their information...

Automation with NodeJS

NodeJS can help you automate simple tasks. Being a web developer you can achieve the same goals as o...

Automation with Python

Python can take boring tasks away from you so you can continue to do other things. It is a scripting...

Speed With Cache in WordPress

Most WordPress sites use Apache as their server. Apache has a lot of tools built into it to speed up...

Social Media