def create_project(identifier, dest, pros_cli=None, require_empty=False, overwrite=False): if pros_cli is None or not pros_cli: pros_cli = CliConfig() filename = os.path.join(pros_cli.directory, identifier.depot, '{}-{}'.format(identifier.name, identifier.version), 'template.pros') if not os.path.isfile(filename): click.echo('Error: template.pros not found for {}-{}'.format(identifier.name, identifier.version)) click.get_current_context().abort() sys.exit() if require_empty: if os.path.isfile(dest) or (os.path.isdir(dest) and len(os.listdir(dest)) > 0): click.echo('Error! Destination is a file or a nonempty directory! Delete the file(s) and try again.') click.get_current_context().abort() sys.exit() config = TemplateConfig(file=filename) copytree(config.directory, dest, overwrite=overwrite) for root, dirs, files in os.walk(dest): for d in dirs: d = os.path.relpath(os.path.join(root, d), dest) if any([fnmatch.fnmatch(d, p) for p in config.template_ignore]): verbose('Removing {}'.format(d)) os.rmdir(os.path.join(root, d)) for f in files: f = os.path.relpath(os.path.join(root, f), dest) if any([fnmatch.fnmatch(f, p) for p in config.template_ignore]): verbose('Removing {}'.format(f)) os.remove(os.path.join(root, f)) proj_config = prosconfig.ProjectConfig(dest, create=True) proj_config.kernel = identifier.version proj_config.save()
def install_lib(identifier, dest, pros_cli, overwrite=False): if pros_cli is None or not pros_cli: pros_cli = CliConfig() filename = os.path.join(pros_cli.directory, identifier.depot, '{}-{}'.format(identifier.name, identifier.version), 'template.pros') if not os.path.isfile(filename): click.echo('Error: template.pros not found for {}-{}'.format(identifier.name, identifier.version)) click.get_current_context().abort() sys.exit() proj_config = prosconfig.ProjectConfig(dest) config = TemplateConfig(file=filename) copytree(config.directory, dest, overwrite=overwrite) for root, dirs, files in os.walk(dest): for d in dirs: if any([fnmatch.fnmatch(d, p) for p in config.template_ignore]): verbose('Removing {}'.format(d)) os.rmdir(os.path.join(root, d)) for f in files: if any([fnmatch.fnmatch(f, p) for p in config.template_ignore]): verbose('Removing {}'.format(f)) os.remove(os.path.join(root, f)) if type(proj_config.libraries) is list: proj_config.libraries = dict() proj_config.libraries[identifier.name] = identifier.version proj_config.save()
def upgrade_project(identifier: Identifier, dest: str, pros_cli: CliConfig = None): if pros_cli is None or not pros_cli: pros_cli = CliConfig() filename = os.path.join( pros_cli.directory, identifier.depot, '{}-{}'.format(identifier.name, identifier.version), 'template.pros') if not os.path.isfile(filename): click.echo('Error: template.pros not found for {}-{}'.format( identifier.name, identifier.version)) click.get_current_context().abort() sys.exit() proj_config = prosconfig.ProjectConfig(dest, raise_on_error=True) config = TemplateConfig(file=filename) for root, dirs, files in os.walk(config.directory): for d in dirs: if any([fnmatch.fnmatch(d, p) for p in config.upgrade_paths]): verbose('Upgrading {}'.format(d)) relpath = os.path.relpath(os.path.join(root, d), config.directory) shutil.copytree(os.path.join(config.directory, relpath), os.path.join(proj_config.directory, relpath)) for f in files: if any([fnmatch.fnmatch(f, p) for p in config.upgrade_paths]): verbose('Upgrading {}'.format(f)) relpath = os.path.relpath(os.path.join(root, f), config.directory) shutil.copyfile(os.path.join(config.directory, relpath), os.path.join(proj_config.directory, relpath))
def create_template(identifier, location=None, pros_cli=None): if pros_cli is None or not pros_cli: pros_cli = CliConfig() if location is None or not location: location = os.path.join(location, identifier.depot, '{}-{}'.format(identifier.name, identifier.version)) filename = os.path.join(location, 'template.pros') config = TemplateConfig(file=filename) config.name = identifier.name config.version = identifier.version config.depot = identifier.depot config.save() return config
def create_template(identifier: Identifier, pros_cli: CliConfig = None) -> TemplateConfig: if pros_cli is None or not pros_cli: pros_cli = CliConfig() filename = os.path.join( pros_cli.directory, identifier.depot, '{}-{}'.format(identifier.name, identifier.version), 'template.pros') config = TemplateConfig(file=filename) config.name = identifier.name config.version = identifier.version config.depot = identifier.depot config.save() return config
def create_template(identifier, pros_cli=None): if pros_cli is None or not pros_cli: pros_cli = CliConfig() filename = os.path.join(pros_cli.directory, identifier.depot, '{}-{}'.format(identifier.name, identifier.version), 'template.pros') config = TemplateConfig(file=filename) config.name = identifier.name config.version = identifier.version config.depot = identifier.depot config.save() return config
def download(self, identifier): self.verify_configuration() template_dir = get_template_dir(self, identifier) if os.path.isdir(template_dir): shutil.rmtree(template_dir) elif os.path.isfile(template_dir): os.remove(template_dir) # verify release exists: click.echo('Fetching release on {} with tag {}'.format( self.config.location, identifier.version)) r = requests.get( 'https://api.github.com/repos/{}/releases/tags/{}'.format( self.config.location, identifier.version), headers=self.create_headers(), verify=get_cert_attr()) if r.status_code == 200: for asset in [ a for a in r.json()['assets'] if a['name'] == '{}-template.zip'.format(identifier.name) ]: # Time to download the file proscli.utils.debug('Found {}'.format(asset['url'])) dr = requests.get( asset['url'], headers=self.create_headers('application/octet-stream'), stream=True, verify=get_cert_attr()) if dr.status_code == 200 or dr.status_code == 302: with tempfile.NamedTemporaryFile(delete=False) as tf: # todo: no temp file necessary - go straight from download to zipfile extraction with click.progressbar(length=asset['size'], label='Downloading {} (v: {})'.format(asset['name'], identifier.version)) \ as progress_bar: for chunk in dr.iter_content(128): tf.write(chunk) progress_bar.update(128) tf.close( ) # need to close since opening again as ZipFile with zipfile.ZipFile(tf.name) as zf: with click.progressbar( length=len(zf.namelist()), label='Extracting {}'.format( asset['name'])) as progress_bar: for file in zf.namelist(): zf.extract(file, path=template_dir) progress_bar.update(1) os.remove(tf.name) template_config = TemplateConfig( os.path.join(template_dir, 'template.pros')) if template_config.identifier.version != identifier.version: click.echo( 'WARNING: Version fetched does not have the same version downloaded {0} != {1}.' .format(template_config.identifier.version, identifier.version)) os.rename( template_dir, get_template_dir(self, template_config.identifier)) template_dir = get_template_dir( self, template_config.identifier) click.echo( 'Template downloaded to {}'.format(template_dir)) return template_config.identifier else: click.echo( 'Unable to download {} from {} (Status code: {})'. format(asset['name'], self.config.location, dr.status_code)) proscli.utils.debug(dr.__dict__) return False else: click.echo('Unable to find {} on {} (Status code: {})'.format( identifier.version, self.config.name, r.status_code)) proscli.utils.debug(r.__dict__) return False