socsvn commit: r253519 - soc2013/jmuniz/PackageKit-Setter

jmuniz at FreeBSD.org jmuniz at FreeBSD.org
Wed Jun 26 04:35:42 UTC 2013


Author: jmuniz
Date: Wed Jun 26 04:35:41 2013
New Revision: 253519
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=253519

Log:
  created initial pk-setter script

Added:
  soc2013/jmuniz/PackageKit-Setter/pk-setter

Added: soc2013/jmuniz/PackageKit-Setter/pk-setter
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/jmuniz/PackageKit-Setter/pk-setter	Wed Jun 26 04:35:41 2013	(r253519)
@@ -0,0 +1,135 @@
+#!/bin/sh
+#
+# Copyright (C) 2013 Justin Edward Muniz <jmuniz at freebsd.org>
+#
+# Licensed under the GNU General Public License Version 2
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version. 
+#
+# Script to list and select from possible PackageKit backends
+
+# There are three argugments that can be provided to this script:
+# Instruction: mandatory; can be either 'list' or 'set'
+# Backend Name: mandatory for 'set'; a string that identifies for the set instruction
+# Prefix: optional; if included, must be a valid prefix path to the PackageKit installation
+
+# ENTRY POINT OF SCRIPT
+# Make sure there is an appropriate number of arguements
+if [ $# -eq 1 -o $# -eq 2 -o $# -eq 3 ]; then
+  # Determine if there is a user provided prefix and save it for later
+  setPrefix
+  # Determine if the user wants a list of backends, if so print it
+  listBackends
+  # Determine if the user wants a backend change, and process it
+  setBackend
+fi  
+# Let the user know how to use this script
+printUsage
+# Return to the shell with failure
+exit 1
+# EXIT POINT OF SCRIPT
+
+# This function presents the user with a list of available backednds
+listBackends(){
+  # Make sure that the user has requested a list of the available backends
+  if [ "$1" == "list" ]; then	
+    # Create a string with each line representing the name of one backend
+	generateBackendList
+    # Print a header to describe the data to follow
+    echo "Available PackageKit backends:"
+	# Print the list of backends
+    echo "${PK_BACKENDS}"
+    # Return to the shell with success
+    exit 0
+  fi
+# End listBackends
+}
+
+# This function uses the prefix and backend name to configure PackageKit to use the chosen backend
+setBackend(){
+  # Make sure that the user wants to select the backend
+  if [ "$1" == "set" ]; then
+    # Determine if the prefix contains the neccessary configuration file
+    if [ ! -e "${PK_PREFIX}etc/PackageKit/PackageKit.conf" ]; then
+      # Let the user know that the path does not lead to the configuration file
+      echo "Error: could not find PackageKit.conf, check prefix"
+      # Return to the shell with failure
+      exit 1
+    fi
+	# Run the algorithm to create a new-line delimited list of available backends
+	generateBackendList
+    # Test the backend value provided against the list of valid backend names
+    if [ ${PK_BACKENDS} != *$2* ]; then
+      # Let the user know that they made a mistake
+      echo "The backend provided was not found, please check your entry"
+      # Give advice to the user since they made a mistake, then return to the shell with failure
+      printUsage
+	fi
+	# Find the first instance of "DefaultBackend" and replace the line to reflect the new backend
+	sed -i 's/DefaultBackend=.*/DefaultBackend='$2
+    # Let the user know that the changes were successful
+    echo "PackageKit is now using the $2 backend"
+    # Return to the shell with success
+    exit 0
+  fi
+# End setBackend
+}
+
+# This function determines which argument is the prefix, if any, and sets the value of PK_PREFIX for later
+setPrefix(){
+  # Set PK_PREFIX to default (root) prefix 
+  ${PK_PREFIX}="/"
+  # Determine if there are three arguements
+  if [ $# -eq 3 ]; then 
+	# Set PK_PREFIX for later
+    ${PK_PREFIX}="$3"
+  # If there were two arguements given
+  elif [ $# -eq 2 ]; then 
+	# There must be a prefix if the "list" instruction is given
+	if [ "$1" == "list" ]; then 
+	  # Set PK_PREFIX to the third argument for later
+      ${PK_PREFIX}="$2"	  
+    fi
+  fi
+  # Find out if the path given does not end with a slash
+  if [ ${PK_PREFIX} != */ ]; then
+    # Then add a slash to the end to make a proper prefix
+    ${PK_PREFIX} .= "${PK_PREFIX}/"
+  fi
+  # Determine if the provided prefix even exists and if it refers to a directory
+  if [ ! -e "${PK_PREFIX}" -o ! -d "${PK_PREFIX}" ]; then 
+	# Inform the user that the prefix provided does not refer to an existing directory
+    echo "Error: $2 is not a valid directory."
+    # Return to shell with failure
+    exit 1
+  fi
+# End setPrefix
+}
+ 
+# This function explains to the user how to use this script, then exits
+printUsage(){
+  # Print the instructions to the user in a easily readable way
+  echo "pk-setter is used to select the PackageKit backend"
+  echo "usage: pk-setter list [prefix]"
+  echo "       pk-setter set [backend] [prefix]"
+  echo "note:  prefix is optional and defaults to the root directory"
+  echo "       and refers to the intallation prefix of PackageKit"
+  # Returns to the shell with failure
+  exit 1
+# End printUsage
+}  
+
+# Create a list consisting of backend options, each on their own line  
+generateBackendList(){
+  # Until a better way is figured out, a hardcoded set of options for backend selection are stored in PK_BACKENDS
+  ${PK_BACKENDS}="ports"$'\n'"pkgng"$'\n'"dummy (for debugging)"
+# End generateBackendList  
+}
+
+# TODO for listBackends: might need exception handling for if list cannot be generated
+# TODO: test validation and error handling in general
+# TODO: check spelling
+# TODO for generateBackendList: generate the list without hardcoding it
+# TODO for setBackend: stop and start PackageKitd to load in the new backend
\ No newline at end of file


More information about the svn-soc-all mailing list