예제 #1
0
    def extract_files(self, zip_file):
        """Extract all files to a temp location"""

        sys.stdout.write("*\n")
        sys.stdout.write("* temp location == %s\n" % self.working_dir)
        sys.stdout.write(
            "* Extracting all files to a temporary location; please wait...")
        sys.stdout.flush()

        # Speedup debug by not extracting when path exists and it's not empty.
        #   Works because we don't use a randomly generated temp name in debug mode.
        if settings.DEBUG and os.path.exists(self.working_dir + "/install.sh"):
            sys.stdout.write("** NOTE ** NOT EXTRACTING IN DEBUG MODE")
            return

        try:
            system_specific_unzipping(
                zip_file=zip_file,
                dest_dir=self.working_dir,
                callback=self._callback_unzip,
            )
            sys.stdout.write("\n")
        except Exception as e:
            raise CommandError("Error unzipping %s: %s" % (zip_file, e))

        # Error checking (successful unpacking would skip all the following logic.)
        if not os.path.exists(self.working_dir + "/kalite/"):
            subdirs = os.listdir(self.working_dir)

            if len(subdirs) == 1:
                # This happens if zip was downloaded from git, rather than being created through the zip_kalite command.
                self.working_dir += "/" + subdirs[0] + "/"
                sys.stdout.write(
                    "Note: found a git-based package.  Updating working dir to %s\n"
                    % self.working_dir)

            else:
                # Unexpected situation: no kalite dir, and more than one directory.  What could it be?
                raise CommandError(
                    "Expected %s to exist, but it doesn't.  Unknown failure in extraction; exiting."
                    % (self.working_dir + "/kalite/"))
예제 #2
0
    def extract_files(self, zip_file):
        """Extract all files to a temp location"""

        sys.stdout.write("*\n")
        sys.stdout.write("* temp location == %s\n" % self.working_dir)
        sys.stdout.write("* Extracting all files to a temporary location; please wait...")
        sys.stdout.flush()

        # Speedup debug by not extracting when path exists and it's not empty.
        #   Works because we don't use a randomly generated temp name in debug mode.
        if settings.DEBUG and os.path.exists(self.working_dir + "/install.sh"):
            sys.stdout.write("** NOTE ** NOT EXTRACTING IN DEBUG MODE")
            return

        try:
            system_specific_unzipping(
                zip_file=zip_file,
                dest_dir=self.working_dir,
                callback = self._callback_unzip,
            )
            sys.stdout.write("\n")
        except Exception as e:
            raise CommandError("Error unzipping %s: %s" % (zip_file, e))

        # Error checking (successful unpacking would skip all the following logic.)
        if not os.path.exists(self.working_dir + "/kalite/"):
            subdirs = os.listdir(self.working_dir)

            if len(subdirs) == 1:
                # This happens if zip was downloaded from git, rather than being created through the zip_kalite command.
                self.working_dir += "/" + subdirs[0] + "/"
                sys.stdout.write("Note: found a git-based package.  Updating working dir to %s\n" % self.working_dir)

            else:
                # Unexpected situation: no kalite dir, and more than one directory.  What could it be?
                raise CommandError("Expected %s to exist, but it doesn't.  Unknown failure in extraction; exiting." % (self.working_dir + "/kalite/"))
def install_from_package(data_json_file, signature_file, zip_file, dest_dir=None, install_files=[]):
    """
    NOTE: This docstring (below this line) will be dumped as a README file.
    Congratulations on downloading KA Lite!  These instructions will help you install KA Lite.
    """
    import glob
    import os
    import shutil
    import sys

    # Make the true paths
    src_dir = os.path.dirname(__file__) or os.getcwd()  # necessary on Windows
    data_json_file = os.path.join(src_dir, data_json_file)
    signature_file = os.path.join(src_dir, signature_file)
    zip_file = os.path.join(src_dir, zip_file)

    # Validate the unpacked files
    for file in [data_json_file, signature_file, zip_file]:
        if not os.path.exists(file):
            raise Exception("Could not find expected file from zip package: %s" % file)

    # get the destination directory
    while not dest_dir or not os.path.exists(dest_dir) or raw_input("%s: Directory exists; install? [Y/n] " % dest_dir) not in ["", "y", "Y"]:
        if dest_dir and raw_input("%s: Directory does not exist; Create and install? [y/N] " % dest_dir) in ["y","Y"]:
            try:
                os.makedirs(os.path.realpath(dest_dir)) # can't use ensure_dir; external dependency.
                break
            except Exception as e:
                sys.stderr.write("Failed to create dest dir (%s): %s\n" % (dest_dir, e))
        dest_dir = raw_input("Please enter the directory where you'd like to install KA Lite (blank=%s): " % src_dir) or src_dir

    # unpack the inner zip to the destination
    system_specific_unzipping(zip_file, dest_dir)
    sys.stdout.write("\n")

    # Copy, so that if the installation fails, it can be restarted
    shutil.copy(data_json_file, os.path.join(dest_dir, "kalite/static/data/"))

    # Run the setup/start scripts
    files = [f for f in glob.glob(os.path.join(dest_dir, "setup*%s" % system_script_extension())) if not "from_zip" in f]
    return_code = os.system('"%s"' % files[0])
    if return_code:
        sys.stderr.write("Failed to set up KA Lite: exit-code = %s" % return_code)
        sys.exit(return_code)
    return_code = os.system('"%s"' % os.path.join(dest_dir, "start%s" % system_script_extension()))
    if return_code:
        sys.stderr.write("Failed to start KA Lite: exit-code = %s" % return_code)
        sys.exit(return_code)

    # move the data file to the expected location
    static_zip_dir = os.path.join(dest_dir, "kalite/static/zip")
    if not os.path.exists(static_zip_dir):
        os.mkdir(static_zip_dir)
    shutil.move(zip_file, static_zip_dir)
    shutil.move(signature_file, static_zip_dir)

    # Remove the remaining install files
    os.remove(data_json_file)  # was copied in earlier
    for f in install_files:
        fpath = os.path.join(src_dir, f)
        if not os.path.exists(fpath):
            continue
        try:
            os.remove(fpath)
            sys.stdout.write("Removed installation file %s\n" % fpath)
        except Exception as e:
            sys.stderr.write("Failed to delete installation file %s: %s\n" % (fpath, e))
예제 #4
0
def install_from_package(data_json_file,
                         signature_file,
                         zip_file,
                         dest_dir=None,
                         install_files=[]):
    """
    NOTE: This docstring (below this line) will be dumped as a README file.
    Congratulations on downloading KA Lite!  These instructions will help you install KA Lite.
    """
    import glob
    import os
    import shutil
    import sys

    # Make the true paths
    src_dir = os.path.dirname(__file__) or os.getcwd()  # necessary on Windows
    data_json_file = os.path.join(src_dir, data_json_file)
    signature_file = os.path.join(src_dir, signature_file)
    zip_file = os.path.join(src_dir, zip_file)

    # Validate the unpacked files
    for file in [data_json_file, signature_file, zip_file]:
        if not os.path.exists(file):
            raise Exception(
                "Could not find expected file from zip package: %s" % file)

    # get the destination directory
    while not dest_dir or not os.path.exists(dest_dir) or raw_input(
            "%s: Directory exists; install? [Y/n] " %
            dest_dir) not in ["", "y", "Y"]:
        if dest_dir and raw_input(
                "%s: Directory does not exist; Create and install? [y/N] " %
                dest_dir) in ["y", "Y"]:
            try:
                os.makedirs(os.path.realpath(
                    dest_dir))  # can't use ensure_dir; external dependency.
                break
            except Exception as e:
                sys.stderr.write("Failed to create dest dir (%s): %s\n" %
                                 (dest_dir, e))
        dest_dir = raw_input(
            "Please enter the directory where you'd like to install KA Lite (blank=%s): "
            % src_dir) or src_dir

    # unpack the inner zip to the destination
    system_specific_unzipping(zip_file, dest_dir)
    sys.stdout.write("\n")

    # Copy, so that if the installation fails, it can be restarted
    shutil.copy(data_json_file, os.path.join(dest_dir, "kalite/static/data/"))

    # Run the setup/start scripts
    files = [
        f for f in glob.glob(
            os.path.join(dest_dir, "setup*%s" % system_script_extension()))
        if not "from_zip" in f
    ]
    return_code = os.system('"%s"' % files[0])
    if return_code:
        sys.stderr.write("Failed to set up KA Lite: exit-code = %s" %
                         return_code)
        sys.exit(return_code)
    return_code = os.system(
        '"%s"' % os.path.join(dest_dir, "start%s" % system_script_extension()))
    if return_code:
        sys.stderr.write("Failed to start KA Lite: exit-code = %s" %
                         return_code)
        sys.exit(return_code)

    # move the data file to the expected location
    static_zip_dir = os.path.join(dest_dir, "kalite/static/zip")
    if not os.path.exists(static_zip_dir):
        os.mkdir(static_zip_dir)
    shutil.move(zip_file, static_zip_dir)
    shutil.move(signature_file, static_zip_dir)

    # Remove the remaining install files
    os.remove(data_json_file)  # was copied in earlier
    for f in install_files:
        fpath = os.path.join(src_dir, f)
        if not os.path.exists(fpath):
            continue
        try:
            os.remove(fpath)
            sys.stdout.write("Removed installation file %s\n" % fpath)
        except Exception as e:
            sys.stderr.write("Failed to delete installation file %s: %s\n" %
                             (fpath, e))