4
4
import os
5
5
import urllib .parse
6
6
from pathlib import Path
7
- from typing import List
7
+ from typing import List , Set
8
8
9
9
import more_itertools
10
+ import requests
10
11
from markdown import Markdown
11
12
12
13
from repogen import pkg_info
@@ -31,10 +32,27 @@ def fix_manifest_url(item: PackageInfo, app_dir: Path):
31
32
item ['manifestUrl' ] = manifest_url [manifest_url .find ('/api/apps' ):]
32
33
33
34
34
- def generate (packages : List [PackageInfo ], outdir : Path ):
35
+ def save_ipk (item : PackageInfo , apps_dir : Path , site_url : str ):
36
+ """
37
+ Save the ipk file to the apps directory, and modify PackageInfo to point to the local file.
38
+ """
39
+ manifest = item ['manifest' ]
40
+ app_ipk = apps_dir .joinpath (item ["id" ], 'releases' , f'{ manifest ["ipkHash" ]["sha256" ]} .ipk' )
41
+ if not app_ipk .parent .exists ():
42
+ app_ipk .parent .mkdir (parents = True , exist_ok = True )
43
+ # Not likely to happen, but just in case someday we host some ipk files by ourselves directly.
44
+ if site_url and manifest ['ipkUrl' ].startswith (site_url ):
45
+ return
46
+ with requests .get (manifest ['ipkUrl' ], allow_redirects = True ) as resp :
47
+ with ensure_open (app_ipk , 'wb' ) as f :
48
+ f .write (resp .content )
49
+ manifest ['ipkUrl' ] = f'{ site_url .removesuffix ("/" )} /{ "/" .join (app_ipk .parts [- 4 :])} '
50
+
51
+
52
+ def generate (packages : List [PackageInfo ], api_dir : Path , apps_dir : Path = None , host_packages : Set [str ] = None ):
35
53
markdown = Markdown ()
36
54
37
- appsdir : Path = outdir .joinpath ('apps' )
55
+ appsdir : Path = api_dir .joinpath ('apps' )
38
56
site_url = siteurl ()
39
57
40
58
def package_item (p_info : PackageInfo , in_apps_dir : bool , is_details : bool ) -> PackageInfo :
@@ -56,7 +74,7 @@ def package_item(p_info: PackageInfo, in_apps_dir: bool, is_details: bool) -> Pa
56
74
max_page = math .ceil (packages_length / ITEMS_PER_PAGE )
57
75
58
76
def save_page (page : int , items : [PackageInfo ]):
59
- json_file = appsdir .joinpath ('%d.json' % page ) if page > 1 else outdir .joinpath ('apps.json' )
77
+ json_file = appsdir .joinpath ('%d.json' % page ) if page > 1 else api_dir .joinpath ('apps.json' )
60
78
with ensure_open (json_file , 'w' , encoding = 'utf-8' ) as pf :
61
79
json .dump ({
62
80
'paging' : {
@@ -71,13 +89,16 @@ def save_page(page: int, items: [PackageInfo]):
71
89
chunks = more_itertools .chunked (packages , ITEMS_PER_PAGE ) if packages else [[]]
72
90
for index , chunk in enumerate (chunks ):
73
91
for item in chunk :
74
- app_dir = appsdir .joinpath (item ['id' ])
75
- releases_dir = app_dir .joinpath ('releases' )
76
- fix_manifest_url (item , app_dir )
92
+ api_app_dir = appsdir .joinpath (item ['id' ])
93
+ releases_dir = api_app_dir .joinpath ('releases' )
94
+ if host_packages and item ['id' ] in host_packages :
95
+ save_ipk (item , apps_dir , site_url )
96
+ fix_manifest_url (item , api_app_dir )
97
+ # This will be used by dev-manager-desktop
77
98
app_info = releases_dir .joinpath ('latest.json' )
78
99
with ensure_open (app_info , 'w' , encoding = 'utf-8' ) as f :
79
100
json .dump (package_item (item , True , True ), f )
80
- desc_html = app_dir .joinpath ('full_description.html' )
101
+ desc_html = api_app_dir .joinpath ('full_description.html' )
81
102
with ensure_open (desc_html , 'w' , encoding = 'utf-8' ) as f :
82
103
f .write (markdown .convert (item ['description' ]))
83
104
save_page (index + 1 , chunk )
0 commit comments