Class: Cfruby::Packages::FreeBSDPackageManager

Inherits:
PackageManager show all
Defined in:
lib/libcfruby/osmodules/freebsd.rb

Overview

PackageManager implementation for the FreeBSD ports system

Instance Method Summary collapse

Methods inherited from PackageManager

#[], #installed_version, #method_missing, #version

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cfruby::Packages::PackageManager

Instance Method Details

#install(packagename, force = false) ⇒ Object

Installs the latest version of the named package



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/libcfruby/osmodules/freebsd.rb', line 367

def install(packagename, force=false)
	packagename.strip!()

	# determine whether to use portinstall or pkg_add
	if(test(?x, '/usr/local/sbin/portinstall'))
		@portadd = '/usr/local/sbin/portinstall -M BATCH=YES'
	else
		@portadd = '/usr/sbin/pkg_add -r'
	end

	if force
		Cfruby.controller.attempt("Installing \"#{packagename}\"", 'destructive', 'unknown', 'install') {
			Exec.exec("#{@portadd} -f #{packagename}")
		}
		
		return(true)
	elsif !installed?(packagename)
		Cfruby.controller.attempt("Installing \"#{packagename}\"", 'destructive', 'unknown', 'install') {
			output = Exec.exec("#{@portadd} #{packagename}")
			if(output[1].length > 0)
				# portinstall doesn't have useful positive feedback, so we check known negatives
			       	if (output[1][0] =~ /\*\* No such installed package or port/)
					raise(InstallError, "#{packagename} not found by \"#{@portadd}\"")
				end
				if(output[1][0] =~ /\*\* Port marked as IGNORE:/)
					raise(InstallError, "#{packagename} is marked as IGNORE \"#{@portadd}\" for reason: #{output[1][1]}")
				end
				if(output[1][0] =~ /\*\* Command failed/)
					raise(InstallError, "#{packagename} installation failed")
				end
			end
		}
		
		return(true)
	end
	
	return(false)
end

#installed?(package) ⇒ Boolean

Return true if the package is installed Checks the origin as well

Returns:

  • (Boolean)


353
354
355
356
357
358
359
360
361
362
363
# File 'lib/libcfruby/osmodules/freebsd.rb', line 353

def installed?(package)
	Cfruby.controller.inform('debug', "Getting installed? status of \"#{package}\" by origin")

	installed_packages.each_value() { |packageinfo|
		if(packageinfo.origin == package)
			return(true)
		end
	}

	return(super(package))
end

#installed_packagesObject

Returns a PackageList object that contains key value pairs for every installed package where the key is the package name and the value is the currently installed version. See PackageList for more information



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/libcfruby/osmodules/freebsd.rb', line 435

def installed_packages()
	packages = PackageList.new()
	packageregex = /([^ ]+)-([^- ]+)\s+(.*)/

	installedpackageslist = `/usr/sbin/pkg_info`
	installedpackageslist.each_line() { |line|
		line.strip!()
		match = packageregex.match(line)
		if(match != nil)
			name = match[1]
			version = match[2]
			description = match[3]

			packages[name] = PackageInfo.new()
			packages[name].name = name
			packages[name].version = version
			packages[name].description = description
			packages[name].origin = `/usr/sbin/pkg_info -q -o #{name}-#{version}`.strip()
		end
	}

	return(packages)
end

#packagesObject

Returns a PackageList object that contains key value pairs for every package (installed or not) where the key is the package name and the value is a PackageInfo object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/libcfruby/osmodules/freebsd.rb', line 463

def packages()
	packages = PackageList.new()

	os = Cfruby::OS::OSFactory.new().get_os()
	indexfile = '/usr/ports/INDEX'
	if(os['version'] =~ /^5/)
		indexfile = '/usr/ports/INDEX-5'
	end

	# run make fetchindex if the index doesn't exist
	if(!test(?e, indexfile))
		Exec.exec("cd /usr/ports && make fetchindex")
	end

	# parse /usr/ports/INDEX to get a listing
	File.open(indexfile, File::RDONLY) { |fp|
		fp.each_line() { |line|
			line = line.split(/\|/)
			name = line[0][/^(.*)-[^-]+/, 1]
			version = line[0][/-([^-]+)$/, 1]
			description = line[3]

			packages[name] = PackageInfo.new()
			packages[name].name = name
			packages[name].version = version
			packages[name].description = description
			packages[name].origin = line[1]
		}
	}

	return(packages)
end

#uninstall(packagename) ⇒ Object

Uninstalls the named package



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/libcfruby/osmodules/freebsd.rb', line 408

def uninstall(packagename)
	packagename.strip!()

	Cfruby.controller.attempt("Uninstalling \"#{packagename}\"", 'destructive', 'unknown') {
		# we have to iterate through all the attributes (name, name-version, origin) to properly delete what this could be.
		packagetodelete = nil
		installed_packages().each_value() { |package|
			if(packagename == package.name) or (packagename == "#{package.name}-#{package.version}") or (packagename == package.origin)
				packagetodelete = package.name + '-' + package.version
				break
			end
		}

		if packagetodelete.nil?
			Cfruby.controller.attempt_abort("package \"#{packagename}\": not installed")
		else
			Exec.exec("/usr/sbin/pkg_delete #{packagetodelete}`")
		end
	}

end