22
22
23
23
RAPIDS_LICENSE = "Apache 2.0"
24
24
ACCEPTABLE_LICENSES = {
25
- "Apache 2.0" ,
25
+ RAPIDS_LICENSE ,
26
26
"BSD-3-Clause" ,
27
27
}
28
28
29
29
30
- def find_value_location (document , key ):
30
+ def find_value_location (document , key , append ):
31
31
copied_document = copy .deepcopy (document )
32
32
placeholder = uuid .uuid4 ()
33
33
34
+ # tomlkit does not provide "mark" information to determine where exactly in the
35
+ # document a value is located, so instead we replace it with a placeholder and
36
+ # look for that in the new document.
34
37
node = copied_document
35
- while len (key ) > 1 :
38
+ while len (key ) > ( 0 if append else 1 ) :
36
39
node = node [key [0 ]]
37
40
key = key [1 :]
38
- old_value = node [key [0 ]]
39
- node [key [0 ]] = str (placeholder )
40
-
41
- begin_loc = copied_document .as_string ().find (
42
- tomlkit .string (str (placeholder )).as_string ()
41
+ if append :
42
+ node .add (str (placeholder ), tomlkit .string (str (placeholder )))
43
+ else :
44
+ old_value = node [key [0 ]]
45
+ node [key [0 ]] = str (placeholder )
46
+
47
+ value_to_find = (
48
+ f"{ placeholder } = { tomlkit .string (str (placeholder )).as_string ()} "
49
+ if append
50
+ else tomlkit .string (str (placeholder )).as_string ()
43
51
)
44
- end_loc = begin_loc + len (old_value .as_string ())
52
+ begin_loc = copied_document .as_string ().find (value_to_find )
53
+ end_loc = begin_loc + (0 if append else len (old_value .as_string ()))
45
54
return begin_loc , end_loc
46
55
47
56
@@ -73,16 +82,7 @@ def check_pyproject_license(linter, args):
73
82
f"{{ text = { tomlkit .string (RAPIDS_LICENSE ).as_string ()} }}\n " ,
74
83
)
75
84
else :
76
- placeholder = uuid .uuid4 ()
77
- copied_document = copy .deepcopy (document )
78
- copied_document ["project" ].add (
79
- str (placeholder ), tomlkit .string (str (placeholder ))
80
- )
81
- index = copied_document .as_string ().find (
82
- f"{ placeholder } = { tomlkit .string (str (placeholder )).as_string ()} "
83
- )
84
-
85
- loc = (index , index )
85
+ loc = find_value_location (document , ("project" ,), True )
86
86
linter .add_warning (
87
87
loc , f'add project.license with value {{ text = "{ RAPIDS_LICENSE } " }}'
88
88
).add_replacement (
@@ -93,7 +93,7 @@ def check_pyproject_license(linter, args):
93
93
return
94
94
95
95
if license_value not in ACCEPTABLE_LICENSES :
96
- loc = find_value_location (document , ("project" , "license" , "text" ))
96
+ loc = find_value_location (document , ("project" , "license" , "text" ), False )
97
97
linter .add_warning (
98
98
loc , f'license should be "{ RAPIDS_LICENSE } "'
99
99
).add_replacement (loc , tomlkit .string (RAPIDS_LICENSE ).as_string ())
0 commit comments