Class: Squared::Workspace::Project::Python

Inherits:
Git
  • Object
show all
Defined in:
lib/squared/workspace/project/python.rb

Constant Summary

Constants included from Common

Common::ARG, Common::PATH

Instance Attribute Summary

Attributes inherited from Base

#dependfile, #exception, #group, #name, #parent, #path, #pipe, #project, #theme, #verbose, #workspace

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Git

#branch, #checkout, #clone, #clone?, #commit, #diff, #enabled?, #fetch, #generate, #logx, #ls_files, #ls_remote, #pull, #rebase, #reset, #restore, #rev_parse, #show, #stash, #status, #tag

Methods inherited from Base

#add, aliasargs, #allref, #as, as_path, #basepath, #build, #build?, #clean, #clean?, #copy, #copy?, #dependtype, #dev?, #doc, #doc?, #enabled?, #error, #event, #exclude?, #first, #generate, #graph, #graph?, #has?, #initialize_build, #initialize_env, #initialize_events, #initialize_logger, #initialize_ref, #inject, #inspect, #last, #lint, #lint?, #localname, #log, #prod?, ref, #ref?, #script?, #task_include?, #test, #test?, to_s, #to_s, #to_sym, #version, #with

Methods included from Common::Format

#enable_aixterm

Constructor Details

#initialize(**kwargs) ⇒ Python

Returns a new instance of Python.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/squared/workspace/project/python.rb', line 47

def initialize(*, **kwargs)
  super
  if @pass.include?(Python.ref)
    initialize_ref(Python.ref)
    initialize_logger(**kwargs)
  else
    initialize_build(Python.ref, **kwargs)
    initialize_env(**kwargs)
  end
  @dependindex = REQUIREMENTS.index { |file| basepath(file).exist? }
  @dependfile = basepath(REQUIREMENTS[@dependindex || 0])
end

Class Method Details

.bannerargsObject



32
33
34
# File 'lib/squared/workspace/project/python.rb', line 32

def bannerargs
  [:dependfile].freeze
end

.batchargsObject



26
# File 'lib/squared/workspace/project/python.rb', line 26

def batchargs(*); end

.config?(val) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/squared/workspace/project/python.rb', line 40

def config?(val)
  return false unless (val = as_path(val))

  DIR_PYTHON.any? { |file| val.join(file).exist? }
end

.populateObject



25
# File 'lib/squared/workspace/project/python.rb', line 25

def populate(*); end

.tasksObject



28
29
30
# File 'lib/squared/workspace/project/python.rb', line 28

def tasks
  [:outdated].freeze
end

.venv?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/squared/workspace/project/python.rb', line 36

def venv?
  Dir.exist?(ENV.fetch('VIRTUAL_ENV', ''))
end

Instance Method Details

#depend(flag = nil, opts = [], sync: invoked_sync?('depend', flag), target: nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/squared/workspace/project/python.rb', line 121

def depend(flag = nil, opts = [], sync: invoked_sync?('depend', flag), target: nil, **)
  if @depend && !flag
    super
  elsif outdated?
    cmd = pip_session 'install'
    case flag
    when :user
      cmd << "--#{flag}"
    when :target
      cmd << quote_option('target', basepath(target))
    when :force
      cmd << '--force-reinstall'
    else
      append_global
    end
    append_pip(flag, opts, from: :install) if flag
    if dependtype == 1 && !session_arg?('e', 'editable')
      cmd << '-r requirements.txt' unless session_arg?('r', 'requirement')
    elsif !session_arg?('e', 'editable', value: true)
      cmd << '.'
    end
    run(from: :depend, sync: sync)
  end
end

#depend?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/squared/workspace/project/python.rb', line 249

def depend?
  @depend != false && (!@depend.nil? || outdated?)
end

#install(flag, opts, strategy: nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/squared/workspace/project/python.rb', line 146

def install(flag, opts, strategy: nil)
  cmd = pip_session 'install'
  out = append_pip(flag, opts, from: :install)
  case flag
  when :editable
    cmd << '--editable' << (out.pop || '.')
    option_clear out
  when :upgrade
    cmd << '--upgrade'
    cmd << basic_option('upgrade-strategy', strategy) if strategy
    append_value out
  end
  run(from: :install)
end

#outdated(sync: invoked_sync?('outdated')) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/squared/workspace/project/python.rb', line 161

def outdated(*, sync: invoked_sync?('outdated'))
  cmd = pip_session 'list', '--outdated'
  append_global
  cmd = session_done(cmd)
  log.info cmd
  on :first, :outdated
  banner = format_banner(cmd)
  print_item banner if sync
  start = 0
  found = 0
  major = 0
  pwd_set(from: :outdated) do
    buffer = []
    out = ->(val) { sync ? puts(val) : buffer << val }
    IO.popen(cmd).each do |line|
      next if line =~ /^[\s-]+$/

      if start > 0
        unless stdin?
          data = line.scan(SEM_VER)
          next unless (cur = data.shift) && (lat = data.shift)

          latest = lat.join
          current = cur.join
          semver(cur)
          semver(lat)
          if semmajor?(cur, lat)
            type = 2
            major += 1
          else
            type = cur[2] == lat[2] ? 0 : 1
          end
          if type == 0
            styles = color(:yellow)
          else
            styles = color(:green)
            line = sub_style(line, pat: /^(\S+)(.+)$/, styles: if type == 2
                                                                 styles << :bold
                                                                 theme[:major]
                                                               else
                                                                 theme[:active]
                                                               end)
          end
          if theme[:current]
            line = sub_style(line, pat: /^(.+)(#{Regexp.escape(current)})(.+)$/, styles: theme[:current],
                                   index: 2)
          end
          line = sub_style(line, pat: /^(.+)(#{Regexp.escape(latest)})(.+)$/, styles: styles, index: 2)
          found += 1
        end
        out.("#{start.to_s.rjust(2)}. #{line}")
        start += 1
      elsif line =~ /^Package /
        unless stdin?
          sub = { pat: /^(.*)(?<!\dm)(Package|Latest)(.+)$/, styles: theme[:header], index: 2 }
          out.(print_footer(" #  #{line.chomp}", reverse: true, sub: [sub, sub]))
        end
        start += 1
      end
    end
    unless sync
      print_item banner
      puts buffer
    end
    if found > 0
      puts print_footer empty_status('Updates are available', 'major', major)
    elsif start == 0
      puts 'No updates were found'
    end
  end
  on :last, :outdated
end

#outdated?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/squared/workspace/project/python.rb', line 253

def outdated?
  dependtype > 0
end

#populateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/squared/workspace/project/python.rb', line 68

def populate(*, **)
  super
  return unless outdated? && ref?(Python.ref)

  namespace name do
    @@tasks[Python.ref].each do |action, flags|
      next if @pass.include?(action)

      namespace action do
        flags.each do |flag|
          case action
          when 'install'
            format_desc(action, flag, 'opts*', before: case flag
                                                       when :target
                                                         'dir'
                                                       when :editable
                                                         'path/url?'
                                                       when :upgrade
                                                         'eager?,package+'
                                                       end)
            case flag
            when :editable
              task flag do |_, args|
                install flag, args.to_a
              end
            when :upgrade
              task flag, [:strategy] do |_, args|
                case (strategy = args.strategy)
                when 'eager', 'only-if-needed'
                  opts = args.extras
                else
                  opts = args.to_a
                  strategy = nil
                end
                install(flag, opts, strategy: strategy)
              end
            when :target
              task flag, [:dir] do |_, args|
                dir = param_guard(action, flag, args: args, key: :dir)
                depend(flag, args.extras, target: dir)
              end
            else
              task flag do |_, args|
                depend flag, args.to_a
              end
            end
          end
        end
      end
    end
  end
end

#refObject



64
65
66
# File 'lib/squared/workspace/project/python.rb', line 64

def ref
  Python.ref
end

#variable_set(key, *val) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/squared/workspace/project/python.rb', line 234

def variable_set(key, *val, **)
  case key
  when :dependfile
    req = basepath(*val)
    if (index = REQUIREMENTS.index(req.basename.to_s))
      @dependindex = index
      @dependfile = req
    else
      log.warn "variable_set: @#{key}=#{req} (not supported)"
    end
  else
    super
  end
end