个人工具

UbuntuHelp:AptGet/Off

来自Ubuntu中文

跳转至: 导航, 搜索

See also AptGet/Offline


Tired for waiting my internet connection, and as many people ask for how to do it, here is a little script to allow the use of apt-get offline. The main advantage is that it doesn't need to have apt on the connected computer (i.e. you can download packages from a rpm-based distribution, for instance). Here it is, just copy this code in your favorite text editor, save it as apt-off on your usb flash/hard drive (or any other kind of mobile disk) and make it executable by typing chmod +x apt-off in a terminal.

#!/bin/bash
#
# Copyright 2006, Goulven Guillard.
#
# 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.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

BOLD="\\033[1;39m"
NORMAL="\\033[0;39m"
GREEN="\\033[32m"
RED="\\033[31m"
PINK="\\033[35m"
BLUE="\\033[34m"


if [[ $1 == -h || $1 == --help ]]; then
	echo -e "\\033[2J\\033[0;0f$BOLD$BLUE""NAME"$NORMAL
	echo -e "\tapt-off\n"
	echo -e $BOLD$BLUE"SYNOPSIS"$NORMAL
	echo -e $GREEN"\tapt-off [option] [packages/dist-upgrade]\n"$NORMAL
	echo -e $BOLD$BLUE"DESCRIPTION"$NORMAL
	echo -e "\t$GREEN""apt-off$NORMAL is a script designed to allow the use of the apt tool on a"
	echo -e "\tcomputer without any network connection. It proceeds in three steps,"
	echo -e "\twhich must be done in the directory containing the apt-off file :"
	echo -e "\t1- packages selection (option -g)"
	echo -e "\t2- packages download (option -d); this step has to be done on a Linux"
	echo -e "\tsystem computer (it$BOLD does not need$NORMAL apt, but uses$GREEN wget$NORMAL ; if you only"
	echo -e "\thave access to a Windows system, you should uncomment one of the two"
	echo -e "\tdedicated lines at the end of the apt-off file and download all the"
	echo -e "\tpackages listed in the $GREEN""offline.packages_url$NORMAL or in the"
	echo -e "\t$GREEN""offline.packages_list$NORMAL files by hand...) with an internet connection"
	echo -e "\t3- packages installation (option -x)\n"
	echo -e $BOLD$BLUE"WARNING"$NORMAL
	echo -e $BOLD$RED"\tCAUTION :$NORMAL the installation of packages and the use of the dist-upgrade"
	echo -e "\toption can pose some risks. You are strongly encouraged to"
	echo -e "\tsimulate these operations with apt-get before to use apt-off."
	echo -e "\tThe author cannot be considered as responseable for any problem.\n"
fi
	

if [[ $1 == -h || $1 == --help || $1 == -o || $1 == --options ]]; then
	echo -e $BOLD$BLUE"OPTIONS"$NORMAL
	echo -e $GREEN"\t-c$NORMAL ou$GREEN --configure$NORMAL"
	echo -e "\tDownload apt configuration files.\n"
	echo -e $GREEN"\t-d$NORMAL ou$GREEN --download$NORMAL"
	echo -e "\tDownload packages. This option has to be used on a Linux system"
	echo -e "\twith an internet connection.$BOLD$RED Don't forget the offline.packages"
	echo -e "\tfile !!!$NORMAL\n"
	echo -e $GREEN"\t-g$NORMAL or$GREEN --generate$BLUE [package1 package2 ...]$NORMAL or$BLUE [dist-upgrade]$NORMAL"
	echo -e "\tGenerate packages list, stored in the $BLUE offline.packages$NORMAL file.\n"
	echo -e $GREEN"\t-h$NORMAL or$GREEN --help"$NORMAL
	echo -e "\tPrint this help.\n"
	echo -e $GREEN"\t-o$NORMAL or$GREEN --options$NORMAL"
	echo -e "\tDisplay options.\n"
	echo -e $GREEN"\t-x$NORMAL or$GREEN --extract$BLUE [package1 package2 ...]$NORMAL or$BLUE [dist-upgrade]$NORMAL"
	echo -e "\tExtract and install packages.\n"
	
	if [[ $1 == -h || $1 == --help ]]; then	
		echo -e $BOLD$BLUE"COPYRIGHT"$NORMAL
		echo -e "\tCopyright 2006, Goulven Guillard."
		echo -e "\tThis program is copyleft : copy, modification and diffusion are"
		echo -e "\tauthorized and encouraged if they are free."
		echo -e "\tPlease report bugs at$BLUE lecotegougdelaforce [at] free.fr$NORMAL.\n"
	fi
	

elif [[ $1 == -c || $1 == --configure ]]; then
	if [[ -e config_files/config_apt ]]; then
		cd config_files
		sh -x ./config_apt
		cd ..
	else
		echo -e "You didn't execute$GREEN apt-off -p$NORMAL yet, or you have deleted the$VERT config_files$NORMAL directory and the$VERT config_apt$NORMAL file there was in it... Please restart all the procedure."
	fi


elif [[ $1 == -g || $1 == --generate ]]; then
	if [[ `ls /var/lib/apt/lists/ | grep binary-i386_Packages | wc -l` == 0 ]]; then
		if [[ ! -e config_files ]]; then
			mkdir config_files
			awk '($1=="deb"||$1=="deb-src")&&!match($2,"cdrom"){split($2,A,"/");for(i=4;i<=NF;++i) print "wget -O "A[3]"_"A[4]"_dists_"$3"_"$i"_binary-i386_Packages.gz "$2"dists/"$3"/"$i"/binary-i386/Packages.gz"}' < /etc/apt/sources.list > config_files/config_apt
			echo -e "WARNING : Your computer isn't correctly configured in order to use the apt tool."
			echo -e "This program will configure it for you. You now have to execute the$VERT apt-off -c$NORMAL"
			echo -e "command on a computer with an internet connexion. The configuration will end"
			echo -e "automatically next time you will use apt-off."
			exit
		elif [[ `ls config_files | grep gz | wc -l` > 0 ]]; then
			sudo cp config_files/*.gz /var/lib/apt/lists/
			sudo gunzip /var/lib/apt/lists/*.gz
		else
			echo -e "Vous devez d'abord exécuter$VERT apt-off -c$NORMAL sur un ordinateur avec internet !"
			exit
		fi
	fi

	if [[ -e offline.packages ]]; then
		echo "WARNING : The offline.packages file already exists, do you want to overwrite it (if not, the packages will be added to the existing list) ? (y/n)"
		read OVERWRITE
	fi

	if [[ $OVERWRITE == y ]]; then
		echo "cd ./deb" > offline.packages
	else
		echo "cd ./deb" >> offline.packages
	fi
	if [[ "$#" < 2 ]]; then
		echo "WARNING : You have to indicate which packages you want to install,"
		echo "or \"dist-upgrade\" !!!"
		exit
	elif [[ $2 == dist-upgrade ]]; then
		sudo apt-get -qq --print-uris dist-upgrade > .offline_tmp
	else
		for ((i=2; i<="$#"; i++ ))
		do 
			sudo apt-get -qq --print-uris install ${!i} >> .offline_tmp
		done
	fi
       	awk '{print "wget -O " $2 " " $1}' < .offline_tmp >> offline.packages
	echo "cd .." >> offline.packages

#	awk '{print $1}' < .offline_tmp >> offline.packages_url
#	awk '{print $2}' < .offline_tmp >> offline.packages_list

        rm -f .offline_tmp	


elif [[ $1 == -d || $1 == --download ]]; then
	if [[ ! -e deb ]]; then
		mkdir deb
	fi
	sh -x ./offline.packages


elif [[ $1 == -x || $1 == --extract ]]; then
	if [[ ! -e deb/partial ]]; then
		PARTIAL_CREATED=true
		mkdir deb/partial
	else
		PARTIAL_CREATED=false
	fi
	if [[ "$#" < 2 ]]; then
		echo "WARNING : You have to indicate which packages you want to install, or \"dist-upgrade\" !"
		exit
	elif [[ $2 == dist-upgrade ]]; then
		sudo apt-get -o dir::cache::archives=$PWD/deb/ dist-upgrade
	else
		for ((i=2; i<="$#"; i++ ))
		do 
			sudo apt-get -o dir::cache::archives=$PWD/deb/ install ${!i}
		done
	fi
	if [[ PARTIAL_CREATED ]]; then
		rm -rf deb/partial
	fi


else
	./apt-off -o


fi

Source