1
1
from setuptools import setup
2
2
3
- from setuptools .command .install import install as install_command
4
- import os
5
- import subprocess
6
- import shutil
7
- import re
8
-
9
-
10
- class Wheel (install_command ):
11
-
12
- user_options = install_command .user_options + [
13
- ('whlsrc=' , None , "Build a wheel for the python code that is in this directory. Copy into 'libs' directory." ),
14
- ('libdir=' , None , "The directory to put the whl files." ),
15
- ('reqfiles=' , None , "List of requirement.txt to update." ),
16
- ]
17
-
18
- def initialize_options (self ):
19
- install_command .initialize_options (self )
20
- self .whlsrc = None
21
- self .libdir = None
22
- self .reqfiles = None
23
-
24
- def finalize_options (self ):
25
- install_command .finalize_options (self )
26
-
27
- def run (self ):
28
- global whlsrc
29
- global libdir
30
- global reqfiles
31
- whlsrc = self .whlsrc
32
- libdir = self .libdir
33
- reqfiles = self .reqfiles
34
-
35
- if isinstance (reqfiles , list ) is False :
36
- reqfiles = [reqfiles ]
37
-
38
- current_dir = os .getcwd ()
39
- try :
40
- # Get existing fiels in the lib directory.
41
- os .chdir (self .libdir )
42
- sp = subprocess .run (["ls" ], capture_output = True , text = True )
43
- existing_whls = []
44
- for file in sp .stdout .split ("\n " ):
45
- if file .endswith ("whl" ) is True :
46
- existing_whls .append (file )
47
-
48
- # Installed required modules and build a wheel
49
- os .chdir (whlsrc )
50
- subprocess .run (["pip3" , "install" , "-r" , "requirements.txt" ])
51
- subprocess .run (["python3" , "setup.py" , "bdist_wheel" ])
52
-
53
- # Find the whl file in the dist folder.
54
- os .chdir (os .path .join (whlsrc , "dist" ))
55
- sp = subprocess .run (["ls" ], capture_output = True , text = True )
56
- wheel_file = None
57
- for file in sp .stdout .split ("\n " ):
58
- if file .endswith ("whl" ) is True :
59
- wheel_file = file
60
- break
61
- if wheel_file is None :
62
- raise ValueError (f"Cannot find a whl file in the dist directory of the { whlsrc } project." )
63
-
64
- # Copy the whl to the lib directory
65
- subprocess .run (["cp" , wheel_file , self .libdir ])
66
-
67
- project_name = wheel_file [:wheel_file .index ("-" )]
68
-
69
- # Remove old versions of the wheel.
70
- os .chdir (self .libdir )
71
- for existing_whl in existing_whls :
72
- if existing_whl .startswith (project_name ) is False :
73
- continue
74
- if existing_whl == wheel_file :
75
- continue
76
- os .unlink (existing_whl )
77
-
78
- for req in reqfiles :
79
- shutil .copy (req , f"{ req } .bak" )
80
- requirement_data = []
81
- with open (req , "r" ) as fh :
82
- requirement_data = fh .readlines ()
83
- fh .close ()
84
-
85
- pattern = re .compile (re .escape (project_name ) + "-.*?.whl" )
86
- with open (req , "w" ) as fh :
87
- for line in requirement_data :
88
- line = re .sub (pattern , wheel_file , line )
89
- fh .write (line )
90
- fh .close ()
91
- os .unlink (f"{ req } .bak" )
92
-
93
- finally :
94
- os .chdir (current_dir )
95
-
96
-
97
3
if __name__ == '__main__' :
98
- setup (
99
- cmdclass = {
100
- 'wheel' : Wheel
101
- }
102
- )
4
+ setup ()
0 commit comments