Class: FPM::Source::Python

Inherits:
FPM::Source show all
Defined in:
lib/fpm/source/python.rb

Instance Attribute Summary

Attributes inherited from FPM::Source

#paths, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FPM::Source

#[], #[]=, #dependencies, #initialize, #metadata, #package

Constructor Details

This class inherits a constructor from FPM::Source

Class Method Details

.flags(opts, settings) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fpm/source/python.rb', line 11

def self.flags(opts, settings)
  settings.source[:python] = "python"
  settings.source[:easy_install] = "easy_install"
  settings.source[:pypi] ="http://pypi.python.org/simple"

  opts.on("--bin PYTHON_BINARY_LOCATION",
          "The path to the python you want to run. Default is 'python'") do |path|
    settings.source[:python] = path
  end

  opts.on("--easyinstall EASY_INSTALL_PATH",
          "The path to your easy_install tool. Default is 'easy_install'") do |path|
    settings.source[:easy_install] = path
  end

  opts.on("--pypi PYPI_SERVER",
          "PyPi Server uri for retrieving packages. Default is 'http://pypi.python.org/simple'") do |pypi|
    settings.source[:pypi] = pypi
  end

  opts.on("--package-prefix PREFIX",
          "Prefix for python packages") do |package_prefix|
    settings.source[:package_prefix] = package_prefix
  end
end

Instance Method Details

#download(package, version = nil) ⇒ Object

def get_source



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fpm/source/python.rb', line 52

def download(package, version=nil)
  puts "Trying to download #{package} (using: #{self[:settings][:easy_install]})"
  @tmpdir = ::Dir.mktmpdir("python-build", ::Dir.pwd)

  if version.nil?
    want_pkg = "#{package}"
  else
    want_pkg = "#{package}==#{version}"
  end

  safesystem(self[:settings][:easy_install], "-i", self[:settings][:pypi],
             "--editable", "--build-directory", @tmpdir, want_pkg)

  # easy_install will put stuff in @tmpdir/packagename/, flatten that.
  #  That is, we want @tmpdir/setup.py, and start with
  #  @tmpdir/somepackage/setup.py
  dirs = ::Dir.glob(File.join(@tmpdir, "*"))
  if dirs.length != 1
    raise "Unexpected directory layout after easy_install. Maybe file a bug? The directory is #{@tmpdir}"
  end
  @paths = dirs
end

#garbageObject

def make_tarball!



131
132
133
134
135
# File 'lib/fpm/source/python.rb', line 131

def garbage
  trash = []
  trash << @tmpdir if @tmpdir
  return trash
end

#get_metadataObject

def download



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
# File 'lib/fpm/source/python.rb', line 75

def 
  setup_py = @paths.first
  if File.directory?(setup_py)
    setup_py = File.join(setup_py, "setup.py")
    @paths = [setup_py]
  end

  if !File.exists?(setup_py)
    raise "Unable to find python package; tried #{setup_py}"
  end

  pylib = File.expand_path(File.dirname(__FILE__))
  setup_cmd = "env PYTHONPATH=#{pylib} #{self[:settings][:python]} #{setup_py} --command-packages=pyfpm get_metadata"
  output = ::Dir.chdir(File.dirname(setup_py)) { `#{setup_cmd}` }
  puts output
   = JSON.parse(output[/\{.*\}/msx])
  #p metadata

  if self[:settings][:package_prefix]
    self[:package_prefix] = self[:settings][:package_prefix]
  else
    self[:package_prefix] = "python"
  end

  self[:architecture] = ["architecture"]
  self[:description] = ["description"]
  self[:license] = ["license"]
  self[:version] = ["version"]
  self[:name] = "#{self[:package_prefix]}#{self[:suffix]}-#{["name"]}"
  self[:url] = ["url"]

  self[:dependencies] = ["dependencies"].collect do |dep|
    name, cmp, version = dep.split
    "#{self[:package_prefix]}#{self[:suffix]}-#{name} #{cmp} #{version}"
  end
end

#get_source(params) ⇒ Object

def flags



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fpm/source/python.rb', line 37

def get_source(params)
  package = @paths.first
  if ["setup.py", "."].include?(package)
    # Assume we're building from an existing python package.
    # Source already acquired, nothing to do!
    return
  end

  if !File.exists?(package)
    download(package, params[:version])
  else
    @paths = [ File.expand_path(package) ]
  end
end

#make_tarball!(tar_path, builddir) ⇒ Object

def get_metadata



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fpm/source/python.rb', line 112

def make_tarball!(tar_path, builddir)
  setup_py = @paths.first
  dir = File.dirname(setup_py)

  # Some setup.py's assume $PWD == current directory of setup.py, so let's
  # chdir first.
  ::Dir.chdir(dir) do
    safesystem(self[:settings][:python], "setup.py", "bdist")
  end

  dist_tar = ::Dir.glob(File.join(dir, "dist", "*.tar.gz")).first
  puts "Found dist tar: #{dist_tar}"
  puts "Copying to #{tar_path}"

  @paths = [ "." ]

  safesystem("cp", dist_tar, "#{tar_path}.gz")
end