def download(self, project, project_dir=None, parent_dir=None): """Download project archive and extract.""" if '/' in project: owner, project_name = project.split('/') else: owner = self._username() project_name = project if not self._exists(project_name, owner): raise BinstarError('404') url = "{}/apps/{}/projects/{}/download".format(self._api.domain, owner, project_name) data, headers = binstar_utils.jencode({}) with self._api.session.get(url, data=data, headers=headers, stream=True) as res: res.raise_for_status() filename = eval( re.findall("filename=(.+);", res.headers["Content-Disposition"])[0]) if parent_dir: filename = os.path.join(parent_dir, filename) print('Downloading {}'.format(project)) print('.', end='') with open(filename, 'wb') as f: for chunk in res.iter_content(chunk_size=4096): if chunk: print('.', end='') f.write(chunk) print() self._check_response(res) return os.path.abspath(filename)
def submit_for_build(self, username, package, fd, instructions, test_only=False, channels=None, queue=None, queue_tags=None, callback=None): url = '%s/build/%s/%s/stage' % (self.domain, username, package) data, headers = jencode(instructions=instructions, test_only=test_only, channels=channels, queue_name=queue, queue_tags=queue_tags) res = self.session.post(url, data=data, headers=headers) self._check_response(res) obj = res.json() s3url = obj['post_url'] s3data = obj['form_data'] _hexmd5, b64md5, size = compute_hash(fd) s3data['Content-Length'] = size s3data['Content-MD5'] = b64md5 data_stream, headers = stream_multipart(s3data, files={'file':(obj['basename'], fd)}, callback=callback) s3res = requests.post(s3url, data=data_stream, verify=True, timeout=10 * 60 * 60, headers=headers) if s3res.status_code != 201: raise BinstarError('Error uploading build', s3res.status_code) url = '%s/build/%s/%s/commit/%s' % (self.domain, username, package, obj['build_id']) res = self.session.post(url, verify=True) self._check_response(res, [201]) return obj
def interactive_get_token(): bs = get_binstar() config = get_config() url = config.get('url', 'https://api.binstar.org') token = None username = input('Username: '******'s " % username) password = getpass.getpass(stream=sys.stderr) token = bs.authenticate(username, password, 'binstar_client:%s' % (socket.gethostname()), url, created_with=' '.join(sys.argv)) break except Unauthorized: log.error( 'Invalid Username password combination, please try again') continue if token is None: raise BinstarError( 'Sorry. Please try again (go to https://binstar.org/account/forgot_password to reset your password)' ) return token
def validate(self, validator): if inspect.isfunction(validator): return validator(basename=self.basename, relativepath=self.relativepath, fullpath=self.fullpath) elif inspect.isclass(validator): return validator(self)() raise BinstarError("Invalid validator {}".format(validator))
def save_config(data, config_file): data_dir = dirname(config_file) try: if not exists(data_dir): os.makedirs(data_dir) with open(config_file, 'w') as fd: yaml_dump(data, stream=fd) except EnvironmentError as exc: raise BinstarError('%s: %s' % (exc.filename, exc.strerror,))
def _mk_channel_url(self, channel, owner, package=None, version=None, filename=None): url = '%s/channel/%s/%s' % (self.domain, channel, owner) if package: url += '/%s' % package if version: url += '/%s' % version if filename: url += '/%s' % filename elif filename: raise BinstarError( "version can not be none if filename is given") elif version or filename: raise BinstarError( "package can not be none if version or filename is given") return url
def save_config(data, config_file): data_dir = dirname(config_file) try: if not exists(data_dir): os.makedirs(data_dir) with open(config_file, 'w') as fd: yaml.safe_dump(data, fd, default_flow_style=False) except EnvironmentError as exc: raise BinstarError('%s: %s' % ( exc.filename, exc.strerror, ))
def download(self, project, project_dir=None, parent_dir=None): """Download project archive and extract.""" if '/' in project: owner, project_name = project.split('/') else: owner = self._username() project_name = project if not self._exists(project_name, owner): raise BinstarError('404') url = "{}/apps/{}/projects/{}/download".format(self._api.domain, owner, project_name) data, headers = binstar_utils.jencode({}) with self._api.session.get(url, data=data, headers=headers, stream=True) as res: res.raise_for_status() filename = eval( re.findall("filename=(.+);", res.headers["Content-Disposition"])[0]) if parent_dir: filename = os.path.join(parent_dir, filename) progress = tqdm( unit='KiB', total=int(res.headers.get('Content-Length', None)) / 1024, unit_scale=True, desc='Download') with open(filename, 'wb') as f: for chunk in res.iter_content(chunk_size=1024): if chunk: progress.update(len(chunk) / 1024) f.write(chunk) progress.close() self._check_response(res) return os.path.abspath(filename)
def main(args): binstar = get_binstar() spec = args.spec[0] org = spec.org name = spec.name if args.show: show_collection(binstar, spec) return if not name: raise BinstarError('invaid collection spec') if args.create: public = True if args.public is None else args.public binstar.add_collection(org, name, public=public, description=args.description) if args.update: binstar.update_collection(org, name, public=args.public, description=args.description) if args.remove: binstar.remove_collection(org, name) if args.add_package: package = args.add_package data = binstar.collection_add_packages(org, name, owner=package.org, package=package.name) if data[package.name]['before'] == data[package.name]['after']: log.info('Package %s is already at the latest revision (%s)' % (package.name, data[package.name]['after'])) else: log.info('Updated package %s from revision %s to %s' % (package.name, data[package.name]['before'], data[package.name]['after'])) if args.add_packages: for owner, package in read_package_file(args.add_packages): data = binstar.collection_add_packages(org, name, owner=owner, package=package) if data[package]['before'] == data[package]['after']: log.info('Package %s is already at the latest revision (%s)' % (package, data[package]['after'])) else: log.info('Updated package %s from revision %s to %s' % (package.name, data[package]['before'], data[package]['after'])) if args.remove_package: package = args.remove_package binstar.collection_remove_packages(org, name, owner=package.org, package=package.name) if args.clone_from: binstar.collection_clone(args.clone_from.org, args.clone_from.name, org, name) if args.pull_from: binstar.collection_pull(args.pull_from.org, args.pull_from.name, org, name) if args.sync: sync_data = binstar.collection_sync(org, name) updated = { key: value for (key, value) in sync_data.items() if value['after'] != value['before'] } if updated: for package_name, info in updated.items(): log.info('Updated package %s from revision %s to %s' % (package_name, info['before'], info['after'])) else: log.info('All packages are up to date')