Skip to content

Instantly share code, notes, and snippets.

@reedacartwright reedacartwright/sync-ports
Last active Jan 20, 2017

Embed
What would you like to do?
A script to syncronize a local ports directory with the latest revision used to build FreeBSD packages.
#!/bin/sh
# Copyright (c) 2014-2015 Reed A. Cartwright <cartwright@asu.edu>
# Copyright (c) 2014-2015 Alberto Villa <avilla@FreeBSD.org>
# Copyright (c) 2015 Mike Clarke <jmc-freebsd2@milibyte.co.uk>
#
# This script determines the revision number used to build FreeBSD packages
# and syncs a local ports directory to match it.
#
# USAGE: sync-ports [name or abs_path]
#
# REQUIREMENTS: textproc/jq, ports-mgmt/poudriere (optional)
#
# If the argument is not an absolute path, the script will treat it as
# the name of a poudriere-managed ports directory. If poudriere is
# not installed, it will use /usr/ports as the ports directory.
#
# If the argument is not specified, it defaults to "default".
#
# The script uses uname to choose which pkg jail to sync with.
# You can specify PKG_SERVER, PKG_JAIL, and/or PKG_URL in the
# environment to override the default options.
# Set the path
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
# Determine PKG_JAIL
case `uname -r` in
*CURRENT) : ${PKG_JAIL:=head-`uname -m`-default} ;;
*) : ${PKG_JAIL:=`uname -r | cut -d- -f1 | tr -d .``uname -m`-default} ;;
esac
# Determine PKG_SERVER
if [ -z "${PKG_SERVER}" ]; then
case "${PKG_JAIL}" in
93i386-default) PKG_SERVER=beefy1.isc.freebsd.org ;;
101i386-quarterly) PKG_SERVER=beefy1.isc.freebsd.org ;;
93amd64-default) PKG_SERVER=beefy2.isc.freebsd.org ;;
101amd64-quarterly) PKG_SERVER=beefy2.isc.freebsd.org ;;
head-i386-default) PKG_SERVER=beefy3.isc.freebsd.org ;;
93i386-quarterly) PKG_SERVER=beefy3.isc.freebsd.org ;;
head-amd64-default) PKG_SERVER=beefy4.isc.freebsd.org ;;
93amd64-quarterly) PKG_SERVER=beefy4.isc.freebsd.org ;;
101i386-default) PKG_SERVER=beefy5.nyi.freebsd.org ;;
84i386-default) PKG_SERVER=beefy5.nyi.freebsd.org ;;
101amd64-default) PKG_SERVER=beefy6.nyi.freebsd.org ;;
84amd64-default) PKG_SERVER=beefy6.nyi.freebsd.org ;;
*) >&2 echo "ERROR: Unable to determine package server for ${PKG_JAIL}."
exit 1
;;
esac
fi
# Build PKG_URL
: ${PKG_URL:=http://${PKG_SERVER}/data/${PKG_JAIL}/.data.json}
# If the argument is not an absolute path, use poudriere to resolve it.
# We test for absolute path by seeing if it begins with a slash.
PORTSTREE="${1:-default}"
if [ "${PORTSTREE#/}" != "${PORTSTREE}" ]; then
PORTSDIR="${PORTSTREE}"
elif which poudriere >/dev/null 2>&1 ; then
PORTSDIR=`poudriere ports -ql | awk -v PT="${PORTSTREE}" '$1 == PT { print $3 }'`
else
PORTSTREE="/usr/ports"
PORTSDIR="${PORTSTREE}"
fi
# Check if the directory exists
if [ ! -d "${PORTSDIR}" ]; then
>&2 echo "ERROR: Unable to resolve ports tree '${PORTSTREE}' to a directory."
exit 1
fi
# Fetch data from server
JSON=`fetch -qo - "${PKG_URL}"`
if [ $? -ne 0 ]; then
>&2 echo "ERROR: Unable to fetch data from package server."
exit 1
fi
# Parse revision information from server (exclude incomplete builds)
REV=`printf %s "${JSON}" | jq -c '.builds[]' |
awk '/status":"stopped[^"]+done/ { build = $0 }; END { print build }' |
jq -r '.svn_url | split("@")[1]'`
# Check revision information
if expr "${REV}" : '^[[:digit:]][[:digit:]]*$' >/dev/null; then
# Skip update if revisions are in sync
CURREV=`svnlite info "${PORTSDIR}" | awk -F': ' '$1 == "Revision" {print $2; exit}'`
if [ -z "${CURREV}" ]; then
>&2 echo "ERROR: Unable to get current revision for ports tree '${PORTSTREE}'."
exit 1
fi
if [ "${CURREV}" -ne "${REV}" ]; then
echo "====>> Updating ports tree '${PORTSTREE}' from revision ${CURREV} to ${REV}"
svnlite up -q -r "${REV}" "${PORTSDIR}"
else
echo "====>> Ports tree '${PORTSTREE}' is up to date at revision ${CURREV}"
fi
exit
else
>& echo "ERROR: Unable to determine revision number for latest packages."
exit 1
fi
@xzhavilla

This comment has been minimized.

Copy link

commented Dec 7, 2014

I suggest some modifications to your script:
https://gist.github.com/xzhavilla/8be8f95885470804cc4a

@reedacartwright

This comment has been minimized.

Copy link
Owner Author

commented Jan 16, 2015

Thanks. I merged your changes and added some more info.

@xzhavilla

This comment has been minimized.

Copy link

commented Jun 7, 2015

I made yet another modification to avoid checking out the ports tree for an incomplete build:
https://gist.github.com/xzhavilla/61dedc55c33cb32c1546

@reedacartwright

This comment has been minimized.

Copy link
Owner Author

commented Jan 13, 2016

Merged thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.