Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f5e4f83f97
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 256 lines (219 sloc) 7.115 kb
1 2 3 4 5 6 7 8 9 10 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 94 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
#!/usr/bin/ruby -w

require 'rubygems'
require 'net/ssh'
require 'net/sftp'
require 'find'
require 'script_constants'

def get_files_in_directory_list(directoryNames)
  filenames = []
  directoryNames.each do |direct|
    Dir.new(direct).each do |fname|
      if (fname != "." && fname != ".." && File.file?(direct + fname))
        filenames.push(direct + fname)
      end
    end
  end
  return filenames
end

def fullPath(fname)
  return File.expand_path(fname)
end

def buildNavDiv
  latest = get_files_in_directory_list(Dir['Latest/**/'].sort)
  all = get_files_in_directory_list(Dir['All/**/'].sort)
  reviews = get_files_in_directory_list(Dir['Reviews/**/'].sort)
  
  output = ["<div id=\"right\">\n\t<ul>\nLatest<br />"]
  latest.each do |l|
    (File.expand_path(l)).match(DIR_REGEX)
    output.push("\t\t<li>" + createLink(l, $2) + "</li>\n")
  end
      
  output.push("\t\t<br />Reviews<br />")
  reviews.each do |r|
    (File.expand_path(r)).match(DIR_REGEX)
    output.push("\t\t<li>#{createLink(r, $2)}</li>\n")
  end
  
  output.push("\t\t<br />All Versions<br />")
  all = all.sort
  lastpath = ""
  all.each do |a|
    (File.expand_path(a)).match(DIR_REGEX)
    if lastpath != $2
      lastpath = $2
      output.push("\t\t#{$2}<br />\n")
    end
      
    output.push("\t\t<li> #{createLink(a, File.basename(a))} </li>\n")
  end
  
  output.push("\t</ul>\n</div><!--\/right-->")
  return output.join
end

def buildTable(filename)
  lines = []
  open("table.csv", 'r') { |f| lines = f.readlines }
  filename.match(FILENAME_PATTERN)
  name = $1
  name.capitalize!
  
  output = ["<div id=\"time_table\"><table>\n"]
  output.push("\t<tr>\n\t\t<td>Date</td>\n\t\t<td>Length (min)</td>\n\t\t<td>Members Present</td>\n\t\t<td>Results</td>\n\t</tr>\n")
  lines.each do |l|
    date, len, peeps, done = l.split(';')
    if (peeps.include?(name))
      output.push("\t<tr>\n\t\t<td>" + date + "</td>\n\t\t<td>" + len + \
          "</td>\n\t\t<td>" + peeps + "</td>\n\t\t<td>" + done.chop + \
          "</td>\n\t</tr>\n")
    end
  end
  output.push("</table></div><!--/timetable-->")
  return output.join
end

def buildBlogPosts
  lines = []
  open("blog.csv", 'r') { |f| lines = f.readlines }
  output = ["<div id=\"blog\">\n"]
  lines.each do |l|
    header, entry = l.split(';')
    output.push("\t<div class=\"blogentry\">\n\t\t<h3>" + header + "</h3>\n" + \
        entry.chop + "\n\t</div>\n")
  end
  output.push("</div><!--\/blog-->")
  return output.join
end


def mergeWithTemplate(filename)
  puts "Processing : " + filename
  
  filename.match(FILENAME_PATTERN)
  name = $1
  
  lines = []
  open("template.part", 'r') { |file| lines = file.readlines }

  lines = lines.join("<line>")
  lines = lines.gsub(/\n/, "<newline>")
  
  TAGS.each do |t|
    if (s = getMatchFromFile(filename, t))
      lines = lines.gsub(t, s)
    end
  end
  if (name != "index" && s = getMatchFromString(buildTable(filename), TABLE_TAG))
    lines = lines.gsub(TABLE_TAG, s)
  end
  if (name == "index" && s = getMatchFromString(buildBlogPosts(), BLOG_TAG))
    lines = lines.gsub(BLOG_TAG, s)
  end
      
  s = getMatchFromString(buildNavDiv, FILE_NAV)
  lines = lines.gsub(FILE_NAV, s)
  
  lines = lines.gsub(/<newline>/, "\n")
  lines = lines.split("<line>")
  
  existlines = []
  open(name + '.html', 'r') { |f| existlines = f.readlines }
  if existlines.join != lines.join
    outfile = open(name + ".html", 'w')
    outfile.write(lines)
    outfile.close()
    uploadFile(name + ".html")
  end
end

def getMatchFromString(string, r)
  string = string.gsub("\n", "<newline>")
  if (string.match(r))
    return $1
  end
end

def getMatchFromFile(filename, r)
  lines = []
  open(filename, 'r') { |f| lines = f.readlines }
  lines = lines.join
  return getMatchFromString(lines, r)
end

def writeStringToRemoteFile(filename, string)
  Net::SFTP.start(HOSTNAME, USERNAME, :password => PASSWORD) do |sftp|
    file_perm = 0644
    remote_path = 'sp/website/'
    begin
      file = sftp.open!(remote_path + filename, 'w')
    rescue Net::SFTP::StatusException => e
      raise unless e.code == 2
      puts 'Error: file #{filename} on remote server'
      next
    end
    
    sftp.write!(file, 0,string)
    sftp.close!(file)
  end
end

def uploadFile(file)
  Net::SFTP.start(HOSTNAME, USERNAME, :password => PASSWORD) do |sftp|
    f = file
    file_perm = 0644
    remote_path = "sp/website"
    next if f.include?('.part') or f.include?('.table') or f.include?('.sh') or f.include?('.rb') or f.include?('.csv') or File.stat(f).directory?
    local_file = "#{f}"
    remote_file = remote_path + "/" + local_file
    
    begin
      rstat = sftp.stat!(remote_file)
    rescue Net::SFTP::StatusException => e
      raise unless e.code == 2
      puts "Copying #{local_file} to #{remote_file}"
      sftp.upload(local_file, remote_file)
      sftp.setstat(remote_file, :permissions => file_perm)
      next
    end
    
    puts File.stat(local_file).mtime
    puts Time.at(rstat.mtime)
    if File.stat(local_file).mtime > Time.at(rstat.mtime + SERVER_TIME_OFFSET)
      puts "Updating #{local_file} to #{remote_file}"
      sftp.upload(local_file, remote_file)
    end
  end
end

def uploadDirectory(dir)
  Net::SFTP.start(HOSTNAME, USERNAME, :password => PASSWORD) do |sftp|
    dir_perm = 0755
    file_perm = 0644
    remote_path = "sp/website"
    Find.find(dir) do |f|
      next if f.include?('.part') or f.include?('.table') or f.include?('.sh') or f.include?('.rb') or f.include?('.csv')
      if File.stat(f).directory?
        begin
          remote_dir = remote_path + "/" + f
          sftp.stat!(remote_dir)
        rescue Net::SFTP::StatusException => e
          raise unless e.code == 2
          puts "Creating remote directory #{remote_dir}"
          sftp.mkdir(remote_dir, :permissions => dir_perm)
        end
        next
      end
      local_file = "#{f}"
      remote_file = remote_path + "/" + local_file
      
      begin
        remote_dir = File.dirname(remote_file)
        sftp.stat!(remote_dir)
      rescue Net::SFTP::StatusException => e
        raise unless e.code == 2
        puts "Creating remote directory #{remote_dir}"
        sftp.mkdir(remote_dir, :permissions => dir_perm)
      end
      
      begin
        rstat = sftp.stat!(remote_file)
      rescue Net::SFTP::StatusException => e
        raise unless e.code == 2
        puts "Copying #{local_file} to #{remote_file}"
        sftp.upload(local_file, remote_file)
        sftp.setstat(remote_file, :permissions => file_perm)
        next
      end
      
      if File.stat(local_file).mtime > Time.at(rstat.mtime + SERVER_TIME_OFFSET)
        puts "Updating #{local_file} to #{remote_file}"
        sftp.upload(local_file, remote_file)
      end
    end
  end
end

pages = ["index.part", "dan.part", "brett.part", "matt.part", "marc.part", "about_us.part"]

pages.each do |p|
  mergeWithTemplate(p)
end

uploadFile('index.css')

puts 'Checking latest...'
uploadDirectory("Latest")
puts 'Checking all...'
uploadDirectory("All")
puts 'Checking reviews...'
uploadDirectory("Reviews")
puts 'Finished'
Something went wrong with that request. Please try again.