1
+ import sys
2
+ import os
3
+ import json
4
+ import zipfile
5
+ import pathlib
6
+ import shutil
7
+ import argparse
8
+ import subprocess
9
+ from pathlib import Path
10
+ from artifactory import ArtifactoryPath
11
+
12
+ parser = argparse .ArgumentParser ()
13
+ parser .add_argument (
14
+ '-d' , '--distribute' ,
15
+ help = 'Upload the built files to their distribution channels.' ,
16
+ action = 'store_true'
17
+ )
18
+ parser .add_argument (
19
+ '-c' , '--compile' ,
20
+ help = 'Produce a compiled version of the code in addition to the source version' ,
21
+ action = 'store_true'
22
+ )
23
+ parser .add_argument (
24
+ '-a' , '--addon' ,
25
+ help = 'Produce a zipped version for installation through the addon manager.' ,
26
+ action = 'store_true'
27
+ )
28
+ args = parser .parse_args ()
29
+
30
+ # build the distribution
31
+ distribution_relative_dir = 'dist'
32
+ distribution_abs_dir = os .path .join (os .getcwd (), distribution_relative_dir )
33
+ if os .path .isdir (distribution_abs_dir ):
34
+ shutil .rmtree (distribution_abs_dir )
35
+ build_command = ['python3.8' , 'setup.py' , 'bdist_wheel' ,
36
+ '-d' , distribution_relative_dir ,
37
+ f'--python-tag=py{ sys .version_info .major } { sys .version_info .minor } ' ]
38
+ subprocess .run (build_command , cwd = os .getcwd ())
39
+ source_wheel = max (
40
+ [os .path .join (distribution_abs_dir , f ) for f in os .listdir (distribution_abs_dir )],
41
+ key = os .path .getctime
42
+ )
43
+
44
+ source_wheel_name = os .path .split (source_wheel )[- 1 ]
45
+ version = source_wheel_name .split ('-' )[1 ]
46
+
47
+ compiled_wheel = None
48
+ if args .compile :
49
+ print ('Creating pyc file' )
50
+ pyc_relative_dir = os .path .join (distribution_relative_dir , 'bin' )
51
+ pyc_abs_dir = os .path .join (distribution_abs_dir , 'bin' )
52
+ build_command = [sys .executable , 'setup.py' , 'bdist_egg' ,
53
+ '-d' , pyc_relative_dir ,
54
+ '--exclude-source-files' ,
55
+ '-m' , '+c' ]
56
+ build_result = subprocess .run (build_command , cwd = os .getcwd (), capture_output = True , text = True )
57
+ wheel_command = ['wheel' , 'convert' , os .path .join (pyc_relative_dir , '*.egg' ), '-d' , pyc_relative_dir ]
58
+ wheel_result = subprocess .run (wheel_command , cwd = os .getcwd (), capture_output = True , text = True )
59
+
60
+ # move the pyc wheel file to the dist dir
61
+ path = Path ('.' )
62
+ wheel_file = list (path .glob ('**/bin/*.whl' ))[0 ]
63
+ wheel_file .rename (Path (wheel_file .parent .parent , wheel_file .name ))
64
+ compiled_wheel = os .path .join (wheel_file .parent .parent , wheel_file .name )
65
+
66
+ # remove the bin dir
67
+ shutil .rmtree (pyc_abs_dir )
68
+
69
+ addon_manager_artifacts = []
70
+ if args .addon :
71
+ name = 'mps'
72
+ print (f'Creating { name } .addon' )
73
+ # Ensure output folder exists
74
+ bin = os .path .join (os .getcwd (), 'bin' )
75
+ if not os .path .exists (bin ):
76
+ os .makedirs (bin )
77
+
78
+ with open ('addon.json' ) as json_file :
79
+ parsed_json = json .load (json_file )
80
+ parsed_json ['version' ] = version
81
+
82
+ addon = os .path .join (bin , f'{ name } .addon' )
83
+ addon_meta = os .path .join (bin , f'{ name } .addonmeta' )
84
+
85
+ # Build addon
86
+ with zipfile .ZipFile (addon , 'w' ) as z :
87
+ z .write (source_wheel , arcname = os .path .join ('data-lab-functions' , source_wheel_name ))
88
+ z .writestr ('data-lab-functions/requirements.txt' , f"./{ source_wheel_name } " )
89
+ with z .open ("addon.json" , "w" ) as c :
90
+ c .write (json .dumps (parsed_json , indent = 2 ).encode ("utf-8" ))
91
+ directory = pathlib .Path ("./seeq/addons/mps/deployment_notebook/" )
92
+ for file in directory .rglob ('*ipynb' ):
93
+ z .write (file , arcname = os .path .join ('data-lab-functions' , file .name ))
94
+ directory = pathlib .Path ("./additional_content/" )
95
+ for file in directory .iterdir ():
96
+ z .write (file )
97
+ addon_manager_artifacts .append (addon )
98
+ # Build addonmeta
99
+ print (f'Creating { name } .addonmeta' )
100
+ with zipfile .ZipFile (addon_meta , 'w' ) as z :
101
+ with z .open ("addon.json" , "w" ) as c :
102
+ c .write (json .dumps (parsed_json , indent = 2 ).encode ("utf-8" ))
103
+ directory = pathlib .Path ("./additional_content/" )
104
+ for file in directory .iterdir ():
105
+ z .write (file )
106
+ addon_manager_artifacts .append (addon_meta )
107
+
108
+ print ('Successfully created.' )
109
+
110
+ if args .distribute :
111
+
112
+ # THIS BLOCK OF CODE IS NO MORE IN USE BUT COULD LATER BE USED IN FUTURE PURPOSES
113
+ # if compiled_wheel is not None:
114
+ # print(f'Distributing compiled wheel {compiled_wheel} to pipy.seeq.com')
115
+ # command_distribute_compiled = \
116
+ # ['twine', 'upload',
117
+ # '--repository-url', 'https://pypi.seeq.com',
118
+ # '-u', username,
119
+ # '-p', password,
120
+ # compiled_wheel]
121
+ # result = subprocess.run(' '.join(command_distribute_compiled))
122
+ # if result.stderr:
123
+ # print(f'There was an error uploading the compiled version: {result.stderr}')
124
+ #
125
+ # print(f'Distributing source wheel {source_wheel} to pypi.seeq.com:8081')
126
+ # command_distribute_source = \
127
+ # ['twine', 'upload',
128
+ # '--repository-url', 'https://pypi.seeq.com:8081',
129
+ # '-u', username,
130
+ # '-p', password,
131
+ # source_wheel]
132
+ # result = subprocess.run(' '.join(command_distribute_source))
133
+ # if result.stderr:
134
+ # print(f'There was an error uploading the source version: {result.stderr}')
135
+
136
+ if addon_manager_artifacts :
137
+ print (f'Distributing addon manager artifacts to seeq.jfrog.io' )
138
+ api_key = os .getenv ('JFROG_API_KEY' )
139
+ for artifact in addon_manager_artifacts :
140
+ _ , file = os .path .split (artifact )
141
+ path = ArtifactoryPath (f"https://seeq.jfrog.io/artifactory/seeq-add-ons-prod-local/mps/{ file } " ,
142
+ apikey = api_key )
143
+ try :
144
+ path .deploy_file (artifact )
145
+ properties = path .properties
146
+ # Add identifier property
147
+ properties ["identifier" ] = "com.seeq.addon.mps"
148
+ path .properties = properties
149
+ except Exception as e :
150
+ print (e )
0 commit comments