def __init__(self): atexit.register(self.close) self.__timestamp = time.time() self.__projects = [] self.__projectByName = {} self.__fields = {} if findConfig("jasyproject"): header("Initializing project") try: self.addProject(getProjectFromPath(".")) except JasyError as err: outdent(True) error(err) raise JasyError("Critical: Could not initialize session!")
def __init__(self): atexit.register(self.close) self.__timestamp = time.time() self.__projects = [] self.__projectByName = {} self.__fields = {} if os.path.exists("jasyproject.json"): try: # TODO: Figure out repo type and branch/revision of current project version = None repo = None revision = None project = getProjectFromPath(".", version=version, repo=repo, revision=revision) self.addProject(project) except JasyError as jasyerr: outdent(True) error(jasyerr) raise JasyError("Critical: Could not initialize session!")
def create(name="myproject", origin=None, originVersion=None, skeleton=None, destination=None, session=None, **argv): """ Creates a new project from a defined skeleton or an existing project's root directory (only if there is a jasycreate config file). :param name: The name of the new created project :type name: string :param origin: Path or git url to the base project :type origin: string :param originVersion: Version of the base project from wich will be created. :type originVersion: string :param skeleton: Name of a defined skeleton. None for creating from root :type skeleton: string :param destination: Destination path for the new created project :type destination: string :param session: An optional session to use as origin project :type session: object """ if not validProjectName.match(name): raise UserError( "Invalid project name: %s (Use lowercase characters and numbers only for broadest compabibility)" % name) # # Initial Checks # # Figuring out destination folder if destination is None: destination = name destinationPath = os.path.abspath(os.path.expanduser(destination)) if os.path.exists(destinationPath): raise UserError( "Cannot create project %s in %s. File or folder exists!" % (name, destinationPath)) # Origin can be either: # 1) None, which means a skeleton from the current main project # 2) An repository URL # 3) A project name known inside the current session # 4) Relative or absolute folder path originPath = None originName = None if origin is None: originProject = session and session.getMain() if originProject is None: raise UserError( "Auto discovery failed! No Jasy projects registered!") originPath = originProject.getPath() originName = originProject.getName() originRevision = None elif Repository.isUrl(origin): Console.info("Using remote skeleton") tempDirectory = tempfile.TemporaryDirectory() originPath = os.path.join(tempDirectory.name, "clone") originUrl = origin Console.indent() originRevision = Repository.update(originUrl, originVersion, originPath) Console.outdent() if originRevision is None: raise UserError("Could not clone origin repository!") Console.debug("Cloned revision: %s" % originRevision) if findConfig(os.path.join( originPath, "jasycreate")) or os.path.isfile( os.path.join(originPath, "jasycreate.py")): originProject = None else: originProject = getProjectFromPath(originPath, session) originName = originProject.getName() else: originProject = session and session.getProjectByName(origin) originVersion = None originRevision = None if originProject is not None: originPath = originProject.getPath() originName = origin elif os.path.isdir(origin): originPath = origin if findConfig(os.path.join( originPath, "jasycreate")) or os.path.isfile( os.path.join(originPath, "jasycreate.py")): originProject = None else: originProject = getProjectFromPath(originPath, session) originName = originProject.getName() else: raise UserError("Invalid value for origin: %s" % origin) # Figure out the skeleton root folder if originProject is not None: skeletonDir = os.path.join( originPath, originProject.getConfigValue("skeletonDir", "skeleton")) else: skeletonDir = originPath if not os.path.isdir(skeletonDir): raise UserError('The project %s offers no skeletons!' % originName) # For convenience: Use first skeleton in skeleton folder if no other selection was applied if skeleton is None: if originProject is not None: skeleton = getFirstSubFolder(skeletonDir) else: skeleton = skeletonDir # Finally we have the skeleton path (the root folder to copy for our app) skeletonPath = os.path.join(skeletonDir, skeleton) if not os.path.isdir(skeletonPath): raise UserError('Skeleton %s does not exist in project "%s"' % (skeleton, originName)) # # Actual Work # # Prechecks done if originName: Console.info('Creating %s from %s %s...', Console.colorize(name, "bold"), Console.colorize(skeleton + " @", "bold"), Console.colorize(originName, "magenta")) else: Console.info('Creating %s from %s...', Console.colorize(name, "bold"), Console.colorize(skeleton, "bold")) Console.debug('Skeleton: %s', Console.colorize(skeletonPath, "grey")) Console.debug('Destination: %s', Console.colorize(destinationPath, "grey")) # Copying files to destination Console.info("Copying files...") shutil.copytree(skeletonPath, destinationPath) Console.debug("Files were copied successfully.") # Close origin project if originProject: originProject.close() # Change to directory before continuing os.chdir(destinationPath) # Create configuration file from question configs and custom scripts Console.info("Starting configuration...") config = Config() config.set("name", name) config.set("jasy.version", jasy.__version__) if originName: config.set("origin.name", originName) config.set("origin.version", originVersion) config.set("origin.revision", originRevision) config.set("origin.skeleton", os.path.basename(skeletonPath)) config.injectValues(**argv) if originProject is not None: config.readQuestions("jasycreate", optional=True) config.executeScript("jasycreate.py", optional=True) # Do actual replacement of placeholders massFilePatcher(destinationPath, config) Console.debug("Files were patched successfully.") # Done Console.info('Your application %s was created successfully!', Console.colorize(name, "bold"))
def create(name="myproject", origin=None, originVersion=None, skeleton=None, destination=None, session=None, **argv): """ Creates a new project from a defined skeleton or an existing project's root directory (only if there is a jasycreate config file). :param name: The name of the new created project :type name: string :param origin: Path or git url to the base project :type origin: string :param originVersion: Version of the base project from wich will be created. :type originVersion: string :param skeleton: Name of a defined skeleton. None for creating from root :type skeleton: string :param destination: Destination path for the new created project :type destination: string :param session: An optional session to use as origin project :type session: object """ if not validProjectName.match(name): raise UserError("Invalid project name: %s (Use lowercase characters and numbers only for broadest compabibility)" % name) # # Initial Checks # # Figuring out destination folder if destination is None: destination = name destinationPath = os.path.abspath(os.path.expanduser(destination)) if os.path.exists(destinationPath): raise UserError("Cannot create project %s in %s. File or folder exists!" % (name, destinationPath)) # Origin can be either: # 1) None, which means a skeleton from the current main project # 2) An repository URL # 3) A project name known inside the current session # 4) Relative or absolute folder path originPath = None originName = None if origin is None: originProject = session and session.getMain() if originProject is None: raise UserError("Auto discovery failed! No Jasy projects registered!") originPath = originProject.getPath() originName = originProject.getName() originRevision = None elif Repository.isUrl(origin): Console.info("Using remote skeleton") tempDirectory = tempfile.TemporaryDirectory() originPath = os.path.join(tempDirectory.name, "clone") originUrl = origin Console.indent() originRevision = Repository.update(originUrl, originVersion, originPath) Console.outdent() if originRevision is None: raise UserError("Could not clone origin repository!") Console.debug("Cloned revision: %s" % originRevision) if findConfig(os.path.join(originPath, "jasycreate")) or os.path.isfile(os.path.join(originPath, "jasycreate.py")): originProject = None else: originProject = getProjectFromPath(originPath, session) originName = originProject.getName() else: originProject = session and session.getProjectByName(origin) originVersion = None originRevision = None if originProject is not None: originPath = originProject.getPath() originName = origin elif os.path.isdir(origin): originPath = origin if findConfig(os.path.join(originPath, "jasycreate")) or os.path.isfile(os.path.join(originPath, "jasycreate.py")): originProject = None else: originProject = getProjectFromPath(originPath, session) originName = originProject.getName() else: raise UserError("Invalid value for origin: %s" % origin) # Figure out the skeleton root folder if originProject is not None: skeletonDir = os.path.join(originPath, originProject.getConfigValue("skeletonDir", "skeleton")) else: skeletonDir = originPath if not os.path.isdir(skeletonDir): raise UserError('The project %s offers no skeletons!' % originName) # For convenience: Use first skeleton in skeleton folder if no other selection was applied if skeleton is None: if originProject is not None: skeleton = getFirstSubFolder(skeletonDir) else: skeleton = skeletonDir # Finally we have the skeleton path (the root folder to copy for our app) skeletonPath = os.path.join(skeletonDir, skeleton) if not os.path.isdir(skeletonPath): raise UserError('Skeleton %s does not exist in project "%s"' % (skeleton, originName)) # # Actual Work # # Prechecks done if originName: Console.info('Creating %s from %s %s...', Console.colorize(name, "bold"), Console.colorize(skeleton + " @", "bold"), Console.colorize(originName, "magenta")) else: Console.info('Creating %s from %s...', Console.colorize(name, "bold"), Console.colorize(skeleton, "bold")) Console.debug('Skeleton: %s', Console.colorize(skeletonPath, "grey")) Console.debug('Destination: %s', Console.colorize(destinationPath, "grey")) # Copying files to destination Console.info("Copying files...") shutil.copytree(skeletonPath, destinationPath) Console.debug("Files were copied successfully.") # Close origin project if originProject: originProject.close() # Change to directory before continuing os.chdir(destinationPath) # Create configuration file from question configs and custom scripts Console.info("Starting configuration...") config = Config() config.set("name", name) config.set("jasy.version", jasy.__version__) if originName: config.set("origin.name", originName) config.set("origin.version", originVersion) config.set("origin.revision", originRevision) config.set("origin.skeleton", os.path.basename(skeletonPath)) config.injectValues(**argv) if originProject is not None: config.readQuestions("jasycreate", optional=True) config.executeScript("jasycreate.py", optional=True) # Do actual replacement of placeholders massFilePatcher(destinationPath, config) Console.debug("Files were patched successfully.") # Done Console.info('Your application %s was created successfully!', Console.colorize(name, "bold"))
def create(name="myproject", origin=None, skeleton=None, **argv): """Creates a new project""" header("Creating project %s" % name) if not validProjectName.match(name): raise JasyError("Invalid project name: %s" % name) # # Initial Checks # # Figuring out destination folder destinationPath = os.path.abspath(name) if os.path.exists(destinationPath): raise JasyError("Cannot create project in %s. File or folder exists!" % destinationPath) # Origin can be either: # 1) None, which means a skeleton from the current main project # 2) An repository URL # 3) A project name known inside the current session # 4) Relative or absolute folder path if origin is None: originProject = session.getMain() if originProject is None: raise JasyError("Auto discovery failed! No Jasy projects registered!") originPath = originProject.getPath() originName = originProject.getName() elif isRepository(origin): info("Using remote skeleton") tempDirectory = tempfile.TemporaryDirectory() originPath = os.path.join(tempDirectory.name, "clone") originUrl = origin originVersion = getKey(argv, "origin-version") indent() originRevision = updateRepository(originUrl, originVersion, originPath) outdent() if originRevision is None: raise JasyError("Could not clone origin repository!") debug("Cloned revision: %s" % originRevision) originProject = getProjectFromPath(originPath) originName = originProject.getName() else: originProject = session.getProjectByName(origin) if originProject is not None: originPath = originProject.getPath() originName = origin elif os.path.isdir(origin): originPath = origin originProject = getProjectFromPath(originPath) originName = originProject.getName() else: raise JasyError("Invalid value for origin: %s" % origin) # Figure out the skeleton root folder skeletonDir = os.path.join(originPath, originProject.getConfigValue("skeletonDir", "skeleton")) if not os.path.isdir(skeletonDir): raise JasyError("The project %s offers no skeletons!" % originName) # For convenience: Use first skeleton in skeleton folder if no other selection was applied if skeleton is None: skeleton = getFirstSubFolder(skeletonDir) # Finally we have the skeleton path (the root folder to copy for our app) skeletonPath = os.path.join(skeletonDir, skeleton) if not os.path.isdir(skeletonPath): raise JasyError('Skeleton %s does not exist in project "%s"' % (skeleton, originName)) # # Actual Work # # Prechecks done info( "Creating %s from %s %s...", colorize(name, "bold"), colorize(skeleton + " @", "bold"), colorize(originName, "magenta"), ) debug("Skeleton: %s", colorize(skeletonPath, "grey")) debug("Destination: %s", colorize(destinationPath, "grey")) # Copying files to destination info("Copying files...") shutil.copytree(skeletonPath, destinationPath) debug("Files were copied successfully.") # Build data for template substitution data = {} data.update(argv) data["name"] = name data["origin"] = originName data["skeleton"] = os.path.basename(skeletonPath) data["jasy"] = jasy.__version__ # Do actual replacement of placeholders massFilePatcher(destinationPath, data) debug("Files were patched successfully.") # Change to directory before continuing os.chdir(destinationPath) # Create configuration file from question configs and custom scripts info("Starting configuration...") config = Config() config.injectValues(**argv) config.readQuestions("jasycreate", optional=True) config.executeScript("jasycreate.py", optional=True) config.write("jasyscript.yaml") # Done info("Your application %s was created successfully!", colorize(name, "bold"))