Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Commit 290e66a

Browse files
Ignore project.pymakr file in upload/download, multiple bugfixes in upload and download features
1 parent ae520db commit 290e66a

File tree

5 files changed

+89
-56
lines changed

5 files changed

+89
-56
lines changed

lib/board/project-status.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default class ProjectStatus {
1616
constructor(shell,settings,local_folder){
1717
this.shell = shell
1818
this.logger = new Logger('ProjectStatus')
19+
this.utils = new Utils(settings)
1920
this.local_folder = local_folder
2021
this.settings = settings
2122
this.allowed_file_types = this.settings.get_allowed_file_types()
@@ -27,7 +28,6 @@ export default class ProjectStatus {
2728

2829
read(cb){
2930
var _this = this
30-
console.log("Reading project status file")
3131
this.shell.readFile('project.pymakr',function(err,content_buffs,content_str){
3232
if(err){
3333
cb(err)
@@ -100,9 +100,11 @@ export default class ProjectStatus {
100100
}
101101
var file_hashes = {}
102102

103+
files = this.utils.ignore_filter(files)
104+
103105
for(var i=0;i<files.length;i++){
104106
var filename = path + files[i]
105-
if(filename.length > 0 && filename.substring(0,1) != "." && files[i].substring(0,1) != "." && files[i].length > 0){
107+
if(filename.length > 0 && filename.substring(0,1) != "."){
106108
var file_path = this.local_folder + filename
107109
var stats = fs.lstatSync(file_path)
108110
if(stats.isDirectory()){
@@ -112,10 +114,8 @@ export default class ProjectStatus {
112114
file_hashes[filename] = [filename,"d",hash]
113115
var hashes_in_folder = this.__get_local_files_hashed(files_from_folder,filename+"/")
114116
file_hashes = Object.assign(file_hashes,hashes_in_folder)
115-
}else{
116-
console.log("No files in folder "+file_path)
117117
}
118-
}else if(this.settings.sync_all_file_types || this.allowed_file_types.indexOf(filename.split('.').pop()) > -1){
118+
}else{
119119
this.total_file_size += stats.size
120120
this.total_number_of_files += 1
121121
var contents = fs.readFileSync(file_path)
@@ -124,7 +124,6 @@ export default class ProjectStatus {
124124
}
125125
}
126126
}
127-
console.log(file_hashes)
128127
return file_hashes
129128
}
130129

@@ -135,28 +134,20 @@ export default class ProjectStatus {
135134
var board_hashes = Object.assign({}, this.board_file_hashes)
136135
var local_hashes = Object.assign({}, this.local_file_hashes)
137136

138-
console.log(board_hashes)
139-
console.log(local_hashes)
140-
141137
// all local files
142138
for(var name in local_hashes){
143139
var local_hash = this.local_file_hashes[name]
144140
var board_hash = board_hashes[name]
145-
console.log(board_hash)
146-
console.log(local_hash)
147141

148142
if(board_hash){
149-
// check is hash is the same
150-
console.log(local_hash[2],board_hash[2])
143+
// check if hash is the same
151144
if (local_hash[2] != board_hash[2]){
152145

153146
if(local_hash[1] == "f"){
154147
changed_files.push(local_hash)
155148
}else{
156149
changed_folders.push(local_hash)
157150
}
158-
}else{
159-
console.log("Same..")
160151
}
161152
delete board_hashes[name]
162153

lib/board/pyboard.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,13 @@ export default class Pyboard {
159159
this.send(CTRL_D,cb,5000)
160160
}
161161

162-
safe_boot(cb){
162+
safe_boot(cb,timeout){
163163
var _this = this
164164
this.logger.info("Safe boot")
165165
this.send_wait_for(CTRL_F,'Type "help()" for more information.\r\n>>>',function(err){
166+
this.logger.info("Safe boot done...")
166167
cb(err)
167-
},4000)
168+
},timeout)
168169

169170
}
170171

@@ -174,10 +175,11 @@ export default class Pyboard {
174175
},5000)
175176
}
176177

177-
stop_running_programs_double(cb){
178+
stop_running_programs_double(cb,timeout){
179+
178180
this.send_wait_for(CTRL_C+CTRL_C,">>>",function(err){
179181
if(cb) cb(err)
180-
},5000)
182+
},timeout)
181183
}
182184

183185

@@ -483,15 +485,17 @@ export default class Pyboard {
483485

484486

485487
var _this = this
488+
clearTimeout(this.waiting_for_timer)
486489
if(timeout){
487490
this.waiting_for_timer = setTimeout(function(){
488491
if (_this.waiting_for_cb) {
489-
_this.waiting_for_cb(new Error("timeout"),_this.receive_buffer)
492+
var tmp_cb = _this.waiting_for_cb
490493
_this.waiting_for_cb = null
491494
_this.wait_for_block = false
492495
_this.waiting_for = null
493496
_this.receive_buffer = ""
494497
_this.receive_buffer_raw = Buffer(0)
498+
tmp_cb(new Error("timeout"),_this.receive_buffer)
495499
}
496500
},timeout)
497501
}

lib/board/shell.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class Shell {
2222
this.api = new ApiWrapper()
2323
this.logger = new Logger('Shell')
2424
this.workers = new ShellWorkers(this,pyboard,settings)
25-
this.utils = new Utils()
25+
this.utils = new Utils(settings)
2626
var lib_folder = this.api.getPackageSrcPath()
2727

2828
this.logger.silly("Try to enter raw mode")
@@ -199,6 +199,24 @@ export default class Shell {
199199
})
200200
}
201201

202+
203+
get_version(cb){
204+
var _this = this
205+
var command = "import os; os.uname().release\r\n"
206+
207+
this.eval(command,function(err,content){
208+
var version = content.replace(command,'').replace(/>>>/g,'').replace(/'/g,"").replace(/\r\n/g,"").trim()
209+
var version_int = _this.utils.calculate_int_version(version)
210+
if(version_int == 0 || isNaN(version_int)){
211+
err = new Error("Error retrieving version number")
212+
}else{
213+
err = undefined
214+
}
215+
cb(err,version_int,version)
216+
})
217+
}
218+
219+
202220
compare_hash(filename,contents,cb){
203221
cb(true)
204222
return

lib/board/sync.js

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var path = require('path');
1212

1313
export default class Sync {
1414

15+
16+
1517
constructor(pyboard,settings,terminal) {
1618
this.logger = new Logger('Sync')
1719
this.api = new ApiWrapper()
@@ -24,7 +26,8 @@ export default class Sync {
2426
this.number_of_changed_files = 0
2527
this.method_action = "Downloading"
2628
this.method_name = "Download"
27-
this.utils = new Utils()
29+
30+
this.utils = new Utils(settings)
2831
this.config = Config.constants()
2932
this.allowed_file_types = this.settings.get_allowed_file_types()
3033
this.project_path = this.api.getProjectPath()
@@ -170,7 +173,7 @@ export default class Sync {
170173

171174
this.terminal.write(this.method_action+" project ("+this.folder_name+")...\r\n")
172175

173-
this.__safe_boot(function(err){
176+
_this.__safe_boot(function(err){
174177

175178
if(err){
176179
_this.logger.error("Safeboot failed")
@@ -180,9 +183,9 @@ export default class Sync {
180183
}
181184

182185
_this.logger.silly("Start shell")
183-
184186
_this.start_shell(function(err){
185187
_this.in_raw_mode = true
188+
186189
_this.project_status = new ProjectStatus(_this.shell,_this.settings,_this.py_folder)
187190
_this.logger.silly("Entered raw mode")
188191

@@ -192,8 +195,6 @@ export default class Sync {
192195
_this.exit()
193196

194197
}else{
195-
196-
197198
if(_this.method=='receive'){
198199
_this.__receive(cb,err)
199200
}else{
@@ -224,14 +225,13 @@ export default class Sync {
224225
_this.files = _this._getFilesRecursive("")
225226
var new_files = []
226227
var existing_files = []
228+
file_list = _this.utils.ignore_filter(file_list)
227229
for(var i=0;i<file_list.length;i++){
228230
var file = file_list[i]
229-
if(_this.settings.sync_all_file_types || _this.allowed_file_types.indexOf(file.split('.').pop()) > -1){
230-
if(_this.files.indexOf(file) > -1){
231-
existing_files.push(file)
232-
}else{
233-
new_files.push(file)
234-
}
231+
if(_this.files.indexOf(file) > -1){
232+
existing_files.push(file)
233+
}else{
234+
new_files.push(file)
235235
}
236236
}
237237
file_list = existing_files.concat(new_files)
@@ -276,7 +276,6 @@ export default class Sync {
276276
_this.progress("Downloading "+file_list.length+" "+_this.utils.plural("file",file_list.length)+"...")
277277
_this.progress_file_count = 0
278278
_this.number_of_changed_files = file_list.length
279-
console.log(file_list)
280279
_this.receive_files(0,file_list,function(){
281280
_this.logger.info("All items received")
282281
_this.progress("All items overritten")
@@ -335,22 +334,11 @@ export default class Sync {
335334
cb()
336335
return false
337336
}
338-
_this.get_version(function(err,version,version_str){
339-
if(err){
340-
_this.logger.info("Error requesting version")
341-
_this.progress("Failed getting version number, not safe booting")
342-
cb()
343-
}else if(version >= _this.config.safeboot_version){
344-
_this.logger.info("Safe booting...")
345-
_this.progress("Safe booting device... (see settings for more info)")
346-
_this.pyboard.safe_boot(cb)
347-
}else{
348-
_this.logger.info("Version doesn't support safe booting")
349-
_this.progress("Safe boot not available for this version ("+version_str+")")
350-
cb()
351-
}
352-
})
353-
})
337+
338+
_this.logger.info("Safe booting...")
339+
_this.progress("Safe booting device... (see settings for more info)")
340+
_this.pyboard.safe_boot(cb,4000)
341+
},500)
354342
}
355343

356344
receive_files(i,list,cb){
@@ -365,19 +353,27 @@ export default class Sync {
365353

366354
if(err){
367355
_this.progress("Failed to download "+filename)
356+
_this.logger.error(err)
368357
_this.receive_files(i+1,list,cb)
369358

370359
}else{
371360
var f = _this.py_folder + filename
372361
_this.ensureDirectoryExistence(f)
373-
var stream = fs.createWriteStream(f)
374-
stream.once('open', function(fd) {
375-
for(var j=0;j<content_buffer.length;j++){
376-
stream.write(content_buffer[j])
377-
}
378-
stream.end()
362+
try{
363+
var stream = fs.createWriteStream(f)
364+
stream.once('open', function(fd) {
365+
for(var j=0;j<content_buffer.length;j++){
366+
stream.write(content_buffer[j])
367+
}
368+
stream.end()
369+
_this.receive_files(i+1,list,cb)
370+
})
371+
}catch(e){
372+
_this.logger.error("Failed to open and write "+f)
373+
_this.logger.error(e)
374+
_this.progress("Failed to write to local file "+filename)
379375
_this.receive_files(i+1,list,cb)
380-
})
376+
}
381377
}
382378
})
383379
}

lib/helpers/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import Config from '../config.js'
1010

1111
export default class Utils {
1212

13+
constructor(settings){
14+
this.settings = settings
15+
16+
// TODO: grab from a .pyignore file or setting
17+
this.ignore_list = ["project.pymakr"]
18+
this.allowed_file_types = this.settings.get_allowed_file_types()
19+
}
20+
1321
// runs a worker recursively untill a task is Done
1422
// worker should take 2 params: value and a continuation callback
1523
// continuation callback takes 2 params: error and the processed value
@@ -58,6 +66,22 @@ export default class Utils {
5866
}
5967
}
6068

69+
ignore_filter(file_list){
70+
var _this = this
71+
var new_list = []
72+
for(var i=0;i<file_list.length;i++){
73+
var file = file_list[i]
74+
if(file && file != "" && file.length > 0 && file.substring(0,1) != "."){
75+
if(file.indexOf(".") == -1 || this.settings.sync_all_file_types || this.allowed_file_types.indexOf(file.split('.').pop()) > -1){
76+
if(this.ignore_list.indexOf(file) == -1){
77+
new_list.push(file)
78+
}
79+
}
80+
}
81+
}
82+
return new_list
83+
}
84+
6185

6286
calculate_int_version(version){
6387
var known_types = ['a', 'b', 'rc', 'r']

0 commit comments

Comments
 (0)