core.py 5.02 KB
Newer Older
1 2
"""Provides the core functionality for the HDLMake module"""

3
from __future__ import absolute_import
4
import os
5
import sys
6 7
import logging

8 9
from hdlmake import fetch
from hdlmake.util import path as path_mod
10

11 12

class ModuleConfig(object):
13

14 15
    """This class containt the base properties and methods that
    need to be initialized for a proper behavior"""
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    def __init__(self):
        self.source = None
        self.parent = None
        self.url = None
        self.branch = None
        self.revision = None
        self.path = None
        self.isfetched = False

    def process_manifest(self):
        """process_manifest does nothing for ModuleConfig"""
        pass

    def basename(self):
        """Get the basename for the module"""
        if self.source == fetch.SVN:
            return path_mod.svn_basename(self.url)
        else:
            return path_mod.url_basename(self.url)

    def fetchto(self):
        """Get the fetchto folder for the module"""
        return os.path.dirname(self.path)

41
    def init_config(self, module_args):
42 43
        """This initializes the module configuration.
        The function is executed by Module constructor"""
44 45 46 47 48
        parent = module_args.parent
        url = module_args.url
        source = module_args.source
        fetchto = module_args.fetchto

49 50 51 52
        self.source = source
        self.parent = parent

        if self.source != fetch.LOCAL:
53 54 55 56 57 58
            if self.source == fetch.SVN:
                self.url, self.revision = \
                    path_mod.svn_parse(url)
            else:
                self.url, self.branch, self.revision = \
                    path_mod.url_parse(url)
59
            basename = self.basename()
60 61
            path = path_mod.relpath(os.path.abspath(
                os.path.join(fetchto, basename)))
62

63 64 65 66 67
            # Check if the module dir exists and is not empty
            if os.path.exists(path) and os.listdir(path):
                self.path = path
                self.isfetched = True
                logging.debug("Module %s (parent: %s) is fetched.",
68
                              url, self.parent.path)
69 70 71 72
            else:
                self.path = path
                self.isfetched = False
                logging.debug("Module %s (parent: %s) is NOT fetched.",
73
                              url, self.parent.path)
74 75 76 77
        else:
            self.url, self.branch, self.revision = url, None, None

            if not os.path.exists(url):
78
                raise Exception(
79 80
                    "Path to the local module doesn't exist:\n" + url
                    + "\nThis module was instantiated in: " + str(self.parent))
81
            self.path = path_mod.relpath(url)
82 83
            self.isfetched = True

84 85 86 87 88 89 90 91 92 93
    def _check_filepath(self, filepath):
        """Check the provided filepath against several conditions"""
        if filepath:
            if path_mod.is_abs_path(filepath):
                logging.warning(
                    "Specified path seems to be an absolute path: " +
                    filepath + "\nOmitting.")
                return False
            filepath = os.path.join(self.path, filepath)
            if not os.path.exists(filepath):
94
                raise Exception(
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
                    "Path specified in manifest in %s doesn't exist: %s",
                    self.path, filepath)

            filepath = path_mod.rel2abs(filepath, self.path)
            if os.path.isdir(filepath):
                logging.warning(
                    "Path specified in manifest %s is a directory: %s",
                    self.path, filepath)
        return True

    def _make_list_of_paths(self, list_of_paths):
        """Get a list with only the valid absolute paths from the provided"""
        paths = []
        for filepath in list_of_paths:
            if self._check_filepath(filepath):
                paths.append(path_mod.rel2abs(filepath, self.path))
        return paths


114
class ModuleCore(ModuleConfig):
115

116
    """This is the class providing the module core functionality"""
117

118 119 120 121
    def __init__(self):
        # Universal Manifest Properties
        self.library = "work"
        self.action = None
122 123 124
        self.pool = None
        self.top_module = None
        self.manifest_dict = None
125 126
        super(ModuleCore, self).__init__()

127 128 129 130
    def set_pool(self, pool):
        """Set the associated pool for the module instance"""
        self.pool = pool
        self.top_module = pool.get_top_module()
131

132
    def process_manifest(self):
133
        """Method that process the core manifest section"""
134
        self._process_manifest_universal()
135
        super(ModuleCore, self).process_manifest()
136

137
    def _process_manifest_universal(self):
138
        """Method processing the universal manifest directives"""
139
        # if "top_module" in self.manifest_dict:
140
        #    self.top_module = self.manifest_dict["top_module"]
141
        # Libraries
142 143
        if "library" in self.manifest_dict:
            self.library = self.manifest_dict["library"]
144 145 146
        elif self.parent:
            self.library = self.parent.library

147 148
        if "action" in self.manifest_dict:
            self.action = self.manifest_dict["action"].lower()