from pathlib import Path
import yaml
from fastrelease.conda import latest_pypi, pypi_json
from fastcore.all import ifnone, patch, store_attr , compose, L, call_parse, Param
@patch
def wlns(self:Path, lst:list):
with self.open('w') as f: f.writelines(lst)
@patch
def d2yml(self:Path, d):
yaml.SafeDumper.ignore_aliases = lambda *args : True
with self.open('w') as f: f.write('---\n')
with self.open('a') as f: yaml.safe_dump(d, f)
@patch
def yml2d(self:Path):
with self.open('r') as f: return yaml.safe_load(f)
def _mkdir(path):
p = Path(path)
p.mkdir(exist_ok=True, parents=True)
return p
class CondaBuild:
def __init__(self, pypinm, deps=None, import_nm=None, path=None):
store_attr('pypinm,deps')
self.import_nm = ifnone(import_nm, pypinm)
try: self.ver = str(latest_pypi(pypinm))
except: raise ValueError(f'package name: {pypinm} not found on pypi.')
self.info = pypi_json(f'{pypinm}/{self.ver}')['info']
self.path = _mkdir(ifnone(path,self.pypinm))
self.meta ={'package': {'name': self.pypinm, 'version': self.ver},
'build':{'number':0, 'binary_relocation':False, 'detect_binary_files_with_prefix':False},
'requirents':{'host': ['pip', 'python'], 'run':['python']+list(L(self.deps))},
'test':{'imports': [self.import_nm], 'requires':['pip']},
'about':{'home':self.info['home_page'], 'summary':self.info['summary'], 'license':self.info['license']},
'extra':{'recipe-maintainers': ['jph00']}
}
def create_meta(self): (self.path/'meta.yaml').d2yml(self.meta)
def create_sh(self): (self.path/'build.sh').wlns(['#!/usr/bin/env bash\n','PIP_NO_INDEX=False python -m pip install -Uq $PKG_NAME'])
def create_bat(self): (self.path/'bld.bat').wlns(['setlocal\n', 'set PIP_NO_INDEX=False\n','python -m pip install -Uq %PKG_NAME%'])
def create_build_files(self):
for f in [self.create_meta, self.create_sh, self.create_bat]: f()
@classmethod
def from_yaml(cls, path):
p = Path(path)
assert p.is_file() and p.exists(), f"Did not find file: {path}."
cb = L(cls(**d) for d in p.yml2d())
for c in cb: c.create_build_files()
return cb
from collections import OrderedDict
od = OrderedDict({'foo':1, 'bar':2, 'a':3})
Path('test.yaml').d2yml(od)
--------------------------------------------------------------------------- RepresenterError Traceback (most recent call last) <ipython-input-25-6e8763c98bc1> in <module> 1 from collections import OrderedDict 2 od = OrderedDict({'foo':1, 'bar':2, 'a':3}) ----> 3 Path('test.yaml').d2yml(od) <ipython-input-5-417d6a0b0cdf> in d2yml(self, d) 6 def d2yml(self:Path, d): 7 yaml.SafeDumper.ignore_aliases = lambda *args : True ----> 8 with self.open('w') as f: yaml.safe_dump(d, f) 9 10 @patch ~/anaconda3/lib/python3.8/site-packages/yaml/__init__.py in safe_dump(data, stream, **kwds) 304 If stream is None, return the produced string instead. 305 """ --> 306 return dump_all([data], stream, Dumper=SafeDumper, **kwds) 307 308 def add_implicit_resolver(tag, regexp, first=None, ~/anaconda3/lib/python3.8/site-packages/yaml/__init__.py in dump_all(documents, stream, Dumper, default_style, default_flow_style, canonical, indent, width, allow_unicode, line_break, encoding, explicit_start, explicit_end, version, tags, sort_keys) 276 dumper.open() 277 for data in documents: --> 278 dumper.represent(data) 279 dumper.close() 280 finally: ~/anaconda3/lib/python3.8/site-packages/yaml/representer.py in represent(self, data) 25 26 def represent(self, data): ---> 27 node = self.represent_data(data) 28 self.serialize(node) 29 self.represented_objects = {} ~/anaconda3/lib/python3.8/site-packages/yaml/representer.py in represent_data(self, data) 56 node = self.yaml_multi_representers[None](self, data) 57 elif None in self.yaml_representers: ---> 58 node = self.yaml_representers[None](self, data) 59 else: 60 node = ScalarNode(None, str(data)) ~/anaconda3/lib/python3.8/site-packages/yaml/representer.py in represent_undefined(self, data) 229 230 def represent_undefined(self, data): --> 231 raise RepresenterError("cannot represent an object", data) 232 233 SafeRepresenter.add_representer(type(None), RepresenterError: ('cannot represent an object', OrderedDict([('foo', 1), ('bar', 2), ('a', 3)]))
@call_parse
def main(
path: str='build.yaml' # Path to build file
):
CondaBuild.from_yaml(path)
cb = CondaBuild.from_yaml('build.yaml')
for c in cb:
ver = c.ver
path = c.path
print(ver,path)
4.5.1.48 opencv-python-headless 0.1.95 sentencepiece