99import sqlite3
1010import threading
1111from collections import defaultdict
12- from xml .etree import ElementTree
1312import re
1413from datetime import datetime , timedelta
1514import getpass
@@ -440,16 +439,15 @@ def evaluate_args(self) -> Any:
440439 except subprocess .CalledProcessError :
441440 raise NixEvalError
442441
443- def evaluate_config (self , attr ):
442+ def evaluate_config (self , attr ) -> Dict :
444443 try :
445- # FIXME: use --json
446- xml = subprocess .check_output (
444+ _json = subprocess .check_output (
447445 ["nix-instantiate" ]
448446 + self .extra_nix_eval_flags
449447 + self ._eval_flags (self .nix_exprs )
450448 + [
451449 "--eval-only" ,
452- "--xml " ,
450+ "--json " ,
453451 "--strict" ,
454452 "--arg" ,
455453 "checkConfigurationOptions" ,
@@ -461,25 +459,19 @@ def evaluate_config(self, attr):
461459 text = True ,
462460 )
463461 if DEBUG :
464- print ("XML output of nix-instantiate:\n " + xml , file = sys .stderr )
462+ print ("JSON output of nix-instantiate:\n " + _json , file = sys .stderr )
465463 except OSError as e :
466464 raise Exception ("unable to run ‘nix-instantiate’: {0}" .format (e ))
467465 except subprocess .CalledProcessError :
468466 raise NixEvalError
469467
470- tree = ElementTree .fromstring (xml )
471-
472- # Convert the XML to a more Pythonic representation. This is
473- # in fact the same as what json.loads() on the output of
474- # "nix-instantiate --json" would yield.
475- config = nixops .util .xml_expr_to_python (tree .find ("*" ))
476- return (tree , config )
468+ return json .loads (_json )
477469
478470 def evaluate_network (self , action : str = "" ) -> None :
479471 if not self .network_attr_eval :
480472 # Extract global deployment attributes.
481473 try :
482- ( _ , config ) = self .evaluate_config ("info.network" )
474+ config = self .evaluate_config ("info.network" )
483475 except Exception as e :
484476 if action not in ("destroy" , "delete" ):
485477 raise e
@@ -494,22 +486,20 @@ def evaluate(self) -> None:
494486 self .definitions = {}
495487 self .evaluate_network ()
496488
497- (tree , config ) = self .evaluate_config ("info" )
489+ config = self .evaluate_config ("info" )
490+
491+ tree = None
498492
499493 # Extract machine information.
500- for x in tree .findall ("attrs/attr[@name='machines']/attrs/attr" ):
501- name = x .get ("name" )
502- cfg = config ["machines" ][name ]
503- defn = _create_definition (x , cfg , cfg ["targetEnv" ])
494+ for name , cfg in config ["machines" ].items ():
495+ defn = _create_definition (name , cfg , cfg ["targetEnv" ])
504496 self .definitions [name ] = defn
505497
506498 # Extract info about other kinds of resources.
507- for x in tree .findall ("attrs/attr[@name='resources']/attrs/attr" ):
508- res_type = x .get ("name" )
509- for y in x .findall ("attrs/attr" ):
510- name = y .get ("name" )
499+ for res_type , cfg in config ["resources" ].items ():
500+ for name , y in cfg .items ():
511501 defn = _create_definition (
512- y , config ["resources" ][res_type ][name ], res_type
502+ name , config ["resources" ][res_type ][name ], res_type
513503 )
514504 self .definitions [name ] = defn
515505
@@ -604,13 +594,13 @@ def do_machine(m: nixops.backends.MachineState) -> None:
604594 attrs_list = attrs_per_resource [m .name ]
605595
606596 # Set system.stateVersion if the Nixpkgs version supports it.
607- nixos_version = nixops .util .parse_nixos_version (defn .config [ " nixosRelease" ] )
597+ nixos_version = nixops .util .parse_nixos_version (defn .config . nixosRelease )
608598 if nixos_version >= ["15" , "09" ]:
609599 attrs_list .append (
610600 {
611601 ("system" , "stateVersion" ): Call (
612602 RawValue ("lib.mkDefault" ),
613- m .state_version or defn .config [ " nixosRelease" ] ,
603+ m .state_version or defn .config . nixosRelease ,
614604 )
615605 }
616606 )
@@ -1674,16 +1664,14 @@ def _subclasses(cls: Any) -> List[Any]:
16741664 return [cls ] if not sub else [g for s in sub for g in _subclasses (s )]
16751665
16761666
1677- def _create_definition (xml : Any , config : Dict [str , Any ], type_name : str ) -> Any :
1667+ def _create_definition (
1668+ name : str , config : Dict [str , Any ], type_name : str
1669+ ) -> nixops .resources .ResourceDefinition :
16781670 """Create a resource definition object from the given XML representation of the machine's attributes."""
16791671
16801672 for cls in _subclasses (nixops .resources .ResourceDefinition ):
16811673 if type_name == cls .get_resource_type ():
1682- # FIXME: backward compatibility hack
1683- if len (inspect .getargspec (cls .__init__ ).args ) == 2 :
1684- return cls (xml )
1685- else :
1686- return cls (xml , config )
1674+ return cls (name , nixops .resources .ResourceEval (config ))
16871675
16881676 raise nixops .deployment .UnknownBackend (
16891677 "unknown resource type ‘{0}’" .format (type_name )
0 commit comments