-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchMacOS.py
More file actions
130 lines (103 loc) · 4.95 KB
/
fetchMacOS.py
File metadata and controls
130 lines (103 loc) · 4.95 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
#!/usr/bin/python
import logging
import plistlib
import os
import errno
import click
import requests
import sys
logging.basicConfig(format='%(asctime)-15s %(message)s', level=logging.INFO)
logger = logging.getLogger('webactivity')
class ClientMeta:
osinstall = {"User-Agent":"osinstallersetupplaind (unknown version) CFNetwork/720.5.7 Darwin/14.5.0 (x86_64)"}
swupdate = {"User-Agent":"Software%20Update (unknown version) CFNetwork/807.0.1 Darwin/16.0.0 (x86_64)"}
class Filesystem:
@staticmethod
def download_file(url, size, path):
label = url.split('/')[-1]
filename = os.path.join(path, label)
# Set to stream mode for large files
remote = requests.get(url, stream=True, headers=ClientMeta.osinstall)
with open(filename, 'wb') as f:
with click.progressbar(remote.iter_content(1024), length=size/1024, label="Fetching {} ...".format(filename)) as stream:
for data in stream:
f.write(data)
return filename
@staticmethod
def check_directory(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
@staticmethod
def fetch_plist(url):
logging.info("Network Request: %s", "Fetching {}".format(url))
plist_raw = requests.get(url, headers=ClientMeta.swupdate)
plist_data = plist_raw.text.encode('UTF-8')
return plist_data
@staticmethod
def parse_plist(catalog_data):
if sys.version_info > (3, 0):
root = plistlib.loads(catalog_data)
return root
class SoftwareService:
catalogs = {
"10.15": {
"CustomerSeed":"https://swscan.apple.com/content/catalogs/others/index-10.15customerseed-10.15-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog",
"DeveloperSeed":"https://swscan.apple.com/content/catalogs/others/index-10.15seed-10.15-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog",
"PublicSeed":"https://swscan.apple.com/content/catalogs/others/index-10.15beta-10.15-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog",
"PublicRelease":"https://swscan.apple.com/content/catalogs/others/index-10.15-10.14-10.13-10.12-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog"
}
}
def __init__(self, version, catalog_id):
self.version = version
self.catalog_url = self.catalogs.get(version).get(catalog_id)
self.catalog_data = ""
def getcatalog(self):
self.catalog_data = Filesystem.fetch_plist(self.catalog_url)
return self.catalog_data
def getosinstall(self):
# Load catalogs based on Py3/2 lib
root = Filesystem.parse_plist(self.catalog_data)
# Iterate to find valid OSInstall packages
ospackages = []
products = root['Products']
for product in products:
if products.get(product, {}).get('ExtendedMetaInfo', {}).get('InstallAssistantPackageIdentifiers', {}).get('OSInstall', {}) == 'com.apple.mpkg.OSInstall':
ospackages.append(product)
# Iterate for an specific version
candidates = []
for product in ospackages:
meta_url = products.get(product, {}).get('ServerMetadataURL', {})
if self.version in Filesystem.parse_plist(Filesystem.fetch_plist(meta_url)).get('CFBundleShortVersionString', {}):
candidates.append(product)
return candidates
class MacOSProduct:
def __init__(self, catalog, product_id):
root = Filesystem.parse_plist(catalog)
products = root['Products']
self.date = root['IndexDate']
self.product = products[product_id]
def fetchpackages(self, path, keyword=None):
Filesystem.check_directory(path)
packages = self.product['Packages']
if keyword:
for item in packages:
if keyword in item.get("URL"):
Filesystem.download_file(item.get("URL"), item.get("Size"), path)
else:
for item in packages:
Filesystem.download_file(item.get("URL"), item.get("Size"), path)
@click.command()
@click.option('-o', '--output-dir', default="~/images/", help="Target directory for package output.")
def fetchmacos(output_dir="~/images/", catalog_version="10.15", catalog_id="PublicRelease", product_id=""):
remote = SoftwareService(catalog_version, catalog_id)
catalog = remote.getcatalog()
product_id = remote.getosinstall()[0]
product = MacOSProduct(catalog, product_id)
logging.info("Selected macOS Product: {}".format(product_id))
# Download package to disk
product.fetchpackages(output_dir, keyword="BaseSystem")
if __name__ == "__main__":
fetchmacos()