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')
def list(self): res = [] for user in os.listdir(conf.get('paths', 'sushi_cookbooks')): for cookbook in os.listdir( '%s/%s' % (conf.get('paths', 'sushi_cookbooks'), user)): res.append('%s/%s' % (user, cookbook)) return res
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')
def get_recipes(self, cookbook_name): res = [] if not os.path.exists(os.path.join(conf.get('paths', 'sushi_cookbooks'), cookbook_name)): raise CookbookNotFound('This cookbook does not exists') for recipe in os.listdir('%s/%s' % (conf.get('paths', 'sushi_cookbooks'), cookbook_name)): if recipe != '.git': res.append(recipe) return res
def remove(self, repo_name): path = '%s/%s' % (conf.get('paths', 'sushi_cookbooks'), repo_name) if not os.path.exists(path): raise CookbookRemoveFailed('This cookbook not exists') shutil.rmtree(path, ignore_errors=True) if os.path.exists('%s/%s' % (conf.get('paths', 'sushi_recipes'), repo_name)): shutil.rmtree('%s/%s' % (conf.get('paths', 'sushi_recipes'), repo_name), ignore_errors=True)
def delete(self, name): recipe = self.name_handler(name) dst_user = '******' % (conf.get('paths', 'sushi_recipes'), recipe.split('/')[0]) dst_repo = '%s/%s' % (conf.get('paths', 'sushi_recipes'), '/'.join(recipe.split('/')[:-1])) dst_recipe = '%s/%s' % (dst_repo, recipe.split('/')[-1]) os.remove(dst_recipe) if not os.listdir(dst_repo): shutil.rmtree(dst_repo) if not os.listdir(dst_user): shutil.rmtree(dst_user)
def get_recipes(self, cookbook_name): res = [] if not os.path.exists( os.path.join(conf.get('paths', 'sushi_cookbooks'), cookbook_name)): raise CookbookNotFound('This cookbook does not exists') for recipe in os.listdir( '%s/%s' % (conf.get('paths', 'sushi_cookbooks'), cookbook_name)): if recipe != '.git': res.append(recipe) return res
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')
def delete(self, name): recipe = self.name_handler(name) dst_user = '******' % (conf.get('paths', 'sushi_recipes'), recipe.split('/')[0]) dst_repo = '%s/%s' % (conf.get('paths', 'sushi_recipes'), '/'.join( recipe.split('/')[:-1])) dst_recipe = '%s/%s' % (dst_repo, recipe.split('/')[-1]) os.remove(dst_recipe) if not os.listdir(dst_repo): shutil.rmtree(dst_repo) if not os.listdir(dst_user): shutil.rmtree(dst_user)
def delete(self, name): recipe = self.name_handler(name) dst_user = '******' % (conf.get('paths', 'sushi_recipes'), recipe.split('/')[0]) dst_repo = '%s/%s' % (conf.get('paths', 'sushi_recipes'), '/'.join(recipe.split('/')[:-1])) dst_recipe = '%s/%s' % (dst_repo, recipe.split('/')[-1]) try: os.remove(dst_recipe) if not os.listdir(dst_repo): shutil.rmtree(dst_repo) if not os.listdir(dst_user): shutil.rmtree(dst_user) except: raise RecipeUnmatched("Nothing to forget.")
def add(self, repo_name): # repo_name = socketubs/sushi-recipes # renamed as socketubs-recipes if len(repo_name.split('/')) < 2: raise CookbookBadURL('Bad cookbook url (socketubs/sushi-recipes)') if repo_name.split('/')[1].split('-')[0] != 'sushi': raise CookbookBadURL('Bad cookbook url (socketubs/sushi-recipes)') url = "https://github.com/%s.git" % repo_name user = repo_name.split('/')[0].lower() repo_name = '-'.join(repo_name.split('/')[1].split('-')[1:]).lower() os.chdir(conf.get('paths', 'sushi_cookbooks')) if not os.path.exists(user): os.makedirs(user) else: if repo_name.split('-')[-1] in os.listdir(user): raise CookbookAlreadyExists('Cookbook already added') return_code = os.system('git clone %s %s/%s' % (url, user, repo_name)) if return_code >= 1: if not os.listdir(user): shutil.rmtree(user, ignore_errors=True) raise CookbookAddFailed('Failed to add this cookbook (%s/%s)\n!! Return code: %s' % ( user, repo_name, return_code))
def add(self, repo_name): # repo_name = socketubs/sushi-recipes # renamed as socketubs-recipes if len(repo_name.split('/')) < 2: raise CookbookException('Bad cookbook url (socketubs/sushi-recipes)') if repo_name.split('/')[1].split('-')[0] != 'sushi': raise CookbookException('Bad cookbook url (socketubs/sushi-recipes)') url = "git://github.com/%s.git" % repo_name user = repo_name.split('/')[0].lower() repo_name = '-'.join(repo_name.split('/')[1].split('-')[1:]).lower() os.chdir(conf.get('paths', 'sushi_cookbooks')) if not os.path.exists(user): os.makedirs(user) else: if repo_name.split('-')[-1] in os.listdir(user): raise CookbookException('Cookbook already added') try: os.system('git clone %s %s/%s' % (url, user, repo_name)) except Exception as err: if not os.listdir(user): os.remove(user) raise CookbookException('Failed to add this cookbook (%s/%s)\nError: %s' % (user, repo_name, err))
def add(self, repo_name): # repo_name = socketubs/sushi-recipes # renamed as socketubs-recipes if len(repo_name.split('/')) < 2: raise CookbookBadURL('Bad cookbook url (socketubs/sushi-recipes)') if repo_name.split('/')[1].split('-')[0] != 'sushi': raise CookbookBadURL('Bad cookbook url (socketubs/sushi-recipes)') url = "https://github.com/%s.git" % repo_name user = repo_name.split('/')[0].lower() repo_name = '-'.join(repo_name.split('/')[1].split('-')[1:]).lower() os.chdir(conf.get('paths', 'sushi_cookbooks')) if not os.path.exists(user): os.makedirs(user) else: if repo_name.split('-')[-1] in os.listdir(user): raise CookbookAlreadyExists('Cookbook already added') return_code = os.system('git clone %s %s/%s' % (url, user, repo_name)) if return_code >= 1: if not os.listdir(user): shutil.rmtree(user, ignore_errors=True) raise CookbookAddFailed( 'Failed to add this cookbook (%s/%s)\n!! Return code: %s' % (user, repo_name, return_code))
def run(dst): licenses = Licenses() license = conf.get('settings', 'license') dst = os.path.join(dst, 'LICENSE') with open(dst, 'w') as l: l.write(licenses[license]['url'])
def add(self, repo_name): # repo_name = socketubs/sushi-recipes # renamed as socketubs-recipes if len(repo_name.split('/')) < 2: raise CookbookException( 'Bad cookbook url (socketubs/sushi-recipes)') if repo_name.split('/')[1].split('-')[0] != 'sushi': raise CookbookException( 'Bad cookbook url (socketubs/sushi-recipes)') url = "git://github.com/%s.git" % repo_name user = repo_name.split('/')[0].lower() repo_name = '-'.join(repo_name.split('/')[1].split('-')[1:]).lower() os.chdir(conf.get('paths', 'sushi_cookbooks')) if not os.path.exists(user): os.makedirs(user) else: if repo_name.split('-')[-1] in os.listdir(user): raise CookbookException('Cookbook already added') try: os.system('git clone %s %s/%s' % (url, user, repo_name)) except Exception as err: if not os.listdir(user): os.remove(user) raise CookbookException( 'Failed to add this cookbook (%s/%s)\nError: %s' % (user, repo_name, err))
def list(self): root = conf.get('paths', 'sushi_recipes') res = [] for user in os.listdir(root): for cookbook in os.listdir('%s/%s' % (root, user)): for recipe in os.listdir('%s/%s/%s' % (root, user, cookbook)): res.append('%s/%s/%s' % (user, cookbook, recipe)) return res
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
def get_recipes(self, cookbook_name): res = [] for recipe in os.listdir( '%s/%s' % (conf.get('paths', 'sushi_cookbooks'), cookbook_name)): if recipe != '.git': res.append(recipe) return res
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))
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')
def list_available(self): cb = Cookbook() res = [] root = conf.get('paths', 'sushi_cookbooks') for user in os.listdir(root): for cookbook in os.listdir('%s/%s' % (root, user)): for recipe in os.listdir('%s/%s/%s' % (root, user, cookbook)): if recipe != '.git': res.append('%s/%s/%s' % (user, cookbook, recipe)) return res
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()))
def add(self, name): recipe = self.name_handler(name) src_repo = '%s/%s' % (conf.get('paths', 'sushi_cookbooks'), '/'.join(recipe.split('/')[:-1])) dst_repo = '%s/%s' % (conf.get('paths', 'sushi_recipes'), '/'.join(recipe.split('/')[:-1])) src_recipe = '%s/%s' % (src_repo, recipe.split('/')[-1]) dst_recipe = '%s/%s' % (dst_repo, recipe.split('/')[-1]) # recipe: socketubs/recipes/django # src_repo: /Users/socketubs/.sushi/cookbooks/socketubs-recipes # dst_repo: /Users/socketubs/.sushi/recipes/socketubs-recipes # src_recipe: src_repo + /django # dst_recipe: dst_repo _ /django if not os.path.exists(dst_repo): os.makedirs(dst_repo) try: os.symlink(os.path.join(src_recipe, 'content'), dst_recipe) except OSError: raise RecipeAlreadyLearn('Recipe %s already learned' % recipe)
def get_env(name): now = datetime.datetime.now() extras = [u'firstname', u'lastname', u'email'] d = { u'username': unicode(getpass.getuser()), u'app': unicode(name), u'year': unicode(now.year), u'day': unicode(now.day), u'month': unicode(now.month), u'hour': unicode(now.hour), u'minute': unicode(now.minute), u'second': unicode(now.second), u'date': unicode(now.strftime("%Y-%m-%d %H:%M")) } for extra in extras: if conf.has_option('settings', extra): d[extra] = conf.get('settings', extra).decode('utf-8') else: d[extra] = '## Set %s' % extra return d
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)
def get_recipes(self, cookbook_name): res = [] for recipe in os.listdir('%s/%s' % (conf.get('paths', 'sushi_cookbooks'), cookbook_name)): if recipe != '.git': res.append(recipe) return res
def list(self): res = [] for user in os.listdir(conf.get('paths', 'sushi_cookbooks')): for cookbook in os.listdir('%s/%s' % (conf.get('paths', 'sushi_cookbooks'), user)): res.append('%s/%s' % (user, cookbook)) return res
def ignore(folder, names): return [ n for n in names if not os.path.isdir(os.path.join(folder, n)) and n in conf.get('settings', 'ignore').split() ]
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)
def get(self, name): recipe = self.name_handler(name) return '%s/%s' % (conf.get('paths', 'sushi_recipes'), recipe)
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)