diff --git a/Gemfile b/Gemfile index f97f4a242..9d74b4cf8 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ gem 'rails', '3.0.11' #, :git => 'git://github.com/rails/rails.git' gem 'pg', '~> 0.11.0' gem 'silent-postgres', '~> 0.1.1' +gem 'schema_plus', '~> 0.2.1' gem 'devise', '~> 1.5.2' gem 'omniauth', '~> 1.0.1' diff --git a/Gemfile.lock b/Gemfile.lock index b2ccdde64..fa60ae811 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -209,6 +209,9 @@ GEM i18n (>= 0.5.0) sanitize (2.0.3) nokogiri (>= 1.4.4, < 1.6) + schema_plus (0.2.1) + rails + valuable sexp_processor (3.0.8) silent-postgres (0.1.1) sinatra (1.2.7) @@ -225,6 +228,7 @@ GEM kgio (~> 2.4) rack raindrops (~> 0.6) + valuable (0.9.2) warden (1.1.0) rack (>= 1.0) web-app-theme (0.7.0) @@ -281,6 +285,7 @@ DEPENDENCIES ruby-debug ruby_parser russian + schema_plus (~> 0.2.1) silent-postgres (~> 0.1.1) unicorn (~> 4.1.1) web-app-theme diff --git a/app/controllers/personal_repositories_controller.rb b/app/controllers/personal_repositories_controller.rb index 1b9e84556..e5fadb31d 100644 --- a/app/controllers/personal_repositories_controller.rb +++ b/app/controllers/personal_repositories_controller.rb @@ -7,7 +7,7 @@ class PersonalRepositoriesController < ApplicationController def show if params[:query] - @projects = @repository.projects.recent.by_name(params[:query]).paginate :page => params[:project_page], :per_page => 30 + @projects = @repository.projects.recent.by_name("%#{params[:query]}%").paginate :page => params[:project_page], :per_page => 30 else @projects = @repository.projects.recent.paginate :page => params[:project_page], :per_page => 30 end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index a870d584c..094268d4e 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -17,13 +17,13 @@ class ProjectsController < ApplicationController end.accessible_by(current_ability) @projects = if params[:query] - @projects.by_name(params[:query]).order("CHAR_LENGTH(name) ASC") + @projects.by_name("%#{params[:query]}%").order("CHAR_LENGTH(name) ASC") else @projects end.paginate(:page => params[:project_page]) @own_projects = current_user.own_projects - @part_projects = current_user.projects + current_user.groups.map(&:projects).flatten.uniq - @own_projects + #@part_projects = current_user.projects + current_user.groups.map(&:projects).flatten.uniq - @own_projects end def show diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 85d0175cc..47217859f 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -19,7 +19,7 @@ class RepositoriesController < ApplicationController def show if params[:query] - @projects = @repository.projects.recent.by_name(params[:query]).paginate :page => params[:project_page], :per_page => 30 + @projects = @repository.projects.recent.by_name("%#{params[:query]}%").paginate :page => params[:project_page], :per_page => 30 else @projects = @repository.projects.recent.paginate :page => params[:project_page], :per_page => 30 end diff --git a/app/models/group.rb b/app/models/group.rb index 864acc69a..5ea1427f5 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -21,6 +21,7 @@ class Group < ActiveRecord::Base delegate :ssh_key, :to => :owner after_create :add_owner_to_members + after_initialize lambda {|r| r.name ||= r.uname } # default include Modules::Models::PersonalRepository # include Modules::Models::Owner diff --git a/app/models/platform.rb b/app/models/platform.rb index e0768062a..afbb31da3 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -14,7 +14,7 @@ class Platform < ActiveRecord::Base has_many :groups, :through => :objects, :source => :object, :source_type => 'Group' validates :description, :presence => true, :uniqueness => true - validates :name, :uniqueness => true, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-]+$/ } + validates :name, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-]+$/ } validates :distrib_type, :presence => true, :inclusion => {:in => APP_CONFIG['distr_types']} before_create :xml_rpc_create, :unless => lambda {Thread.current[:skip]} diff --git a/app/models/project.rb b/app/models/project.rb index 8f8600ee3..3a0343c49 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -9,7 +9,7 @@ class Project < ActiveRecord::Base has_many :build_lists, :dependent => :destroy has_many :auto_build_lists, :dependent => :destroy - # has_many :project_imports, :dependent => :destroy + has_many :project_imports, :dependent => :destroy has_many :project_to_repositories, :dependent => :destroy has_many :repositories, :through => :project_to_repositories @@ -17,18 +17,18 @@ class Project < ActiveRecord::Base has_many :collaborators, :through => :relations, :source => :object, :source_type => 'User' has_many :groups, :through => :relations, :source => :object, :source_type => 'Group' - validates :name, :uniqueness => {:scope => [:owner_id, :owner_type]}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-\+\.]+$/ } + validates :name, :uniqueness => {:scope => [:owner_id, :owner_type], :case_sensitive => false}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-\+\.]+$/ } validates :owner, :presence => true validate { errors.add(:base, :can_have_less_or_equal, :count => MAX_OWN_PROJECTS) if owner.projects.size >= MAX_OWN_PROJECTS } # validate {errors.add(:base, I18n.t('flash.project.save_warning_ssh_key')) if owner.ssh_key.blank?} validates_attachment_size :srpm, :less_than => 500.megabytes - validates_attachment_content_type :srpm, :content_type => ['application/octet-stream'], :message => I18n.t('layout.invalid_content_type') # "application/x-rpm", "application/x-redhat-package-manager" ? + validates_attachment_content_type :srpm, :content_type => ['application/octet-stream', "application/x-rpm", "application/x-redhat-package-manager"], :message => I18n.t('layout.invalid_content_type') #attr_accessible :category_id, :name, :description, :visibility attr_readonly :name scope :recent, order("name ASC") - scope :by_name, lambda { |name| where('name like ?', "%#{ name }%") } + scope :by_name, lambda {|name| where('projects.name ILIKE ?', name)} scope :by_visibilities, lambda {|v| {:conditions => ['visibility in (?)', v.join(',')]}} scope :addable_to_repository, lambda { |repository_id| where("projects.id NOT IN (SELECT project_to_repositories.project_id FROM project_to_repositories WHERE (project_to_repositories.repository_id = #{ repository_id }))") } scope :automateable, where("projects.id NOT IN (SELECT auto_build_lists.project_id FROM auto_build_lists)") @@ -39,7 +39,7 @@ class Project < ActiveRecord::Base after_destroy :destroy_git_repo after_destroy :destroy_wiki - after_save {|p| p.delay.import_srpm if p.srpm?} # should be after create_git_repo + after_save {|p| p.delay.import_attached_srpm if p.srpm?} # should be after create_git_repo # after_rollback lambda { destroy_git_repo rescue true if new_record? } has_ancestry @@ -152,11 +152,8 @@ class Project < ActiveRecord::Base @platforms ||= repositories.map(&:platform).uniq end - def import_srpm(branch_name = 'import') - if srpm? - system("#{Rails.root.join('bin', 'import_srpm.sh')} #{srpm.path} #{path} #{branch_name} >> /dev/null 2>&1") - self.srpm = nil; save # clear srpm - end + def import_srpm(srpm_path = srpm.path, branch_name = 'import') + system("#{Rails.root.join('bin', 'import_srpm.sh')} #{srpm_path} #{path} #{branch_name} >> /dev/null 2>&1") end class << self @@ -168,36 +165,43 @@ class Project < ActiveRecord::Base protected - def build_path(dir) - File.join(APP_CONFIG['root_path'], 'git_projects', "#{dir}.git") - end + def build_path(dir) + File.join(APP_CONFIG['root_path'], 'git_projects', "#{dir}.git") + end - def build_wiki_path(dir) - File.join(APP_CONFIG['root_path'], 'git_projects', "#{dir}.wiki.git") - end + def build_wiki_path(dir) + File.join(APP_CONFIG['root_path'], 'git_projects', "#{dir}.wiki.git") + end - def attach_to_personal_repository - repositories << self.owner.personal_repository if !repositories.exists?(:id => self.owner.personal_repository) - end + def attach_to_personal_repository + repositories << self.owner.personal_repository if !repositories.exists?(:id => self.owner.personal_repository) + end - def create_git_repo - is_root? ? Grit::Repo.init_bare(path) : parent.git_repository.repo.delay.fork_bare(path) - end + def create_git_repo + is_root? ? Grit::Repo.init_bare(path) : parent.git_repository.repo.delay.fork_bare(path) + end - def destroy_git_repo - FileUtils.rm_rf path - end + def destroy_git_repo + FileUtils.rm_rf path + end - def create_wiki - if has_wiki && !FileTest.exist?(wiki_path) - Grit::Repo.init_bare(wiki_path) - wiki = Gollum::Wiki.new(wiki_path, {:base_path => Rails.application.routes.url_helpers.project_wiki_index_path(self)}) - wiki.write_page('Home', :markdown, I18n.t("wiki.seed.welcome_content"), - {:name => owner.name, :email => owner.email, :message => 'Initial commit'}) - end + def import_attached_srpm + if srpm? + import_srpm # srpm.path + self.srpm = nil; save # clear srpm end + end - def destroy_wiki - FileUtils.rm_rf wiki_path + def create_wiki + if has_wiki && !FileTest.exist?(wiki_path) + Grit::Repo.init_bare(wiki_path) + wiki = Gollum::Wiki.new(wiki_path, {:base_path => Rails.application.routes.url_helpers.project_wiki_index_path(self)}) + wiki.write_page('Home', :markdown, I18n.t("wiki.seed.welcome_content"), + {:name => owner.name, :email => owner.email, :message => 'Initial commit'}) end + end + + def destroy_wiki + FileUtils.rm_rf wiki_path + end end diff --git a/app/models/project_import.rb b/app/models/project_import.rb new file mode 100644 index 000000000..b50ca7761 --- /dev/null +++ b/app/models/project_import.rb @@ -0,0 +1,9 @@ +class ProjectImport < ActiveRecord::Base + belongs_to :project + + validates :name, :uniqueness => {:case_sensitive => false}, :presence => true + + scope :by_name, lambda {|name| where('project_imports.name ILIKE ?', name)} + + after_initialize lambda {|r| r.file_mtime ||= Time.current - 10.years } # default +end diff --git a/app/models/project_to_repository.rb b/app/models/project_to_repository.rb index a5a623123..0679957eb 100644 --- a/app/models/project_to_repository.rb +++ b/app/models/project_to_repository.rb @@ -13,8 +13,7 @@ class ProjectToRepository < ActiveRecord::Base protected def one_project_in_platform_repositories - c = Platform.scoped.select('projects.*').joins(:repositories => :projects).where( - :projects => {:name => project.name}, :id => repository.platform_id).count - errors.add(:project, 'should be one in platform') if c > 0 + errors.add(:project, 'should be one in platform') if Project.joins(:repositories => :platform). + where('platforms.id = ?', repository.platform_id).by_name(project.name).count > 0 end end diff --git a/app/models/repository.rb b/app/models/repository.rb index b9246f86a..188b13e27 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -11,7 +11,7 @@ class Repository < ActiveRecord::Base has_many :groups, :through => :objects, :source => :object, :source_type => 'Group' validates :description, :uniqueness => {:scope => :platform_id}, :presence => true - validates :name, :uniqueness => {:scope => :platform_id}, :presence => true, :format => { :with => /^[a-z0-9_\-]+$/ } + validates :name, :uniqueness => {:scope => :platform_id, :case_sensitive => false}, :presence => true, :format => { :with => /^[a-z0-9_\-]+$/ } # validates :platform_id, :presence => true # if you uncomment this platform clone will not work scope :recent, order("name ASC") diff --git a/app/views/projects/_own_projects_sidebar.html.haml b/app/views/projects/_own_projects_sidebar.html.haml index ca8ba9880..5b7ac9a92 100644 --- a/app/views/projects/_own_projects_sidebar.html.haml +++ b/app/views/projects/_own_projects_sidebar.html.haml @@ -6,11 +6,11 @@ - @own_projects.each do |project| %li = link_to project.name, project_path(project) -.block.notice - %h3= t("layout.users.part_projects") - .content - %p - %ul - - @part_projects.each do |project| - %li - = link_to project.owner.uname + '/' + project.name, project_path(project) +-#.block.notice +-# %h3= t("layout.users.part_projects") +-# .content +-# %p +-# %ul +-# - @part_projects.each do |project| +-# %li +-# = link_to project.owner.uname + '/' + project.name, project_path(project) diff --git a/config/production.pill b/config/production.pill index f347a5941..ff49759fc 100644 --- a/config/production.pill +++ b/config/production.pill @@ -13,6 +13,16 @@ Bluepill.application(app_name) do |app| process.stop_command = "/usr/bin/env RAILS_ENV=production script/delayed_job stop" process.pid_file = File.join(app.working_dir, 'tmp', 'pids', 'delayed_job.pid') end + + app.process("newrelic") do |process| + process.start_grace_time = 10.seconds + process.stop_grace_time = 10.seconds + process.restart_grace_time = 10.seconds + + path = File.join(app.working_dir, 'tmp', 'pids', 'newrelic.pid') + process.start_command = "/usr/bin/env /usr/local/bin/nrsysmond -c /etc/newrelic/nrsysmond.cfg -p #{path}" + process.pid_file = path + end app.process("unicorn") do |process| process.start_grace_time = 8.seconds diff --git a/config/schedule.rb b/config/schedule.rb index a9b4c3440..2488e53e3 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -11,3 +11,7 @@ every 5.minutes do runner "Download.rotate_nginx_log" runner "Download.parse_and_remove_nginx_log" end + +every 1.day, :at => '4:00 am' do + rake "import:sync:all", :output => 'log/sync.log' +end diff --git a/db/migrate/20120117210132_create_project_imports.rb b/db/migrate/20120117210132_create_project_imports.rb new file mode 100644 index 000000000..28231a844 --- /dev/null +++ b/db/migrate/20120117210132_create_project_imports.rb @@ -0,0 +1,16 @@ +class CreateProjectImports < ActiveRecord::Migration + def self.up + create_table :project_imports do |t| + t.references :project + t.string :name + t.string :version + t.datetime :file_mtime + + t.timestamps + end + end + + def self.down + drop_table :project_imports + end +end diff --git a/db/migrate/20120127234602_add_index_for_projects_name.rb b/db/migrate/20120127234602_add_index_for_projects_name.rb new file mode 100644 index 000000000..c82a885b7 --- /dev/null +++ b/db/migrate/20120127234602_add_index_for_projects_name.rb @@ -0,0 +1,11 @@ +class AddIndexForProjectsName < ActiveRecord::Migration + def self.up + add_index :projects, [:name, :owner_id, :owner_type], :unique => true, :case_sensitive => false + add_index :project_imports, :name, :unique => true, :case_sensitive => false + end + + def self.down + remove_index :projects, [:name, :owner_id, :owner_type] + remove_index :project_imports, :name + end +end diff --git a/db/schema.rb b/db/schema.rb index dbe011a36..bdaefd2bc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,27 +10,25 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120126214447) do +ActiveRecord::Schema.define(:version => 20120127234602) do create_table "arches", :force => true do |t| t.string "name", :null => false t.datetime "created_at" t.datetime "updated_at" + t.index ["name"], :name => "index_arches_on_name", :unique => true end - add_index "arches", ["name"], :name => "index_arches_on_name", :unique => true - create_table "authentications", :force => true do |t| t.integer "user_id" t.string "provider" t.string "uid" t.datetime "created_at" t.datetime "updated_at" + t.index ["provider", "uid"], :name => "index_authentications_on_provider_and_uid", :unique => true + t.index ["user_id"], :name => "index_authentications_on_user_id" end - add_index "authentications", ["provider", "uid"], :name => "index_authentications_on_provider_and_uid", :unique => true - add_index "authentications", ["user_id"], :name => "index_authentications_on_user_id" - create_table "auto_build_lists", :force => true do |t| t.integer "project_id" t.integer "arch_id" @@ -48,10 +46,9 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.datetime "created_at" t.datetime "updated_at" t.string "version" + t.index ["build_list_id"], :name => "index_build_list_items_on_build_list_id" end - add_index "build_list_items", ["build_list_id"], :name => "index_build_list_items_on_build_list_id" - create_table "build_lists", :force => true do |t| t.integer "bs_id" t.string "container_path" @@ -74,12 +71,11 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.boolean "auto_publish", :default => true t.string "package_version" t.string "commit_hash" + t.index ["arch_id"], :name => "index_build_lists_on_arch_id" + t.index ["bs_id"], :name => "index_build_lists_on_bs_id", :unique => true + t.index ["project_id"], :name => "index_build_lists_on_project_id" end - add_index "build_lists", ["arch_id"], :name => "index_build_lists_on_arch_id" - add_index "build_lists", ["bs_id"], :name => "index_build_lists_on_bs_id", :unique => true - add_index "build_lists", ["project_id"], :name => "index_build_lists_on_project_id" - create_table "categories", :force => true do |t| t.string "name" t.string "ancestry" @@ -116,10 +112,9 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.string "locked_by" t.datetime "created_at" t.datetime "updated_at" + t.index ["priority", "run_at"], :name => "delayed_jobs_priority" end - add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority" - create_table "downloads", :force => true do |t| t.string "name", :null => false t.string "version" @@ -164,10 +159,9 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.string "status", :default => "open" t.datetime "created_at" t.datetime "updated_at" + t.index ["project_id", "serial_id"], :name => "index_issues_on_project_id_and_serial_id", :unique => true end - add_index "issues", ["project_id", "serial_id"], :name => "index_issues_on_project_id_and_serial_id", :unique => true - create_table "platforms", :force => true do |t| t.string "description" t.string "name" @@ -197,10 +191,9 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.datetime "notified_at" t.datetime "created_at" t.datetime "updated_at" + t.index ["product_id"], :name => "index_product_build_lists_on_product_id" end - add_index "product_build_lists", ["product_id"], :name => "index_product_build_lists_on_product_id" - create_table "products", :force => true do |t| t.string "name", :null => false t.integer "platform_id", :null => false @@ -222,6 +215,15 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.boolean "use_cron", :default => false end + create_table "project_imports", :force => true do |t| + t.integer "project_id" + t.string "name" + t.string "version" + t.datetime "file_mtime" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "project_to_repositories", :force => true do |t| t.integer "project_id" t.integer "repository_id" @@ -239,16 +241,17 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.integer "category_id" t.text "description" t.string "ancestry" +<<<<<<< HEAD t.boolean "has_wiki", :default => false t.boolean "has_issues", :default => true t.integer "srpm_file_size" t.string "srpm_file_name" t.string "srpm_content_type" t.datetime "srpm_updated_at" + t.index ["category_id"], :name => "index_projects_on_category_id" + t.index ["owner_id"], :name => "index_projects_on_name_and_owner_id_and_owner_type", :unique => true end - add_index "projects", ["category_id"], :name => "index_projects_on_category_id" - create_table "relations", :force => true do |t| t.integer "object_id" t.string "object_type" @@ -269,30 +272,16 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.string "owner_type" end - create_table "role_lines", :force => true do |t| - t.integer "role_id" - t.integer "relation_id" - t.datetime "created_at" - t.datetime "updated_at" - end - - create_table "roles", :force => true do |t| - t.string "name" - t.datetime "created_at" - t.datetime "updated_at" - end - create_table "rpms", :force => true do |t| t.string "name", :null => false t.integer "arch_id", :null => false t.integer "project_id", :null => false t.datetime "created_at" t.datetime "updated_at" + t.index ["project_id"], :name => "index_rpms_on_project_id" + t.index ["project_id", "arch_id"], :name => "index_rpms_on_project_id_and_arch_id" end - add_index "rpms", ["project_id", "arch_id"], :name => "index_rpms_on_project_id_and_arch_id" - add_index "rpms", ["project_id"], :name => "index_rpms_on_project_id" - create_table "settings_notifiers", :force => true do |t| t.integer "user_id", :null => false t.boolean "can_notify", :default => true @@ -327,10 +316,9 @@ ActiveRecord::Schema.define(:version => 20120126214447) do t.string "role" t.integer "own_projects_count", :default => 0, :null => false t.string "language", :default => "en" + t.index ["email"], :name => "index_users_on_email", :unique => true + t.index ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true + t.index ["uname"], :name => "index_users_on_uname", :unique => true end - add_index "users", ["email"], :name => "index_users_on_email", :unique => true - add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true - add_index "users", ["uname"], :name => "index_users_on_uname", :unique => true - end diff --git a/lib/grack/base.rb b/lib/grack/base.rb index a4fd2a4d5..bc5b1657f 100644 --- a/lib/grack/base.rb +++ b/lib/grack/base.rb @@ -37,7 +37,8 @@ module Grack name.gsub! /\.git$/, '' name.gsub! /\.wiki$/, '' owner = User.find_by_uname(uname) || Group.find_by_uname(uname) - Project.where(:owner_id => owner.id, :owner_type => owner.class).find_by_name(name) + scoped = Project.where(:owner_id => owner.id, :owner_type => owner.class) + scoped.find_by_name(name) || scoped.by_name(name).first end end diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 88f65117d..c7e15781b 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -25,4 +25,84 @@ namespace :import do end say 'DONE' end + + namespace :sync do + desc "Sync all repos" + task :all do + system("bundle exec rake import:sync:run RELEASE=official/2011 PLATFORM=mandriva2011 REPOSITORY=main") + system("bundle exec rake import:sync:run RELEASE=official/2011 PLATFORM=mandriva2011 REPOSITORY=contrib") + system("bundle exec rake import:sync:run RELEASE=official/2011 PLATFORM=mandriva2011 REPOSITORY=non-free") + system("bundle exec rake import:sync:run RELEASE=devel/cooker PLATFORM=cooker REPOSITORY=main") + system("bundle exec rake import:sync:run RELEASE=devel/cooker PLATFORM=cooker REPOSITORY=contrib") + system("bundle exec rake import:sync:run RELEASE=devel/cooker PLATFORM=cooker REPOSITORY=non-free") + end + + task :run => [:rsync, :parse] + + desc "Rsync with mirror.yandex.ru" + task :rsync => :environment do + release = ENV['RELEASE'] || 'official/2011' + repository = ENV['REPOSITORY'] || 'main' + source = "rsync://mirror.yandex.ru/mandriva/#{release}/SRPMS/#{repository}/" + destination = ENV['DESTINATION'] || File.join(APP_CONFIG['root_path'], 'mirror.yandex.ru', 'mandriva', release, 'SRPMS', repository) + say "START rsync projects (*.src.rpm) from '#{source}' to '#{destination}'" + if system "rsync -rtv --delete #{source} #{destination}" # TODO --include='*.src.rpm' --exclude='*' + say 'Rsync ok!' + else + say 'Rsync failed!' + end + say 'DONE' + end + + desc "Parse repository for changes" + task :parse => :environment do + release = ENV['RELEASE'] || 'official/2011' + platform = Platform.find_by_name(ENV['PLATFORM'] || "mandriva2011") + repository = platform.repositories.find_by_name(ENV['REPOSITORY'] || 'main') + source = ENV['SOURCE'] || File.join(APP_CONFIG['root_path'], 'mirror.yandex.ru', 'mandriva', release, 'SRPMS', repository.name) + owner = Group.find_or_create_by_uname(ENV['OWNER'] || 'import') {|g| g.owner = User.first} + branch = "import_#{platform.name}" + + say 'START' + Dir[File.join source, '{release,updates}', '*.src.rpm'].each do |srpm_file| + say "=== Processing '#{srpm_file}'..." + if name = `rpm -q --qf '[%{Name}]' -p #{srpm_file}` and $?.success? and name.present? and + version = `rpm -q --qf '[%{Version}]' -p #{srpm_file}` and $?.success? and version.present? + project_import = ProjectImport.find_by_name(name) || ProjectImport.by_name(name).first || ProjectImport.new(:name => name) + if version != project_import.version.to_s and File.mtime(srpm_file) > project_import.file_mtime + unless project = project_import.project + if project = repository.projects.find_by_name(name) || repository.projects.by_name(name).first # fallback to speedup + say "Found project '#{project.owner.uname}/#{project.name}'" + elsif scoped = Project.where(:owner_id => owner.id, :owner_type => owner.class) and + project = scoped.find_by_name(name) || scoped.by_name(name).first + repository.projects << project + say "Add project '#{project.owner.uname}/#{project.name}' to '#{platform.name}/#{repository.name}'" + else + description = ::Iconv.conv('UTF-8//IGNORE', 'UTF-8', `rpm -q --qf '[%{Description}]' -p #{srpm_file}`) + project = Project.create!(:name => name, :description => description) {|p| p.owner = owner} + repository.projects << project + say "Create project #{project.owner.uname}/#{project.name} in #{platform.name}/#{repository.name}" + end + end + project.import_srpm(srpm_file, branch) + say "New version (#{version}) for '#{project.owner.uname}/#{project.name}' successfully imported to branch '#{branch}'!" + + project_import.project = project + project_import.version = version + project_import.file_mtime = File.mtime(srpm_file) + project_import.save! + + # TODO notify import.members + + say '=== Success!' + else + say '=== Not changed!' + end + else + say '=== Fail!' + end + end + say 'DONE' + end + end end diff --git a/spec/factories/project_imports.rb b/spec/factories/project_imports.rb new file mode 100644 index 000000000..e52c8a374 --- /dev/null +++ b/spec/factories/project_imports.rb @@ -0,0 +1,9 @@ +# Read about factories at http://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :project_import do + project nil + name "MyString" + version "MyString" + end +end \ No newline at end of file diff --git a/spec/models/project_import_spec.rb b/spec/models/project_import_spec.rb new file mode 100644 index 000000000..e7448f1d6 --- /dev/null +++ b/spec/models/project_import_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe ProjectImport do + pending "add some examples to (or delete) #{__FILE__}" +end