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

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

Constant Summary

Constants inherited from Base

Base::SEM_VER

Constants included from Common

Common::KEY

Instance Attribute Summary collapse

Attributes inherited from Base

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Git

#checkout, #commit, #diff, #fetch, #files, #pull, #rebase, #refs, #reset, #restore, #rev, #stash, #status

Methods inherited from Base

#add, as_path, #base_path, #build, #build?, #clean, #clean?, #color, #copy, #copy?, #depend?, #dev?, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_env, #initialize_logger, #initialize_ref, #inspect, #log, #prod?, ref, #ref?, #refresh, #refresh?, #script?, #test, #test?, to_s, #to_s, #to_sym, #variable_all

Constructor Details

#initialize(**kwargs) ⇒ Python

Returns a new instance of Python.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/squared/workspace/project/python.rb', line 35

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
  @reqindex = REQUIREMENTS.index { |file| base_path(file).exist? } || 0
  @requirements = base_path(REQUIREMENTS[@reqindex])
end

Instance Attribute Details

#requirementsObject (readonly)

Returns the value of attribute requirements.



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

def requirements
  @requirements
end

Class Method Details

.is_a?(val) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_a?(val)
  if (val = as_path(val))
    REQUIREMENTS.any? { |file| val.join(file).exist? }
  else
    super
  end
end

.populateObject



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

def populate(*); end

.tasksObject



16
17
18
# File 'lib/squared/workspace/project/python.rb', line 16

def tasks
  nil
end

.venv?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/squared/workspace/project/python.rb', line 20

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

Instance Method Details

#depend(flag = nil, dir: nil, opts: []) ⇒ Object



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
120
121
122
123
124
125
# File 'lib/squared/workspace/project/python.rb', line 95

def depend(flag = nil, dir: nil, opts: [])
  if @depend && !flag
    super
  elsif outdated?
    sync = invoked_sync?('depend', flag)
    case (type = install_type)
    when 1, 2
      cmd = pip_session 'install'
      case flag
      when :user
        cmd << '--user'
        append_pip opts, OPT_USER
      when :target
        cmd << "--target=#{shell_escape(base_path(dir).to_s, quote: true)}"
        append_pip opts, OPT_USER + ['upgrade']
        append_eager opts
      when :upgrade
        cmd << '--upgrade'
        append_pip opts, OPT_FORCE
        append_eager opts
      when :force
        cmd << '--force-reinstall'
        append_pip opts, OPT_FORCE
      end
      cmd << (type == 1 ? '-r requirements.txt' : '.')
      run(sync: sync)
    when 3
      run_s("#{@bin} setup.py install", sync: sync)
    end
  end
end

#install_typeObject



132
133
134
# File 'lib/squared/workspace/project/python.rb', line 132

def install_type(*)
  requirements.exist? ? @reqindex + 1 : 0
end

#outdatedObject



127
128
129
130
# File 'lib/squared/workspace/project/python.rb', line 127

def outdated(*)
  pip_session 'list', '--outdated'
  run
end

#outdated?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/squared/workspace/project/python.rb', line 151

def outdated?
  install_type > 0
end

#populateObject



56
57
58
59
60
61
62
63
64
65
66
67
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
# File 'lib/squared/workspace/project/python.rb', line 56

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

  namespace name do
    @@tasks[Python.ref].each do |action, flags|
      namespace action do
        flags.each do |flag|
          case action
          when :install
            list = case flag
                   when :target
                     req = 'dir'
                     OPT_USER + %w[upgrade eager]
                   when :upgrade
                     OPT_FORCE + ['eager']
                   when :force
                     OPT_FORCE
                   else
                     OPT_USER
                   end
            desc format_desc(action, flag, list + OPT_GENERAL, req: req)
            if flag == :target
              task flag, [:dir, :opts] do |_, args|
                guard_params(action, flag, args: args, key: :dir)
                depend(flag, dir: args.dir, opts: args.to_a)
              end
            else
              task flag do |_, args|
                depend(flag, opts: args.to_a)
              end
            end
          end
        end
      end
    end
  end
end

#refObject



52
53
54
# File 'lib/squared/workspace/project/python.rb', line 52

def ref
  Python.ref
end

#variable_set(key, *val) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/squared/workspace/project/python.rb', line 136

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