-
Notifications
You must be signed in to change notification settings - Fork 3
/
freezer.py
executable file
·86 lines (66 loc) · 2.53 KB
/
freezer.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
#!/usr/bin/env python3
import os.path
import subprocess
import json
import argparse
from contextlib import contextmanager
from packager import DEPENDENCIES_DIR, path_for_package
@contextmanager
def cd(path):
"""
Changes current directory to path then gets back to previous directory
on exit.
Similar to pushd/popd commands.
"""
old_dir = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_dir)
def dump_dict(to_dump):
""" Serializes a dictionary into a string. Currently uses JSON. """
return json.dumps(to_dump, indent=2)
def load_dict(string):
""" Loads a dictionary from its serialized version. """
return json.loads(string)
def load_versions_from_file(path):
with open(path) as f:
versions = load_dict(f.read())
for directory, version in versions.items():
dependency_path = path_for_package(directory)
if os.path.exists(dependency_path):
print("Checking out {0} at {1}".format(directory, version))
with cd(dependency_path):
git_cmd = "git checkout -f {0}".format(version)
subprocess.call(git_cmd.split())
def dump_versions_to_file(path):
versions = dict()
if not os.path.exists(DEPENDENCIES_DIR):
return
for directory in os.listdir(DEPENDENCIES_DIR):
with cd(os.path.join(DEPENDENCIES_DIR, directory)):
git_sha = subprocess.check_output("git rev-parse HEAD".split())
git_sha = git_sha.decode("ascii") # converts to str
git_sha = git_sha.rstrip() # removes trailing newline
versions[directory] = git_sha
with open(path, "w") as output:
output.write(dump_dict(versions))
def create_argument_parser():
""" Creates an argument parser with the correct arguments. """
descr = "Dumps the dependencies versions into a JSON file or loads them."
parser = argparse.ArgumentParser(description=descr)
parser.add_argument("-f", "--file",
default="versions.json",
help="Path to versions file (default: versions.json)")
parser.add_argument("-l", "--load",
dest="action", action="store_const",
const=load_versions_from_file,
default=dump_versions_to_file,
help="Load the version from file (default: dump versions to file")
return parser
def main():
args = create_argument_parser().parse_args()
args.action(args.file)
if __name__ == "__main__":
main()