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 6

Go to most recent revision | 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
#       - error-types and functions
47
#         (see SOCMaker::ERR)
48
#
49
#
50
#
51
###############################################################
52
require 'logger'
53
require 'yaml'
54
require 'digest/md5'
55
require 'fileutils'
56
 
57
 
58
# from
59
# http://stackoverflow.com/questions/2281490/how-to-add-a-custom-log-level-to-logger-in-ruby
60
class Logger
61
  def self.custom_level(tag)
62
    SEV_LABEL << tag
63
    idx = SEV_LABEL.size - 1
64
 
65
    define_method(tag.downcase.gsub(/\W+/, '_').to_sym) do |progname, &block|
66
      add(idx, nil, progname, &block)
67
    end
68
  end
69
  # add processing log level
70
  custom_level 'PROC'
71
end
72
 
73
 
74
 
75
 
76
module SOCMaker
77
 
78
 
79
  class << self
80
    public
81
    attr_accessor :logger
82
    attr_accessor :conf
83
    attr_accessor :lib
84
    def load( options={} )
85
      options = { skip_refresh: false, logger_out: STDOUT }.merge( options )
86
      @conf   = Conf::instance
87
      @logger = Logger.new(options[ :logger_out ] )
88
      @lib    = Lib.new()
89
      @logger.progname = @conf[ :app_name ]
90
      @lib.refresh( options[ :libpath ] ) unless options[ :skip_refresh ]
91
    end
92
 
93
    #
94
    # loading from from a YAML string
95
    #
96
    def from_s( s )
97
 
98
      objs = []
99
      SOCMaker::YPP.to_yaml( s ) do |yaml_obj_str|
100
 
101
        begin
102
          YAML::load( yaml_obj_str )
103
          o = YAML::load( yaml_obj_str )
104
 
105
          # ensure, that we load only our classes
106
          if SOCMaker::conf[ :yaml_classes ].include?( o.class )
107
            o.verify
108
            objs << o
109
          else
110
            SOCMaker::logger.warn( "Tried to load something, which does not belong to #{SOCMaker::conf[ :app_name ]}" )
111
          end
112
        rescue ArgumentError, Psych::SyntaxError #=> e
113
          #p e
114
          SOCMaker::logger.error( 'YAML loading failed, invalid YAML syntax?' )
115
          raise ERR::YAMLParseError
116
        else
117
        end
118
      end
119
 
120
      if block_given?
121
        objs.each{ |o| yield(o) }
122
      end
123
      return ( objs.size >1 ? objs : objs[0] )
124
    end
125
 
126
    # Path argument can be an array of paths
127
    # or a file (wildcards are allowed)
128
    # loading from a YAML file
129
    def from_f( path )
130
 
131
      path = Dir[ path ].sort if path.is_a?( String )
132
 
133
      SOCMaker::logger.warn( "No file(s) found to load" ) if path.size == 0
134
 
135
      yaml_str = ""
136
      path.each do |file|
137
        SOCMaker::logger.info "reading:" + file
138
        yaml_str << File.read( file )
139
      end
140
      o = from_s( yaml_str )
141
      o.dir = File.dirname( path.first )
142
      return o
143
    end
144
 
145
  end
146
 
147
 
148
 
149
  #
150
  # small module to extend classes,
151
  # which need to be written as yaml
152
  # output
153
  module YAML_EXT
154
 
155
    # we remember always, were we've loaded a yaml file
156
    attr_accessor :dir
157
 
158
    def save_yaml( args )
159
      path =  args.size==0 ? @spec_path : args.first
160
      File.open( path, 'w') {|f| f.write SOCMaker::YPP.from_yaml( YAML.dump( self ) ) }
161
    end
162
  end
163
 
164
 
165
  #
166
  # This sub-module contains some error-functionallity,
167
  # which is used in different classes via mixins.
168
  #
169
  # serr_if  means  raise Structure ERRor IF ...
170
  # verr_if  means  raise Value ERRor IF ...
171
  # lerr_if  means  raise Library ERRor IF ...
172
  # perr_if  mean   raise Processing ERRor IF
173
  module ERR
174
    class YAMLParseError < RuntimeError
175
    end
176
 
177
    class StructureError < RuntimeError
178
      attr :name
179
      attr :field
180
      def initialize( name, field, message )
181
        super message
182
        @name   = name
183
        @field  = field
184
       # p message
185
        SOCMaker::logger.error( "StructureError raised: " + message + " (#{name},#{field})" )
186
      end
187
      def to_s
188
        "->#{@name}:#{@field}"
189
      end
190
    end
191
 
192
    class LibError < RuntimeError
193
      attr :name
194
      def initialize( requested, message )
195
        super message
196
        @name = requested
197
        SOCMaker::logger.error( "LibError raised: " + message + " (#{requested})" )
198
      end
199
      def to_s
200
        "->#{@name}"
201
      end
202
    end
203
 
204
    class ProcessingError < RuntimeError
205
      def initialize( message )
206
        super message
207
        SOCMaker::logger.error( "ProcessingError raised: " + message )
208
      end
209
    end
210
 
211
    class ValueError < RuntimeError
212
      attr :name
213
      attr :field
214
      def initialize( name, field, message )
215
        super message
216
        @name = name
217
        @field = field
218
        SOCMaker::logger.error( "ValueError raised: " + message + " (#{name},#{field})" )
219
      end
220
      def to_s
221
        "->#{@name}:#{@field}"
222
      end
223
    end
224
 
225
 
226
 
227
    def serr_if( res, msg, o={} )
228
      o = { instance: '??', field: '??' }.merge( o )
229
      if !!( res )
230
        raise  StructureError.new( o[:instance], o[:field], msg )
231
      end
232
    end
233
 
234
    def verr_if( res, msg, o={})
235
      o = { instance: '??', field: '??' }.merge( o )
236
      if !!( res )
237
        raise ValueError.new( o[:instance], o[:field], msg )
238
      end
239
    end
240
 
241
    def lerr_if( res, msg, o={})
242
      o = { requested: '??' }.merge( o )
243
      if !!( res )
244
        raise LibError.new( o[:requested], msg )
245
      end
246
    end
247
 
248
    def perr_if( res, msg )
249
      if !!( res )
250
        raise ProcessingError.new( msg )
251
      end
252
    end
253
 
254
  end # module ERR
255
 
256
 
257
 
258
 
259
  # :stopdoc:
260
  LIBPATH = ::File.expand_path('..', __FILE__) + ::File::SEPARATOR
261
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
262
  VERSION = ::File.read(PATH + 'version.txt').strip
263
  # :startdoc:
264
 
265
  # Returns the library path for the module. If any arguments are given,
266
  # they will be joined to the end of the libray path using
267
  # File.join.
268
  #
269
  def self.libpath( *args )
270
    rv =  args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
271
    if block_given?
272
      begin
273
        $LOAD_PATH.unshift LIBPATH
274
        rv = yield
275
      ensure
276
        $LOAD_PATH.shift
277
      end
278
    end
279
    return rv
280
  end
281
 
282
  # Returns the lpath for the module. If any arguments are given,
283
  # they will be joined to the end of the path using
284
  # File.join.
285
  #
286
  def self.path( *args )
287
    rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
288
    if block_given?
289
      begin
290
        $LOAD_PATH.unshift PATH
291
        rv = yield
292
      ensure
293
        $LOAD_PATH.shift
294
      end
295
    end
296
    return rv
297
  end
298
 
299
  def self.require_all_libs
300
    file  = ::File.basename(__FILE__, '.*')
301
    dir = ::File.dirname(__FILE__)
302
    %w[ ypp         lib_inc
303
        component
304
        core_def    core_inst
305
        hdl_file    ifc_def
306
        ifc_port    ifc_spc
307
        soc_def     parameter
308
        sparameter  hdl_coder
309
        lib  cli conf].each { |rb| require ::File.expand_path(
310
                  ::File.join( dir, file, rb ) )  }
311
  end
312
 
313
end  # module SOCMaker
314
 
315
SOCMaker.require_all_libs
316
 
317
# 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.