/ laxyfile / scripts / deploy_all.py
swadhin biswas Ok batman Β· 7/28/2025
fb9342e
348 lines Β· 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3
"""
Comprehensive Deployment Script for LaxyFile

Automated deployment to all distribution channels:
- PyPI (Python Package Index)
- GitHub Releases
- Package managers (Homebrew, Chocolatey, etc.)
"""

import os
import sys
import subprocess
import json
import requests
from pathlib import Path
from typing import Dict, List, Optional
import tempfile

# Project paths
PROJECT_ROOT = Path(__file__).parent.parent
DIST_DIR = PROJECT_ROOT / "dist"
SCRIPTS_DIR = PROJECT_ROOT / "scripts"

# Import our deployment modules
sys.path.insert(0, str(SCRIPTS_DIR))
from deploy_pypi import PyPIDeployer
from build_release import ReleaseBuilder


class GitHubReleaseManager:
    """Manages GitHub releases and asset uploads"""

    def __init__(self, repo_owner: str, repo_name: str, token: str):
        self.repo_owner = repo_owner
        self.repo_name = repo_name
        self.token = token
        self.api_base = "https://api.github.com"
        self.headers = {
            "Authorization": f"token {token}",
            "Accept": "application/vnd.github.v3+json"
        }

    def create_release(self, tag_name: str, name: str, body: str,
                      draft: bool = False, prerelease: bool = False) -> Optional[Dict]:
        """Create a new GitHub release"""
        print(f"πŸš€ Creating GitHub release: {name}")

        url = f"{self.api_base}/repos/{self.repo_owner}/{self.repo_name}/releases"

        data = {
            "tag_name": tag_name,
            "target_commitish": "main",
            "name": name,
            "body": body,
            "draft": draft,
            "prerelease": prerelease
        }

        try:
            response = requests.post(url, headers=self.headers, json=data)

            if response.status_code == 201:
                release_data = response.json()
                print(f"βœ… Release created: {release_data['html_url']}")
                return release_data
            else:
                print(f"❌ Failed to create release: {response.status_code}")
                print(response.text)
                return None

        except requests.RequestException as e:
            print(f"❌ Error creating release: {e}")
            return None

    def upload_release_asset(self, release_id: int, file_path: Path,
                           content_type: str = "application/octet-stream") -> bool:
        """Upload an asset to a GitHub release"""
        print(f"πŸ“€ Uploading asset: {file_path.name}")

        upload_url = f"{self.api_base}/repos/{self.repo_owner}/{self.repo_name}/releases/{release_id}/assets"

        params = {"name": file_path.name}
        headers = {**self.headers, "Content-Type": content_type}

        try:
            with open(file_path, "rb") as f:
                response = requests.post(upload_url, headers=headers,
                                       params=params, data=f)

            if response.status_code == 201:
                asset_data = response.json()
                print(f"βœ… Asset uploaded: {asset_data['browser_download_url']}")
                return True
            else:
                print(f"❌ Failed to upload asset: {response.status_code}")
                print(response.text)
                return False

        except Exception as e:
            print(f"❌ Error uploading asset: {e}")
            return False


class ComprehensiveDeployer:
    """Main deployment orchestrator"""

    def __init__(self, version: str):
        self.version = version
        self.github_token = os.getenv("GITHUB_TOKEN")
        self.repo_owner = "swadhinbiswas"
        self.repo_name = "laxyfile"

    def validate_environment(self) -> bool:
        """Validate deployment environment"""
        print("πŸ” Validating deployment environment...")

        # Check required environment variables
        required_vars = ["GITHUB_TOKEN"]
        missing_vars = []

        for var in required_vars:
            if not os.getenv(var):
                missing_vars.append(var)

        if missing_vars:
            print(f"❌ Missing required environment variables: {', '.join(missing_vars)}")
            return False

        # Check required tools
        required_tools = ["git", "python3"]
        missing_tools = []

        for tool in required_tools:
            try:
                subprocess.run([tool, "--version"], capture_output=True, check=True)
            except (subprocess.CalledProcessError, FileNotFoundError):
                missing_tools.append(tool)

        if missing_tools:
            print(f"❌ Missing required tools: {', '.join(missing_tools)}")
            return False

        print("βœ… Environment validation passed")
        return True

    def build_release_packages(self) -> List[Path]:
        """Build all release packages"""
        print("πŸ”¨ Building release packages...")

        builder = ReleaseBuilder()
        packages = builder.build_all_packages()

        if not packages:
            print("❌ No packages were built")
            return []

        print(f"βœ… Built {len(packages)} packages")
        return packages

    def deploy_to_pypi(self, test_first: bool = True) -> bool:
        """Deploy to PyPI"""
        print("🐍 Deploying to PyPI...")

        # Deploy to Test PyPI first
        if test_first:
            print("πŸ“ Deploying to Test PyPI first...")
            test_deployer = PyPIDeployer(test_pypi=True)
            if not test_deployer.deploy():
                print("❌ Test PyPI deployment failed")
                return False

            response = input("Test PyPI deployment successful. Deploy to production PyPI? (y/N): ")
            if response.lower() != 'y':
                print("❌ Production PyPI deployment cancelled")
                return False

        # Deploy to production PyPI
        prod_deployer = PyPIDeployer(test_pypi=False)
        return prod_deployer.deploy()

    def deploy_to_github(self, packages: List[Path]) -> bool:
        """Deploy to GitHub Releases"""
        print("πŸ™ Deploying to GitHub Releases...")

        if not self.github_token:
            print("❌ GITHUB_TOKEN not found")
            return False

        github = GitHubReleaseManager(self.repo_owner, self.repo_name, self.github_token)

        # Read release notes
        release_notes_path = DIST_DIR / f"RELEASE_NOTES_v{self.version}.md"
        if release_notes_path.exists():
            release_body = release_notes_path.read_text()
        else:
            release_body = f"LaxyFile v{self.version} release"

        # Create release
        tag_name = f"v{self.version}"
        release_name = f"LaxyFile v{self.version}"

        release_data = github.create_release(
            tag_name=tag_name,
            name=release_name,
            body=release_body,
            draft=False,
            prerelease=False
        )

        if not release_data:
            return False

        # Upload assets
        release_id = release_data["id"]
        success_count = 0

        for package_path in packages:
            if package_path.exists():
                # Determine content type
                content_type = "application/octet-stream"
                if package_path.suffix == ".tar.gz":
                    content_type = "application/gzip"
                elif package_path.suffix == ".zip":
                    content_type = "application/zip"
                elif package_path.suffix == ".deb":
                    content_type = "application/vnd.debian.binary-package"
                elif package_path.suffix == ".msi":
                    content_type = "application/x-msi"

                if github.upload_release_asset(release_id, package_path, content_type):
                    success_count += 1

        print(f"βœ… Uploaded {success_count}/{len(packages)} assets to GitHub")
        return success_count > 0

    def create_deployment_summary(self, packages: List[Path]) -> None:
        """Create deployment summary"""
        print("\n" + "="*60)
        print(f"πŸŽ‰ LaxyFile v{self.version} Deployment Summary")
        print("="*60)

        print(f"\nπŸ“¦ Packages Built: {len(packages)}")
        for package in packages:
            if package.exists():
                size_mb = package.stat().st_size / (1024 * 1024)
                print(f"  βœ… {package.name} ({size_mb:.1f} MB)")

        print(f"\nπŸš€ Deployment Channels:")
        print(f"  πŸ“¦ PyPI: https://pypi.org/project/laxyfile/{self.version}/")
        print(f"  πŸ™ GitHub: https://github.com/{self.repo_owner}/{self.repo_name}/releases/tag/v{self.version}")

        print(f"\nπŸ“‹ Installation Commands:")
        print(f"  pip install laxyfile=={self.version}")

        print(f"\nπŸ”— Useful Links:")
        print(f"  πŸ“š Documentation: https://github.com/{self.repo_owner}/{self.repo_name}#readme")
        print(f"  πŸ› Issues: https://github.com/{self.repo_owner}/{self.repo_name}/issues")
        print(f"  πŸ’¬ Discussions: https://github.com/{self.repo_owner}/{self.repo_name}/discussions")

    def deploy_all(self) -> bool:
        """Deploy to all channels"""
        print(f"πŸš€ Starting comprehensive deployment for LaxyFile v{self.version}")
        print()

        # Validate environment
        if not self.validate_environment():
            return False

        # Build packages
        packages = self.build_release_packages()
        if not packages:
            return False

        deployment_success = True

        # Deploy to PyPI
        try:
            if not self.deploy_to_pypi():
                print("⚠️ PyPI deployment failed")
                deployment_success = False
        except Exception as e:
            print(f"❌ PyPI deployment error: {e}")
            deployment_success = False

        # Deploy to GitHub
        try:
            if not self.deploy_to_github(packages):
                print("⚠️ GitHub deployment failed")
                deployment_success = False
        except Exception as e:
            print(f"❌ GitHub deployment error: {e}")
            deployment_success = False

        # Create summary
        self.create_deployment_summary(packages)

        return deployment_success


def main():
    """Main entry point"""
    import argparse

    parser = argparse.ArgumentParser(description="Deploy LaxyFile to all channels")
    parser.add_argument("--version", help="Version to deploy (default: from setup.py)")
    parser.add_argument("--skip-pypi", action="store_true", help="Skip PyPI deployment")
    parser.add_argument("--skip-github", action="store_true", help="Skip GitHub deployment")
    parser.add_argument("--dry-run", action="store_true", help="Build packages only")

    args = parser.parse_args()

    # Get version
    version = args.version
    if not version:
        # Try to get version from setup.py or __init__.py
        try:
            import laxyfile
            version = laxyfile.__version__
        except:
            version = "1.0.0"  # fallback

    deployer = ComprehensiveDeployer(version)

    if args.dry_run:
        print("πŸ§ͺ Dry run mode - building packages only")
        packages = deployer.build_release_packages()
        if packages:
            print(f"βœ… Built {len(packages)} packages successfully")
            return 0
        else:
            print("❌ Package building failed")
            return 1

    try:
        success = deployer.deploy_all()
        return 0 if success else 1

    except KeyboardInterrupt:
        print("\n❌ Deployment cancelled by user")
        return 1
    except Exception as e:
        print(f"❌ Deployment failed: {e}")
        return 1


if __name__ == "__main__":
    sys.exit(main())