This repository was archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathelm_self_publish.py
executable file
·115 lines (81 loc) · 3.18 KB
/
elm_self_publish.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
#! /usr/bin/env python
from __future__ import print_function
import sys
import json
import shutil
import os
import errno
import argparse
from collections import OrderedDict
def copy_package(location, destination, ignorer=None):
shutil.rmtree(destination, ignore_errors=True)
shutil.copytree(location, destination, ignore=ignorer)
def package_name(url):
""" get the package name from a github url """
project = url.split('/')[-1].split('.')[0]
user = url.split('/')[-2]
return {
"project": project,
"user": user
}
def make_elm_stuff_folder(path):
actual_path = '/'.join(path.split('/')[:-1])
try:
os.makedirs(actual_path)
except OSError as e:
if e.errno == errno.EEXIST and os.path.isdir(actual_path):
pass
else:
raise
def gitignores(path):
try:
with open(path) as f:
return [line.strip() for line in f]
except:
return []
def self_publish(package_location, destination=".", quiet=False):
""" package_location should be the local package to install
"""
elm_package_file = "{location}/elm-package.json".format(location=package_location)
destination_elm_package_file = "{destination}/elm-package.json".format(destination=destination)
exact_deps_file = "{destination}/elm-stuff/exact-dependencies.json".format(
destination=destination,
location=package_location
)
git_ignore_file = "{location}/.gitignore".format(location=package_location)
with open(elm_package_file) as f:
elm_package = json.load(f)
package_details = package_name(elm_package['repository'])
version = elm_package['version']
place = package_details['user'] + '/' + package_details['project']
copy_package(package_location, '{destination}/elm-stuff/packages/{place}/{version}'.format(
place=place,
version=version,
destination=destination
), ignorer=shutil.ignore_patterns(*gitignores(git_ignore_file)))
try:
with open(exact_deps_file) as f:
data = f.read()
package_info = {}
if data:
package_info = json.loads(data)
except IOError:
package_info = {}
make_elm_stuff_folder(exact_deps_file)
with open(exact_deps_file, 'w') as f:
package_info[place] = version
json.dump(package_info, f, sort_keys=False, indent=4)
with open(destination_elm_package_file) as f:
destination_elm_package = json.load(f, object_pairs_hook=OrderedDict)
with open(destination_elm_package_file, 'w') as f:
destination_elm_package['dependencies'][place] = "{version} <= v <= {version}".format(version=version)
json.dump(destination_elm_package, f, sort_keys=False, indent=4)
def main():
parser = argparse.ArgumentParser(description='Publish a local package into your project')
parser.add_argument('--quiet', '-q', action='store_true', help='don\'t print anything', default=False)
parser.add_argument('package_location')
parser.add_argument('destination')
args = parser.parse_args()
self_publish(args.package_location, args.destination, quiet=args.quiet)
if __name__ == '__main__':
main()