def get_features_and_pools(container_name): """Install features and pools needed by given container""" from ape.installtools import cleanup, get_ape_venv, create_project_venv, fetch_pool, add_to_path CONTAINER_DIR = os.path.join(os.environ['APE_ROOT_DIR'], container_name) if os.path.exists(CONTAINER_DIR): os.environ['CONTAINER_DIR'] = CONTAINER_DIR else: print 'ERROR: this container does not exist!' return install_candidates = tasks.features_and_pools_to_get(container_name) cleanup() venv = create_project_venv() tmp_req_path = os.path.join(tasks.get_container_dir(container_name), 'tmp_requirements.txt') with codecs.open(tmp_req_path, 'w', encoding='utf-8') as tmp_requirements: for feature in install_candidates['features']: tmp_requirements.write(install_candidates['features'][feature]['giturl']+'\n') venv.pip_install_requirements('tmp_requirements.txt') pools = [] for pool in install_candidates['pools']: pools.append(fetch_pool(install_candidates['pools'][pool]['giturl'])) paths = venv.get_paths() for entry in pools: paths.append(entry.get_path('features')) paths.append(entry.get_path('features_exp')) add_to_path( paths )
def export_config_to_equation(poi=None): """ Generates a product.equation file for the given product name. It generates it from the <product_name>.config file in the products folder. For that you need to have your project imported to featureIDE and set the correct settings. """ import os config_file_path = None equation_file_path = None if poi: parts = poi.split(':') if len(parts) == 2: container_name, product_name = parts if container_name not in tasks.get_containers(): print('No such container') elif product_name not in tasks.get_products(container_name): print('No such product') else: cont_dir = tasks.get_container_dir(container_name) equation_file_path = os.path.join(cont_dir, 'products', product_name, 'product.equation') config_file_path = os.path.join(cont_dir, 'products', product_name + '.config') else: print('Please check your arguments: --poi <container>:<product>') else: cont_dir = os.environ.get('CONTAINER_DIR') equation_file_path = os.path.join(cont_dir, 'products', os.environ.get('PRODUCT_NAME'), 'product.equation') config_file_path = os.path.join( cont_dir, 'products', os.environ.get('PRODUCT_NAME') + '.config') if equation_file_path and config_file_path: config_new = list() try: with open(config_file_path, 'r') as config_file: config_old = config_file.readlines() for line in config_old: # in FeatureIDE we cant use '.' for the paths to sub-features so we used '__' # e.g. django_productline__features__development if len(line.split('__')) <= 2: config_new.append(line) else: config_new.append(line.replace('__', '.')) except IOError as e: print( 'Config file not found. Please make sure you have a valid .config-file in your products folder.\n' ' Also make sure that this file has the same name as your product.' ) try: with open(equation_file_path, 'w+') as eq_file: eq_file.writelines(config_new) except IOError as e: print( 'product.equation file not found. Please make sure you have a valid product.equation in your chosen product' ) else: print('Please check your arguments: --poi <container>:<product>')
def cd(doi): '''cd to directory of interest(doi) a doi can be: herbert - the container named "herbert" herbert:website - product "website" located in container "herbert" ''' parts = doi.split(':') if len(parts) == 1: container_name, product_name = parts[0], None elif len(parts) == 2: container_name, product_name = parts[0], parts[1] else: print 'unable to parse context - format: <container_name>:<product_name>' sys.exit(1) if container_name not in tasks.get_containers(): print 'No such container' else: if product_name: if product_name not in tasks.get_products(container_name): print 'No such product' else: print tasks.conf.SOURCE_HEADER print 'cd ' + tasks.get_product_dir(container_name, product_name) else: print tasks.conf.SOURCE_HEADER print 'cd ' + tasks.get_container_dir(container_name)
def get_poi_tuple(poi=None): """ Takes the poi or None and returns the container_dir and the product name either of the passed poi (<container_name>: <product_name>) or from os.environ- :param poi: optional; <container_name>: <product_name> :return: tuple of the container directory and the product name """ if poi: parts = poi.split(':') if len(parts) == 2: container_name, product_name = parts if container_name not in tasks.get_containers(): print('No such container') sys.exit(1) elif product_name not in tasks.get_products(container_name): print('No such product') sys.exit(1) else: container_dir = tasks.get_container_dir(container_name) else: print('Please check your arguments: --poi <container>:<product>') sys.exit(1) else: container_dir = os.environ.get('CONTAINER_DIR') product_name = os.environ.get('PRODUCT_NAME') return container_dir, product_name
def get_containers(): entries = os.listdir(tasks.conf.APE_ROOT) containers = [] for entry in entries: if os.path.isdir(tasks.get_container_dir(entry) + '/products'): containers.append(entry) return containers
def import_config_from_equation(poi=None): """ Generates a <productname>.config file from the product.equation of the given (or activated) product name and places it in your products dir. """ import os config_file_path = None equation_file_path = None if poi: parts = poi.split(':') if len(parts) == 2: container_name, product_name = parts if container_name not in tasks.get_containers(): print('No such container') elif product_name not in tasks.get_products(container_name): print('No such product') else: cont_dir = tasks.get_container_dir(container_name) equation_file_path = os.path.join(cont_dir, 'products', product_name, 'product.equation') config_file_path = os.path.join(cont_dir, 'products', product_name + '.config') else: print('Please check your arguments: --poi <container>:<product>') else: # If a product is already activated it gets selected automatically if no arguments are passed. product_name = os.environ.get('PRODUCT_NAME') cont_dir = os.environ.get('CONTAINER_DIR') equation_file_path = os.path.join(cont_dir, 'products', product_name, 'product.equation') config_file_path = os.path.join(cont_dir, 'products', product_name + '.config') if equation_file_path and config_file_path: config_new = list() try: with open(equation_file_path, 'r') as eq_file: config_old = eq_file.readlines() for line in config_old: # in FeatureIDE we cant use '.' for the paths to sub-features so we used '__' # e.g. django_productline__features__development if not line.startswith('#'): if len(line.split('.')) <= 2: config_new.append(line) else: config_new.append(line.replace('.', '__')) except IOError as e: print( 'Equation file not found. Please make sure you have a valid product.equation in your products/<product_name>/. \n' ) try: with open(config_file_path, 'w+') as config_file: config_file.writelines(config_new) except IOError as e: print( '{product_name}.config file not found. \n ' 'Please make sure you have a valid <product_name>.config in your products directory.' .format(product_name=product_name)) else: print('Please check your arguments: --poi <container>:<product>')
def get_products(container_name): products_dir = tasks.get_container_dir(container_name) + '/products' if not os.path.isdir(products_dir): return [] products = os.listdir(products_dir) def is_product(p): return not p.startswith('.') and not p.startswith('_') return [p for p in products if is_product(p)]
def get_products(container_name): products_dir = tasks.get_container_dir(container_name) + '/products' if not os.path.isdir(products_dir): return [] products = os.listdir(products_dir) def predicate(p): return not p.startswith('.') and not p.startswith('_') products = filter(predicate, products) return products
def import_config_from_equation(poi=None): """ Generates a <productname>.config file from the product.equation of the given (or activated) product name and places it in your products dir. """ import os config_file_path = None equation_file_path = None if poi: parts = poi.split(':') if len(parts) == 2: container_name, product_name = parts if container_name not in tasks.get_containers(): print('No such container') elif product_name not in tasks.get_products(container_name): print('No such product') else: cont_dir = tasks.get_container_dir(container_name) equation_file_path = os.path.join(cont_dir, 'products', product_name, 'product.equation') config_file_path = os.path.join(cont_dir, 'products', product_name + '.config') else: print('Please check your arguments: --poi <container>:<product>') else: # If a product is already activated it gets selected automatically if no arguments are passed. product_name = os.environ.get('PRODUCT_NAME') cont_dir = os.environ.get('CONTAINER_DIR') equation_file_path = os.path.join(cont_dir, 'products', product_name, 'product.equation') config_file_path = os.path.join(cont_dir, 'products', product_name + '.config') if equation_file_path and config_file_path: config_new = list() try: with open(equation_file_path, 'r') as eq_file: config_old = eq_file.readlines() for line in config_old: # in FeatureIDE we cant use '.' for the paths to sub-features so we used '__' # e.g. django_productline__features__development if not line.startswith('#'): if len(line.split('.')) <= 2: config_new.append(line) else: config_new.append(line.replace('.', '__')) except IOError as e: print( 'Equation file not found. Please make sure you have a valid product.equation in your products/<product_name>/. \n') try: with open(config_file_path, 'w+') as config_file: config_file.writelines(config_new) except IOError as e: print('{product_name}.config file not found. \n ' 'Please make sure you have a valid <product_name>.config in your products directory.'.format( product_name=product_name)) else: print('Please check your arguments: --poi <container>:<product>')
def get_features_in_container(container_name): """Get a list of features bundled with container""" feature_dir = os.path.join(tasks.get_container_dir(container_name), 'features') if not os.path.isdir(feature_dir): return [] features = os.listdir(feature_dir) def predicate(p): return not p.startswith('.') and not p.startswith('_') features = filter(predicate, features) return features
def get_meta_data(container_name=None): """Load meta.json as a python dict and return it, meta.json from APE_ROOT if conainter_name is unset, else from container_dir""" try: if container_name: meta_json_path = os.path.join(tasks.get_container_dir(container_name) + '/meta.json') else: meta_json_path = os.path.join(tasks.conf.APE_ROOT + '/meta.json') meta_json_file = codecs.open(meta_json_path, 'r', encoding='utf-8').read() meta_json_data = json.loads(meta_json_file) except: print 'Could not load meta data' return meta_json_data
def create_feature(feature_name, container_name=None, location=None): ''' Create a feature <feature_name>. If <location> and <container_name> is specified, an error is raised. If <location> is given the feature is created at that location(relative from current working directory) If <container_name> is given the feature is created in the "features" directory of the given container. If neither <location> nor <container_name> is given, the feature is created in the current container - if no container is active an error is raised. ''' if location and container_name: print 'ERROR: combining container_name and location options is not supported.' sys.exit(1) if not location and not container_name: container_name = os.environ['CONTAINER_NAME'] if container_name: location = os.path.join(tasks.get_container_dir(container_name), 'features') else: location = os.path.abspath(os.path.normpath(location)) from cookiecutter.generate import generate_files from django_productline.context import PRODUCT_CONTEXT template_dir = tasks.get_cookiecutter_template_dir('djpl_feature') feature_dir = os.path.join(location, feature_name) if os.path.isdir(feature_dir): print 'ERROR: %s already exists.' % feature_dir return generate_files( template_dir, context=dict( cookiecutter={ 'feature_name': feature_name, } ), output_dir=location ) print '*** Created feature %s in %s' % (feature_name, location)
def cd(doi): """ cd to directory of interest(doi) a doi can be: herbert - the container named "herbert" sdox:dev - product "website" located in container "herbert" :param doi: :return: """ parts = doi.split(':') if len(parts) == 2: container_name, product_name = parts[0], parts[1] elif len(parts) == 1 and os.environ.get('CONTAINER_NAME'): # interpret poi as product name if already zapped into a product in order # to enable simply switching products by doing ape zap prod. product_name = parts[0] container_name = os.environ.get('CONTAINER_NAME') else: print('unable to parse context - format: <container_name>:<product_name>') sys.exit(1) if container_name not in tasks.get_containers(): print('No such container') else: if product_name: if product_name not in tasks.get_products(container_name): print('No such product') else: print(tasks.conf.SOURCE_HEADER) print('cd ' + tasks.get_product_dir(container_name, product_name)) else: print(tasks.conf.SOURCE_HEADER) print('cd ' + tasks.get_container_dir(container_name))
def get_product_dir(container_name, product_name): return tasks.get_container_dir(container_name) + '/products/' + product_name