Make install command download and install all requirements.
authorKristian H?gsberg <krh@redhat.com>
Sun Apr 06 23:57:13 2008 -0400 (2008-04-06)
changeset 1946d25d2fa0935
parent 193 2d0d5e7dd439
child 195 7a53d1711083
Make install command download and install all requirements.
bash-completion.sh
main.c
     1.1 --- a/bash-completion.sh	Sun Apr 06 23:36:45 2008 -0400
     1.2 +++ b/bash-completion.sh	Sun Apr 06 23:57:13 2008 -0400
     1.3 @@ -39,7 +39,7 @@
     1.4  	    list-files|list-file-packages) __razor_files $cur ;;
     1.5  	    what-requires) __razor_requires $cur ;;
     1.6  	    what-provides) __razor_provides $cur ;;
     1.7 -	    download) __razor_upstream_packages $cur ;;
     1.8 +	    install|download) __razor_upstream_packages $cur ;;
     1.9  	esac
    1.10      fi
    1.11  }
     2.1 --- a/main.c	Sun Apr 06 23:36:45 2008 -0400
     2.2 +++ b/main.c	Sun Apr 06 23:57:13 2008 -0400
     2.3 @@ -245,13 +245,18 @@
     2.4  }
     2.5  
     2.6  static int
     2.7 -download_if_missing(CURL *curl, const char *url, const char *file)
     2.8 +download_if_missing(const char *url, const char *file)
     2.9  {
    2.10 +	CURL *curl;
    2.11  	struct stat buf;
    2.12  	char error[256];
    2.13  	FILE *fp;
    2.14  	CURLcode res;
    2.15  
    2.16 +	curl = curl_easy_init();
    2.17 +	if (curl == NULL)
    2.18 +		return 1;
    2.19 +
    2.20  	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
    2.21  	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
    2.22  	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
    2.23 @@ -271,6 +276,8 @@
    2.24  		fprintf(stderr, "\n");
    2.25  	}
    2.26  
    2.27 +	curl_easy_cleanup(curl);
    2.28 +
    2.29  	return 0;
    2.30  }
    2.31  
    2.32 @@ -281,21 +288,13 @@
    2.33  command_import_yum(int argc, const char *argv[])
    2.34  {
    2.35  	struct razor_set *set;
    2.36 -	CURL *curl;
    2.37  
    2.38 -	curl = curl_easy_init();
    2.39 -	if (curl == NULL)
    2.40 -		return 1;
    2.41 -
    2.42 -	if (download_if_missing(curl,
    2.43 -				REPO_URL "/repodata/primary.xml.gz",
    2.44 +	if (download_if_missing(REPO_URL "/repodata/primary.xml.gz",
    2.45  				"primary.xml.gz") < 0)
    2.46  		return -1;
    2.47 -	if (download_if_missing(curl,
    2.48 -				REPO_URL "/repodata/filelists.xml.gz",
    2.49 +	if (download_if_missing(REPO_URL "/repodata/filelists.xml.gz",
    2.50  				"filelists.xml.gz") < 0)
    2.51  		return -1;
    2.52 -	curl_easy_cleanup(curl);
    2.53  
    2.54  	set = razor_set_create_from_yum();
    2.55  	if (set == NULL)
    2.56 @@ -473,49 +472,57 @@
    2.57  	return 0;
    2.58  }
    2.59  
    2.60 -static struct razor_set *
    2.61 -create_set_from_rpms(int argc, const char *argv[])
    2.62 +static void
    2.63 +download_package(const char *name,
    2.64 +		 const char *old_version,
    2.65 +		 const char *new_version,
    2.66 +		 const char *arch,
    2.67 +		 void *data)
    2.68  {
    2.69 -	struct razor_importer *importer;
    2.70 +	char file[PATH_MAX], url[256];
    2.71 +
    2.72 +	if (old_version)
    2.73 +		return;
    2.74 +
    2.75 +	snprintf(url, sizeof url,
    2.76 +		 REPO_URL "/Packages/%s-%s.%s.rpm", name, new_version, arch);
    2.77 +	snprintf(file, sizeof file,
    2.78 +		 "rpms/%s-%s.%s.rpm", name, new_version, arch);
    2.79 +	if (download_if_missing(url, file) < 0)
    2.80 +		fprintf(stderr, "failed to download %s\n", name);
    2.81 +}
    2.82 +
    2.83 +static void
    2.84 +install_package(const char *name,
    2.85 +		const char *old_version,
    2.86 +		const char *new_version,
    2.87 +		const char *arch,
    2.88 +		void *data)
    2.89 +{
    2.90 +	const char *root = data;
    2.91 +	char file[PATH_MAX];
    2.92  	struct razor_rpm *rpm;
    2.93 -	int i;
    2.94  
    2.95 -	importer = razor_importer_new();
    2.96 -	for (i = 0; i < argc; i++) {
    2.97 -		rpm = razor_rpm_open(argv[i]);
    2.98 -		if (rpm == NULL) {
    2.99 -			fprintf(stderr,
   2.100 -				"failed to open rpm \"%s\"\n", argv[i]);
   2.101 -			continue;
   2.102 -		}
   2.103 -		if (razor_importer_add_rpm(importer, rpm)) {
   2.104 -			fprintf(stderr, "couldn't import %s\n", argv[i]);
   2.105 -			break;
   2.106 -		}
   2.107 -		razor_rpm_close(rpm);
   2.108 +	if (old_version) {
   2.109 +		printf("removing %s %s not handled\n", name, old_version);
   2.110 +		return;
   2.111  	}
   2.112  
   2.113 -	return razor_importer_finish(importer);
   2.114 -}
   2.115 +	printf("install %s %s\n", name, new_version);
   2.116 +	snprintf(file, sizeof file,
   2.117 +		 "rpms/%s-%s.%s.rpm", name, new_version, arch);
   2.118  
   2.119 -static char **
   2.120 -list_packages(int count, struct razor_set *set)
   2.121 -{
   2.122 -	struct razor_package_iterator *pi;
   2.123 -	struct razor_package *package;
   2.124 -	const char *name, *version, *arch;
   2.125 -	char **packages;
   2.126 -	int i;
   2.127 -
   2.128 -	packages = malloc(count * sizeof *packages);
   2.129 -	pi = razor_package_iterator_create(set);
   2.130 -	i = 0;
   2.131 -	while (razor_package_iterator_next(pi, &package,
   2.132 -					   &name, &version, &arch))
   2.133 -		packages[i++] = strdup(name);
   2.134 -	razor_package_iterator_destroy(pi);
   2.135 -
   2.136 -	return packages;
   2.137 + 	rpm = razor_rpm_open(file);
   2.138 +	if (rpm == NULL) {
   2.139 +		fprintf(stderr, "failed to open rpm %s\n", file);
   2.140 +		return;
   2.141 +	}
   2.142 +	if (razor_rpm_install(rpm, root) < 0) {
   2.143 +		fprintf(stderr,
   2.144 +			"failed to install rpm %s\n", file);
   2.145 +		return;
   2.146 +	}
   2.147 +	razor_rpm_close(rpm);
   2.148  }
   2.149  
   2.150  static int
   2.151 @@ -523,25 +530,18 @@
   2.152  {
   2.153  	struct razor_set *system, *upstream, *next;
   2.154  	struct razor_transaction *trans;
   2.155 -	struct razor_rpm *rpm;
   2.156 -	const char *filename;
   2.157 -	char path[PATH_MAX], new_path[PATH_MAX], **packages;
   2.158 -	int errors, i;
   2.159 +	char path[PATH_MAX], new_path[PATH_MAX];
   2.160 +	CURL *curl;
   2.161 +	int errors;
   2.162  
   2.163 -	upstream = create_set_from_rpms(argc, argv);
   2.164 +	upstream = razor_set_open(rawhide_repo_filename);
   2.165  	snprintf(path, sizeof path,
   2.166  		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   2.167  	system = razor_set_open(path);
   2.168 -	if (system == NULL) {
   2.169 -		fprintf(stderr, "couldn't open system package database\n");
   2.170 -		return -1;
   2.171 -	}
   2.172 -
   2.173 -	packages = list_packages(argc, upstream);
   2.174 +	if (system == NULL || upstream == NULL)
   2.175 +		return 1;
   2.176  	trans = razor_transaction_create(system, upstream,
   2.177 -					 argc, (const char **)packages,
   2.178 -					 0, NULL);
   2.179 -	free(packages);
   2.180 +					 argc, argv, 0, NULL);
   2.181  	errors = razor_transaction_describe(trans);
   2.182  	if (errors)
   2.183  		return 1;
   2.184 @@ -558,29 +558,22 @@
   2.185  	 * up front here or fail if it already exists. */
   2.186  	snprintf(new_path, sizeof new_path,
   2.187  		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   2.188 -	razor_set_write(next, path);
   2.189 +
   2.190 +	razor_set_write(next, new_path);
   2.191 +	printf("wrote %s\n", new_path);
   2.192 +
   2.193 +	curl = curl_easy_init();
   2.194 +	if (curl == NULL)
   2.195 +		return 1;
   2.196 +	razor_set_diff(system, next, download_package, curl);	
   2.197 +	curl_easy_cleanup(curl);
   2.198 +
   2.199 +	razor_set_diff(system, next, install_package, (void *) root);
   2.200  
   2.201  	razor_set_destroy(next);
   2.202  	razor_set_destroy(system);
   2.203  	razor_set_destroy(upstream);
   2.204  
   2.205 -	printf("wrote %s\n", new_path);
   2.206 -
   2.207 -	for (i = 0; i < argc; i++) {
   2.208 -		filename = argv[i];
   2.209 -		rpm = razor_rpm_open(argv[i]);
   2.210 -		if (rpm == NULL) {
   2.211 -			fprintf(stderr, "failed to open rpm %s\n", filename);
   2.212 -			return -1;
   2.213 -		}
   2.214 -		if (razor_rpm_install(rpm, root) < 0) {
   2.215 -			fprintf(stderr,
   2.216 -				"failed to install rpm %s\n", filename);
   2.217 -			return -1;
   2.218 -		}
   2.219 -		razor_rpm_close(rpm);
   2.220 -	}	
   2.221 -
   2.222  	/* Make it so. */
   2.223  	rename(new_path, path);
   2.224  	printf("renamed %s to %s\n", new_path, path);
   2.225 @@ -636,11 +629,6 @@
   2.226  	struct razor_package *package;
   2.227  	const char *pattern = argv[0], *name, *version, *arch;
   2.228  	char url[256], file[256];
   2.229 -	CURL *curl;
   2.230 -
   2.231 -	curl = curl_easy_init();
   2.232 -	if (curl == NULL)
   2.233 -		return 1;
   2.234  
   2.235  	set = razor_set_open(rawhide_repo_filename);
   2.236  	pi = razor_package_iterator_create(set);
   2.237 @@ -653,12 +641,11 @@
   2.238  			 REPO_URL "/Packages/%s-%s.i386.rpm", name, version);
   2.239  		snprintf(file, sizeof file,
   2.240  			 "rpms/%s-%s.i386.rpm", name, version);
   2.241 -		if (download_if_missing(curl, url, file) < 0)
   2.242 +		if (download_if_missing(url, file) < 0)
   2.243  			fprintf(stderr, "failed to download %s\n", name);
   2.244  	}
   2.245  	razor_package_iterator_destroy(pi);
   2.246  	razor_set_destroy(set);
   2.247 -	curl_easy_cleanup(curl);
   2.248  
   2.249  	return 0;
   2.250  }