-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.py
182 lines (155 loc) · 5.68 KB
/
import.py
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
import hashlib
import requests
import pyreadr
import pandas as pd
import os
import json
from packaging.version import parse
import pickle
import email.utils, datetime
print("Fetching package versions")
stream = os.popen("singularity run /opt/spack.sif list --format version_json")
builtin = stream.read()
stream.close()
builtin = json.loads(builtin)
print("Versions successfully fetched!\n")
packageVersions = {}
locations = {}
for i in builtin:
packageVersions[i["name"]] = i["versions"]
locations[i["name"]] = i["file"]
def importPackage(package, version, type):
if type == "":
version = "latest"
type = "latest"
urlbool = True
packman = "pypi"
url = f"https://pypi.org/pypi/{package}/json"
r = requests.get(url)
record = r.json()
if record["message"] == "Not Found":
raise Exception(f"Package {'r-' + package.lower().replace('.','-')} not found in database")
if version == "latest" or version == "":
version = record["info"]["version"]
name, description = record["info"]["name"], record["info"]["summary"]
def writeDeps(field):
if record["info"][field] == None:
return []
elif pd.isna(record["info"][field]):
return []
newList = [
dep.replace(" ", "")
for dep in record["info"][field]
]
return newList
dependencies = f'Python ({record["info"]["requires_python"]})'
dependencies += writeDeps("requires_dist")
try:
packageURL = record["info"]["home_page"]
except:
urlbool = False
versions = []
versions.append(f""" version("{record['Version']}", sha256="{sha256_hash_latest.hexdigest()}")\n""")
if type != "latest" and type != "any":
for i in record["releases"][version]:
if i["python_version"] == "source":
pkg = i
versions.append(f""" version("{version}", sha256="{pkg["digests"]["sha256"]}")\n""")
if "py-" + package.lower().replace(".","-") in packageVersions.keys():
if os.path.isdir("packages/py-" + package.lower().replace(".","-")) or type == "any":
print(f"package {'py-' + package.lower().replace('.','-')} already exists")
return(f"Package {'py-' + package.lower().replace('.','-')} already exists")
else:
if "at least" in type:
verNum = type.split("at least ")[1]
if parse(verNum) <= parse(packageVersions["py-" + package.lower().replace(".","-")][0]):
print(f"package {'py-' + package.lower().replace('.','-')} already exists")
return(f"Package {'py-' + package.lower().replace('.','-')} already exists")
elif version == "latest":
if record["info"]["version"] in packageVersions["py-" + package.lower().replace(".","-")]:
print(f"package {'py-' + package.lower().replace('.','-')} already exists")
return(f"Package {'py-' + package.lower().replace('.','-')} already exists")
else:
if type in packageVersions["py-" + package.lower().replace(".","-")]:
print(f"package {'py-' + package.lower().replace('.','-')} already exists")
return(f"Package {'py-' + package.lower().replace('.','-')} already exists")
for i in getVersions(package):
versions.append(str(i).replace(" ", "\t") + "\n")
os.makedirs("packages/r-" + package.lower().replace(".","-"))
f = open("packages/r-" + package.lower().replace(".","-") + "/package.py", "w")
dependencylist = []
variants = []
for k in dependencies:
try:
dependencylist.append("\tdepends_on(\"" + packageName(k)[0] + "\", type=(\"build\", \"run\"))\n")
except:
continue
classname = package.split(".")
for i in range(len(classname)):
classname[i] = classname[i].capitalize()
classname = "".join(classname)
backslashN = "\n\t"
f.write(f"""# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
from spack.package import *
class {classname}(PythonPackage):
\"\"\"{name}
{description}
\"\"\"
{f'homepage = "{packageURL}"{backslashN}' if urlbool and str(packageURL) != "nan" else ''}{packman} = "{package}"
{"".join(versions)}
{"".join(dependencylist)}
{"".join(variants)}
""")
f.close()
print(f"Package {'r-' + package.lower().replace('.','-')} has been created!")
def packageName(k):
if "(" in k:
version = k.split("(")
k = version[0]
version = version[1].lower().replace(")","")
getversion = ""
if ">=" in version:
fullname = k.replace(".","-")
version
getversion = f"@{version[2:]}:"
type = "at least " + version[2:]
elif "<=" in version:
fullname = k.replace(".","-")
getversion = f"@:{version[2:]}"
type = version[2:]
else:
fullname = k.replace(".","-")
getversion = f"@={version[2:]}"
type = version[2:]
else:
fullname = k.replace(".","-")
getversion = ""
type = "any"
if fullname.lower() == "r":
return "r", getversion, type, False
try:
importPackage(k, getversion, type)
except:
raise Exception(f"Package {'r-' + k.lower().replace('.','-')} not found in database")
# print(fullname.lower(), getversion, type)
return "r-" + fullname.lower(), getversion, type, False
def getVersions(package):
versions = []
if "https://" in locations["r-" + package.lower().replace(".","-")]:
location = locations["r-" + package.lower().replace(".","-")].replace("https://github.com","https://raw.githubusercontent.com").replace("blob/", "")
recipe = requests.get(location, allow_redirects=True)
pyfile = str(recipe.content)[2:].split("\\n")
else:
recipe = open(locations["r-" + package.lower().replace(".","-")], "r")
pyfile = str(recipe.content)[2:].split("\\n")
pyfile = recipe.readlines()
for i in pyfile:
if "version(" in i:
versions.append(i)
return(versions)
package = str(input("Please enter package name: "))
version = str(input("Please enter package version (leave blank for latest version): "))
importPackage(package, version, version)