Module: Amp::Support

Defined in:
lib/amp.rb,
lib/amp/support/logger.rb,
lib/amp/support/multi_io.rb,
lib/amp/templates/template.rb,
lib/amp/support/support.rb

Defined Under Namespace

Modules: SingletonLogger Classes: FileTemplate, IOForwarder, Logger, MultiIO, Template

Constant Summary collapse

SYSTEM =
{}
UMASK =
File.umask
@@rc_path =
nil

Class Method Summary collapse

Class Method Details

.determine_endiannessObject

Figures up the system is running on a little or big endian processor architecture, and upates the SYSTEM[] hash in the Support module.



1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'lib/amp/support/support.rb', line 1080

def self.determine_endianness
  num = 0x12345678
  little = '78563412'
  big    = '12345678'
  native = [num].pack('l')
  netunpack = native.unpack('N')
  if native == netunpack
    SYSTEM[:endian] = :big
  else
    SYSTEM[:endian] = :little
  end   
end

.get_fully_qualified_domain_nameObject

gets the fully-qualified-domain-name for fake usernames



1062
1063
1064
1065
# File 'lib/amp/support/support.rb', line 1062

def self.get_fully_qualified_domain_name
  require 'socket'
  Socket.gethostbyname(Socket.gethostname).first
end

.get_usernameObject

gets the logged-in username



1057
1058
1059
# File 'lib/amp/support/support.rb', line 1057

def self.get_username
  Etc.getlogin
end

.os_rcpathObject

Returns the paths to hgrc files, specific to this type of system.



1032
1033
1034
1035
1036
1037
# File 'lib/amp/support/support.rb', line 1032

def self.os_rcpath
  path = system_rcpath
  path += user_rcpath
  path.map! {|f| File.expand_path f}
  path
end

.parse_hg_url(url, revs = nil) ⇒ Hash

Parses the URL for amp-specific reasons.

Parameters:

  • url (String)

    The url to parse.

  • revs (Array) (defaults to: nil)

    The revisions that will be used for this operation.

Returns:

  • (Hash)

    A hash, specifying :url, :revs, and :head



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'lib/amp/support/support.rb', line 1019

def self.parse_hg_url(url, revs=nil)
  revs ||= [] # in case nil is passed
  
  unless url =~ /#/
    hds = revs.any? ? revs : nil
    return {:url => url, :revs => hds, :head => revs[-1]}
  end
  
  url, branch = url.split('#')[0..1]
  checkout = revs[-1] || branch
  {:url => url, :revs => revs + [branch], :head => checkout}
end

.rc_files_for_path(path) ⇒ Object

Returns all hgrc files for the given path



1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/amp/support/support.rb', line 1046

def self.rc_files_for_path path
  rcs = [File.join(path, "hgrc")]
  rcdir = File.join(path, "hgrc.d")
  begin
    Dir.stat_list(rcdir) {|f, kind| rcs << File.join(rcdir, f) if f =~ /\.rc$/}
  rescue
  end
  rcs
end

.rc_pathObject

Returns all paths to hgrc files on the system.



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/amp/support/support.rb', line 967

def self.rc_path
  if @@rc_path.nil?
    if ENV['HGRCPATH']
      @@rc_path = []
      ENV['HGRCPATH'].split(File::PATH_SEPARATOR).each do |p|
        next if p.empty?
        if File.directory?(p)
          File.stat_list(p) do |f, kind|
            if f =~ /\.rc$/
              @@rc_path << File.join(p, f)
            end
          end
        else
          @@rc_path << p
        end
      end
    else
      @@rc_path = self.os_rcpath
    end
  end
  @@rc_path
end

.system(command, opts = {}) ⇒ Object

Advanced calling of system().

Allows the caller to provide substitute environment variables and the directory to use



996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/amp/support/support.rb', line 996

def self.system(command, opts={})
  backup_dir = Dir.pwd # in case something goes wrong
  temp_environ, temp_path = opts.delete(:environ), opts.delete(:chdir) || backup_dir
  
if (temp_environ)
 	 	old_env = ENV.to_hash
  	temp_environ["HG"] = $amp_executable || File.amp_find_executable("amp")
  	temp_environ.each {|k, v| ENV[k] = v.to_s}
end
  Dir.chdir(temp_path) do
    rc = Kernel::system(command)
  end
ensure
  ENV.clear.update(old_env) if temp_environ
  Dir.chdir(backup_dir)    
end

.system_rcpathObject

Returns the hgrc paths specific to this type of system, and are system-wide.



1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/amp/support/support.rb', line 1069

def self.system_rcpath
  path = []
  if ARGV.size > 0
    path += rc_files_for_path(File.dirname(ARGV[0]) + "/../etc/mercurial")
  end
  path += rc_files_for_path "/etc/mercurial"
  path
end

.user_rcpathObject

Returns the hgrc files for the current user, specific to the particular OS and user.



1041
1042
1043
# File 'lib/amp/support/support.rb', line 1041

def self.user_rcpath
  [File.expand_path("~/.hgrc")]
end