Add download command to download packages from yum repo.
authorKristian H?gsberg <krh@redhat.com>
Sat Apr 05 23:55:00 2008 -0400 (2008-04-05)
changeset 1876ff03223ce4c
parent 186 7f45d0401e37
child 188 a272b6ce454b
Add download command to download packages from yum repo.

Works with bash completion against upstream packages and
supports wildcards suchs as gcc-*.
bash-completion.sh
main.c
     1.1 --- a/bash-completion.sh	Sat Apr 05 01:15:04 2008 -0400
     1.2 +++ b/bash-completion.sh	Sat Apr 05 23:55:00 2008 -0400
     1.3 @@ -1,12 +1,18 @@
     1.4  __razor_commands () {
     1.5      local IFS=$'\n'
     1.6 -    COMPREPLY=($(IFS=: compgen -S' ' -W "list-requires:list-provides:list-files:list-file-packages:list-package-files:what-requires:what-provides:import-yum:import-rpmdb:validate:update:diff" -- $1))
     1.7 +    COMPREPLY=($(IFS=: compgen -S' ' -W "list-requires:list-provides:list-files:list-file-packages:list-package-files:what-requires:what-provides:import-yum:import-rpmdb:validate:update:diff:install:init:download" -- $1))
     1.8  }
     1.9  
    1.10  __razor_packages () {
    1.11      local IFS=$'\n'
    1.12  
    1.13 -    COMPREPLY=($(./razor list "$1*" | while read p; do echo "$p "; done))
    1.14 +    COMPREPLY=($(./razor list --only-names "$1*" | while read p; do echo "$p "; done))
    1.15 +}
    1.16 +
    1.17 +__razor_upstream_packages () {
    1.18 +    local IFS=$'\n'
    1.19 +
    1.20 +    COMPREPLY=($(RAZOR_REPO=rawhide.repo ./razor list --only-names "$1*" | while read p; do echo "$p "; done))
    1.21  }
    1.22  
    1.23  __razor_files() {
    1.24 @@ -33,6 +39,7 @@
    1.25  	    list-files|list-file-packages) __razor_files $cur ;;
    1.26  	    what-requires) __razor_requires $cur ;;
    1.27  	    what-provides) __razor_provides $cur ;;
    1.28 +	    download) __razor_upstream_packages $cur ;;
    1.29  	esac
    1.30      fi
    1.31  }
     2.1 --- a/main.c	Sat Apr 05 01:15:04 2008 -0400
     2.2 +++ b/main.c	Sat Apr 05 23:55:00 2008 -0400
     2.3 @@ -25,15 +25,25 @@
     2.4  	struct razor_set *set;
     2.5  	struct razor_package_iterator *pi;
     2.6  	struct razor_package *package;
     2.7 -	const char *pattern = argv[0], *name, *version;
     2.8 +	const char *pattern, *name, *version;
     2.9 +	int only_names = 0, i = 0;
    2.10  
    2.11 +	if (strcmp(argv[i], "--only-names") == 0) {
    2.12 +		only_names = 1;
    2.13 +		i++;
    2.14 +	}
    2.15 +
    2.16 +	pattern = argv[i];
    2.17  	set = razor_set_open(repo_filename);
    2.18  	pi = razor_package_iterator_create(set);
    2.19  	while (razor_package_iterator_next(pi, &package, &name, &version)) {
    2.20  		if (pattern && fnmatch(pattern, name, 0) != 0)
    2.21  			continue;
    2.22  
    2.23 -		printf("%s-%s\n", name, version);
    2.24 +		if (only_names)
    2.25 +			printf("%s\n", name);
    2.26 +		else
    2.27 +			printf("%s-%s\n", name, version);
    2.28  	}
    2.29  	razor_package_iterator_destroy(pi);
    2.30  	razor_set_destroy(set);
    2.31 @@ -227,8 +237,6 @@
    2.32  	if (!dlnow < dltotal)
    2.33  		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
    2.34  			file, (int) dlnow / 1024, (int) dltotal / 1024);
    2.35 -	else
    2.36 -		fprintf(stderr, "\n");
    2.37  
    2.38  	return 0;
    2.39  }
    2.40 @@ -240,7 +248,6 @@
    2.41  	char error[256];
    2.42  	FILE *fp;
    2.43  	CURLcode res;
    2.44 -	char buffer[256];
    2.45  
    2.46  	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
    2.47  	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
    2.48 @@ -249,9 +256,8 @@
    2.49  
    2.50  	if (stat(file, &buf) < 0) {
    2.51  		fp = fopen(file, "w");
    2.52 -		snprintf(buffer, sizeof buffer, "%s/%s", url, file);
    2.53  		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    2.54 -		curl_easy_setopt(curl, CURLOPT_URL, buffer);
    2.55 +		curl_easy_setopt(curl, CURLOPT_URL, url);
    2.56  		res = curl_easy_perform(curl);
    2.57  		fclose(fp);
    2.58  		if (res != CURLE_OK) {
    2.59 @@ -261,11 +267,13 @@
    2.60  		}
    2.61  	}
    2.62  
    2.63 +	fprintf(stderr, "\n");
    2.64 +
    2.65  	return 0;
    2.66  }
    2.67  
    2.68  #define REPO_URL "http://download.fedora.redhat.com" \
    2.69 -	"/pub/fedora/linux/development/i386/os/repodata"
    2.70 +	"/pub/fedora/linux/development/i386/os"
    2.71  
    2.72  static int
    2.73  command_import_yum(int argc, const char *argv[])
    2.74 @@ -277,9 +285,13 @@
    2.75  	if (curl == NULL)
    2.76  		return 1;
    2.77  
    2.78 -	if (download_if_missing(curl, REPO_URL, "primary.xml.gz") < 0)
    2.79 +	if (download_if_missing(curl,
    2.80 +				REPO_URL "/repodata/primary.xml.gz",
    2.81 +				"primary.xml.gz") < 0)
    2.82  		return -1;
    2.83 -	if (download_if_missing(curl, REPO_URL, "filelists.xml.gz") < 0)
    2.84 +	if (download_if_missing(curl,
    2.85 +				REPO_URL "/repodata/filelists.xml.gz",
    2.86 +				"filelists.xml.gz") < 0)
    2.87  		return -1;
    2.88  	curl_easy_cleanup(curl);
    2.89  
    2.90 @@ -610,6 +622,40 @@
    2.91  	return 0;
    2.92  }
    2.93  
    2.94 +static int
    2.95 +command_download(int argc, const char *argv[])
    2.96 +{
    2.97 +	struct razor_set *set;
    2.98 +	struct razor_package_iterator *pi;
    2.99 +	struct razor_package *package;
   2.100 +	const char *pattern = argv[0], *name, *version;
   2.101 +	char url[256], file[256];
   2.102 +	CURL *curl;
   2.103 +
   2.104 +	curl = curl_easy_init();
   2.105 +	if (curl == NULL)
   2.106 +		return 1;
   2.107 +
   2.108 +	set = razor_set_open(rawhide_repo_filename);
   2.109 +	pi = razor_package_iterator_create(set);
   2.110 +	while (razor_package_iterator_next(pi, &package, &name, &version)) {
   2.111 +		if (pattern && fnmatch(pattern, name, 0) != 0)
   2.112 +			continue;
   2.113 +
   2.114 +		snprintf(url, sizeof url,
   2.115 +			 REPO_URL "/Packages/%s-%s.i386.rpm", name, version);
   2.116 +		snprintf(file, sizeof file,
   2.117 +			 "rpms/%s-%s.i386.rpm", name, version);
   2.118 +		if (download_if_missing(curl, url, file) < 0)
   2.119 +			fprintf(stderr, "failed to download %s\n", name);
   2.120 +	}
   2.121 +	razor_package_iterator_destroy(pi);
   2.122 +	razor_set_destroy(set);
   2.123 +	curl_easy_cleanup(curl);
   2.124 +
   2.125 +	return 0;
   2.126 +}
   2.127 +
   2.128  static struct {
   2.129  	const char *name;
   2.130  	const char *description;
   2.131 @@ -633,7 +679,8 @@
   2.132  	{ "remove", "remove specified packages", command_remove },
   2.133  	{ "diff", "show diff between two package sets", command_diff },
   2.134  	{ "install", "install rpm", command_install },
   2.135 -	{ "init", "init razor root", command_init }
   2.136 +	{ "init", "init razor root", command_init },
   2.137 +	{ "download", "download packages", command_download }
   2.138  };
   2.139  
   2.140  static int