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



363
364
365
366
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
# File 'lib/libcfruby/osmodules/freebsd.rb', line 363

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 && output[1][0] =~ /\*\* No such installed package or port/)
				raise(InstallError, "#{packagename} not found by \"#{@portadd}\"")
			end
		}
		
		return(true)
	end
	
	return(false)
end

#installed?(package) ⇒ Boolean

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

Returns:

  • (Boolean)


349
350
351
352
353
354
355
356
357
358
359
# File 'lib/libcfruby/osmodules/freebsd.rb', line 349

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



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/libcfruby/osmodules/freebsd.rb', line 422

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



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/libcfruby/osmodules/freebsd.rb', line 450

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



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/libcfruby/osmodules/freebsd.rb', line 395

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