blob: aaaaab7fb8db6e8f3fb8b3725eab36ae8b68b928 [file] [log] [blame]
Éric Araujo3a9f58f2011-06-01 20:42:49 +02001:mod:`packaging.pypi.dist` --- Classes representing query results
2=================================================================
3
4.. module:: packaging.pypi.dist
5 :synopsis: Classes representing the results of queries to indexes.
6
7
8Information coming from the indexes is held in instances of the classes defined
9in this module.
10
11Keep in mind that each project (eg. FooBar) can have several releases
12(eg. 1.1, 1.2, 1.3), and each of these releases can be provided in multiple
13distributions (eg. a source distribution, a binary one, etc).
14
15
16ReleaseInfo
17-----------
18
19Each release has a project name, version, metadata, and related distributions.
20
21This information is stored in :class:`ReleaseInfo`
22objects.
23
24.. class:: ReleaseInfo
25
26
27DistInfo
28---------
29
30:class:`DistInfo` is a simple class that contains
31information related to distributions; mainly the URLs where distributions
32can be found.
33
34.. class:: DistInfo
35
36
37ReleasesList
38------------
39
40The :mod:`~packaging.pypi.dist` module provides a class which works
41with lists of :class:`ReleaseInfo` classes;
42used to filter and order results.
43
44.. class:: ReleasesList
45
46
47Example usage
48-------------
49
50Build a list of releases and order them
51^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52
53Assuming we have a list of releases::
54
55 >>> from packaging.pypi.dist import ReleasesList, ReleaseInfo
56 >>> fb10 = ReleaseInfo("FooBar", "1.0")
57 >>> fb11 = ReleaseInfo("FooBar", "1.1")
58 >>> fb11a = ReleaseInfo("FooBar", "1.1a1")
59 >>> ReleasesList("FooBar", [fb11, fb11a, fb10])
60 >>> releases.sort_releases()
61 >>> releases.get_versions()
62 ['1.1', '1.1a1', '1.0']
63 >>> releases.add_release("1.2a1")
64 >>> releases.get_versions()
65 ['1.1', '1.1a1', '1.0', '1.2a1']
66 >>> releases.sort_releases()
67 ['1.2a1', '1.1', '1.1a1', '1.0']
68 >>> releases.sort_releases(prefer_final=True)
69 >>> releases.get_versions()
70 ['1.1', '1.0', '1.2a1', '1.1a1']
71
72
73Add distribution related information to releases
74^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75
76It's easy to add distribution information to releases::
77
78 >>> from packaging.pypi.dist import ReleasesList, ReleaseInfo
79 >>> r = ReleaseInfo("FooBar", "1.0")
80 >>> r.add_distribution("sdist", url="http://example.org/foobar-1.0.tar.gz")
81 >>> r.dists
82 {'sdist': FooBar 1.0 sdist}
83 >>> r['sdist'].url
84 {'url': 'http://example.org/foobar-1.0.tar.gz', 'hashname': None, 'hashval':
85 None, 'is_external': True}
86
87
88Getting attributes from the dist objects
89^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90
91To abstract querying information returned from the indexes, attributes and
92release information can be retrieved directly from dist objects.
93
94For instance, if you have a release instance that does not contain the metadata
95attribute, it can be fetched by using the "fetch_metadata" method::
96
97 >>> r = Release("FooBar", "1.1")
98 >>> print r.metadata
99 None # metadata field is actually set to "None"
100 >>> r.fetch_metadata()
101 <Metadata for FooBar 1.1>
102
103.. XXX add proper roles to these constructs
104
105
106It's possible to retrieve a project's releases (`fetch_releases`),
107metadata (`fetch_metadata`) and distributions (`fetch_distributions`) using
108a similar work flow.
109
110.. XXX what is possible?
111
112Internally, this is possible because while retrieving information about
113projects, releases or distributions, a reference to the client used is
114stored which can be accessed using the objects `_index` attribute.