Showing posts with label bash script. Show all posts
Showing posts with label bash script. Show all posts

Thursday, 14 June 2012

Create JSON manifest file for directory

The following bash shell script was created for a very specific purpose; however due to licensing agreements the exact purpose cannot be disclosed as of yet. However, the techniques used here might prove useful to others striving to solve similar problems to that which I have just solved.

This script will inspect a directory of your choosing, and for each file in the directory, calculate the SHA-1 hash. A JSON file will then be created called "manifest.json" which lists each file along with its corresponding hash.

Usage: (Created and tested on a Mac)
From a Terminal prompt, enter:
bash manifestMaker.sh dir
where dir is the directory for which you want to create a manifest.

Script
Download the script from ManifestMaker, or see below for the source code. Remember to make the script executable.

#!/bin/bash

#script which generates a JSON file, which has as keys the filenames of all files in the given directory, and as values the SHA1 digest of the corresponding files

#check number of arguments is as expected
if [ $# -ne 1 ]
then
    echo "Need to provide directory on which to operate"
    exit 1
fi

src="$1"

#check directory exists
if [ ! -d $src ]
then
    echo "Directory $src does not exist"
    exit 2
fi

#change to specified directory
cd $src

#declare target output file
out="manifest.json"

#clean up first
rm $out
touch $out

#find the number of files - needed to know whether to put a "," at the end of each line or not
numFiles=$(ls | wc -l)

#start printing to the file
echo "{" >> $out

index=1
for file in *
do
#check if comma is needed at the end of the line
if [ "$index" -lt "$numFiles" ]
then
endLine=","
else
endLine=""
fi

#gives some unfriendly output formatting
digestFull=$(openssl sha1 $file)

#tidy up formatting to get actual value
digest=${digestFull#*= }

#print line to file
echo "    \"$file\" : \"$digest\"$endLine" >> $out

#increment our index
index=$[$index+1]

done

echo "}" >> $out

Tuesday, 10 April 2012

Script to Kill iPhone Simulator if Running

Instructions to end the iPhone Simulator process if it is running.

As part of a CI setup, it is typical to have multiple iOS build jobs running on an individual build slave. As you can only have a single instance of the iPhone Simulator, you might find some of your jobs fail because another job has started the simulator, and you can't get access to it.

What seems like a sensible approach here, is to kill the simulator if it is already running, meaning your build job is guaranteed to start from a known state. If you attempt to close the simulator when it isn't running, however, you will end up with an error, along the lines of "No matching processes belonging to you were found". That's why you need to check if the process is running first, before attempting to close it.

The following script checks if the simulator is running, and if it is, ends the process. You can do this at the start of each build job.


#!/bin/sh
#kill simulator if running
killall -s "iPhone Simulator" &> /dev/null
if [ $? -eq 0 ]; then
    killall -m -KILL "iPhone Simulator"
fi




And in case you were wondering; this works for the iPad simulator too. The process is the same so the script above works unedited.


If you have any problems getting this going, drop me a comment below.