def _make_dirs(): directories = config.TEMPLATE_DIRS_CREATE logger.debug(u"make dirs {0}".format(u','.join(directories))) for directory in directories: if not os.path.exists(directory): os.mkdir(directory)
def _init_template(self): logger.debug("gen %s file for start, stop ... actions" % self.project_name) with codecs.open("template.py", "r", "utf-8") as tfp: content = tfp.read() with codecs.open(self.project_name, "w", "utf-8") as pfp: content = content.replace("{{template}}", self.project_name) pfp.write(content) os.chmod(self.project_name, 0o740) os.remove("template.py")
def _init_projects(self): time_start = time.time() logger.info("init project {0}".format(self.project_name)) logger.debug("copy from {0} to {1}".format(self.template_dir, self.work_dir)) shutil.copytree(self.template_dir, self.work_dir) os.chdir(self.work_dir) self._make_dirs() self._init_config() self._init_template() self._init_dev() self._init_db() self._init_git() self._init_venv() self._clean_project() logger.info("init project success using %.3f seconds !!!" % (time.time() - time_start))
def _init_config(self): # init config.py logger.debug("make config {0}".format(config.TEMPLATE_CONF_NAME)) config_dict = self._get_project_config() config_content = self.render_config(config_dict, fmt_dict=self.fmt_dict) with codecs.open(config.TEMPLATE_CONF_NAME, "w", "utf-8") as cfp: cfp.write(config_content) # init instance/config.py instance_config = "instance/{0}".format(config.TEMPLATE_CONF_NAME) logger.debug("make instance config {0}".format(instance_config)) if "instance/" in config.TEMPLATE_DIRS_CREATE: instance_dict = config.TEMPLATE_INSTANCE config_content = self.render_config(instance_dict) with codecs.open(instance_config, "w", "utf-8") as ifp: ifp.write(config_content) # init gun.py logger.debug("make config {0}".format(config.TEMPLATE_GUN_CONF)) config_content = self.render_config( config.TEMPLATE_GUNICORN, {'workers': self._process_gun_workers}) with codecs.open(config.TEMPLATE_GUN_CONF, "w", "utf-8") as gfp: gfp.write(config_content)
def _process_gun_workers(key, workers): import_lines, config_lines = list(), list() if isinstance(workers, int) or (isinstance(workers, basestring) and workers.isdigit()): config_lines.append('{0} = {1}\n'.format(key, workers)) else: try: wp_cpu = int(workers.split("*")[0]) except Exception as e: logger.debug(str(e)) wp_cpu = 2 if wp_cpu < 1 or wp_cpu > 10: wp_cpu = 2 import_lines.append("import multiprocessing\n") config_lines.append( "{0} = multiprocessing.cpu_count() * {1} \n".format( key, wp_cpu)) return import_lines, config_lines
def _init_venv(): logger.debug("make venv") out = check_output("virtualenv --no-site-packages venv", shell=True) logger.debug(out) out = check_output("venv/bin/pip install -r requirements.txt", shell=True) logger.debug(out) return True
def _init_git(self): logger.debug("init git") try: out = check_output( "git init && git add . && git commit -m 'init {0}'".format( self.project_name), shell=True) logger.debug(out) except Exception as e: logger.debug(str(e)) logger.info("git not found, git init ignore")
def init(name, version, owner, email): logger.debug("raw input project name: {0}, version {1}, owner: {2}, email: {3}".format( name, version, owner, email )) do_init(name, version, owner, email)
def open_debug(ctx, param, value): if not value or ctx.resilient_parsing: return logger.set_level(DEBUG) logger.debug('debug mod opened')