@@ -43,64 +43,87 @@ def yaml_construct_fallback(loader, node):
43
43
)
44
44
45
45
46
+ def harvest_dot_conda (io_like , filename ):
47
+ from conda_package_streaming import package_streaming
48
+ stream = package_streaming .stream_conda_component (
49
+ filename , io_like , component = package_streaming .CondaComponent .info
50
+ )
51
+ data = harvest_tarfile (stream )
52
+ stream .close ()
53
+
54
+ return data
55
+
56
+
46
57
def harvest (io_like ):
47
58
tf = tarfile .open (fileobj = io_like , mode = "r:bz2" )
48
-
49
- # info/files
50
- file_listing = tf .extractfile ("info/files" ).readlines ()
51
- file_listing = (fn .decode ("utf8" ).strip () for fn in file_listing if fn )
52
- file_listing = [fn for fn in file_listing if filter_file (fn )]
53
-
54
- # info/recipe/meta.yaml
55
- try :
56
- rendered_recipe = ruamel_yaml .safe_load (tf .extractfile ("info/recipe/meta.yaml" ))
57
- except ScannerError :
58
- # Non parseable
59
- rendered_recipe = {}
60
- except KeyError :
61
- # older artifacts have a meta.yaml in another location
62
- try :
63
- rendered_recipe = ruamel_yaml .safe_load (tf .extractfile ("info/meta.yaml" ))
64
- except ScannerError :
65
- # Non parseable
66
- rendered_recipe = {}
67
-
68
- try :
69
- raw_recipe = (
70
- tf .extractfile ("info/recipe/meta.yaml.template" ).read ().decode ("utf8" )
71
- )
72
- except KeyError :
73
- raw_recipe = tf .extractfile ("info/recipe/meta.yaml" ).read ().decode ("utf8" )
74
-
75
- try :
76
- conda_build_config = ruamel_yaml .safe_load (
77
- tf .extractfile ("info/recipe/conda_build_config.yaml" )
78
- )
79
- except KeyError :
80
- conda_build_config = {}
81
-
82
- try :
83
- about = json .load (tf .extractfile ("info/about.json" ))
84
- except KeyError :
85
- about = {}
86
- index = json .load (tf .extractfile ("info/index.json" ))
59
+ return harvest_tarfile (tf )
60
+
61
+
62
+ def harvest_tarfile (tf_or_stream ):
63
+ rendered_recipe = {}
64
+ index = {}
65
+ about = {}
66
+ raw_recipe = ""
67
+ conda_build_config = {}
68
+ raw_recipe_backup = ""
69
+
70
+ for _data in tf_or_stream :
71
+ if isinstance (_data , tarfile .TarInfo ):
72
+ mem = _data
73
+ tf = tf_or_stream
74
+ else :
75
+ tf , mem = _data
76
+
77
+ if mem .name == "info/files" :
78
+ # info/files
79
+ file_listing = tf .extractfile (mem ).readlines ()
80
+ file_listing = [fn .decode ("utf8" ).strip () for fn in file_listing if fn ]
81
+ file_listing = [fn for fn in file_listing if filter_file (fn )]
82
+ elif mem .name == "info/recipe/meta.yaml" :
83
+ raw_recipe_backup = tf .extractfile (mem ).read (mem .size ).decode ("utf8" )
84
+ # info/recipe/meta.yaml
85
+ try :
86
+ rendered_recipe = ruamel_yaml .safe_load (raw_recipe_backup )
87
+ except ScannerError :
88
+ # Non parseable
89
+ rendered_recipe = {}
90
+ elif mem .name == "info/meta.yaml" :
91
+ # older artifacts have a meta.yaml in another location
92
+ try :
93
+ rendered_recipe = ruamel_yaml .safe_load (tf .extractfile (mem ).read (mem .size ))
94
+ except ScannerError :
95
+ # Non parseable
96
+ rendered_recipe = {}
97
+ elif mem .name == "info/about.json" :
98
+ about = json .loads (tf .extractfile (mem ).read (mem .size ))
99
+ elif mem .name == "info/index.json" :
100
+ index = json .loads (tf .extractfile (mem ).read (mem .size ))
101
+ elif mem .name == "info/recipe/meta.yaml.template" :
102
+ raw_recipe = tf .extractfile (mem ).read (mem .size ).decode ("utf8" )
103
+ elif mem .name == "info/recipe/conda_build_config.yaml" :
104
+ conda_build_config = ruamel_yaml .safe_load (tf .extractfile (mem ).read (mem .size ))
87
105
88
106
return {
89
107
"metadata_version" : METADATA_VERSION ,
90
- "name" : index [ "name" ] ,
91
- "version" : index [ "version" ] ,
108
+ "name" : index . get ( "name" , "" ) ,
109
+ "version" : index . get ( "version" , "" ) ,
92
110
"index" : index ,
93
111
"about" : about ,
94
112
"rendered_recipe" : rendered_recipe ,
95
- "raw_recipe" : raw_recipe ,
113
+ "raw_recipe" : raw_recipe if len ( raw_recipe ) > 0 else raw_recipe_backup ,
96
114
"conda_build_config" : conda_build_config ,
97
115
"files" : file_listing ,
98
116
}
99
117
100
118
101
119
def harvest_from_filename (filename ):
102
120
with open (filename , "rb" ) as fo :
103
- return harvest (fo )
121
+ if filename .endswith (".tar.bz2" ):
122
+ return harvest (fo )
123
+ elif filename .endswith (".conda" ):
124
+ return harvest_dot_conda (fo , filename )
125
+ else :
126
+ raise RuntimeError (f"File '{ filename } ' is not a recognized conda format!" )
104
127
105
128
106
129
if __name__ == "__main__" :
0 commit comments