Class: Cnvrg::Project
- Inherits:
-
Object
- Object
- Cnvrg::Project
- Defined in:
- lib/cnvrg/project.rb
Instance Attribute Summary collapse
-
#local_path ⇒ Object
readonly
Returns the value of attribute local_path.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
- .clone_dir(project_slug, project_owner, project_name) ⇒ Object
-
.create(project_name, path = nil, clean) ⇒ Object
Create project.
- .link(project_name, path = nil) ⇒ Object
Instance Method Summary collapse
- #clone ⇒ Object
- #compare_idx ⇒ Object
- #generate_idx ⇒ Object
- #get_idx ⇒ Object
- #get_ignore_list ⇒ Object
-
#initialize(project_home) ⇒ Project
constructor
A new instance of Project.
- #last_local_commit ⇒ Object
- #update_idx_with_commit!(commit) ⇒ Object
- #update_idx_with_files_commits!(files, commit_time) ⇒ Object
- #update_ignore_list(new_ignore) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(project_home) ⇒ Project
Returns a new instance of Project.
9 10 11 12 13 14 15 |
# File 'lib/cnvrg/project.rb', line 9 def initialize(project_home) config = YAML.load_file(project_home+"/.cnvrg/config.yml") @local_path = project_home @title = config[:project_name] @slug = config[:project_slug] @owner = config[:owner] end |
Instance Attribute Details
#local_path ⇒ Object (readonly)
Returns the value of attribute local_path.
5 6 7 |
# File 'lib/cnvrg/project.rb', line 5 def local_path @local_path end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
5 6 7 |
# File 'lib/cnvrg/project.rb', line 5 def owner @owner end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
5 6 7 |
# File 'lib/cnvrg/project.rb', line 5 def slug @slug end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
5 6 7 |
# File 'lib/cnvrg/project.rb', line 5 def title @title end |
Class Method Details
.clone_dir(project_slug, project_owner, project_name) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/cnvrg/project.rb', line 142 def self.clone_dir(project_slug,project_owner,project_name) list_dirs = [project_name, project_name + "/.cnvrg" ] list_files = [ project_name + "/.cnvrg/config.yml", ] begin config = { project_name: project_name, project_slug: project_slug, owner: project_owner} FileUtils.mkdir_p list_dirs FileUtils.touch list_files File.open(project_name + "/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } File.open(project_name + "/.cnvrg/idx.yml", "w+") { |f| f.write config.to_yaml } rescue return false end return true end |
.create(project_name, path = nil, clean) ⇒ Object
Create project
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 |
# File 'lib/cnvrg/project.rb', line 64 def self.create(project_name, path=nil,clean) if clean list_dirs = [project_name, project_name + "/.cnvrg"] else list_dirs = [project_name, project_name + "/data", project_name + "/models", project_name + "/notebooks", project_name + "/src", project_name + "/src/visualizations", project_name + "/src/features", project_name + "/.cnvrg" ] end list_files = [ project_name + "/README.md", project_name + "/.cnvrgignore", project_name + "/.cnvrg/config.yml" ] cnvrgreadme = Helpers.readme_content cnvrgignore = Helpers.cnvrgignore_content begin owner = Cnvrg::CLI.get_owner() response = Cnvrg::API.request("cli/create_project", 'POST', { title: project_name, owner:owner }) Cnvrg::CLI.is_response_success(response) response = JSON.parse response["result"] project_slug = response["slug"] config = { project_name: project_name, project_slug: project_slug, owner: owner } FileUtils.mkdir_p list_dirs FileUtils.touch list_files File.open(project_name + "/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } File.open(project_name + "/.cnvrgignore", "w+") { |f| f.write cnvrgignore } File.open(project_name + "/README.md", "w+") { |f| f.write cnvrgreadme } rescue return false end return true end |
.link(project_name, path = nil) ⇒ Object
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 |
# File 'lib/cnvrg/project.rb', line 111 def self.link(project_name, path=nil) list_dirs = [ ".cnvrg" ] list_files = [ "README.md", ".cnvrgignore", ".cnvrg/config.yml" ] cnvrgreadme = Helpers.readme_content cnvrgignore = Helpers.cnvrgignore_content begin response = Cnvrg::API.request("cli/create_project", 'POST', { title: project_name }) Cnvrg::CLI.is_response_success(response) response = JSON.parse response["result"] project_slug = response["slug"] owner = response["owner"] config = { project_name: project_name, project_slug: project_slug, owner: owner } FileUtils.mkdir_p list_dirs FileUtils.touch list_files File.open(".cnvrg/config.yml", "w+") { |f| f.write config.to_yaml } File.open(".cnvrgignore", "w+") { |f| f.write cnvrgignore } File.open("README.md", "w+") { |f| f.write cnvrgreadme } rescue return false end return true end |
Instance Method Details
#clone ⇒ Object
207 208 209 210 211 |
# File 'lib/cnvrg/project.rb', line 207 def clone response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/clone", 'POST', { project_slug: self.slug}) Cnvrg::CLI.is_response_success(response) return response end |
#compare_idx ⇒ Object
212 213 214 215 216 217 |
# File 'lib/cnvrg/project.rb', line 212 def compare_idx local_idx = self.generate_idx response = Cnvrg::API.request("users/#{self.owner}/projects/#{self.slug}/status", 'POST', { idx: local_idx }) CLI.is_response_success(response) return response end |
#generate_idx ⇒ Object
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 |
# File 'lib/cnvrg/project.rb', line 165 def generate_idx if File.exists? "#{self.local_path}/.cnvrg/idx.yml" old_idx = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") else old_idx = nil end tree_idx = Hash.new(0) list = Dir.glob("#{self.local_path}/**/**", File::FNM_DOTMATCH).reject { |x| (x =~ /\/\.{1,2}$/) or (x =~ /^#{self.local_path}\/\.cnvrg\/*/) } list_ignore = self.get_ignore_list() list.each do |e| label = e.gsub(self.local_path + "/", "") if File.directory? e if list_ignore.include? label next end tree_idx[label+"/"] = nil else if list_ignore.include? label next end sha1 = Digest::SHA1.file(e).hexdigest if old_idx.nil? or old_idx.to_h[:tree].nil? tree_idx[label] = { sha1: sha1, commit_time: nil } elsif old_idx[:tree][label].nil? or old_idx[:tree][label][:sha1] != sha1 tree_idx[label] = { sha1: sha1, commit_time: nil } else tree_idx[label] = old_idx[:tree][label] end end end idx = { commit: old_idx.to_h[:commit], tree: tree_idx } File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') { |f| f.write idx.to_yaml } return YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") end |
#get_idx ⇒ Object
204 205 206 |
# File 'lib/cnvrg/project.rb', line 204 def get_idx YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") end |
#get_ignore_list ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cnvrg/project.rb', line 43 def get_ignore_list ignore_list = [] File.open(self.local_path+"/.cnvrgignore", "r").each_line do |line| line = line.strip if line.start_with? "#" next end if line.end_with? "/" ignore_list << line.chop sub_dirs = Dir.glob("#{line}/**/*").each{ |x| x.gsub!("//","/") } ignore_list << sub_dirs.flatten else ignore_list << line end end return ignore_list.flatten end |
#last_local_commit ⇒ Object
18 19 20 21 |
# File 'lib/cnvrg/project.rb', line 18 def last_local_commit idx = YAML.load_file(@local_path + "/.cnvrg/idx.yml") return idx[:commit] end |
#update_idx_with_commit!(commit) ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/cnvrg/project.rb', line 229 def update_idx_with_commit!(commit) idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") idx_hash[:commit] = commit File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') { |f| f.write idx_hash.to_yaml } return true end |
#update_idx_with_files_commits!(files, commit_time) ⇒ Object
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/cnvrg/project.rb', line 218 def update_idx_with_files_commits!(files, commit_time) idx_hash = YAML.load_file("#{self.local_path}/.cnvrg/idx.yml") files.each do |path| idx_hash[:tree].to_h[path].to_h[:commit_time] = commit_time end File.open("#{self.local_path}/.cnvrg/idx.yml", 'w') { |f| f.write idx_hash.to_yaml } return true end |
#update_ignore_list(new_ignore) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cnvrg/project.rb', line 28 def update_ignore_list(new_ignore) if new_ignore.empty? return true end begin File.open(self.local_path+"/.cnvrgignore", "a+") do |f| new_ignore.each do |i| f.puts("#{i}\n") end end return true rescue return false end end |
#url ⇒ Object
23 24 25 26 |
# File 'lib/cnvrg/project.rb', line 23 def url url = Cnvrg::Helpers.remote_url "#{url}/#{self.owner}/projects/#{self.slug}" end |