10 def eprint(text
: str):
11 print(text
, file=sys
.stderr
)
14 if not os
.path
.exists("dub.selections.json"):
15 eprint("The file `dub.selections.json` does not exist in the current working directory")
16 eprint("run `dub upgrade` to generate it")
19 with
open("dub.selections.json") as f
:
20 selectionsJson
= json
.load(f
)
22 depsDict
: dict = selectionsJson
["versions"]
24 # For each dependency expand non-expanded version into a dict with a "version" key
25 depsDict
= {pname
: (versionOrDepDict
if isinstance(versionOrDepDict
, dict) else {"version": versionOrDepDict
}) for (pname
, versionOrDepDict
) in depsDict
.items()}
27 # Don't process path-type selections
28 depsDict
= {pname
: depDict
for (pname
, depDict
) in depsDict
.items() if "path" not in depDict
}
30 # Pre-validate selections before trying to fetch
31 for pname
in depsDict
:
32 depDict
= depsDict
[pname
]
33 version
= depDict
["version"]
34 if version
.startswith("~"):
35 eprint(f
'Expected version of "{pname}" to be non-branch type')
36 eprint(f
'Found: "{version}"')
37 eprint("Please specify a non-branch version inside `dub.selections.json`")
38 eprint("When packaging, you might also need to patch the version value in the appropriate places (`dub.selections.json`, dub.sdl`, `dub.json`)")
40 if "repository" in depDict
:
41 repository
= depDict
["repository"]
42 if not repository
.startswith("git+"):
43 eprint(f
'Expected repository field of "{pname}" to begin with "git+"')
44 eprint(f
'Found: "{repository}"')
46 if (len(version
) < 7 or len(version
) > 40 or not all(c
in string
.hexdigits
for c
in version
)):
47 eprint(f
'Expected version field of "{pname}" to begin be a valid git revision')
48 eprint(f
'Found: "{version}"')
51 lockedDepsDict
: dict[str, dict[str, str]] = {}
53 for pname
in depsDict
:
54 depDict
= depsDict
[pname
]
55 version
= depDict
["version"]
56 if "repository" in depDict
:
57 repository
= depDict
["repository"]
58 strippedRepo
= repository
[4:]
59 eprint(f
"Fetching {pname}@{version} ({strippedRepo})")
60 command
= ["nix-prefetch-git", strippedRepo
, version
]
61 rawRes
= subprocess
.run(command
, check
=True, text
=True, stdout
=subprocess
.PIPE
, stderr
=subprocess
.DEVNULL
).stdout
62 sha256
= json
.loads(rawRes
)["sha256"]
63 lockedDepsDict
[pname
] = {"version": version
, "repository": repository
, "sha256": sha256
}
65 eprint(f
"Fetching {pname}@{version}")
66 url
= f
"https://code.dlang.org/packages/{pname}/{version}.zip"
67 command
= ["nix-prefetch-url", "--type", "sha256", url
]
68 sha256
= subprocess
.run(command
, check
=True, text
=True, stdout
=subprocess
.PIPE
, stderr
=subprocess
.DEVNULL
).stdout
.rstrip()
69 lockedDepsDict
[pname
] = {"version": version
, "sha256": sha256
}
71 print(json
.dumps({"dependencies": lockedDepsDict
}, indent
=2))