Exemplo n.º 1
0
def run_helpers(recipe, dst):
    for helper in conf.get('settings', 'helpers').split():
        try:
            logger.info('    -> %s' % helper)
            m = __import__('sushi_ext_%s' % helper)
            m = sys.modules['sushi_ext_%s' % helper]
            m.run(dst)
        except Exception as err:
            logger.info(' :: Helper %s not found (%s)' % (helper, err))
Exemplo n.º 2
0
def unbundle(recipe, dst):
    manager = RecipesManager()
    recipe_dir = manager.get(recipe)

    if manager.name_handler(recipe) not in manager.list_available():
        raise UnbundlerException("Don't know this recipe.")

    env = get_env(dst)

    if os.path.exists(dst):
        raise UnbundlerException('Destination (%s) already exist' % dst)

    ##################
    # Copy every dir #
    ##################
    def ignore(folder, names):
        _res = []
        for n in names:
            if not os.path.isdir(os.path.join(folder, n)):
                if conf.has_option('settings', 'ignore'):
                    if n in conf.get('settings', 'ignore').split():
                        _res.append(n)
        return _res

    shutil.copytree(recipe_dir, dst, ignore=ignore)

    ############################################
    # Copy every file and render it on the fly #
    ############################################
    for path, dirs, files in os.walk(recipe_dir):
        for f in files:
            if conf.has_option('settings', 'ignore'):
                if f in conf.get('settings', 'ignore').split():
                    continue
            fdst = os.path.join(path.replace(recipe_dir, dst), f)
            try:
                with codecs.open(fdst, mode='w', encoding='utf-8') as r:
                    r.write(render(os.path.join(path, f), **env))
            except Exception as err:
                logger.info('      | Failed for %s (%s)' % (f, err))

    ######################################
    # Rename every __app__ to {{ name }} #
    ######################################
    for r, s, f in os.walk(dst):
        # Folders
        if "__app__" in s:
            os.rename(os.path.join(r, "__app__"),
                      os.path.join(r, env['app'].lower()))
    for r, s, f in os.walk(dst):
        # Files
        if "__app__" in f:
            os.rename(os.path.join(r, "__app__"),
                      os.path.join(r, env['app'].lower()))
Exemplo n.º 3
0
    def run(self):
        url = 'https://github.com/Socketubs/Sushi/raw/master/recipes/basic.tar.gz'

        logger.info(' :: Searching')
        r = requests.get(url, prefetch=False)
        try:
            r.raise_for_status()
        except Exception as err:
            raise StarterException(
                "Sorry I can't find this old recipe in my cookbook. Try later. (%s)"
                % err)

        src = os.path.join(conf.get('paths', 'sushi_recipes'), 'basic.tar.gz')
        dst = conf.get('paths', 'sushi_recipes')

        with open(src, 'w') as f:
            for buf in r.iter_content(1024):
                if buf:
                    f.write(buf)

        logger.info(' :: Teaching')
        # Open tarfile
        tar = tarfile.open(src, 'r:gz')
        if tarfile.is_tarfile(src):
            tar.extractall(dst)
        else:
            raise Exception('Archive invalid (not a gzipped tarfile)')

        # Remove archive
        os.remove(src)
        logger.info(' :: Done')
Exemplo n.º 4
0
    def run(self):
        url = 'https://github.com/Socketubs/Sushi/raw/master/recipes/basic.tar.gz'

        logger.info(' :: Searching')
        r = requests.get(url, prefetch=False)
        try:
            r.raise_for_status()
        except Exception as err:
            raise StarterException("Sorry I can't find this old recipe in my cookbook. Try later. (%s)" % err)

        src = os.path.join(conf.get('paths', 'sushi_recipes'), 'basic.tar.gz')
        dst = conf.get('paths', 'sushi_recipes')

        with open(src, 'w') as f:
            for buf in r.iter_content(1024):
                if buf:
                    f.write(buf)

        logger.info(' :: Teaching')
        # Open tarfile
        tar = tarfile.open(src, 'r:gz')
        if tarfile.is_tarfile(src):
            tar.extractall(dst)
        else:
            raise Exception('Archive invalid (not a gzipped tarfile)')

        # Remove archive
        os.remove(src)
        logger.info(' :: Done')
Exemplo n.º 5
0
def confirm(prompt=None, resp=False):
    try:
        if prompt is None:
            prompt = 'Confirm'

        if resp:
            prompt = '%s [%s|%s]: ' % (prompt, 'n', 'Y')
        else:
            prompt = '%s [%s|%s]: ' % (prompt, 'y', 'N')

        while True:
            ans = raw_input(prompt)
            if not ans:
                return resp
            if ans == 'y' or ans == 'Y':
                return True
            if ans == 'n' or ans == 'N':
                return False
            else:
                return False
    except KeyboardInterrupt:
        logger.info('\nAbort.')
        sys.exit(1)
Exemplo n.º 6
0
def get_conf():
    # Create paths
    user_home = os.getenv('HOME')
    sushi_path = os.path.join(user_home, '.sushi')
    sushi_conf = os.path.join(sushi_path, 'sushi.conf')
    sushi_recipes = os.path.join(sushi_path, 'recipes')
    sushi_cookbooks = os.path.join(sushi_path, 'cookbooks')

    if not os.path.exists(sushi_path):
        logger.info('Create sushi conf folder')
        os.makedirs(sushi_path)
        os.makedirs(sushi_recipes)
        os.makedirs(sushi_cookbooks)
        with codecs.open(sushi_conf, mode='w', encoding='utf-8') as f:
            f.write(default_conf)

    parser = SafeConfigParser()
    parser.read(sushi_conf)

    parser.add_section('paths')
    parser.set('paths', 'home', user_home)
    parser.set('paths', 'sushi', sushi_path)
    parser.set('paths', 'sushi_conf', sushi_conf)
    parser.set('paths', 'sushi_recipes', sushi_recipes)
    parser.set('paths', 'sushi_cookbooks', sushi_cookbooks)

    # Defaults
    if not parser.has_section('settings'):
        parser.add_section('settings')
    if not parser.has_option('settings', 'license'):
        parser.set('settings', 'license', 'agpl-v3')
    if not parser.has_option('settings', 'recipe'):
        parser.set('settings', 'recipe', 'default')
    if not parser.has_option('settings', 'helpers'):
        parser.set('settings', 'helpers', '')

    return parser
Exemplo n.º 7
0
Arquivo: conf.py Projeto: joskid/sushi
def get_conf():
    # Create paths
    user_home = os.getenv('HOME')
    sushi_path = os.path.join(user_home, '.sushi')
    sushi_conf = os.path.join(sushi_path, 'sushi.conf')
    sushi_recipes = os.path.join(sushi_path, 'recipes')
    sushi_cookbooks = os.path.join(sushi_path, 'cookbooks')

    if not os.path.exists(sushi_path):
        logger.info('Create sushi conf folder')
        os.makedirs(sushi_path)
        os.makedirs(sushi_recipes)
        os.makedirs(sushi_cookbooks)
        with codecs.open(sushi_conf, mode='w', encoding='utf-8') as f:
            f.write(default_conf)

    parser = SafeConfigParser()
    parser.read(sushi_conf)

    parser.add_section('paths')
    parser.set('paths', 'home', user_home)
    parser.set('paths', 'sushi', sushi_path)
    parser.set('paths', 'sushi_conf', sushi_conf)
    parser.set('paths', 'sushi_recipes', sushi_recipes)
    parser.set('paths', 'sushi_cookbooks', sushi_cookbooks)

    # Defaults
    if not parser.has_section('settings'):
        parser.add_section('settings')
    if not parser.has_option('settings', 'license'):
        parser.set('settings', 'license', 'agpl-v3')
    if not parser.has_option('settings', 'recipe'):
        parser.set('settings', 'recipe', 'default')
    if not parser.has_option('settings', 'helpers'):
        parser.set('settings', 'helpers', '')

    return parser
Exemplo n.º 8
0
 def upgrade(self, ignore_errors=False):
     for user in os.listdir(conf.get('paths', 'sushi_cookbooks')):
         for cookbook in os.listdir('%s/%s' % (conf.get('paths', 'sushi_cookbooks'), user)):
             os.chdir('%s/%s/%s' % (conf.get('paths', 'sushi_cookbooks'), user, cookbook))
             try:
                 logger.info(' - %s/%s' % (user, cookbook))
                 os.system('git pull')
             except:
                 if ignore_errors:
                     logger.info('Error: could not update %s/%s cookbook' % (user, cookbook))
                 else:
                     raise CookbookUpdateFailed('Could not update %s/%s cookbook' % (user, cookbook))
     logger.info('==> Done')
Exemplo n.º 9
0
 def upgrade(self, ignore_errors=False):
     for user in os.listdir(conf.get('paths', 'sushi_cookbooks')):
         for cookbook in os.listdir(
                 '%s/%s' % (conf.get('paths', 'sushi_cookbooks'), user)):
             os.chdir(
                 '%s/%s/%s' %
                 (conf.get('paths', 'sushi_cookbooks'), user, cookbook))
             try:
                 logger.info(' - %s/%s' % (user, cookbook))
                 os.system('git pull')
             except:
                 if ignore_errors:
                     logger.info('Error: could not update %s/%s cookbook' %
                                 (user, cookbook))
                 else:
                     raise CookbookUpdateFailed(
                         'Could not update %s/%s cookbook' %
                         (user, cookbook))
     logger.info('==> Done')
Exemplo n.º 10
0
def run(dst):
    logger.info('    -> Run {{ name }}')
Exemplo n.º 11
0
Arquivo: cli.py Projeto: joskid/sushi
    def start(self):
        ###################
        # craft           #
        ###################
        if self.args.get('craft', False):
            logger.info('==> Craft your project')
            path = self.args.get('<path>')
            recipe = self.args.get('--recipe', False)
            if not recipe:
                recipe = conf.get('settings', 'recipe', 'default')
            logger.info('  => Recipe: %s' % recipe)
            try:
                unbundle(recipe, path)
            except Exception as err:
                logger.info('Error: %s (%s)' % (err, recipe))
                sys.exit(1)
            logger.info('==> Call helpers')
            run_helpers(recipe, path)
            logger.info('==> Done')
        ###################
        # learn           #
        ###################
        elif self.args.get('learn', False):
            recipes = self.args.get('<name>')
            manager = RecipesManager()
            try:
                for recipe in recipes:
                    logger.info('==> Learn %s' % recipe)
                    manager.add(recipe)
            except RecipeAlreadyLearn as err:
                logger.error('Error: %s' % err)
                sys.exit(1)
            logger.info('==> Done')
        ###################
        # forget          #
        ###################
        elif self.args.get('forget', False):
            recipes = self.args.get('<name>')
            manager = RecipesManager()
            try:
                for recipe in recipes:
                    logger.info('==> Forget %s' % recipe)
                    manager.delete(recipe)
                logger.info('==> Done')
            except Exception as err:
                logger.error('Error: %s' % err)
                sys.exit(1)
        ###################
        # list            #
        ################### 
        elif self.args.get('list', False):
            manager = RecipesManager()
            recipes = manager.list()
            if not recipes:
                logger.info('==> No recipe learned')
                sys.exit(1)
            logger.info('==> Recipes learned')
            for recipe in recipes:
                logger.info(recipe)
            sys.exit(0)
        ###################
        # all             #
        ################### 
        elif self.args.get('all', False):
            manager = RecipesManager()
            recipes = manager.list_available()
            if not recipes:
                logger.info('==> No recipe available')
                sys.exit(1)
            logger.info('==> Recipes available')
            for recipe in recipes:
                logger.info(recipe)
            sys.exit(0)
        ###################
        # upgrade         #
        ################### 
        elif self.args.get('upgrade', False):
            cb = Cookbook()
            logger.info('==> Update cookbooks')
            cookbooks = cb.upgrade(ignore_errors=True)
        ###################
        # cookbook        #
        ###################
        elif self.args.get('cookbook', False):
            cb = Cookbook()
            cookbooks = cb.list()
            if not cookbooks:
                logger.info('==> No cookbooks registered')
                sys.exit(1)
            logger.info('==> Cookbooks')
            for cookbook in cookbooks:
                logger.info(cookbook)
            sys.exit(0)
        ###################
        # cookbook-add    #
        ###################
        elif self.args.get('cookbook-add', False):
            repo_name = self.args.get('<name>')[0]
            logger.info('==> Add %s cookbook' % repo_name)
            cb = Cookbook()
            try:
                cb.add(repo_name)
                logger.info('==> Done')
            except CookbookException as err:
                logger.info('Error: %s' % err)
                sys.exit(1)
            sys.exit(0)
        ###################
        # cookbook-del    #
        ###################
        elif self.args.get('cookbook-del', False):
            repo_name = self.args.get('<name>')[0]
            logger.info('==> Delete %s cookbook' % repo_name)
            cb = Cookbook()
            if confirm():
                try:
                    cb.remove(repo_name)
                    logger.info('==> Done')
                except CookbookException as err:
                    logger.info('Error: %s' % err)
                    sys.exit(1)
            else:
                print('Abort.')
                sys.exit(1)
            sys.exit(0)

        sys.exit(100)
Exemplo n.º 12
0
Arquivo: cli.py Projeto: toxinu/sushi
    def start(self):
        ###################
        # craft           #
        ###################
        if self.args.get("craft", False):
            logger.info("==> Craft your project")
            path = self.args.get("<path>")
            recipe = self.args.get("--recipe", False)
            if not recipe:
                recipe = conf.get("settings", "recipe", "default")
            logger.info("  => Recipe: %s" % recipe)
            try:
                unbundle(recipe, path)
            except Exception as err:
                if hasattr(err, "message"):
                    logger.error("!! %s (%s)" % (err.message, recipe))
                else:
                    logger.error("!! %s (%s)" % (err, recipe))
                sys.exit(1)
            logger.info("==> Call helpers")
            run_helpers(recipe, path)
            logger.info("==> Done")
        ###################
        # learn           #
        ###################
        elif self.args.get("learn", False):
            recipes = self.args.get("<name>")
            manager = RecipesManager()
            try:
                for recipe in recipes:
                    logger.info("==> Learn %s" % recipe)
                    manager.add(recipe)
            except RecipeAlreadyLearn as err:
                logger.error("!! %s" % err.message)
                sys.exit(1)
            except RecipeUnvailable as err:
                logger.error("!! %s" % err.message)
                sys.exit(1)
            logger.info("==> Done")
        ###################
        # forget          #
        ###################
        elif self.args.get("forget", False):
            recipes = self.args.get("<name>")
            manager = RecipesManager()
            try:
                for recipe in recipes:
                    logger.info("==> Forget %s" % recipe)
                    manager.delete(recipe)
                logger.info("==> Done")
            except Exception as err:
                logger.error("!! %s" % err.message)
                sys.exit(1)
        ###################
        # list            #
        ###################
        elif self.args.get("list", False):
            manager = RecipesManager()
            recipes = manager.list()
            if not recipes:
                logger.info("==> No recipe learned")
                sys.exit(1)
            logger.info("==> Recipes learned")
            for recipe in recipes:
                logger.info(recipe)
            sys.exit(0)
        ###################
        # all             #
        ###################
        elif self.args.get("all", False):
            manager = RecipesManager()
            recipes = manager.list_available()
            if not recipes:
                logger.info("==> No recipe available")
                sys.exit(1)
            logger.info("==> Recipes available")
            for recipe in recipes:
                logger.info(recipe)
            sys.exit(0)
        ###################
        # upgrade         #
        ###################
        elif self.args.get("upgrade", False):
            cb = Cookbook()
            logger.info("==> Update cookbooks")
            cookbooks = cb.upgrade(ignore_errors=True)
        ###################
        # cookbook        #
        ###################
        elif self.args.get("cookbook", False):
            cb = Cookbook()
            cookbooks = cb.list()
            if not cookbooks:
                logger.info("==> No cookbooks registered")
                sys.exit(1)
            logger.info("==> Cookbooks")
            for cookbook in cookbooks:
                logger.info(cookbook)
            sys.exit(0)
        ###################
        # cookbook-add    #
        ###################
        elif self.args.get("cookbook-add", False):
            repo_name = self.args.get("<name>")[0]
            logger.info("==> Add %s cookbook" % repo_name)
            cb = Cookbook()
            try:
                cb.add(repo_name)
                logger.info("==> Done")
            except CookbookException as err:
                logger.info("!! %s" % err.message)
                sys.exit(1)
            sys.exit(0)
        ###################
        # cookbook-del    #
        ###################
        elif self.args.get("cookbook-del", False):
            repo_name = self.args.get("<name>")[0]
            logger.info("==> Delete %s cookbook" % repo_name)
            cb = Cookbook()
            if confirm():
                try:
                    cb.remove(repo_name)
                    logger.info("==> Done")
                except CookbookException as err:
                    logger.info("!! %s" % err.message)
                    sys.exit(1)
            else:
                print("Abort.")
                sys.exit(1)
            sys.exit(0)

        sys.exit(100)
Exemplo n.º 13
0
 def check(self):
     if not self.manager.list():
         logger.info(" :: I think it's your first time with sushi")
         logger.info(" :: Can I suggest you simple basic recipe ?")
         return True
     return False
Exemplo n.º 14
0
 def check(self):
     if not self.manager.list():
         logger.info(" :: I think it's your first time with sushi")
         logger.info(" :: Can I suggest you simple basic recipe ?")
         return True
     return False
Exemplo n.º 15
0
    def start(self):
        ###################
        # craft           #
        ###################
        if self.args.get('craft', False):
            logger.info('==> Craft your project')
            path = self.args.get('<path>')
            recipe = self.args.get('--recipe', False)
            if not recipe:
                recipe = conf.get('settings', 'recipe', 'default')
            logger.info('  => Recipe: %s' % recipe)
            try:
                unbundle(recipe, path)
            except Exception as err:
                logger.info('!! %s (%s)' % (err.message, recipe))
                sys.exit(1)
            logger.info('==> Call helpers')
            run_helpers(recipe, path)
            logger.info('==> Done')
        ###################
        # learn           #
        ###################
        elif self.args.get('learn', False):
            recipes = self.args.get('<name>')
            manager = RecipesManager()
            try:
                for recipe in recipes:
                    logger.info('==> Learn %s' % recipe)
                    manager.add(recipe)
            except RecipeAlreadyLearn as err:
                logger.error('!! %s' % err.message)
                sys.exit(1)
            except RecipeUnvailable as err:
                logger.error('!! %s' % err.message)
                sys.exit(1)
            logger.info('==> Done')
        ###################
        # forget          #
        ###################
        elif self.args.get('forget', False):
            recipes = self.args.get('<name>')
            manager = RecipesManager()
            try:
                for recipe in recipes:
                    logger.info('==> Forget %s' % recipe)
                    manager.delete(recipe)
                logger.info('==> Done')
            except Exception as err:
                logger.error('!! %s' % err.message)
                sys.exit(1)
        ###################
        # list            #
        ###################
        elif self.args.get('list', False):
            manager = RecipesManager()
            recipes = manager.list()
            if not recipes:
                logger.info('==> No recipe learned')
                sys.exit(1)
            logger.info('==> Recipes learned')
            for recipe in recipes:
                logger.info(recipe)
            sys.exit(0)
        ###################
        # all             #
        ###################
        elif self.args.get('all', False):
            manager = RecipesManager()
            recipes = manager.list_available()
            if not recipes:
                logger.info('==> No recipe available')
                sys.exit(1)
            logger.info('==> Recipes available')
            for recipe in recipes:
                logger.info(recipe)
            sys.exit(0)
        ###################
        # upgrade         #
        ###################
        elif self.args.get('upgrade', False):
            cb = Cookbook()
            logger.info('==> Update cookbooks')
            cookbooks = cb.upgrade(ignore_errors=True)
        ###################
        # cookbook        #
        ###################
        elif self.args.get('cookbook', False):
            cb = Cookbook()
            cookbooks = cb.list()
            if not cookbooks:
                logger.info('==> No cookbooks registered')
                sys.exit(1)
            logger.info('==> Cookbooks')
            for cookbook in cookbooks:
                logger.info(cookbook)
            sys.exit(0)
        ###################
        # cookbook-add    #
        ###################
        elif self.args.get('cookbook-add', False):
            repo_name = self.args.get('<name>')[0]
            logger.info('==> Add %s cookbook' % repo_name)
            cb = Cookbook()
            try:
                cb.add(repo_name)
                logger.info('==> Done')
            except CookbookException as err:
                logger.info('!! %s' % err.message)
                sys.exit(1)
            sys.exit(0)
        ###################
        # cookbook-del    #
        ###################
        elif self.args.get('cookbook-del', False):
            repo_name = self.args.get('<name>')[0]
            logger.info('==> Delete %s cookbook' % repo_name)
            cb = Cookbook()
            if confirm():
                try:
                    cb.remove(repo_name)
                    logger.info('==> Done')
                except CookbookException as err:
                    logger.info('!! %s' % err.message)
                    sys.exit(1)
            else:
                print('Abort.')
                sys.exit(1)
            sys.exit(0)

        sys.exit(100)
Exemplo n.º 16
0
def run(dst):
	logger.info('    -> Run {{ name }}')