File tree Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+ from __future__ import annotations
6
+
7
+ import sys
8
+
9
+
10
+ def add_future_import (filename ):
11
+ with open (filename , encoding = "utf-8" ) as f :
12
+ lines = f .readlines ()
13
+
14
+ # Check if the import is already present
15
+ for line in lines :
16
+ if line .strip () == "from __future__ import annotations" :
17
+ return # Import already present, no need to modify
18
+
19
+ # Find the position to insert the import
20
+ insert_pos = 0
21
+ for i , line in enumerate (lines ):
22
+ stripped_line = line .strip ()
23
+ if stripped_line and not stripped_line .startswith ("#" ):
24
+ insert_pos = i
25
+ break
26
+
27
+ # Insert the import statement after the first comment block
28
+ lines .insert (insert_pos , "from __future__ import annotations\n \n " )
29
+
30
+ # Write the modified lines back to the file
31
+ with open (filename , "w" , encoding = "utf-8" ) as f :
32
+ f .writelines (lines )
33
+
34
+
35
+ def main ():
36
+ files = sys .argv [1 :]
37
+ for f in files :
38
+ add_future_import (f )
39
+ print ("Processed files to ensure `from __future__ import annotations` is present." )
40
+
41
+
42
+ if __name__ == "__main__" :
43
+ main ()
Original file line number Diff line number Diff line change
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ from __future__ import annotations
7
+
8
+ import sys
9
+
10
+ HEADER = """# Copyright (c) Meta Platforms, Inc. and affiliates.
11
+ #
12
+ # This source code is licensed under the MIT license found in the
13
+ # LICENSE file in the root directory of this source tree.
14
+ """
15
+
16
+
17
+ def check_header (filename ):
18
+ with open (filename , encoding = "utf-8" ) as f :
19
+ file_content = f .read ()
20
+
21
+ if not file_content .startswith (HEADER ):
22
+ print (f"Missing or incorrect header in { filename } " )
23
+ return False
24
+ return True
25
+
26
+
27
+ def main ():
28
+ files = sys .argv [1 :]
29
+ all_passed = True
30
+ for f in files :
31
+ if not check_header (f ):
32
+ all_passed = False
33
+ if not all_passed :
34
+ sys .exit (1 )
35
+ sys .exit (0 )
36
+
37
+
38
+ if __name__ == "__main__" :
39
+ main ()
You can’t perform that action at this time.
0 commit comments