Automate the Development Environment Setup with Scripts and Dotfiles, Other Automation Tasks by Python and Cron

Need to learn from a pro to automate the DE setup with scripts and dotfiles. The setup includes automated scripts for installing essential software, configuring Bash and Zsh shells, and setting up Sublime Text and Visual Studio Code editors. 

He uses MacOS, hence the steps are: 1. git clone https://github.com/CoreyMSchafer/dotfiles.git ~/dotfiles; 2. cd ~/dotfiles; 3. ./install.sh.

The configuration files are

  • .bashrc & .zshrc: Shell configuration files for Bash and Zsh.
  • .shared_prompt: Custom prompt setup used by both .bash_prompt & .zprompt
  • .bash_prompt & .zprompt: Custom prompt setup for Bash and Zsh.
  • `.bash_profile: Setting system-wide environment variables
  • .aliases: Aliases for common commands. Some are personalized to my machines specifically (e.g. the ‘yt’ alias opening my YouTube Scripts’)
  • .private: This is a file you’ll create locally to hold private information and shouldn’t be uploaded to version control
  • settings/: Directory containing editor settings and themes for Sublime Text and Visual Studio Code.

In addition, installing python package playwright, codegen can help automate web browsing, clicking, filling in forms etc. Here are concrete examples

Automating Form Submission

Imagine you need to automate the task of submitting a form on a website, such as a login form or a feedback form. By using Playwright’s codegen tool, you can navigate to the form, fill in the fields, and submit it while codegen records these actions. The generated code can then be used in a Playwright script to automate this process.

Steps:

  1. Start Playwright’s codegen tool by running npx playwright codegen yourwebsite.com in the terminal.
  2. Manually fill in the form and submit it.
  3. codegen will generate the corresponding code for these actions.
  4. You can save this code to a script and run it with Playwright to automate the form submission.

Automated Testing of Web Applications

Playwright can be used to write end-to-end tests for web applications. By using codegen, you can quickly generate boilerplate code for navigating through your app, interacting with elements, and verifying app behavior.

Data Scraping from Web Pages

Suppose you want to scrape data from a web page, such as product information from an e-commerce site. With codegen, you can navigate to the page and select the data you wish to scrape, and Playwright will generate the code to extract this information.

We can also write python scripts to perform File Management Automation such as the task of Sorting and renaming a large number of files in a directory based on their creation date and moving them to corresponding month and year folders.

Database Updates Automation

Task: Updating a database with data from a CSV file. This example assumes a PostgreSQL database, but the concept is similar for other databases.

Complex Workflows Involving Machine Learning

Task: Automating a machine learning workflow for training a model with Scikit-Learn, including data preprocessing, model training, and model evaluation. this example is not to the point of automating though.

Cron is a time-based job scheduler in Unix-like operating systems. It allows you to schedule scripts or commands to run automatically at specified times, dates, or intervals. The cron daemon checks the schedule and executes tasks in the background without manual intervention. Here’s an example:

Suppose you want to create a backup of the /home/user/data directory every day at midnight. You would use a cron job to automate this task. First, you need to write a shell script that performs the backup operation, and then schedule this script using Cron.

Step 1: Write the Backup Script

Create a script named backup.sh in your home directory or another suitable location. The script could look something like this:

#!/bin/bash

# Define source directory and backup directory
SOURCE_DIR="/home/user/data"
BACKUP_DIR="/home/user/backups"

# Create a timestamp
TIMESTAMP=$(date +%Y%m%d%H%M%S)

# Perform the backup
tar -czf ${BACKUP_DIR}/backup-${TIMESTAMP}.tar.gz ${SOURCE_DIR}

chmod +x ~/backup.sh

Step 2: Schedule the Script with Cron

To schedule the backup script, you need to edit the crontab file for your user account. Run the following command to open the crontab file in an editor: crontab -e

In the crontab file, add a line that specifies when the script should run and the path to the script. To run the script every day at midnight, add the following line: 0 0 * * * /home/user/backup.sh

Save and close the file. The cron daemon will automatically pick up this new job and execute it at the specified time.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.