OpenCores
URL https://opencores.org/ocsvn/soc_maker/soc_maker/trunk

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker.rb] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 feddischso
###############################################################
2
#
3
#  File:      soc_maker.rb
4
#
5
#  Author:    Christian Hättich
6
#
7
#  Project:   System-On-Chip Maker
8
#
9
#  Target:    Linux / Windows / Mac
10
#
11
#  Language:  ruby
12
#
13
#
14
###############################################################
15
#
16
#
17
#   Copyright (C) 2014  Christian Hättich  - feddischson [ at ] opencores.org
18
#
19
#   This program is free software: you can redistribute it and/or modify
20
#   it under the terms of the GNU General Public License as published by
21
#   the Free Software Foundation, either version 3 of the License, or
22
#   (at your option) any later version.
23
#
24
#   This program is distributed in the hope that it will be useful,
25
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
#   GNU General Public License for more details.
28
#
29
#   You should have received a copy of the GNU General Public License
30
#   along with this program.  If not, see .
31
#
32
#
33
###############################################################
34
#
35
#   Description:
36
#     This part of the SOCMaker module contains
37
#       - initialization of
38
#         - logger
39
#         - configuration
40
#         - library
41
#         (see SOCMaker::load)
42
#       - creating objects from YAML files/strings
43
#         (see from_f, from_s)
44
#       - creating YAML files from objects
45
#         (see SOCMaker::YAML_EXT::write_yaml)
46
#
47
#
48
#
49
###############################################################
50
require 'logger'
51
require 'yaml'
52
require 'digest/md5'
53
require 'fileutils'
54
 
55
 
56
# from
57
# http://stackoverflow.com/questions/2281490/how-to-add-a-custom-log-level-to-logger-in-ruby
58
class Logger
59
  def self.custom_level(tag)
60
    SEV_LABEL << tag
61
    idx = SEV_LABEL.size - 1
62
 
63
    define_method(tag.downcase.gsub(/\W+/, '_').to_sym) do |progname, &block|
64
      add(idx, nil, progname, &block)
65
    end
66
  end
67
  # add processing log level
68
  custom_level 'PROC'
69
end
70
 
71
 
72
 
73
 
74
module SOCMaker
75
 
76
 
77
  class << self
78
    public
79
    attr_accessor :logger
80
    attr_accessor :conf
81
    attr_accessor :lib
82
    def load( options={} )
83
      options = { skip_refresh: false, logger_out: STDOUT }.merge( options )
84
      @conf   = Conf::instance
85
      @logger = Logger.new(options[ :logger_out ] )
86
      @lib    = Lib.new()
87
      @logger.progname = @conf[ :app_name ]
88
      @lib.refresh( options[ :libpath ] ) unless options[ :skip_refresh ]
89
    end
90
 
91
    #
92
    # loading from from a YAML string
93
    #
94
    def from_s( s )
95
 
96
      objs = []
97
      SOCMaker::YPP.to_yaml( s ) do |yaml_obj_str|
98
 
99
        begin
100
          YAML::load( yaml_obj_str )
101
          o = YAML::load( yaml_obj_str )
102
 
103
          # ensure, that we load only our classes
104
          if SOCMaker::conf[ :yaml_classes ].include?( o.class )
105 7 feddischso
            #o.verify
106 3 feddischso
            objs << o
107
          else
108
            SOCMaker::logger.warn( "Tried to load something, which does not belong to #{SOCMaker::conf[ :app_name ]}" )
109
          end
110 7 feddischso
        rescue ArgumentError, Psych::SyntaxError => e
111 3 feddischso
          SOCMaker::logger.error( 'YAML loading failed, invalid YAML syntax?' )
112 7 feddischso
          SOCMaker::logger.error( ">>> #{e.to_s} <<<" )
113 3 feddischso
          raise ERR::YAMLParseError
114
        else
115
        end
116
      end
117
 
118
      if block_given?
119
        objs.each{ |o| yield(o) }
120
      end
121
      return ( objs.size >1 ? objs : objs[0] )
122
    end
123
 
124
    # Path argument can be an array of paths
125
    # or a file (wildcards are allowed)
126
    # loading from a YAML file
127
    def from_f( path )
128
 
129
      path = Dir[ path ].sort if path.is_a?( String )
130
 
131
      SOCMaker::logger.warn( "No file(s) found to load" ) if path.size == 0
132
 
133
      yaml_str = ""
134
      path.each do |file|
135
        SOCMaker::logger.info "reading:" + file
136
        yaml_str << File.read( file )
137
      end
138
      o = from_s( yaml_str )
139
      o.dir = File.dirname( path.first )
140
      return o
141
    end
142
 
143
  end
144
 
145
 
146
 
147
  #
148
  # small module to extend classes,
149
  # which need to be written as yaml
150
  # output
151
  module YAML_EXT
152
 
153
    # we remember always, were we've loaded a yaml file
154
    attr_accessor :dir
155
 
156
    def save_yaml( args )
157
      path =  args.size==0 ? @spec_path : args.first
158
      File.open( path, 'w') {|f| f.write SOCMaker::YPP.from_yaml( YAML.dump( self ) ) }
159
    end
160
  end
161
 
162
 
163
 
164
 
165
 
166
 
167
  # :stopdoc:
168
  LIBPATH = ::File.expand_path('..', __FILE__) + ::File::SEPARATOR
169
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
170
  VERSION = ::File.read(PATH + 'version.txt').strip
171
  # :startdoc:
172
 
173
  # Returns the library path for the module. If any arguments are given,
174
  # they will be joined to the end of the libray path using
175
  # File.join.
176
  #
177
  def self.libpath( *args )
178
    rv =  args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
179
    if block_given?
180
      begin
181
        $LOAD_PATH.unshift LIBPATH
182
        rv = yield
183
      ensure
184
        $LOAD_PATH.shift
185
      end
186
    end
187
    return rv
188
  end
189
 
190
  # Returns the lpath for the module. If any arguments are given,
191
  # they will be joined to the end of the path using
192
  # File.join.
193
  #
194
  def self.path( *args )
195
    rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
196
    if block_given?
197
      begin
198
        $LOAD_PATH.unshift PATH
199
        rv = yield
200
      ensure
201
        $LOAD_PATH.shift
202
      end
203
    end
204
    return rv
205
  end
206
 
207
  def self.require_all_libs
208
    file  = ::File.basename(__FILE__, '.*')
209
    dir = ::File.dirname(__FILE__)
210 7 feddischso
    %w[ err         ypp
211
        lib_inc     component
212 3 feddischso
        core_def    core_inst
213
        hdl_file    ifc_def
214
        ifc_port    ifc_spc
215
        soc_def     parameter
216
        sparameter  hdl_coder
217
        lib  cli conf].each { |rb| require ::File.expand_path(
218
                  ::File.join( dir, file, rb ) )  }
219
  end
220
 
221
end  # module SOCMaker
222
 
223
SOCMaker.require_all_libs
224
 
225
# vim: noai:ts=2:sw=2

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.