Class: CodeRunner::Repository

Inherits:
Git::Base
  • Object
show all
Defined in:
lib/coderunner/repository.rb

Overview

This is a class which implements methods for managing a CodeRunner repository, which is a slightly customised git repository. It contains methods for initializing standard files, and maintains a small amount of metadata about the repository.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open_in_subfolder(folder = Dir.pwd) ⇒ Object

Open a git object from within a subfolder of a repository Checks to see if the subfolder actually is inside a CodeRunner repository.



44
45
46
47
48
# File 'lib/coderunner/repository.rb', line 44

def self.open_in_subfolder(folder = Dir.pwd)
  f2 = repo_folder(folder)
  raise "#{folder} is not a coderunner repository " if not f2
  return open(f2)
end

.repo_folder(folder = Dir.pwd) ⇒ Object

If the folder is within a coderunner repository return the root folder of the repository; else return nil



31
32
33
34
35
36
37
38
39
40
# File 'lib/coderunner/repository.rb', line 31

def self.repo_folder(folder = Dir.pwd)
  f2 = File.expand_path(folder)
  while not (Dir.entries(f2).include?('.git') and 
        Dir.entries(f2).include?('.code_runner_repo_metadata'))
    f2 = File.expand_path(f2 + '/..')
    (f2=nil; break) if f2 == '/' 
    p 'f2 is ', f2
  end
  return f2
end

Instance Method Details

#add_folder(folder) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coderunner/repository.rb', line 71

def add_folder(folder)
  Dir.chdir(folder) do
    require 'find'
    #files = []
    Find.find('.') { |e| (puts e; add(e)) if
      e =~ /code_runner_info.rb/ or
      e =~ /code_runner_results.rb/ or 
      e =~ /.code-runner-irb-save-history/ or
      e =~ /.code_runner_script_defaults.rb/ or
      (Dir.entries(Dir.pwd).include?('.code_runner_script_defaults') and
       (repo_file_match = (
         rcp = CodeRunner.fetch_runner(Y: folder, U: true).run_class.rcp; 
         rcp.repo_file_match? ? rcp.repo_file_match : false); 
         repo_file_match =~ m
       )
      )
    }
  end
  autocommit_all("Added folder #{relative_path(folder)}")
end

#autocommit(*args) ⇒ Object



91
92
93
# File 'lib/coderunner/repository.rb', line 91

def autocommit(*args)
  commit(*args) if [:autocommit]
end

#autocommit_all(*args) ⇒ Object



94
95
96
# File 'lib/coderunner/repository.rb', line 94

def autocommit_all(*args)
  commit_all(*args) if [:autocommit]
end

#changed_in_folder(folder) ⇒ Object



133
134
135
# File 'lib/coderunner/repository.rb', line 133

def changed_in_folder(folder)
  status.changed.find_all{|k,f| File.expand_path(dir.to_s + '/' + f.path).include?(File.expand_path(folder))}
end

#init_defaults_folderObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/coderunner/repository.rb', line 97

def init_defaults_folder
  FileUtils.makedirs(repo_file("defaults_files"))
  File.open(repo_file("defaults_files/README"), "w"){|f|
     f.puts  <<EOF
This folder is where defaults files for codes should be placed, with
paths such as defaults_files/<code>crmod/my_defaults.rb. This folder 
will automatically be checked for defaults files when submitting simulations
within this repository.
EOF
  
  }
  add(repo_file("defaults_files/README"))
  autocommit_all('Added defaults folder')
end

#init_metadataObject



60
61
62
63
64
65
66
67
# File 'lib/coderunner/repository.rb', line 60

def 
  Hash.phoenix(repo_file('.code_runner_repo_metadata')) do |hash|
    hash[:creation_time] = Time.now.to_i
    hash[:autocommit] = true
  end
  add(repo_file('.code_runner_repo_metadata'))
  autocommit_all('Added metadata')
end

#init_readmeObject



55
56
57
58
59
# File 'lib/coderunner/repository.rb', line 55

def init_readme
  File.open(repo_file("README.md"), "w"){|f| f.puts readme_text}
  add(repo_file("README.md"))
  autocommit_all('Added README.md')
end

#metadataObject



68
69
70
# File 'lib/coderunner/repository.rb', line 68

def 
  Hash.phoenix(repo_file('.code_runner_repo_metadata'))
end

#readme_textObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/coderunner/repository.rb', line 111

def readme_text
  return <<EOF
#{File.basename(dir.path)} CodeRunner Repository
================================================

This is a coderunner repository, which consists of
a managed set of simulation folders and defaults files which are 
synchronised across systems using git. 

This readme is a stub which was created automatically...
feel free to modify this and describe this repo.

Created on: #{Time.now.to_s}

EOF
end

#relative_path(folder = Dir.pwd) ⇒ Object



49
50
51
# File 'lib/coderunner/repository.rb', line 49

def relative_path(folder=Dir.pwd)
  File.expand_path(folder).sub(File.expand_path(dir.to_s) + '/', '')
end

#repo_file(path) ⇒ Object



52
53
54
# File 'lib/coderunner/repository.rb', line 52

def repo_file(path)
  "#{dir}/#{path}"
end

#rsyncd(remote_name, folder) ⇒ Object

Bring all files in the given folder from the given remote. (Obviously folder must be a subfolder within the repository).



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/coderunner/repository.rb', line 139

def rsyncd(remote_name, folder)
  #f2 = File.expand_path(folder)
  namehost, remote_folder = split_url(remote_name)
  rpath = relative_path(folder)
  if File.expand_path(folder) == File.expand_path(dir.to_s)
    raise "Cannot run rsyncd in the top level of a repository"
  end 
  string =  "rsync -av #{namehost}:#{remote_folder}/#{rpath}/ #{folder}/"
  #Dir.chdir(folder) do
    #FileUtils.touch('dummyfile')
    #add('dummyfile')
    #autocommit_all('--Added dummyfile')
    #system "echo 'Hello' >> dummyfile"
    ##add('dummyfile')
  #end
  #p status.changed.map{|k,f| [p1=File.expand_path(folder), p2=File.expand_path(dir.to_s + '/' + f.path), p2.include?(p1), p1.include?(p2)]}
  #p changed_in_folder(folder).map{|k,f| f.path}
  if changed_in_folder(folder).size > 0
    raise "You have some uncommitted changes in the folder #{folder}. Please commit these changes before calling rsyncd"
  end
  puts string
  system string
end

#rsyncu(remote_name, folder) ⇒ Object

Send all files in the given folder to the given remote. (Obviously folder must be a subfolder within the repository).



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/coderunner/repository.rb', line 165

def rsyncu(remote_name, folder)
  #f2 = File.expand_path(folder)
  namehost, remote_folder = split_url(remote_name)
  rpath = relative_path(folder)
  if File.expand_path(folder) == File.expand_path(dir.to_s)
    raise "Cannot run rsyncd in the top level of a repository"
  end 
  string =  "rsync -av  #{folder}/ #{namehost}:#{remote_folder}/#{rpath}/"
  #p status.changed.map{|k,f| [p1=File.expand_path(folder), p2=File.expand_path(dir.to_s + '/' + f.path), p2.include?(p1), p1.include?(p2)]}
  #p changed_in_folder(folder).map{|k,f| f.path}

  cif = `ssh #{namehost} "cd #{remote_folder}/#{rpath} && echo "START" && git status"`
  #p cif
  if cif =~ /START.*modified/m
    raise "You have some uncommitted changes in the remote folder #{rpath}. Please commit these changes before calling rsyncu"
  end
  puts string
  system string
end

#split_url(remote_name) ⇒ Object



127
128
129
130
131
132
# File 'lib/coderunner/repository.rb', line 127

def split_url(remote_name)
  remote(remote_name).url =~ (/ssh:\/\/(?<namehost>[^\/]+)(?<folder>.*$)/)
  namehost = $~[:namehost]
  folder = $~[:folder]
  return [namehost, folder]
end