2 # Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5 # @author Chulwoo Shin <cw1.shin@samsung.com>
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
26 from lxml import etree
27 from tic.utils import file
28 from tic.utils import process
29 from tic.utils.error import TICError
30 from tic.utils.grabber import myurlgrab
33 def _get_uncompressed_data_from_url(url, filename, proxies):
35 filename = myurlgrab(url, filename, proxies)
36 # Check if file compressed or not
37 if filename.endswith(".gz"):
38 decompress_filename = os.path.splitext(filename)[0]
39 filename = file.decompress_gzip(filename, decompress_filename)
40 elif filename.endswith(".bz2"):
41 process.run(['bunzip2', "-f", filename])
42 filename = os.path.splitext(filename)[0]
46 def _get_metadata_from_repo(baseurl, proxies, cachedir, reponame, filename,
47 sumtype=None, checksum=None):
48 url = os.path.join(baseurl, filename)
49 filename_tmp = str("%s/%s/%s" % (cachedir, reponame, os.path.basename(filename)))
50 if os.path.splitext(filename_tmp)[1] in (".gz", ".bz2"):
51 filename = os.path.splitext(filename_tmp)[0]
53 filename = filename_tmp
55 if sumtype and checksum and os.path.exists(filename):
56 if sumtype == 'sha256':
57 file_checksum = hashlib.sha256(open(filename, 'rb').read()).hexdigest()
58 elif sumtype == 'md5':
59 file_checksum = hashlib.md5(open(filename, 'rb').read()).hexdigest()
61 sumcmd = "%ssum" % sumtype
62 result = process.run([sumcmd, filename])[1].strip()
63 file_checksum = result.split()[0]
65 if file_checksum and file_checksum == checksum:
68 return _get_uncompressed_data_from_url(url, filename_tmp, proxies)
71 def get_repodata_from_repos(repos, cachedir):
75 baseurl = repo.baseurl
76 cache_dir = os.path.join(cachedir, reponame)
77 cache_file = os.path.join(cache_dir, 'repomd.xml')
79 # make directory for caching
80 file.make_dirs(cache_dir)
83 url = os.path.join(baseurl, 'repodata/repomd.xml')
84 repomd = myurlgrab(url, cache_file, None)
87 tree = etree.parse(repomd)
89 except etree.XMLSyntaxError:
90 raise TICError("repomd.xml syntax error.")
93 ns = ns[0:ns.rindex("}")+1]
99 for elm in root.findall("%sdata" % ns):
100 if elm.attrib['type'] == 'patterns':
101 filepaths['patterns'] = elm.find("%slocation" % ns).attrib['href']
102 checksums['patterns'] = elm.find("%sopen-checksum" % ns).text
103 sumtypes['patterns'] = elm.find("%sopen-checksum" % ns).attrib['type']
104 elif elm.attrib['type'] == 'group':
105 filepaths['comps'] = elm.find("%slocation" % ns).attrib['href']
106 checksums['comps'] = elm.find("%sopen-checksum" % ns).text
107 sumtypes['comps'] = elm.find("%sopen-checksum" % ns).attrib['type']
108 elif elm.attrib["type"] == 'primary':
109 filepaths['primary'] = elm.find("%slocation" % ns).attrib['href']
110 checksums['primary'] = elm.find("%sopen-checksum" % ns).text
111 sumtypes['primary'] = elm.find("%sopen-checksum" % ns).attrib['type']
113 for item in ("primary", "patterns", "comps"):
114 if item not in filepaths:
115 filepaths[item] = None
118 filepaths[item] = _get_metadata_from_repo(baseurl,
126 my_repodata.append({"name":reponame,
129 "primary":filepaths['primary'],
132 "patterns":filepaths['patterns'],
133 "comps":filepaths['comps']})
138 RepoType = collections.namedtuple('Repo', 'name, baseurl')
139 def Repo(name, baseurl):
140 return RepoType(name, baseurl)
142 if __name__ == '__main__':
143 repo_url_1 = 'https://download.tizen.org/snapshots/tizen/base/latest/repos/arm64/packagesaaa'
144 repo_url_2 = 'https://download.tizen.org/snapshots/tizen/mobile/latest/repos/arm64-wayland/packages'
146 repos.append(Repo(base64.urlsafe_b64encode(repo_url_1), repo_url_1))
147 repos.append(Repo(base64.urlsafe_b64encode(repo_url_2), repo_url_2))
148 cachedir = '/var/tmp/tic-core/cached'
149 repodata = get_repodata_from_repos(repos, cachedir)