#!/usr/bin/env ruby # -*- coding: utf-8 -*- # # Triggers: # CreateThumbnails change-commit ... "%//triggers/CreateThumbnails.rb% # %serverport% %change%" # # Description: # Goes through the changelist looking for files that can be given a # thumbnail. Creates the PNG thumbnails using imagemagick, then sends # the thumbnail to the server as a hex string. # # By default this trigger will attempt to make a thumbnail of any file # that has an extension. For instance, it will try and fail to make a # thumbnail for .txt and .sh files. If you would like to restrict the # file types that this trigger will attempt to make thumbnails for, use # the '-r' option, and add the extension to the extensions array. # SET THESE OR TRIGGER WILL NOT WORK p4bin = '/usr/local/bin/p4' convertbin = '/usr/local/bin/convert' user = 'jbrower' client = 'jbrower_primary' client_path = '/Users/jbrower/ws/local/primary' passwd = 'j1ml1nk1' # ALL SET #valid_extensions = [] describe_regex = /^...\s+(\/\/[^\/]+(\/[^\.]+)(\.([^\.#]+)){1}#[0-9]+)\s+.+$/ # With the example string '... //depot/images/flower.png#1 add' # '//depot/images/flower.png#1' = '\1' # '/images/flower' = '\2' # 'png' = '\4' def bin_to_hex(s) s.each_byte.map {|b| b.to_s(16).rjust(2, '0')}.join end begin if ARGV.length != 2 puts 'Usage: CreateThumbnails %serverport% %change%' end serverport = ARGV[0] change = ARGV[1] p4 = "#{p4bin} -p #{serverport} -u #{user} -c #{client} -P #{passwd}" files = [] output = `#{p4} describe -s #{change}` output.each_line do |line| if line =~ describe_regex files << line end end files.each do |file| depot_file_full = file.gsub(describe_regex, '\1').chomp depot_file_partial = file.gsub(describe_regex, '\2').chomp extension = file.gsub(describe_regex, '\4').chomp local_file_noext = "#{client_path}#{depot_file_partial}_temp" local_file_full = "#{local_file_noext}.#{extension}" thumb_name = "#{local_file_noext}_thumb.png" hex_name = "#{local_file_noext}_hex" # Get file from depot by printing image_string = `#{p4} print -q #{depot_file_full}` File.open(local_file_full, "w") {|file| file.write(image_string)} if system("#{convertbin} #{local_file_full} \ -thumbnail 240x240^ #{thumb_name}") thumb_string = File.read(thumb_name) hex_image = bin_to_hex(thumb_string) File.open(hex_name, "w") {|file| file.write(hex_image)} `cat #{hex_name} | #{p4} attribute -e -f -n thumb -i #{depot_file_full}` File.delete(thumb_name, hex_name) end File.delete(local_file_full) end end exit 0