-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathodoo.rs
1444 lines (1380 loc) · 72.5 KB
/
odoo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::core::config::Config;
use crate::core::entry_point::EntryPointType;
use crate::features::document_symbols::DocumentSymbolFeature;
use crate::threads::SessionInfo;
use crate::features::completion::CompletionFeature;
use crate::features::definition::DefinitionFeature;
use crate::features::hover::HoverFeature;
use std::collections::HashMap;
use std::cell::RefCell;
use std::rc::{Rc, Weak};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Instant;
use byteyarn::{yarn, Yarn};
use lsp_server::ResponseError;
use lsp_types::*;
use request::{RegisterCapability, Request, WorkspaceConfiguration};
use ruff_python_parser::Mode;
use tracing::{debug, error, warn, info, trace};
use std::collections::HashSet;
use weak_table::PtrWeakHashSet;
use std::process::Command;
use std::str::FromStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::env;
use std::cmp;
use regex::Regex;
use crate::{constants::*, oyarn, Sy};
use super::config::{DiagMissingImportsMode, RefreshMode};
use super::entry_point::{EntryPoint, EntryPointMgr};
use super::file_mgr::FileMgr;
use super::import_resolver::ImportCache;
use super::symbols::disk_dir_symbol::DiskDirSymbol;
use super::symbols::symbol::Symbol;
use crate::core::model::Model;
use crate::core::python_arch_builder::PythonArchBuilder;
use crate::core::python_arch_eval::PythonArchEval;
use crate::core::python_odoo_builder::PythonOdooBuilder;
use crate::core::python_validator::PythonValidator;
use crate::utils::{PathSanitizer, ToFilePath as _};
use crate::S;
//use super::python_arch_builder::PythonArchBuilder;
#[allow(non_camel_case_types)]
#[derive(Debug, PartialEq)]
pub enum InitState {
NOT_READY,
PYTHON_READY,
ODOO_READY,
}
#[derive(Debug)]
pub struct SyncOdoo {
pub version_major: u32,
pub version_minor: u32,
pub version_micro: u32,
pub full_version: String,
pub config: Config,
pub entry_point_mgr: Rc<RefCell<EntryPointMgr>>, //An Rc to be able to clone it and free session easily
pub has_main_entry:bool,
pub has_odoo_main_entry: bool,
pub has_valid_python: bool,
pub main_entry_tree: Vec<OYarn>,
pub stubs_dirs: Vec<String>,
pub stdlib_dir: String,
file_mgr: Rc<RefCell<FileMgr>>,
pub modules: HashMap<OYarn, Weak<RefCell<Symbol>>>,
pub models: HashMap<OYarn, Rc<RefCell<Model>>>,
pub interrupt_rebuild: Arc<AtomicBool>,
pub terminate_rebuild: Arc<AtomicBool>,
pub watched_file_updates: Arc<AtomicU32>,
rebuild_arch: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
rebuild_arch_eval: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
rebuild_odoo: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
rebuild_validation: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
pub state_init: InitState,
pub must_reload_paths: Vec<(Weak<RefCell<Symbol>>, String)>,
pub load_odoo_addons: bool, //indicate if we want to load odoo addons or not
pub need_rebuild: bool, //if true, the next process_rebuilds will drop everything and rebuild everything
pub import_cache: Option<ImportCache>,
pub capabilities: lsp_types::ClientCapabilities,
pub opened_files: Vec<String>,
}
unsafe impl Send for SyncOdoo {}
impl SyncOdoo {
pub fn new() -> Self {
let sync_odoo = Self {
version_major: 0,
version_minor: 0,
version_micro: 0,
full_version: "0.0.0".to_string(),
config: Config::new(),
entry_point_mgr: Rc::new(RefCell::new(EntryPointMgr::new())),
has_main_entry: false,
has_odoo_main_entry: false,
has_valid_python: false,
main_entry_tree: vec![],
file_mgr: Rc::new(RefCell::new(FileMgr::new())),
stubs_dirs: vec![env::current_dir().unwrap().join("typeshed").join("stubs").sanitize(),
env::current_dir().unwrap().join("additional_stubs").sanitize()],
stdlib_dir: env::current_dir().unwrap().join("typeshed").join("stdlib").sanitize(),
modules: HashMap::new(),
models: HashMap::new(),
interrupt_rebuild: Arc::new(AtomicBool::new(false)),
terminate_rebuild: Arc::new(AtomicBool::new(false)),
watched_file_updates: Arc::new(AtomicU32::new(0)),
rebuild_arch: PtrWeakHashSet::new(),
rebuild_arch_eval: PtrWeakHashSet::new(),
rebuild_odoo: PtrWeakHashSet::new(),
rebuild_validation: PtrWeakHashSet::new(),
state_init: InitState::NOT_READY,
must_reload_paths: vec![],
load_odoo_addons: true,
need_rebuild: false,
import_cache: None,
capabilities: lsp_types::ClientCapabilities::default(),
opened_files: vec![]
};
sync_odoo
}
pub fn reset(session: &mut SessionInfo, config: Config) {
session.log_message(MessageType::INFO, S!("Resetting Database..."));
info!("Resetting database...");
session.sync_odoo.version_major = 0;
session.sync_odoo.version_minor = 0;
session.sync_odoo.version_micro = 0;
session.sync_odoo.full_version = "0.0.0".to_string();
session.sync_odoo.config = Config::new();
FileMgr::clear(session);//only reset files, as workspace folders didn't change
session.sync_odoo.stubs_dirs = vec![env::current_dir().unwrap().join("typeshed").join("stubs").sanitize(),
env::current_dir().unwrap().join("additional_stubs").sanitize()];
session.sync_odoo.stdlib_dir = env::current_dir().unwrap().join("typeshed").join("stdlib").sanitize();
session.sync_odoo.modules = HashMap::new();
session.sync_odoo.models = HashMap::new();
session.sync_odoo.rebuild_arch = PtrWeakHashSet::new();
session.sync_odoo.rebuild_arch_eval = PtrWeakHashSet::new();
session.sync_odoo.rebuild_odoo = PtrWeakHashSet::new();
session.sync_odoo.rebuild_validation = PtrWeakHashSet::new();
session.sync_odoo.state_init = InitState::NOT_READY;
session.sync_odoo.load_odoo_addons = true;
session.sync_odoo.need_rebuild = false;
session.sync_odoo.watched_file_updates = Arc::new(AtomicU32::new(0));
//drop all entries, except entries of opened files
session.sync_odoo.entry_point_mgr.borrow_mut().reset_entry_points(false);
SyncOdoo::init(session, config);
}
pub fn init(session: &mut SessionInfo, config: Config) {
info!("Initializing odoo");
let start_time = Instant::now();
session.sync_odoo.state_init = InitState::NOT_READY;
session.send_notification("$Odoo/loadingStatusUpdate", "start");
session.sync_odoo.config = config;
if session.sync_odoo.config.no_typeshed {
session.sync_odoo.stubs_dirs.clear();
}
for stub in session.sync_odoo.config.additional_stubs.iter() {
session.sync_odoo.stubs_dirs.push(PathBuf::from(stub.clone()).sanitize());
}
if !session.sync_odoo.config.stdlib.is_empty() {
session.sync_odoo.stdlib_dir = PathBuf::from(session.sync_odoo.config.stdlib.clone()).sanitize();
}
info!("Using stdlib path: {}", session.sync_odoo.stdlib_dir);
for stub in session.sync_odoo.stubs_dirs.iter() {
let path = Path::new(stub);
let found = match path.exists() {
true => "found",
false => "not found",
};
info!("stub {:?} - {}", stub, found)
}
{
session.sync_odoo.entry_point_mgr.borrow_mut().add_entry_to_builtins(session.sync_odoo.stdlib_dir.clone());
for stub_dir in session.sync_odoo.stubs_dirs.clone().iter() {
session.sync_odoo.entry_point_mgr.borrow_mut().add_entry_to_public(stub_dir.clone());
}
let output = Command::new(session.sync_odoo.config.python_path.clone()).args(&["-c", "import sys; import json; print(json.dumps(sys.path))"]).output();
if let Err(_output) = &output {
error!("Wrong python command: {}", session.sync_odoo.config.python_path.clone());
session.send_notification("$Odoo/invalid_python_path", ());
session.send_notification("$Odoo/loadingStatusUpdate", "stop");
return;
}
session.sync_odoo.has_valid_python = true;
let output = output.unwrap();
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
session.log_message(MessageType::INFO, format!("Detected sys.path: {}", stdout));
let paths: Vec<String> = serde_json::from_str(&stdout).expect("Unable to get paths with json of sys.path output");
for path in paths.iter() {
let path = path.replace("\\\\", "\\");
let pathbuf = PathBuf::from(path);
if pathbuf.is_dir() {
let final_path = pathbuf.sanitize();
session.log_message(MessageType::INFO, format!("Adding sys.path: {}", final_path));
session.sync_odoo.entry_point_mgr.borrow_mut().add_entry_to_public( final_path.clone());
}
}
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
error!("{}", stderr);
}
}
if SyncOdoo::load_builtins(session) {
session.sync_odoo.state_init = InitState::PYTHON_READY;
SyncOdoo::build_database(session);
}
session.send_notification("$Odoo/loadingStatusUpdate", "stop");
session.log_message(MessageType::INFO, format!("End of initialization. Time taken: {} ms", start_time.elapsed().as_millis()));
}
pub fn find_stdlib_entry_point(&self) -> Rc<RefCell<EntryPoint>> {
for entry_point in self.entry_point_mgr.borrow().builtins_entry_points.iter() {
if entry_point.borrow().path == self.stdlib_dir {
return entry_point.clone();
}
}
panic!("Unable to find stdlib entry point");
}
pub fn load_builtins(session: &mut SessionInfo) -> bool {
let path = PathBuf::from(&session.sync_odoo.stdlib_dir);
let builtins_path = path.join("builtins.pyi");
if !builtins_path.exists() {
session.log_message(MessageType::ERROR, String::from("Unable to find builtins.pyi. Are you sure that typeshed has been downloaded. If you are building from source, make sure to initialize submodules with 'git submodule init' and 'git submodule update'."));
error!("Unable to find builtins at: {}", builtins_path.sanitize());
return false;
};
let tree_builtins = path.to_tree();
let entry_stdlib = session.sync_odoo.find_stdlib_entry_point();
let disk_dir_builtins = entry_stdlib.borrow().root.borrow().get_symbol(&tree_builtins, u32::MAX);
if disk_dir_builtins.is_empty() {
panic!("Unable to find builtins disk dir symbol");
}
let _builtins_rc_symbol = Symbol::create_from_path(session, &builtins_path, disk_dir_builtins[0].clone(), false);
session.sync_odoo.add_to_rebuild_arch(_builtins_rc_symbol.unwrap());
SyncOdoo::process_rebuilds(session)
}
pub fn build_database(session: &mut SessionInfo) {
session.log_message(MessageType::INFO, String::from("Building Database"));
let result = SyncOdoo::build_base(session);
if result {
SyncOdoo::build_modules(session);
}
}
pub fn read_version(session: &mut SessionInfo, release_path: PathBuf) -> (u32, u32, u32) {
let mut _version_major: u32 = 14;
let mut _version_minor: u32 = 0;
let mut _version_micro: u32 = 0;
// open release.py and get version
let release_file = fs::read_to_string(release_path.sanitize());
let release_file = match release_file {
Ok(release_file) => release_file,
Err(_) => {
session.log_message(MessageType::INFO, String::from("Unable to read release.py - Aborting"));
return (0, 0, 0);
}
};
for line in release_file.lines() {
if line.starts_with("version_info = (") {
let re = Regex::new(r#"version_info = \((['\"]?(\D+~)?\d+['\"]?, \d+, \d+, \w+, \d+, \D+)\)"#).unwrap();
let result = re.captures(line);
match result {
Some(result) => {
let version_info = result.get(1).unwrap().as_str();
let version_info = version_info.split(", ").collect::<Vec<&str>>();
let version_major = version_info[0].replace("saas~", "").replace("'", "").replace(r#"""#, "");
_version_major = version_major.parse().unwrap();
_version_minor = version_info[1].parse().unwrap();
_version_micro = version_info[2].parse().unwrap();
break;
},
None => {
session.log_message(MessageType::ERROR, String::from("Unable to detect the Odoo version. Running the tool for the version 14"));
break;
}
}
}
}
(_version_major, _version_minor, _version_micro)
}
fn build_base(session: &mut SessionInfo) -> bool {
let odoo_path = session.sync_odoo.config.odoo_path.clone();
let Some(odoo_path) = odoo_path else {
info!("Odoo path not provided. Continuing in single file mode");
return false;
};
session.sync_odoo.has_main_entry = true;
let odoo_sym = session.sync_odoo.entry_point_mgr.borrow_mut().set_main_entry(odoo_path.clone());
let odoo_entry = session.sync_odoo.entry_point_mgr.borrow().main_entry_point.as_ref().unwrap().clone();
session.sync_odoo.main_entry_tree = odoo_entry.borrow().tree.clone();
let release_path = PathBuf::from(odoo_path.clone()).join("odoo/release.py");
let odoo_addon_path = PathBuf::from(odoo_path.clone()).join("addons");
if !release_path.exists() {
session.log_message(MessageType::ERROR, String::from("Unable to find release.py - Aborting and switching to non-odoo mode"));
return false;
}
let (_version_major, _version_minor, _version_micro) = SyncOdoo::read_version(session, release_path);
if _version_major == 0 {
return false;
}
let _full_version = format!("{}.{}.{}", _version_major, _version_minor, _version_micro);
session.log_message(MessageType::INFO, format!("Odoo version: {}", _full_version));
if _version_major < 14 {
session.log_message(MessageType::ERROR, String::from("Odoo version is less than 14. The tool only supports version 14 and above. Aborting and switching to non-odoo mode"));
return false;
}
session.sync_odoo.version_major = _version_major;
session.sync_odoo.version_minor = _version_minor;
session.sync_odoo.version_micro = _version_micro;
session.sync_odoo.full_version = _full_version;
//build base
let config_odoo_path = PathBuf::from(odoo_path.clone());
let Some(odoo_sym) = odoo_sym else {
panic!("Odoo root symbol not found")
};
odoo_sym.borrow_mut().set_is_external(false);
let odoo_odoo = Symbol::create_from_path(session, &config_odoo_path.join("odoo"), odoo_sym.clone(), false);
if odoo_odoo.is_none() {
panic!("Not able to find odoo with given path. Aborting...");
}
let odoo_typ = odoo_odoo.as_ref().unwrap().borrow().typ().clone();
match odoo_typ {
SymType::PACKAGE(PackageType::PYTHON_PACKAGE) => {
odoo_odoo.as_ref().unwrap().borrow_mut().as_python_package_mut().self_import = true;
session.sync_odoo.add_to_rebuild_arch(odoo_odoo.as_ref().unwrap().clone());
},
SymType::NAMESPACE => {
//starting from > 18.0, odoo is now a namespace. Start import project from odoo/__main__.py
let main_file = Symbol::create_from_path(session, &PathBuf::from(config_odoo_path.clone()).join("odoo").join("__main__.py"), odoo_odoo.as_ref().unwrap().clone(), false);
if main_file.is_none() {
panic!("Not able to find odoo/__main__.py. Aborting...");
}
main_file.as_ref().unwrap().borrow_mut().as_file_mut().self_import = true;
session.sync_odoo.add_to_rebuild_arch(main_file.unwrap());
},
_ => panic!("Root symbol is not a package or namespace (> 18.0)")
}
session.sync_odoo.has_odoo_main_entry = true; // set it now has we need it to parse base addons
if !SyncOdoo::process_rebuilds(session){
return false;
}
//search common odoo addons path
let mut addon_symbol = session.sync_odoo.get_symbol(&odoo_path.clone(), &tree(vec!["odoo", "addons"], vec![]), u32::MAX);
if addon_symbol.is_empty() {
let odoo = session.sync_odoo.get_symbol(&odoo_path, &tree(vec!["odoo"], vec![]), u32::MAX);
if odoo.is_empty() {
session.log_message(MessageType::WARNING, "Odoo not found. Switching to non-odoo mode...".to_string());
session.sync_odoo.has_odoo_main_entry = false;
return false;
}
//if we are > 18.1, odoo.addons is not imported automatically anymore. Let's try to import it manually
let addons_folder = Symbol::create_from_path(session, &PathBuf::from(config_odoo_path).join("odoo").join("addons"), odoo_odoo.as_ref().unwrap().clone(), false);
if let Some(addons) = addons_folder {
addon_symbol = vec![addons];
} else {
session.log_message(MessageType::WARNING, "Not able to find odoo/addons. Please check your configuration. Switching to non-odoo mode...".to_string());
session.sync_odoo.has_odoo_main_entry = false;
return false;
}
}
let addon_symbol = addon_symbol[0].clone();
if odoo_addon_path.exists() {
if session.sync_odoo.load_odoo_addons {
addon_symbol.borrow_mut().add_path(
odoo_addon_path.sanitize()
);
session.sync_odoo.entry_point_mgr.borrow_mut().add_entry_to_addons(odoo_addon_path.sanitize(),
Some(odoo_entry.clone()),
Some(vec![Sy!("odoo"),
Sy!("addons")]));
}
} else {
session.log_message(MessageType::WARNING, format!("Unable to find odoo addons path at {}. You can ignore this message if you use a nightly build or if your community addons are in another addon paths.", odoo_addon_path.sanitize()));
}
for addon in session.sync_odoo.config.addons.clone().iter() {
let addon_path = PathBuf::from(addon);
if addon_path.exists() {
addon_symbol.borrow_mut().add_path(
addon_path.sanitize()
);
session.sync_odoo.entry_point_mgr.borrow_mut().add_entry_to_addons(addon.clone(),
Some(odoo_entry.clone()),
Some(vec![Sy!("odoo"),
Sy!("addons")]));
}
}
return true;
}
fn build_modules(session: &mut SessionInfo) {
{
let addons_symbol = session.sync_odoo.get_symbol(session.sync_odoo.config.odoo_path.as_ref().unwrap(), &tree(vec!["odoo", "addons"], vec![]), u32::MAX)[0].clone();
let addons_path = addons_symbol.borrow().paths().clone();
for addon_path in addons_path.iter() {
info!("searching modules in {}", addon_path);
if PathBuf::from(addon_path).exists() {
//browse all dir in path
for item in PathBuf::from(addon_path).read_dir().expect("Unable to browse and odoo addon directory") {
match item {
Ok(item) => {
if item.file_type().unwrap().is_dir() && !session.sync_odoo.modules.contains_key(&oyarn!("{}", item.file_name().to_str().unwrap())) {
let module_symbol = Symbol::create_from_path(session, &item.path(), addons_symbol.clone(), true);
if module_symbol.is_some() {
session.sync_odoo.add_to_rebuild_arch(module_symbol.unwrap());
}
}
},
Err(_) => {}
}
}
}
}
}
if !SyncOdoo::process_rebuilds(session){
return;
}
//println!("{}", self.symbols.as_ref().unwrap().borrow_mut().debug_print_graph());
//fs::write("out_architecture.json", self.get_symbol(&tree(vec!["odoo", "addons", "module_1"], vec![])).as_ref().unwrap().borrow().debug_to_json().to_string()).expect("Unable to write file");
let modules_count = session.sync_odoo.modules.len();
info!("End building modules. {} modules loaded", modules_count);
session.log_message(MessageType::INFO, format!("End building modules. {} modules loaded", modules_count));
session.sync_odoo.state_init = InitState::ODOO_READY;
}
//search for a symbol with a tree local to an unknown entrypoint
pub fn get_symbol(&self, from_path: &str, tree: &Tree, position: u32) -> Vec<Rc<RefCell<Symbol>>> {
//find which entrypoint to use
for entry in self.entry_point_mgr.borrow().iter_all() {
let entry_point = entry.borrow();
if entry_point.is_public() || from_path.starts_with(&entry_point.path) {
let symbols = entry_point.root.borrow().get_symbol(&(entry_point.addon_to_odoo_tree.as_ref().unwrap_or(&entry_point.tree).iter().chain(&tree.0).map(|x| x.clone()).collect(), tree.1.clone()), position);
if !symbols.is_empty() {
return symbols;
}
}
}
//no valid entry point? that's wrong, an entry shoud have been created
warn!("Unable to find symbol for path: {}", from_path);
vec![]
}
pub fn get_main_entry(&self) -> Rc<RefCell<EntryPoint>> {
return self.entry_point_mgr.borrow().main_entry_point.as_ref().expect("Unable to find main entry point").clone()
}
fn pop_item(&mut self, step: BuildSteps) -> Option<Rc<RefCell<Symbol>>> {
let mut arc_sym: Option<Rc<RefCell<Symbol>>> = None;
//Part 1: Find the symbol with a unmutable set
{
let set = match step {
BuildSteps::ARCH_EVAL => &self.rebuild_arch_eval,
BuildSteps::ODOO => &self.rebuild_odoo,
BuildSteps::VALIDATION => &self.rebuild_validation,
_ => &self.rebuild_arch
};
let mut selected_sym: Option<Rc<RefCell<Symbol>>> = None;
let mut selected_count: u32 = 999999999;
let mut current_count: u32;
for sym in &*set {
current_count = 0;
let file = sym.borrow().get_file().unwrap().upgrade().unwrap();
let file = file.borrow();
let all_dep = file.get_all_dependencies(step);
if let Some(all_dep) = all_dep {
for (index, dep_set) in all_dep.iter().enumerate() {
if let Some(dep_set) = dep_set {
let index_set = match index {
x if x == BuildSteps::ARCH as usize => &self.rebuild_arch,
x if x == BuildSteps::ARCH_EVAL as usize => &self.rebuild_arch_eval,
x if x == BuildSteps::VALIDATION as usize => &self.rebuild_validation,
_ => continue,
};
current_count +=
dep_set.iter().filter(|dep| index_set.contains(dep)).count() as u32;
}
}
}
if current_count < selected_count {
selected_sym = Some(sym.clone());
selected_count = current_count;
if current_count == 0 {
break;
}
}
}
if selected_sym.is_some() {
arc_sym = selected_sym.map(|x| x.clone());
}
}
{
let set = match step {
BuildSteps::ARCH_EVAL => &mut self.rebuild_arch_eval,
BuildSteps::ODOO => &mut self.rebuild_odoo,
BuildSteps::VALIDATION => &mut self.rebuild_validation,
_ => &mut self.rebuild_arch
};
if arc_sym.is_none() {
set.clear(); //remove any potential dead weak ref
return None;
}
let arc_sym_unwrapped = arc_sym.unwrap();
if !set.remove(&arc_sym_unwrapped) {
panic!("Unable to remove selected symbol from rebuild set")
}
return Some(arc_sym_unwrapped);
}
}
fn add_from_self_reload(session: &mut SessionInfo) {
for (weak_sym, path) in session.sync_odoo.must_reload_paths.clone().iter() {
if let Some(parent) = weak_sym.upgrade() {
let in_addons = parent.borrow().get_main_entry_tree(session) == tree(vec!["odoo", "addons"], vec![]);
let new_symbol = Symbol::create_from_path(session, &PathBuf::from(path), parent, in_addons);
if new_symbol.is_some() {
let new_symbol = new_symbol.as_ref().unwrap().clone();
new_symbol.borrow_mut().set_is_external(false);
let new_sym_typ = new_symbol.borrow().typ();
match new_sym_typ {
SymType::PACKAGE(PackageType::PYTHON_PACKAGE) => {
new_symbol.borrow_mut().as_python_package_mut().self_import = true;
},
SymType::FILE => {
new_symbol.borrow_mut().as_file_mut().self_import = true;
},
SymType::PACKAGE(PackageType::MODULE) => {},
_ => {panic!("Unexpected symbol type: {:?}", new_sym_typ);}
}
if matches!(new_symbol.borrow().typ(), SymType::PACKAGE(PackageType::MODULE)) {
session.sync_odoo.modules.insert(new_symbol.borrow().name().clone(), Rc::downgrade(&new_symbol));
}
session.sync_odoo.must_reload_paths.retain(|x| !Weak::ptr_eq(&x.0, weak_sym));
session.sync_odoo.add_to_rebuild_arch(new_symbol.clone());
}
}
}
}
pub fn process_rebuilds(session: &mut SessionInfo) -> bool {
session.sync_odoo.interrupt_rebuild.store(false, Ordering::SeqCst);
SyncOdoo::add_from_self_reload(session);
session.sync_odoo.import_cache = Some(ImportCache{ modules: HashMap::new(), main_modules: HashMap::new() });
let mut already_arch_rebuilt: HashSet<Tree> = HashSet::new();
let mut already_arch_eval_rebuilt: HashSet<Tree> = HashSet::new();
let mut already_odoo_rebuilt: HashSet<Tree> = HashSet::new();
let mut already_validation_rebuilt: HashSet<Tree> = HashSet::new();
trace!("Starting rebuild: {:?} - {:?} - {:?} - {:?}", session.sync_odoo.rebuild_arch.len(), session.sync_odoo.rebuild_arch_eval.len(), session.sync_odoo.rebuild_odoo.len(), session.sync_odoo.rebuild_validation.len());
while !session.sync_odoo.need_rebuild && (!session.sync_odoo.rebuild_arch.is_empty() || !session.sync_odoo.rebuild_arch_eval.is_empty() || !session.sync_odoo.rebuild_odoo.is_empty() || !session.sync_odoo.rebuild_validation.is_empty()) {
if DEBUG_THREADS {
trace!("remains: {:?} - {:?} - {:?} - {:?}", session.sync_odoo.rebuild_arch.len(), session.sync_odoo.rebuild_arch_eval.len(), session.sync_odoo.rebuild_odoo.len(), session.sync_odoo.rebuild_validation.len());
}
if session.sync_odoo.terminate_rebuild.load(Ordering::SeqCst){
info!("Terminating rebuilds due to server shutdown");
return false;
}
let sym = session.sync_odoo.pop_item(BuildSteps::ARCH);
if let Some(sym_rc) = sym {
let (tree, entry) = sym_rc.borrow().get_tree_and_entry();
if already_arch_rebuilt.contains(&tree) {
info!("Already arch rebuilt, skipping");
continue;
}
already_arch_rebuilt.insert(tree);
//TODO should delete previous first
let mut builder = PythonArchBuilder::new(entry.unwrap(), sym_rc);
builder.load_arch(session);
continue;
}
let sym = session.sync_odoo.pop_item(BuildSteps::ARCH_EVAL);
if let Some(sym_rc) = sym {
let (tree, entry) = sym_rc.borrow().get_tree_and_entry();
if already_arch_eval_rebuilt.contains(&tree) {
info!("Already arch eval rebuilt, skipping");
continue;
}
already_arch_eval_rebuilt.insert(tree);
//TODO should delete previous first
let mut builder = PythonArchEval::new(entry.unwrap(), sym_rc);
builder.eval_arch(session);
continue;
}
let sym = session.sync_odoo.pop_item(BuildSteps::ODOO);
if let Some(sym_rc) = sym {
let tree = sym_rc.borrow().get_tree();
if already_odoo_rebuilt.contains(&tree) {
info!("Already odoo rebuilt, skipping");
continue;
}
already_odoo_rebuilt.insert(tree);
//TODO should delete previous first
let mut builder = PythonOdooBuilder::new(sym_rc);
builder.load_odoo_content(session);
continue;
}
let sym = session.sync_odoo.pop_item(BuildSteps::VALIDATION);
if let Some(sym_rc) = sym {
let (tree, entry) = sym_rc.borrow_mut().get_tree_and_entry();
if already_validation_rebuilt.contains(&tree) {
info!("Already validation rebuilt, skipping");
continue;
}
already_validation_rebuilt.insert(tree);
if session.sync_odoo.state_init == InitState::ODOO_READY && session.sync_odoo.interrupt_rebuild.load(Ordering::SeqCst) {
session.sync_odoo.interrupt_rebuild.store(false, Ordering::SeqCst);
session.log_message(MessageType::INFO, S!("Rebuild interrupted"));
session.request_delayed_rebuild();
session.sync_odoo.add_to_validations(sym_rc.clone());
return true;
}
let mut validator = PythonValidator::new(entry.unwrap(), sym_rc);
validator.validate(session);
continue;
}
}
if session.sync_odoo.need_rebuild {
session.log_message(MessageType::INFO, S!("Rebuild required. Resetting database on breaktime..."));
SessionInfo::request_reload(session);
}
session.sync_odoo.import_cache = None;
trace!("Leaving rebuild with remaining tasks: {:?} - {:?} - {:?} - {:?}", session.sync_odoo.rebuild_arch.len(), session.sync_odoo.rebuild_arch_eval.len(), session.sync_odoo.rebuild_odoo.len(), session.sync_odoo.rebuild_validation.len());
true
}
pub fn rebuild_arch_now(session: &mut SessionInfo, symbol: &Rc<RefCell<Symbol>>) {
session.sync_odoo.rebuild_arch.remove(symbol);
let mut builder = PythonArchBuilder::new(symbol.borrow().get_entry().unwrap(), symbol.clone());
builder.load_arch(session);
}
pub fn add_to_rebuild_arch(&mut self, symbol: Rc<RefCell<Symbol>>) {
if DEBUG_THREADS {
trace!("ADDED TO ARCH - {}", symbol.borrow().paths().first().unwrap_or(&symbol.borrow().name().to_string()));
}
if symbol.borrow().build_status(BuildSteps::ARCH) != BuildStatus::IN_PROGRESS {
let sym_clone = symbol.clone();
let mut sym_borrowed = sym_clone.borrow_mut();
sym_borrowed.set_build_status(BuildSteps::ARCH, BuildStatus::PENDING);
sym_borrowed.set_build_status(BuildSteps::ARCH_EVAL, BuildStatus::PENDING);
sym_borrowed.set_build_status(BuildSteps::ODOO, BuildStatus::PENDING);
sym_borrowed.set_build_status(BuildSteps::VALIDATION, BuildStatus::PENDING);
self.rebuild_arch.insert(symbol);
}
}
pub fn add_to_rebuild_arch_eval(&mut self, symbol: Rc<RefCell<Symbol>>) {
if DEBUG_THREADS {
trace!("ADDED TO EVAL - {}", symbol.borrow().paths().first().unwrap_or(&symbol.borrow().name().to_string()));
}
if symbol.borrow().build_status(BuildSteps::ARCH_EVAL) != BuildStatus::IN_PROGRESS {
let sym_clone = symbol.clone();
let mut sym_borrowed = sym_clone.borrow_mut();
sym_borrowed.set_build_status(BuildSteps::ARCH_EVAL, BuildStatus::PENDING);
sym_borrowed.set_build_status(BuildSteps::ODOO, BuildStatus::PENDING);
sym_borrowed.set_build_status(BuildSteps::VALIDATION, BuildStatus::PENDING);
self.rebuild_arch_eval.insert(symbol);
}
}
pub fn add_to_init_odoo(&mut self, symbol: Rc<RefCell<Symbol>>) {
if DEBUG_THREADS {
trace!("ADDED TO ODOO - {}", symbol.borrow().paths().first().unwrap_or(&symbol.borrow().name().to_string()));
}
if symbol.borrow().build_status(BuildSteps::ODOO) != BuildStatus::IN_PROGRESS {
let sym_clone = symbol.clone();
let mut sym_borrowed = sym_clone.borrow_mut();
sym_borrowed.set_build_status(BuildSteps::ODOO, BuildStatus::PENDING);
sym_borrowed.set_build_status(BuildSteps::VALIDATION, BuildStatus::PENDING);
self.rebuild_odoo.insert(symbol);
}
}
pub fn add_to_validations(&mut self, symbol: Rc<RefCell<Symbol>>) {
if DEBUG_THREADS {
trace!("ADDED TO VALIDATION - {}", symbol.borrow().paths().first().unwrap_or(&symbol.borrow().name().to_string()));
}
if symbol.borrow().build_status(BuildSteps::VALIDATION) != BuildStatus::IN_PROGRESS {
symbol.borrow_mut().set_build_status(BuildSteps::VALIDATION, BuildStatus::PENDING);
self.rebuild_validation.insert(symbol);
}
}
pub fn remove_from_rebuild_arch(&mut self, symbol: &Rc<RefCell<Symbol>>) {
self.rebuild_arch.remove(symbol);
}
pub fn remove_from_rebuild_arch_eval(&mut self, symbol: &Rc<RefCell<Symbol>>) {
self.rebuild_arch_eval.remove(symbol);
}
pub fn remove_from_rebuild_odoo(&mut self, symbol: &Rc<RefCell<Symbol>>) {
self.rebuild_odoo.remove(symbol);
}
pub fn remove_from_rebuild_validation(&mut self, symbol: &Rc<RefCell<Symbol>>) {
self.rebuild_validation.remove(symbol);
}
pub fn is_in_rebuild(&self, symbol: &Rc<RefCell<Symbol>>, step: BuildSteps) -> bool {
if step == BuildSteps::ARCH {
return self.rebuild_arch.contains(symbol);
}
if step == BuildSteps::ARCH_EVAL {
return self.rebuild_arch_eval.contains(symbol);
}
if step == BuildSteps::ODOO {
return self.rebuild_odoo.contains(symbol);
}
if step == BuildSteps::VALIDATION {
return self.rebuild_validation.contains(symbol);
}
false
}
pub fn get_file_mgr(&self) -> Rc<RefCell<FileMgr>> {
self.file_mgr.clone()
}
pub fn _unload_path(session: &mut SessionInfo, path: &PathBuf, clean_cache: bool) -> Vec<Rc<RefCell<Symbol>>> {
let mut parents = vec![];
let ep_mgr = session.sync_odoo.entry_point_mgr.clone();
for entry in ep_mgr.borrow().iter_all() {
if entry.borrow().is_valid_for(path.sanitize().as_str()) {
let tree = entry.borrow().get_tree_for_entry(path);
let path_symbol = entry.borrow().root.borrow().get_symbol(&tree, u32::MAX);
if path_symbol.is_empty() {
continue
}
let path_symbol = path_symbol[0].clone();
let parent = path_symbol.borrow().parent().clone().unwrap().upgrade().unwrap();
if clean_cache {
FileMgr::delete_path(session, &path.sanitize());
let mut to_del = Vec::from_iter(path_symbol.borrow().all_module_symbol().map(|x| x.clone()));
let mut index = 0;
while index < to_del.len() {
FileMgr::delete_path(session, &to_del[index].borrow().paths()[0]);
let mut to_del_child = Vec::from_iter(to_del[index].borrow().all_module_symbol().map(|x| x.clone()));
to_del.append(&mut to_del_child);
index += 1;
}
}
Symbol::unload(session, path_symbol.clone());
parents.push(parent);
}
}
parents
}
/*
* Give the symbol that is linked to the given path. As we consider that the file is opened, we do not search in entries that
* could have it in dependencies but are not the main entry. If not found, create a new entry (is useful if the entry was dropped before
* due to an inclusion in main entry then removed)
*/
pub fn get_symbol_of_opened_file(session: &mut SessionInfo, path: &PathBuf) -> Option<Rc<RefCell<Symbol>>> {
for entry in session.sync_odoo.entry_point_mgr.borrow().iter_for_import(session.sync_odoo.entry_point_mgr.borrow().main_entry_point.as_ref().unwrap()) {
if (entry.borrow().typ == EntryPointType::MAIN || entry.borrow().addon_to_odoo_path.is_some()) && entry.borrow().is_valid_for(path.as_os_str().to_str().unwrap()) {
let tree = entry.borrow().get_tree_for_entry(path);
let path_symbol = entry.borrow().root.borrow().get_symbol(&tree, u32::MAX);
if path_symbol.is_empty() {
continue;
}
return Some(path_symbol[0].clone());
}
}
//Not found? Then return if it is matching a non-public entry strictly matching the file
let mut found_an_entry = false; //there to ensure that a wrongly built entry would create infinite loop
for entry in session.sync_odoo.entry_point_mgr.borrow().custom_entry_points.iter() {
if !entry.borrow().is_public() && path == &PathBuf::from(&entry.borrow().path) {
found_an_entry = true;
let tree = entry.borrow().get_tree_for_entry(path);
let path_symbol = entry.borrow().root.borrow().get_symbol(&tree, u32::MAX);
if path_symbol.is_empty() {
continue;
}
return Some(path_symbol[0].clone());
}
}
if !found_an_entry {
info!("Path {} not found. Creating new entry", path.to_str().expect("unable to stringify path"));
EntryPointMgr::create_new_custom_entry_for_path(session, &path.sanitize());
SyncOdoo::process_rebuilds(session);
return SyncOdoo::get_symbol_of_opened_file(session, path)
}
None
}
/*
* Given a path, return a tree that is valid for main entry, transformed by relational entries if necessary
*/
pub fn path_to_main_entry_tree(&self, path: &PathBuf) -> Option<Tree> {
for entry in self.entry_point_mgr.borrow().iter_main() {
if (entry.borrow().typ == EntryPointType::MAIN || entry.borrow().addon_to_odoo_path.is_some()) && entry.borrow().is_valid_for(path.sanitize().as_str()) {
let tree = entry.borrow().get_tree_for_entry(path);
return Some(tree);
}
}
None
}
pub fn is_in_workspace_or_entry(session: &mut SessionInfo, path: &str) -> bool {
if session.sync_odoo.file_mgr.borrow().is_in_workspace(path) {
return true;
}
for entry in session.sync_odoo.entry_point_mgr.borrow().custom_entry_points.iter() {
let entry = entry.borrow();
if path == entry.path {
return true
}
}
false
}
pub fn is_in_main_entry(session: &mut SessionInfo, path: &Vec<OYarn>) -> bool{
path.starts_with(session.sync_odoo.main_entry_tree.as_slice())
}
pub fn refresh_evaluations(session: &mut SessionInfo) {
let ep_mgr = session.sync_odoo.entry_point_mgr.clone();
for entry in ep_mgr.borrow().iter_all() {
let mut symbols = vec![entry.borrow().root.clone()];
while symbols.len() > 0 {
let s = symbols.pop();
if let Some(s) = s {
if s.borrow().in_workspace() && matches!(&s.borrow().typ(), SymType::FILE | SymType::PACKAGE(_)) {
session.sync_odoo.add_to_rebuild_arch_eval(s.clone());
}
symbols.extend(s.borrow().all_module_symbol().map(|x| {x.clone()}) );
}
}
}
SyncOdoo::process_rebuilds(session);
}
pub fn get_rebuild_queue_size(&self) -> usize {
return self.rebuild_arch.len() + self.rebuild_arch_eval.len() + self.rebuild_odoo.len() + self.rebuild_validation.len()
}
pub fn load_capabilities(&mut self, capabilities: &lsp_types::ClientCapabilities) {
info!("Client capabilities: {:?}", capabilities);
self.capabilities = capabilities.clone();
}
}
#[derive(Debug)]
pub struct Odoo {}
impl Odoo {
fn update_configuration(session: &mut SessionInfo) -> Result<Config, String> {
let configuration_item = ConfigurationItem{
scope_uri: None,
section: Some("Odoo".to_string()),
};
let config_params = ConfigurationParams {
items: vec![configuration_item],
};
let config = match session.send_request::<ConfigurationParams, Vec<serde_json::Value>>(WorkspaceConfiguration::METHOD, config_params) {
Ok(config) => config.unwrap(),
Err(_) => {
return Err(S!("Unable to get configuration from client, client not available"));
}
};
let config = config.get(0);
if !config.is_some() {
session.log_message(MessageType::ERROR, String::from("No config found for Odoo. Exiting..."));
return Err(S!("no config found for Odoo"));
}
let config = config.unwrap();
//values for sync block
let mut _refresh_mode : RefreshMode = RefreshMode::OnSave;
let mut _auto_save_delay : u64 = 2000;
let mut _ac_filter_model_names : bool = true;
let mut _diag_missing_imports : DiagMissingImportsMode = DiagMissingImportsMode::All;
let mut selected_configuration: String = S!("");
let mut configurations = serde_json::Map::new();
if let Some(map) = config.as_object() {
for (key, value) in map {
match key.as_str() {
"autoRefresh" => {
if let Some(refresh_mode) = value.as_str() {
_refresh_mode = match RefreshMode::from_str(refresh_mode) {
Ok(mode) => mode,
Err(_) => {
session.log_message(MessageType::ERROR, String::from("Unable to parse RefreshMode. Setting it to onSave"));
RefreshMode::OnSave
}
};
}
},
"autoRefreshDelay" => {
if let Some(refresh_delay) = value.as_u64() {
_auto_save_delay = std::cmp::max(refresh_delay, 1000);
} else {
session.log_message(MessageType::ERROR, String::from("Unable to parse auto_save_delay. Setting it to 2000"));
_auto_save_delay = 2000
}
},
"autocompletion" => {
if let Some(autocompletion_config) = value.as_object() {
for (key, value) in autocompletion_config {
match key.as_str() {
"filterModelNames" =>{
if let Some(ac_filter_model_names) = value.as_bool() {
_ac_filter_model_names = ac_filter_model_names;
} else {
session.log_message(MessageType::ERROR, String::from("Unable to parse autocompletion.ac_filter_model_names . Setting it to true"));
}
}
_ => {
session.log_message(MessageType::ERROR, format!("Unknown autocompletion config key: autocompletion.{}", key));
},
}
}
} else {
session.log_message(MessageType::ERROR, String::from("Unable to parse autocompletion_config"));
}
},
"diagMissingImportLevel" => {
if let Some(diag_import_level) = value.as_str() {
_diag_missing_imports = match DiagMissingImportsMode::from_str(diag_import_level) {
Ok(mode) => mode,
Err(_) => {
session.log_message(MessageType::ERROR, String::from("Unable to parse diag_import_level. Setting it to all"));
DiagMissingImportsMode::All
}
};
}
},
"configurations" => {
if let Some(values)= value.as_object() {
configurations = values.clone();
}
},
"selectedConfiguration" => {
if let Some(value_str) = value.as_str() {
selected_configuration = value_str.to_string();
}
},
"serverLogLevel" | "disablePythonLanguageServerPopup" => {
// Too late, set it with command line
// disablePythonLanguageServerPopup, does not affect us, pass
},
_ => {
session.log_message(MessageType::ERROR, format!("Unknown config key: {}", key));
},
}
}
}
debug!("configurations: {:?}", configurations);
debug!("selected_configuration: {:?}", selected_configuration);
let mut config = Config::new();
if configurations.contains_key(&selected_configuration) {
let odoo_conf = configurations.get(&selected_configuration).unwrap();
let odoo_conf = odoo_conf.as_object().unwrap();
config.addons = odoo_conf.get("validatedAddonsPaths").expect("An odoo config must contains a addons value")
.as_array().expect("the addons value must be an array")
.into_iter().map(|v| v.as_str().unwrap().to_string()).collect();
let odoo_path = odoo_conf.get("odooPath");
if let Some(odoo_path) = odoo_path {
config.odoo_path = Some(odoo_path.as_str().expect("odooPath must be a String").to_string());
}
if let Some(python_path) = odoo_conf.get("finalPythonPath") {
if python_path.is_string() {
config.python_path = python_path.as_str().unwrap().to_string();
} else {
session.log_message(MessageType::ERROR, String::from("pythonPath must be a string, using 'python' as pythonPath"));
config.python_path = S!("python");
}
} else {
session.log_message(MessageType::ERROR, String::from("pythonPath must be defined, using 'python' as pythonPath"));
config.python_path = S!("python");
}
} else {
config.addons = vec![];
config.odoo_path = None;
session.log_message(MessageType::ERROR, S!("Unable to find selected configuration. No odoo path has been found."));
}
config.refresh_mode = _refresh_mode;
config.auto_save_delay = _auto_save_delay;
config.ac_filter_model_names = _ac_filter_model_names;
config.diag_missing_imports = _diag_missing_imports;
debug!("Final config: {:?}", config);
Ok(config)
}
pub fn init(session: &mut SessionInfo) {
let start = std::time::Instant::now();
session.log_message(MessageType::LOG, String::from("Building new Odoo knowledge database"));
let config = Odoo::update_configuration(session);
match config {