Beispiel #1
0
    def test_copydir_indir(self):
        """Test copydir in a directory"""        
        fs1 = MemoryFS()
        fs2 = MemoryFS()
        self._make_fs(fs1)        
        utils.copydir(fs1, (fs2, "copy"))        
        self._check_fs(fs2.opendir("copy"))

        fs1 = TempFS()
        fs2 = TempFS()
        self._make_fs(fs1)        
        utils.copydir(fs1, (fs2, "copy"))        
        self._check_fs(fs2.opendir("copy"))
Beispiel #2
0
 def test_copydir_root(self):
     """Test copydir from root"""
     fs1 = MemoryFS()
     self._make_fs(fs1)        
     fs2 = MemoryFS()
     utils.copydir(fs1, fs2)        
     self._check_fs(fs2)
             
     fs1 = TempFS()
     self._make_fs(fs1)        
     fs2 = TempFS()
     utils.copydir(fs1, fs2)        
     self._check_fs(fs2)
Beispiel #3
0
def install(device_class, version, firmware_fs, dst_fs):
    """Install a firmware"""
    dst_path = join(device_class, str(version))
    if not dst_fs.exists(dst_path):
        dst_fs.makedir(dst_path, allow_recreate=True, recursive=True)
    install_fs = dst_fs.opendir(dst_path)
    copydir(firmware_fs, install_fs)
    install_path = dst_fs.getsyspath(dst_path)

    try:
        os.chmod(install_path, 0o0775)
    except:
        pass

    # Return install_path
    return install_path
Beispiel #4
0
def install(device_class, version, firmware_fs, dst_fs):
    """Install a firmware"""
    dst_path = join(device_class, str(version))
    if not dst_fs.exists(dst_path):
        dst_fs.makedir(dst_path, allow_recreate=True, recursive=True)
    install_fs = dst_fs.opendir(dst_path)
    copydir(firmware_fs, install_fs)
    install_path = dst_fs.getsyspath(dst_path)

    try:
        os.chmod(install_path, 0o0775)
    except:
        pass

    # Return install_path
    return install_path
Beispiel #5
0
    def _zip(self, destination=None):
        """Compresse a bagit file."""
        # Removes the final forwardslash if there is one
        destination = destination or cfg["ARCHIVER_TMPDIR"]
        if destination.endswith(os.path.sep):
            destination = destination[:-len(os.path.sep)]
        filename = os.path.join(destination, "{0}.zip".format(self.name))

        # Create and FS object
        with OSFS(self.folder) as to_zip_fs:
            with ZipFS(filename, mode='w') as zip_fs:
                copydir(to_zip_fs, zip_fs, overwrite=True)
            file_to_delete = os.path.basename(self.folder)
            to_delete_from = OSFS(os.path.dirname(self.folder))
            to_delete_from.removedir(file_to_delete, recursive=True,
                                     force=True)
        return filename
Beispiel #6
0
    def zip_bagit(self, path):
        """Compresse a bagit file."""
        # Removes the final forwardslash if there is one
        if path.endswith('/'):
            path = path[:-1]

        try:
            # Create and FS object
            with OSFS(path) as to_zip_fs:
                with ZipFS("%s.zip" % path, mode='w') as zip_fs:
                    copydir(to_zip_fs, zip_fs)
            self.remove_dir(path)
            return True

        except Exception as excep:
            raise ArchiverError("Zipping of %s failed. An '%s' occured" %
                                (path, str(type(excep).__name__)))
Beispiel #7
0
    def start_library(self):
        console = self.console

        from ...tools import get_moya_dir
        from os.path import join, abspath
        project_path = None
        if self.args.location is not None:
            library_path = self.args.location
        else:
            try:
                project_path = get_moya_dir(self.args.project_location)
            except:
                console.error("Please run 'moya start library' inside your project directory, or specifiy the -o switch")
                return False
            library_path = abspath(join(project_path, './local/'))

        cfg = None
        if not self.args.location and project_path:
            from ... import build
            cfg = build.read_config(project_path, self.get_settings())

        if not self.args.acceptdefaults:
            console.table([[Cell("Moya Library Wizard", bold=True, fg="green", center=True)],
                          ["""This will ask you a few questions, then create a new library in your Moya project based on your answers.

Default values are shown in grey (simply hit return to accept defaults). Some defaults may be taken from your ".bashrc" file, if it exists.
"""]])
        author = self.get_author_details()
        library = {}
        library["title"] = LibraryTitle.ask(console, default=self.args.title)
        longname = self.args.longname or make_name(author["organization"], library["title"])
        longname = library["longname"] = LibraryLongName.ask(console, default=longname)
        library["url"] = LibraryURL.ask(console, default="")
        library["namespace"] = LibraryNamespace.ask(console, default="")
        mount = None
        appname = None

        do_mount = DoMount.ask(console, default="yes")
        if do_mount:
            mount = Mount.ask(console, default=self.args.mount or "/{}/".format(make_name(library["title"])))
            appname = AppName.ask(console, default=self.args.name or make_name(library["title"]))

        data = dict(author=author,
                    library=library,
                    timezone=self.get_timezone())

        actions = []

        from ...command.sub import library_template
        from fs.memoryfs import MemoryFS
        from fs.opener import fsopendir
        memfs = MemoryFS()
        templatebuilder.compile_fs_template(memfs,
                                            library_template.template,
                                            data=data)
        dest_fs = fsopendir(join(library_path, library["longname"]), create_dir=True, writeable=True)

        continue_overwrite = 'overwrite'
        if not dest_fs.isdirempty('.'):
            if self.args.force:
                continue_overwrite = 'overwrite'
            elif self.args.new:
                continue_overwrite = 'new'
            else:
                continue_overwrite = DirNotEmpty.ask(console, default="cancel")

        if continue_overwrite != 'cancel':
            if continue_overwrite == 'overwrite':
                from fs.utils import copydir
                copydir(memfs, dest_fs)
                actions.append("Written library files to {}".format(dest_fs.getsyspath('.')))
            elif continue_overwrite == 'new':
                files_copied = copy_new(memfs, dest_fs)
                table = [[
                         Cell("{} new file(s) written".format(len(files_copied)), fg="green", bold=True, center=True),
                         ]]
                for path in files_copied:
                    table.append([Cell(dest_fs.desc(path), bold=True, fg="black")])
                console.table(table)
                return 0

            if cfg:
                project_cfg = cfg['project']
                location = project_cfg['location']
                server_name = "main"

                if location:
                    with fsopendir(project_path) as project_fs:
                        with project_fs.opendir(location) as server_fs:
                            from lxml.etree import fromstring, ElementTree, parse
                            from lxml.etree import XML, Comment
                            server_xml_path = server_fs.getsyspath(project_cfg['startup'])
                            root = parse(server_xml_path)
                            import_tag = XML('<import location="./local/{longname}" />\n\n'.format(**library))
                            import_tag.tail = "\n"
                            install_tag = None

                            if mount:
                                tag = '<install name="{appname}" lib="{longname}" mount="{mount}" />'
                            else:
                                tag = '<install name="{appname}" lib="{longname}" />'
                            install_tag = XML(tag.format(appname=appname,
                                                         longname=longname,
                                                         mount=mount))
                            install_tag.tail = "\n\n"

                            def has_child(node, tag, **attribs):
                                for el in node.findall(tag):
                                    #items = dict(el.items())
                                    if all(el.get(k, None) == v for k, v in attribs.items()):
                                        return True
                                return False

                            for server in root.findall("{{http://moyaproject.com}}server[@docname='{}']".format(server_name)):
                                add_import_tag = not has_child(server, "{http://moyaproject.com}import", location="./local/{}".format(longname))
                                add_install_tag = not has_child(server, "{http://moyaproject.com}install", lib=longname) and install_tag is not None

                                if add_import_tag or add_install_tag:
                                    comment = Comment("Added by 'moya start library'")
                                    comment.tail = "\n"
                                    server.append(comment)
                                if add_import_tag:
                                    server.append(import_tag)
                                    actions.append("Added <import> tag")
                                if add_install_tag:
                                    server.append(install_tag)
                                    actions.append("Added <install> tag")
                                    if mount:
                                        actions.append("Mounted application on {}".format(mount))

                            root.write(server_xml_path)

            table = [[Cell("Library files written successfully!", fg="green", bold=True, center=True)]]

            actions_text = "\n".join(" * " + action for action in actions)
            table.append([Cell(actions_text, fg="blue", bold=True)])
            table.append(["""A new library has been added to the project, containing some simple example functionality.\nSee http://moyaproject.com/docs/creatinglibraries/ for more information."""])
            console.table(table)

            return 0

        console.text("No project files written.", fg="red", bold=True).nl()
        return -1
Beispiel #8
0
    def start_project(self):
        console = self.console

        if not self.args.acceptdefaults:
            console.table([[Cell("Moya Project Wizard", bold=True, fg="green", center=True)],
                          ["""This will ask you a few questions, then create a new Moya project based on your answers.

Default values are shown in blue (hit return to accept defaults). Some defaults may be taken from your ".moyarc" file, if it exists."""]])

        author = self.get_author_details()
        project = {}
        project["title"] = ProjectTitle.ask(console, default=self.args.title)
        longname = make_name(author["organization"], project["title"])
        project["database"] = Database.ask(console, default='y')
        if project["database"]:
            project["auth"] = Auth.ask(console, default='y')
            project['signup'] = Signup.ask(console, default='y')
            project["pages"] = Pages.ask(console, default='y')
            project["feedback"] = Feedback.ask(console, default='y')
            project["blog"] = Blog.ask(console, default='y')
        project["comments"] = project.get("feedback", False) or project.get("pages", False)
        project["wysihtml5"] = project.get("feedback", False) or project.get("pages", False)
        project['jsonrpc'] = JSONRPC.ask(console, default='y')

        dirname = longname.split('.', 1)[-1].replace('.', '_')
        dirname = ProjectDirName.ask(console, default="./" + dirname)

        data = dict(author=author,
                    project=project,
                    timezone=self.get_timezone())

        from ...command.sub import project_template
        from fs.memoryfs import MemoryFS
        from fs.opener import fsopendir
        memfs = MemoryFS()
        templatebuilder.compile_fs_template(memfs,
                                            project_template.template,
                                            data=data)

        dest_fs = fsopendir(self.args.location or dirname, create_dir=True, writeable=True)
        continue_overwrite = 'overwrite'
        if not dest_fs.isdirempty('.'):
            if self.args.force:
                continue_overwrite = 'overwrite'
            elif self.args.new:
                continue_overwrite = 'new'
            else:
                continue_overwrite = DirNotEmpty.ask(console, default="cancel")

        if continue_overwrite == 'overwrite':
            from fs.utils import copydir
            copydir(memfs, dest_fs)
            console.table([[Cell("Project files written successfully!", fg="green", bold=True, center=True)],
                          ["""See readme.txt in the project directory for the next steps.\n\nBrowse to http://moyaproject.com/gettingstarted/ if you need further help."""]])
            return 0
        elif continue_overwrite == 'new':
            files_copied = copy_new(memfs, dest_fs)
            table = [[
                     Cell("{} new file(s) written".format(len(files_copied)), fg="green", bold=True, center=True),
                     ]]
            for path in files_copied:
                table.append([Cell(dest_fs.desc(path), bold=True, fg="black")])
            console.table(table)
            return 0

        console.text("No project files written.", fg="red", bold=True).nl()
        return -1
Beispiel #9
0
    def run(self):
        args = self.args
        application = WSGIApplication(self.location,
                                      self.get_settings(),
                                      args.server,
                                      disable_autoreload=True)
        archive = application.archive

        filesystems = archive.filesystems

        fs = None
        if args.fs:
            try:
                fs = filesystems[args.fs]
            except KeyError:
                self.console.error("No filesystem called '%s'" % args.fs)
                return -1

        if args.tree is not None:
            if fs is None:
                self.console.error("Filesystem required")
                return -1
            with fs.opendir(args.tree) as tree_fs:
                tree_fs.tree()
            return

        if args.listdir:
            if fs is None:
                self.console.error("Filesystem required")
                return -1

            dir_fs = fs.opendir(args.listdir)
            file_paths = dir_fs.listdir(files_only=True)
            dir_paths = dir_fs.listdir(dirs_only=True)
            _ls(self.console, file_paths, dir_paths)

        elif args.cat:
            if fs is None:
                self.console.error("Filesystem required")
                return -1
            contents = fs.getcontents(args.cat)
            self.console.cat(contents, args.cat)

        elif args.open:
            if fs is None:
                self.console.error("Filesystem required")
                return -1

            filepath = fs.getsyspath(args.open, allow_none=True)
            if filepath is None:
                self.console.error("No system path for '%s' in filesystem '%s'" % (args.open, args.fs))
                return -1

            import subprocess
            if os.name == 'mac':
                subprocess.call(('open', filepath))
            elif os.name == 'nt':
                subprocess.call(('start', filepath), shell=True)
            elif os.name == 'posix':
                subprocess.call(('xdg-open', filepath))
            else:
                self.console.error("Don't know how to open files on this platform (%s)" % os.name)

        elif args.syspath:
            if fs is None:
                self.console.error("Filesystem required (use -cat FILESYSTEM)")
                return -1
            if not fs.exists(args.syspath):
                self.console.error("No file called '%s' found in filesystem '%s'" % (args.syspath, args.fs))
                return -1
            syspath = fs.getsyspath(args.syspath, allow_none=True)
            if syspath is None:
                self.console.error("No system path for '%s' in filesystem '%s'" % (args.syspath, args.fs))
            else:
                self.console(syspath).nl()

        elif args.copy:
            if len(args.copy) == 1:
                src = '/'
                dst = args.copy[0]
            elif len(args.copy) == 2:
                src, dst = args.copy
            else:
                self.console.error("--copy requires 1 or 2 arguments")
                return -1

            if fs.isdir(src):
                src_fs = fs.opendir(src)
                dst_fs = fsopendir(dst, create_dir=True)

                if not args.force and not dst_fs.isdirempty('/'):
                    response = raw_input("'%s' is not empty. Copying may overwrite directory contents. Continue? " % dst)
                    if response.lower() not in ('y', 'yes'):
                        return 0

                from fs.utils import copydir
                copydir(src_fs, dst_fs)
            else:
                with fs.open(src, 'rb') as read_f:
                    with open(dst, 'wb') as write_f:
                        while 1:
                            chunk = read_f.read(16384)
                            if not chunk:
                                break
                            write_f.write(chunk)

        else:
            table = [[Cell("Name", bold=True),
                      Cell("Type", bold=True),
                      Cell("Location", bold=True)]]

            if fs is None:
                list_filesystems = filesystems.items()
            else:
                list_filesystems = [(args.fs, fs)]

            for name, fs in sorted(list_filesystems):

                if isinstance(fs, MultiFS):
                    location = '\n'.join(mount_fs.desc('/') for mount_fs in fs.fs_sequence)
                    fg = "yellow"
                elif isinstance(fs, MountFS):
                    mount_desc = []
                    for path, dirmount in fs.mount_tree.items():
                        mount_desc.append('%s->%s' % (path, dirmount.fs.desc('/')))
                    location = '\n'.join(mount_desc)
                    fg = "magenta"
                else:
                    syspath = fs.getsyspath('/', allow_none=True)
                    if syspath is not None:
                        location = syspath
                        fg = "green"
                    else:
                        try:
                            location = fs.desc('/')
                        except FSError as e:
                            location = text_type(e)
                            fg = "red"
                        else:
                            fg = "blue"
                table.append([Cell(name),
                             Cell(type(fs).__name__),
                             Cell(location, bold=True, fg=fg)
                              ])
            self.console.table(table, header=True)
Beispiel #10
0
    def run(self):
        args = self.args
        application = WSGIApplication(self.location,
                                      self.get_settings(),
                                      args.server,
                                      disable_autoreload=True,
                                      master_settings=self.master_settings)
        archive = application.archive

        filesystems = archive.filesystems

        fs = None
        if args.fs:
            try:
                fs = filesystems[args.fs]
            except KeyError:
                self.console.error("No filesystem called '%s'" % args.fs)
                return -1

        if args.tree is not None:
            if fs is None:
                self.console.error("Filesystem required")
                return -1
            with fs.opendir(args.tree) as tree_fs:
                tree_fs.tree()
            return

        if args.listdir:
            if fs is None:
                self.console.error("Filesystem required")
                return -1

            dir_fs = fs.opendir(args.listdir)
            file_paths = dir_fs.listdir(files_only=True)
            dir_paths = dir_fs.listdir(dirs_only=True)
            _ls(self.console, file_paths, dir_paths)

        elif args.cat:
            if fs is None:
                self.console.error("Filesystem required")
                return -1
            contents = fs.getcontents(args.cat)
            self.console.cat(contents, args.cat)

        elif args.open:
            if fs is None:
                self.console.error("Filesystem required")
                return -1

            filepath = fs.getsyspath(args.open, allow_none=True)
            if filepath is None:
                self.console.error(
                    "No system path for '%s' in filesystem '%s'" %
                    (args.open, args.fs))
                return -1

            import subprocess
            if os.name == 'mac':
                subprocess.call(('open', filepath))
            elif os.name == 'nt':
                subprocess.call(('start', filepath), shell=True)
            elif os.name == 'posix':
                subprocess.call(('xdg-open', filepath))
            else:
                self.console.error(
                    "Moya doesn't know how to open files on this platform (%s)"
                    % os.name)

        elif args.syspath:
            if fs is None:
                self.console.error("Filesystem required")
                return -1
            if not fs.exists(args.syspath):
                self.console.error(
                    "No file called '%s' found in filesystem '%s'" %
                    (args.syspath, args.fs))
                return -1
            syspath = fs.getsyspath(args.syspath, allow_none=True)
            if syspath is None:
                self.console.error(
                    "No system path for '%s' in filesystem '%s'" %
                    (args.syspath, args.fs))
            else:
                self.console(syspath).nl()

        elif args.copy:
            if fs is None:
                self.console.error("Filesystem required")
                return -1
            if len(args.copy) == 1:
                src = '/'
                dst = args.copy[0]
            elif len(args.copy) == 2:
                src, dst = args.copy
            else:
                self.console.error("--copy requires 1 or 2 arguments")
                return -1

            if fs.isdir(src):
                src_fs = fs.opendir(src)
                dst_fs = fsopendir(dst, create_dir=True)

                if not args.force and not dst_fs.isdirempty('/'):
                    response = raw_input(
                        "'%s' is not empty. Copying may overwrite directory contents. Continue? "
                        % dst)
                    if response.lower() not in ('y', 'yes'):
                        return 0

                from fs.utils import copydir
                copydir(src_fs, dst_fs)
            else:
                with fs.open(src, 'rb') as read_f:
                    if os.path.isdir(dst):
                        dst = os.path.join(dst, os.path.basename(src))
                    try:
                        os.makedirs(dst)
                        with open(dst, 'wb') as write_f:
                            while 1:
                                chunk = read_f.read(16384)
                                if not chunk:
                                    break
                                write_f.write(chunk)
                    except IOError as e:
                        self.error('unable to write to {}'.format(dst))

        elif args.extract:
            if fs is None:
                self.console.error("Filesystem required")
                return -1
            src_path, dst_dir_path = args.extract
            src_fs = fs
            dst_fs = fsopendir(dst_dir_path, create_dir=True)

            if not args.force and dst_fs.exists(src_path):
                response = raw_input(
                    "'%s' exists. Do you want to overwrite? " % src_path)
                if response.lower() not in ('y', 'yes'):
                    return 0

            dst_fs.makedir(dirname(src_path),
                           recursive=True,
                           allow_recreate=True)
            with src_fs.open(src_path, 'rb') as read_file:
                dst_fs.setcontents(src_path, read_file)

        else:
            table = [[
                Cell("Name", bold=True),
                Cell("Type", bold=True),
                Cell("Location", bold=True)
            ]]

            if fs is None:
                list_filesystems = filesystems.items()
            else:
                list_filesystems = [(args.fs, fs)]

            for name, fs in sorted(list_filesystems):

                if isinstance(fs, MultiFS):
                    location = '\n'.join(
                        mount_fs.desc('/') for mount_fs in fs.fs_sequence)
                    fg = "yellow"
                elif isinstance(fs, MountFS):
                    mount_desc = []
                    for path, dirmount in fs.mount_tree.items():
                        mount_desc.append('%s->%s' %
                                          (path, dirmount.fs.desc('/')))
                    location = '\n'.join(mount_desc)
                    fg = "magenta"
                else:
                    syspath = fs.getsyspath('/', allow_none=True)
                    if syspath is not None:
                        location = syspath
                        fg = "green"
                    else:
                        try:
                            location = fs.desc('/')
                        except FSError as e:
                            location = text_type(e)
                            fg = "red"
                        else:
                            fg = "blue"
                table.append([
                    Cell(name),
                    Cell(type(fs).__name__),
                    Cell(location, bold=True, fg=fg)
                ])
            self.console.table(table, header=True)
Beispiel #11
0
 def sync_dirs(self):
     for path in self.userfs.walkdirs():
         if not self.remotefs.exists(path):
             copydir((self.userfs, path), (self.remotefs, path))
             print(term.green + " " * 4 + "copied " + path + term.normal)
Beispiel #12
0
            else:
                write('%s %s' %
                      (wrap_prefix(prefix + '--'), wrap_filename(item)))

        return len(dir_listing)

    print_dir(fs, path)


if __name__ == "__main__":
    #from osfs import *
    #fs = OSFS('~/copytest')

    #from memoryfs import *
    #m = MemoryFS()
    #m.makedir('maps')

    #copydir((fs, 'maps'), (m, 'maps'))

    #from browsewin import browse
    #browse(m)

    from osfs import *
    #f = OSFS('/home/will/projects')
    f = OSFS('/home/will/tests')
    from fs.memoryfs import MemoryFS
    mem = MemoryFS()
    from fs.utils import copydir
    copydir(f, mem)
    print_fs(mem)
Beispiel #13
0
 def sync_dirs(self):
     for path in self.userfs.walkdirs():
         if not self.remotefs.exists(path):
             copydir((self.userfs, path), (self.remotefs, path))
             print(term.green + " " * 4 + "copied " + path + term.normal)
Beispiel #14
0
                
        return len(dir_listing)
                
    print_dir(fs, path)
            
            


if __name__ == "__main__":
    #from osfs import *
    #fs = OSFS('~/copytest')

    #from memoryfs import *
    #m = MemoryFS()
    #m.makedir('maps')

    #copydir((fs, 'maps'), (m, 'maps'))

    #from browsewin import browse
    #browse(m)

    from osfs import *
    #f = OSFS('/home/will/projects')
    f=OSFS('/home/will/tests')
    from fs.memoryfs import MemoryFS
    mem = MemoryFS()
    from fs.utils import copydir
    copydir(f, mem)
    print_fs(mem)
    
Beispiel #15
0
    def start_library(self):
        console = self.console

        from ...tools import get_moya_dir
        from os.path import join, abspath
        project_path = None
        if self.args.location is not None:
            library_path = self.args.location
        else:
            try:
                project_path = get_moya_dir(self.args.project_location)
            except:
                console.error("Please run 'moya start library' inside your project directory, or specifiy the -o switch")
                return False
            library_path = abspath(join(project_path, './local/'))

        cfg = None
        if not self.args.location and project_path:
            from ... import build
            cfg = build.read_config(project_path, self.get_settings())

        if not self.args.acceptdefaults:
            console.table([[Cell("Moya Library Wizard", bold=True, fg="green", center=True)],
                          ["""This will ask you a few questions, then create a new library in your Moya project based on your answers.

Default values are shown in grey (simply hit return to accept defaults). Some defaults may be taken from your ".bashrc" file, if it exists.
"""]])
        author = self.get_author_details()
        library = {}
        library["title"] = LibraryTitle.ask(console, default=self.args.title)
        longname = self.args.longname or make_name(author["organization"], library["title"])
        longname = library["longname"] = LibraryLongName.ask(console, default=longname)
        library["url"] = LibraryURL.ask(console, default="")
        library["namespace"] = LibraryNamespace.ask(console, default="")
        mount = None
        appname = None

        do_mount = DoMount.ask(console, default="yes")
        if do_mount:
            mount = Mount.ask(console, default=self.args.mount or "/{}/".format(make_name(library["title"])))
            appname = AppName.ask(console, default=self.args.name or make_name(library["title"]))

        data = dict(author=author,
                    library=library,
                    timezone=self.get_timezone())

        actions = []

        from ...command.sub import library_template
        from fs.memoryfs import MemoryFS
        from fs.opener import fsopendir
        memfs = MemoryFS()
        templatebuilder.compile_fs_template(memfs,
                                            library_template.template,
                                            data=data)
        dest_fs = fsopendir(join(library_path, library["longname"]), create_dir=True, writeable=True)

        continue_overwrite = 'overwrite'
        if not dest_fs.isdirempty('.'):
            if self.args.force:
                continue_overwrite = 'overwrite'
            elif self.args.new:
                continue_overwrite = 'new'
            else:
                continue_overwrite = DirNotEmpty.ask(console, default="cancel")

        if continue_overwrite != 'cancel':
            if continue_overwrite == 'overwrite':
                from fs.utils import copydir
                copydir(memfs, dest_fs)
                actions.append("Written library files to {}".format(dest_fs.getsyspath('.')))
            elif continue_overwrite == 'new':
                files_copied = copy_new(memfs, dest_fs)
                table = [[
                         Cell("{} new file(s) written".format(len(files_copied)), fg="green", bold=True, center=True),
                         ]]
                for path in files_copied:
                    table.append([Cell(dest_fs.desc(path), bold=True, fg="black")])
                console.table(table)
                return 0

            if cfg:
                project_cfg = cfg['project']
                location = project_cfg['location']
                server_name = "main"

                if location:
                    with fsopendir(project_path) as project_fs:
                        with project_fs.opendir(location) as server_fs:
                            from lxml.etree import fromstring, ElementTree, parse
                            from lxml.etree import XML, Comment
                            server_xml_path = server_fs.getsyspath(project_cfg['startup'])
                            root = parse(server_xml_path)
                            import_tag = XML('<import location="./local/{longname}" />\n\n'.format(**library))
                            import_tag.tail = "\n"
                            install_tag = None

                            if mount:
                                tag = '<install name="{appname}" lib="{longname}" mount="{mount}" />'
                            else:
                                tag = '<install name="{appname}" lib="{longname}" />'
                            install_tag = XML(tag.format(appname=appname,
                                                         longname=longname,
                                                         mount=mount))
                            install_tag.tail = "\n\n"

                            def has_child(node, tag, **attribs):
                                for el in node.findall(tag):
                                    #items = dict(el.items())
                                    if all(el.get(k, None) == v for k, v in attribs.items()):
                                        return True
                                return False

                            for server in root.findall("{{http://moyaproject.com}}server[@docname='{}']".format(server_name)):
                                add_import_tag = not has_child(server, "{http://moyaproject.com}import", location="./local/{}".format(longname))
                                add_install_tag = not has_child(server, "{http://moyaproject.com}install", lib=longname) and install_tag is not None

                                if add_import_tag or add_install_tag:
                                    comment = Comment("Added by 'moya start library'")
                                    comment.tail = "\n"
                                    server.append(comment)
                                if add_import_tag:
                                    server.append(import_tag)
                                    actions.append("Added <import> tag")
                                if add_install_tag:
                                    server.append(install_tag)
                                    actions.append("Added <install> tag")
                                    if mount:
                                        actions.append("Mounted application on {}".format(mount))

                            root.write(server_xml_path)

            table = [[Cell("Library files written successfully!", fg="green", bold=True, center=True)]]

            actions_text = "\n".join(" * " + action for action in actions)
            table.append([Cell(actions_text, fg="blue", bold=True)])
            table.append(["""A new library has been added to the project, containing some simple example functionality.\nSee http://moyaproject.com/docs/creatinglibraries/ for more information."""])
            console.table(table)

            return 0

        console.text("No project files written.", fg="red", bold=True).nl()
        return -1
Beispiel #16
0
    def start_project(self):
        console = self.console

        if not self.args.acceptdefaults:
            console.table([[Cell("Moya Project Wizard", bold=True, fg="green", center=True)],
                          ["""This will ask you a few questions, then create a new Moya project based on your answers.

Default values are shown in blue (hit return to accept defaults). Some defaults may be taken from your ".moyarc" file, if it exists."""]])

        author = self.get_author_details()
        project = {}
        project["title"] = ProjectTitle.ask(console, default=self.args.title)
        longname = make_name(author["organization"], project["title"])
        project["database"] = Database.ask(console, default='y')
        if project["database"]:
            project["auth"] = Auth.ask(console, default='y')
            project['signup'] = Signup.ask(console, default='y')
            project["pages"] = Pages.ask(console, default='y')
            project["feedback"] = Feedback.ask(console, default='y')
            project["blog"] = Blog.ask(console, default='y')
        project["comments"] = project.get("feedback", False) or project.get("pages", False)
        project["wysihtml5"] = project.get("feedback", False) or project.get("pages", False)
        project['jsonrpc'] = JSONRPC.ask(console, default='y')

        dirname = longname.split('.', 1)[-1].replace('.', '_')
        dirname = ProjectDirName.ask(console, default="./" + dirname)

        data = dict(author=author,
                    project=project,
                    timezone=self.get_timezone(),
                    secret=make_secret())

        from ...command.sub import project_template
        from fs.memoryfs import MemoryFS
        from fs.opener import fsopendir
        memfs = MemoryFS()
        templatebuilder.compile_fs_template(memfs,
                                            project_template.template,
                                            data=data)

        dest_fs = fsopendir(self.args.location or dirname, create_dir=True, writeable=True)
        continue_overwrite = 'overwrite'
        if not dest_fs.isdirempty('.'):
            if self.args.force:
                continue_overwrite = 'overwrite'
            elif self.args.new:
                continue_overwrite = 'new'
            else:
                continue_overwrite = DirNotEmpty.ask(console, default="cancel")

        if continue_overwrite == 'overwrite':
            from fs.utils import copydir
            copydir(memfs, dest_fs)
            console.table([[Cell("Project files written successfully!", fg="green", bold=True, center=True)],
                          ["""See readme.txt in the project directory for the next steps.\n\nBrowse to http://moyaproject.com/gettingstarted/ if you need further help."""]])
            return 0
        elif continue_overwrite == 'new':
            files_copied = copy_new(memfs, dest_fs)
            table = [[
                     Cell("{} new file(s) written".format(len(files_copied)), fg="green", bold=True, center=True),
                     ]]
            for path in files_copied:
                table.append([Cell(dest_fs.desc(path), bold=True, fg="black")])
            console.table(table)
            return 0

        console.text("No project files written.", fg="red", bold=True).nl()
        return -1