2017-06-22 01:43:08 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# HACK: seems 'requests' module bundled with blender isn't bundled with 'idna' module. So force system python for now
|
2017-07-06 22:58:17 -07:00
|
|
|
# import sys
|
|
|
|
# sys.path.insert(0, '/usr/lib/python3.6/site-packages')
|
2017-06-22 17:41:41 -07:00
|
|
|
|
2017-07-06 22:58:17 -07:00
|
|
|
from pathlib import Path
|
2017-06-22 01:43:08 -07:00
|
|
|
import requests
|
2017-06-22 17:41:41 -07:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2017-07-05 02:45:30 -07:00
|
|
|
|
2017-07-06 22:10:26 -07:00
|
|
|
class Package:
|
2017-07-05 02:45:30 -07:00
|
|
|
"""
|
2017-07-06 22:10:26 -07:00
|
|
|
Stores package methods and metadata
|
2017-07-05 02:45:30 -07:00
|
|
|
"""
|
|
|
|
|
2017-07-06 22:10:26 -07:00
|
|
|
def __init__(self, package_dict:dict = None):
|
|
|
|
self.from_dict(package_dict)
|
|
|
|
|
|
|
|
def to_dict(self) -> dict:
|
|
|
|
"""
|
|
|
|
Return a dict representation of the package
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
'bl_info': self.bl_info,
|
|
|
|
'url': self.url,
|
|
|
|
}
|
|
|
|
|
|
|
|
def from_dict(self, package_dict: dict):
|
|
|
|
"""
|
|
|
|
Get attributes from a dict such as produced by `to_dict`
|
|
|
|
"""
|
|
|
|
if package_dict is None:
|
|
|
|
package_dict = {}
|
|
|
|
|
|
|
|
for attr in ('name', 'url', 'bl_info'):
|
|
|
|
setattr(self, attr, package_dict.get(attr))
|
|
|
|
|
|
|
|
|
|
|
|
class Repository:
|
2017-07-05 02:45:30 -07:00
|
|
|
"""
|
2017-07-06 22:10:26 -07:00
|
|
|
Stores repository metadata (including packages)
|
2017-07-05 02:45:30 -07:00
|
|
|
"""
|
|
|
|
|
2017-07-06 22:10:26 -07:00
|
|
|
def __init__(self, repo_dict:dict = None):
|
|
|
|
self.from_dict(repo_dict)
|
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
"""
|
|
|
|
Requests repo.json from URL and embeds etag/last-modification headers
|
|
|
|
"""
|
|
|
|
|
|
|
|
req_headers = {}
|
|
|
|
|
|
|
|
# Do things this way to avoid adding empty objects/None to the req_headers dict
|
|
|
|
if 'etag' in self._headers:
|
|
|
|
req_headers['If-None-Match'] = self._headers['etag']
|
|
|
|
if 'last-modified' in self._headers:
|
|
|
|
req_headers['If-Modified-Since'] = self._headers['last-modified']
|
|
|
|
|
|
|
|
#try
|
|
|
|
resp = requests.get(self.url, headers=req_headers)
|
|
|
|
|
|
|
|
repodict = json.loads(resp.json())
|
|
|
|
repodict['etag'] = resp.headers.get('etag')
|
|
|
|
repodict['last-modified'] = resp.headers.get('last-modified')
|
|
|
|
|
|
|
|
self.from_dict(repodict)
|
|
|
|
|
|
|
|
|
|
|
|
def to_dict(self) -> dict:
|
|
|
|
"""
|
|
|
|
Return a dict representation of the repository
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
'name': self.name,
|
|
|
|
'packages': [p.to_dict() for p in self.packages],
|
|
|
|
'url': self.url,
|
|
|
|
'_headers': self._headers,
|
|
|
|
}
|
|
|
|
|
|
|
|
def from_dict(self, repodict: dict):
|
|
|
|
"""
|
|
|
|
Get attributes from a dict such as produced by `to_dict`
|
|
|
|
"""
|
|
|
|
if repodict is None:
|
|
|
|
repodict = {}
|
2017-07-05 02:45:30 -07:00
|
|
|
|
2017-07-06 22:10:26 -07:00
|
|
|
for attr in ('name', 'url', 'packages', '_headers'):
|
|
|
|
if attr == 'package':
|
|
|
|
value = set(Package(pkg) for pkg in repodict.get('packages', []))
|
|
|
|
else:
|
|
|
|
value = repodict.get(attr)
|
2017-07-05 02:45:30 -07:00
|
|
|
|
2017-07-06 22:10:26 -07:00
|
|
|
setattr(self, attr, value)
|
2017-07-05 02:45:30 -07:00
|
|
|
|
2017-07-06 22:58:17 -07:00
|
|
|
def dump(self, path: Path):
|
2017-07-06 22:10:26 -07:00
|
|
|
"""
|
|
|
|
Dump repository as a repo.json file in 'path'
|
|
|
|
"""
|
|
|
|
with (path / 'repo.json').open('w', encoding='utf-8') as repo_file:
|
|
|
|
json.dump(self.to_dict(), repo_file, indent=4, sort_keys=True)
|
|
|
|
log.info("repo.json written to %s" % path)
|
2017-07-05 02:45:30 -07:00
|
|
|
|
2017-07-06 22:10:26 -07:00
|
|
|
# def fetch_repo(url: str, repo: Repository) -> dict:
|
2017-07-05 02:45:30 -07:00
|
|
|
def refresh(url):
|
2017-06-29 19:55:35 -07:00
|
|
|
# we have to explicitly close the end of the pipe we are NOT using,
|
|
|
|
# otherwise no exception will be generated when the other process closes its end.
|
2017-06-29 17:46:08 -07:00
|
|
|
pipe[0].close()
|
2017-06-29 19:55:35 -07:00
|
|
|
|
2017-07-06 22:58:17 -07:00
|
|
|
local_repo_path = Path(__file__).parent / 'packages'
|
2017-06-29 19:55:35 -07:00
|
|
|
local_repo_path.mkdir(exist_ok=True)
|
|
|
|
|
2017-06-29 17:46:08 -07:00
|
|
|
try:
|
2017-07-05 02:45:30 -07:00
|
|
|
fetch_repojson(url)
|
2017-06-29 19:55:35 -07:00
|
|
|
pipe[1].send(re.status_code)
|
2017-06-29 17:46:08 -07:00
|
|
|
finally:
|
|
|
|
pipe[1].close()
|
|
|
|
|
|
|
|
|