Beispiel #1
0
    def make_package_name(base_path, path, prefix=None, filters=None):
        """Builds a package name based on the path"""

        package_name = os.path.abspath(path).replace(base_path, "")
        package_name = remove_leading_slash(package_name)

        package_parts = package_name.split("/")

        if prefix:
            package_parts = [prefix] + package_parts

        package_name = "-".join(package_parts)
        return Debian.normalise_package_name(package_name, filters)
Beispiel #2
0
    def add_asset_version_to_path(match):
        
        """Callback to make this a one-pass replace"""
        
        version_path = os.path.abspath(
            os.path.join(
                path, 
                match.group("path")
            )).replace(package_config["working_dir"], 
            ""
        )
        
        branch_path = version_path
        
        while len(branch_path) > 0:
            if global_config["reverse_destination_mapping"].\
                get(branch_path):
                
                remainder = version_path.replace(branch_path,"")
                remainder = remove_leading_slash(remainder)
                branch_path = os.path.join(
                    global_config["base_path"],
                    global_config["reverse_destination_mapping"][branch_path],
                    remainder
                )
                break
                
            branch_path = "/".join(branch_path.split("/")[:-1])    

        new_path = version_path.replace(
            global_config["destination_mapping"]["assets"],
            "",
        )
        version = None

        if package_config.get("asset_versions"):
            version = package_config["asset_versions"].get(new_path)

        if version is None:
            try:
                version = vcs_instance.get_directory_revision(file_path)
            except SuitcaseVcsError:
                version = package_config["version"]


        return "%s/%s%s%s" % (
            match.group(1), 
            version.replace(".",""),
            new_path, 
            match.group(3)
        )
Beispiel #3
0
def assets(global_config, package_config):

    """Generates a dictionary of asset versions"""

    vcs = global_config.get('version_control')
    if vcs:
        # dynamically import the vcs code
        vcs_module = 'suitcase.vcs.%s' % vcs
        vcs_instance = get_dynamic_class_instance(vcs_module, vcs.capitalize())

    asset_dir = os.path.abspath(
        os.path.join(package_config["working_dir"],
        package_config["destination_mapping"]["root"])
    )
    
    target_dir = os.path.abspath(os.path.join(asset_dir,"../"))
    if os.path.exists(target_dir):
        asset_versions = {}
        for path, dirs, files in os.walk(target_dir):
            if 'tiny_mce' in dirs:
                dirs.remove('tiny_mce')

            for file_path in files:
                original_path = os.path.join(path, file_path)
                original_path = original_path.replace(target_dir, "")
                
                file_path = os.path.join(path, file_path)\
                    .replace(package_config["working_dir"], "")

                branch_path = file_path
                while len(branch_path) > 0:
                    if global_config["reverse_destination_mapping"]\
                        .get(branch_path):
                        
                        remainder = file_path.replace(branch_path, "")
                        remainder = remove_leading_slash(remainder)

                        file_path = os.path.join(
                            global_config["base_path"],
                            global_config["reverse_destination_mapping"]\
                                [branch_path],
                            remainder
                        )
                        
                        break
                    branch_path = "/".join(branch_path.split("/")[:-1])    

                non_minified_file = file_path.replace('-minified.js', '.js')
                if file_path.endswith("-minified.js") and \
                    os.path.exists(non_minified_file):

                    file_path = non_minified_file

                try:
                    version = vcs_instance.get_directory_revision(file_path)
                    
                except SuitcaseVcsError:
                    display_warning(
                        "Suitcase Warning: Can't find version "\
                        "for %s setting to package version: %s" % \
                        (original_path,package_config["version"])
                    )
                    version = package_config["version"]

                asset_versions[original_path] = version

        asset_versions[asset_dir.replace(target_dir, "")] = \
            package_config["version"]
        
        asset_version_file = os.path.join(
            package_config["working_dir"], 
            "etc/suitcase/asset_versions/",
            "%s.py"%package_config["package"]
        )
        
        asset_version_dir = os.path.split(asset_version_file)[0]
        
        if not os.path.exists(asset_version_dir):
            try:
                os.makedirs(asset_version_dir)
            except OSError, error:
                raise SuitcasePackagingError(error)
            
        open(asset_version_file,"w").write(
            "asset_versions=%s" % pprint.pformat(asset_versions)
        )
         
        package_config["configs"] = package_config.get("configs", [])
        package_config["configs"].append(asset_version_file)
        package_config["asset_versions"] = asset_versions
        return package_config
Beispiel #4
0
    def copy_files_to_package_dir(self, extra_excludes=None):
        """Copys files from the branch into the destination file layout

        ready for packaging

        """
        if extra_excludes is None:
            extra_excludes = []

        package_config = self.package_config
        to_dir = package_config["working_dir"]
        from_dir = package_config["path"]

        build_exclusions = self.global_config["default_build_exclusions"] + self.global_config.get(
            "build_exclusions", []
        )

        # config based excludes
        if package_config.get("build_exclusions"):
            build_exclusions += package_config["build_exclusions"]

        mapped_files = []

        file_mapping = package_config.get("destination_mapping", {})

        # find which global_destination_map applies
        if self.global_config.get("destination_mapping"):

            global_mapping = self.global_config["destination_mapping"]
            global_mapping["root"] = global_mapping.get("root", "")

            # Create reverse destination mapping
            self.global_config["reverse_destination_mapping"] = {}

            for key, value in global_mapping.items():
                self.global_config["reverse_destination_mapping"][os.path.join(global_mapping["root"], value)] = key

            package_path = os.path.abspath(self.package_config["path"]).replace(self.global_config["base_path"], "")

            package_path = remove_leading_slash(package_path)

            # this is so we can walk up the path from the package root dir:
            # apps/blah wants to match the destination mapping for apps as well
            test_path = package_path
            global_mapping_root = global_mapping.get("root", "")
            found_mapping = False

            while len(test_path) > 0:
                # if we find something
                if global_mapping.get(test_path):
                    path_remainder = package_path.replace(test_path, "")
                    path_remainder = remove_leading_slash(path_remainder)

                    # append the relative path (from the matched key)
                    # so in the above example:
                    # in global:
                    # destination_mapping:
                    # apps: /foo/bar/apps

                    # so apps/blah wants to go to /foo/bar/apps/blah
                    global_mapping_root = os.path.join(global_mapping_root, global_mapping[test_path], path_remainder)
                    found_mapping = True

                    break
                # walk up
                test_path = "/".join(test_path.split("/")[:-1])

            # check that something was actually added else the path_remainder
            # is value.
            if not found_mapping:
                global_mapping_root = os.path.join(global_mapping_root, package_path)

            # overwrite the root mapping (if it starts with / ignore)
            root = file_mapping.get("root", "")
            file_mapping["root"] = os.path.join(global_mapping_root, root)

        # Check if file mapping exists

        if file_mapping != {}:

            if DEBUG:
                print "USING MAPPING"
            for key, value in file_mapping.items():

                if key == "root":
                    continue

                if not value.startswith("/"):
                    try:
                        value = os.path.join(file_mapping["root"], value)
                    except KeyError:
                        raise SuitcasePackagingError(
                            "You are using relative mapping settings in your " "debian file without setting a root"
                        )

                value = remove_leading_slash(value)

                # Joining paths to make absolute
                source = os.path.join(from_dir, key)
                destination = os.path.join(to_dir, value)

                mapped_files.append("/%s" % key)

                # do the magic
                if os.path.exists(source):
                    copy_files(source, destination, build_exclusions)

            file_mapping["root"] = file_mapping.get("root", "/")
            file_mapping["root"] = remove_leading_slash(file_mapping["root"])

            # AWOOGA - be careful with slashes as this tells rsync
            # whether it's creating the last dir in the destination or not
            # Now copy root (which will normally be everything)
            if len(mapped_files) > 0:
                build_exclusions += mapped_files

            copy_files(add_trailing_slash(from_dir), os.path.join(to_dir, file_mapping["root"]), build_exclusions)

            self.package_config["destination_mapping"] = file_mapping
        else:
            from_dir = add_trailing_slash(from_dir)
            extra_build_exclusions = package_config.get("build_exclusions", [])

            if extra_excludes:
                build_exclusions += extra_build_exclusions

            copy_files(from_dir, to_dir, build_exclusions)
            self.package_config["destination_mapping"] = {"root": ""}
Beispiel #5
0
 def test_strip_leading_slash3(self):
     """testing stripping a non-existent slash"""
     test = remove_leading_slash("win")
     expected = "win"
     self.assert_equal(test, expected)
Beispiel #6
0
 def test_strip_leading_slash2(self):
     """test stripping of the leading slash with leading + trailing space"""
     test = remove_leading_slash(" /win ")
     expected = "win"
     self.assert_equal(test, expected)
Beispiel #7
0
 def test_strip_leading_slash(self):
     """test stripping of the leading slash"""
     test = remove_leading_slash(" /win")
     expected = "win"
     self.assert_equal(test, expected)