Skip to content

Commit

Permalink
Add LRI_MIRRORS option
Browse files Browse the repository at this point in the history
  • Loading branch information
Tojaj committed Mar 28, 2013
1 parent 9846447 commit 056a115
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 12 deletions.
29 changes: 29 additions & 0 deletions doc/python/examples.rst
Expand Up @@ -228,3 +228,32 @@ More complex download
pprint (r.getinfo(librepo.LRR_YUM_REPOMD))


How to get urls in a local mirrorlist
-------------------------------------

::

import os
import sys
import librepo

DESTDIR = "downloaded_metadata"

if __name__ == "__main__":
h = librepo.Handle()

# Correct repotype is important. Without repotype
# metalink parser doesn't know suffix which should
# be stripped off from the mirrors urls.
h.setopt(librepo.LRO_REPOTYPE, librepo.LR_YUMREPO)

# Set local mirrorlist file as mirrorlist
if os.path.isfile(os.path.join(DESTDIR, "mirrorlist")):
h.mirrorlist = os.path.join(DESTDIR, "mirrorlist")
elif os.path.isfile(os.path.join(DESTDIR, "metalink.xml")):
h.mirrorlist = os.path.join(DESTDIR, "metalink.xml")
else:
print "Mirrorlist of downloaded repodata isn't available"
sys.exit(0)

print h.mirrors
5 changes: 5 additions & 0 deletions examples/python/download_remote_yum_repo.py
Expand Up @@ -78,6 +78,11 @@ def callback(data, total_to_download, downloaded):
h.setopt(librepo.LRO_YUMDLIST, ["primary"])
h.perform(r)

# List of mirrors
# (In this case no mirrorlist is used -> list will contain only one url)
# Example of access info via attr insted of .getinfo() method
pprint (h.mirrors)

# Get and show final results
pprint (r.getinfo(librepo.LRR_YUM_REPO))
pprint (r.getinfo(librepo.LRR_YUM_REPOMD))
Expand Down
30 changes: 30 additions & 0 deletions examples/python/read_metalink_of_downloaded_repo.py
@@ -0,0 +1,30 @@
#!/usr/bin/python

"""
librepo - example of usage
"""

import os
import sys
import librepo

DESTDIR = "downloaded_metadata"

if __name__ == "__main__":
h = librepo.Handle()

# Correct repotype is important. Without repotype
# metalink parser doesn't know suffix which should
# be stripped off from the mirrors urls.
h.setopt(librepo.LRO_REPOTYPE, librepo.LR_YUMREPO)

# Set local mirrorlist file as mirrorlist
if os.path.isfile(os.path.join(DESTDIR, "mirrorlist")):
h.mirrorlist = os.path.join(DESTDIR, "mirrorlist")
elif os.path.isfile(os.path.join(DESTDIR, "metalink.xml")):
h.mirrorlist = os.path.join(DESTDIR, "metalink.xml")
else:
print "Mirrorlist of downloaded repodata isn't available"
sys.exit(0)

print h.mirrors
34 changes: 30 additions & 4 deletions librepo/handle.c
Expand Up @@ -339,11 +339,14 @@ lr_handle_last_curlm_strerror(lr_Handle handle)
}

int
lr_handle_prepare_internal_mirrorlist(lr_Handle handle,
const char *metalink_suffix)
lr_handle_prepare_internal_mirrorlist(lr_Handle handle)
{
int rc = LRE_OK;
lr_Metalink metalink = NULL;
char *metalink_suffix = NULL;

if (handle->repotype == LR_YUMREPO)
metalink_suffix = "repodata/repomd.xml";

if (handle->internal_mirrorlist)
return LRE_OK; /* Internal mirrorlist already exists */
Expand Down Expand Up @@ -395,7 +398,10 @@ lr_handle_prepare_internal_mirrorlist(lr_Handle handle,
if (handle->mirrorlist) {
/* Download and parse metalink or mirrorlist to internal mirrorlist */
lr_Mirrorlist mirrorlist = NULL;
int mirrors_fd = lr_gettmpfile();
char *mirrorlist_url;
int mirrors_fd;

mirrors_fd = lr_gettmpfile();
if (mirrors_fd < 1) {
rc = LRE_IO;
DPRINTF("%s: Cannot create a temporary file\n", __func__);
Expand All @@ -404,7 +410,10 @@ lr_handle_prepare_internal_mirrorlist(lr_Handle handle,

handle->mirrorlist_fd = mirrors_fd;

rc = lr_curl_single_download(handle, handle->mirrorlist, mirrors_fd);
mirrorlist_url = lr_prepend_url_protocol(handle->mirrorlist);
rc = lr_curl_single_download(handle, mirrorlist_url, mirrors_fd);
lr_free(mirrorlist_url);

if (rc != LRE_OK)
goto mirrorlist_error;

Expand Down Expand Up @@ -631,6 +640,23 @@ lr_handle_getinfo(lr_Handle handle, lr_HandleOption option, ...)
*lnum = handle->status_code;
break;

case LRI_MIRRORS: {
int x;
char ***list = va_arg(arg, char ***);
*list = NULL;
rc = lr_handle_prepare_internal_mirrorlist(handle);
if (rc != LRE_OK)
break;
/* Make list of urls from internal mirrorlist */
lr_InternalMirrorlist ml = handle->internal_mirrorlist;
*list = lr_malloc((ml->nom + 1) * sizeof(char *));
for (x = 0; x < ml->nom; x++)
(*list)[x] = ml->mirrors[x]->url;
(*list)[x] = NULL;
break;
}


default:
rc = LRE_UNKNOWNOPT;
break;
Expand Down
2 changes: 2 additions & 0 deletions librepo/handle.h
Expand Up @@ -112,6 +112,8 @@ typedef enum {
LRI_LASTCURLSTRERR, /* (char **) */
LRI_LASTCURLMSTRERR, /* (char **) */
LRI_LASTBADSTATUSCODE, /* (long *) */
LRI_MIRRORS, /* (char **) Returned list must be freed
but its elements doesn't! */
LRI_SENTINEL,
} lr_HandleInfoOption; /*!< Handle info options */

Expand Down
5 changes: 1 addition & 4 deletions librepo/handle_internal.h
Expand Up @@ -66,12 +66,9 @@ struct _lr_Handle {
* Create (if do not exists) internal mirrorlist. Insert baseurl (if
* specified) and download, parse and insert mirrors from mirrorlist url.
* @param handle Librepo handle.
* @param metalink_suffix Suffix of metalink mirror urls that will be removed
* (e.g. "repodata/repomd.xml").
* @return Librepo return code.
*/
int lr_handle_prepare_internal_mirrorlist(lr_Handle handle,
const char *metalink_suffix);
int lr_handle_prepare_internal_mirrorlist(lr_Handle handle);


#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion librepo/package_downloader.c
Expand Up @@ -67,7 +67,7 @@ lr_download_package(lr_Handle handle,


if (handle->repotype == LR_YUMREPO)
rc = lr_handle_prepare_internal_mirrorlist(handle, "repodata/repomd.xml");
rc = lr_handle_prepare_internal_mirrorlist(handle);
else {
DPRINTF("%s: Bad repo type\n", __func__);
assert(0);
Expand Down
3 changes: 3 additions & 0 deletions librepo/python/__init__.py
Expand Up @@ -227,6 +227,7 @@
.. data:: LRI_LASTCURLSTRERR
.. data:: LRI_LASTCURLMSTRERR
.. data:: LRI_LASTBADSTATUSCODE
.. data:: LRI_MIRRORS
.. _proxy-type-label:
Expand Down Expand Up @@ -518,6 +519,7 @@
LRI_LASTCURLSTRERR = _librepo.LRI_LASTCURLSTRERR
LRI_LASTCURLMSTRERR = _librepo.LRI_LASTCURLMSTRERR
LRI_LASTBADSTATUSCODE = _librepo.LRI_LASTBADSTATUSCODE
LRI_MIRRORS = _librepo.LRI_MIRRORS

ATTR_TO_LRI = {
"update": LRI_UPDATE,
Expand All @@ -535,6 +537,7 @@
"lastcurlstrerr": LRI_LASTCURLSTRERR,
"lastcurlmstrerr": LRI_LASTCURLMSTRERR,
"lastbadstatuscode": LRI_LASTBADSTATUSCODE,
"mirrors": LRI_MIRRORS,
}

LR_CHECK_GPG = _librepo.LR_CHECK_GPG
Expand Down
10 changes: 8 additions & 2 deletions librepo/python/handle-py.c
Expand Up @@ -405,7 +405,8 @@ getinfo(_HandleObject *self, PyObject *args)

/* char*** options */
case LRI_YUMDLIST:
case LRI_YUMBLIST: {
case LRI_YUMBLIST:
case LRI_MIRRORS: {
PyObject *list;
char **strlist;
res = lr_handle_getinfo(self->handle, (lr_HandleInfoOption)option, &strlist);
Expand All @@ -414,8 +415,13 @@ getinfo(_HandleObject *self, PyObject *args)
if (strlist == NULL)
Py_RETURN_NONE;
list = PyList_New(0);
for (int x=0; strlist[x] != NULL; x++)
for (int x=0; strlist[x] != NULL; x++) {
PyList_Append(list, PyString_FromString(strlist[x]));
}

if (option == LRI_MIRRORS)
lr_free(strlist);

return list;
}

Expand Down
1 change: 1 addition & 0 deletions librepo/python/librepomodule.c
Expand Up @@ -128,6 +128,7 @@ init_librepo(void)
PyModule_AddIntConstant(m, "LRI_LASTCURLSTRERR", LRI_LASTCURLSTRERR);
PyModule_AddIntConstant(m, "LRI_LASTCURLMSTRERR", LRI_LASTCURLMSTRERR);
PyModule_AddIntConstant(m, "LRI_LASTBADSTATUSCODE", LRI_LASTBADSTATUSCODE);
PyModule_AddIntConstant(m, "LRI_MIRRORS", LRI_MIRRORS);

/* Check options */
PyModule_AddIntConstant(m, "LR_CHECK_GPG", LR_CHECK_GPG);
Expand Down
22 changes: 22 additions & 0 deletions librepo/util.c
Expand Up @@ -296,6 +296,28 @@ lr_copy_content(int source, int dest)
return (size < 0) ? -1 : 0;
}

char *
lr_prepend_url_protocol(const char *path)
{
if (!path)
return NULL;

if (strstr(path, "://")) // Protocol was specified
return lr_strdup(path);

if (path[0] == '/') // Path is absolute path
return lr_strconcat("file://", path, NULL);

char *path_with_protocol, *resolved_path = realpath(path, NULL);
if (!resolved_path) {
DPRINTF("%s: %s - realpath: %s ", __func__, path, strerror(errno));
return NULL;
}
path_with_protocol = lr_strconcat("file://", resolved_path, NULL);
free(resolved_path);
return path_with_protocol;
}

int
lr_vasprintf(char **strp, const char *format, va_list va)
{
Expand Down
9 changes: 9 additions & 0 deletions librepo/util.h
Expand Up @@ -130,6 +130,15 @@ int lr_remove_dir(const char *path);
*/
int lr_copy_content(int source, int dest);

/** \ingroup util
* If protocol is specified ("http://foo") return copy of path.
* If path is absolute ("/foo/bar/") return path with "file://" prefix.
* If path is relative ("bar/") return absolute path with "file://" prefix.
* @param path
* @return url with protocol
*/
char *lr_prepend_url_protocol(const char *path);

/** \ingroup util
* Print to allocated string.
* @param strp Location for the newly allocated string.
Expand Down
2 changes: 1 addition & 1 deletion librepo/yum.c
Expand Up @@ -434,7 +434,7 @@ lr_yum_download_remote(lr_Handle handle, lr_Result result)

DPRINTF("%s: Downloading/Copying repo..\n", __func__);

rc = lr_handle_prepare_internal_mirrorlist(handle, "repodata/repomd.xml");
rc = lr_handle_prepare_internal_mirrorlist(handle);
if (rc != LRE_OK)
return rc;

Expand Down

0 comments on commit 056a115

Please sign in to comment.