summaryrefslogtreecommitdiff
path: root/scripts/migrate-chunks
blob: e9351037ab7a1dc7c951680bc3842d30eb68a53d (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python

import json
import morphlib
import os
import subprocess
import sys
import urllib
import urllib2
import urlparse
import yaml
import re

verbose = False
if '--verbose' in sys.argv or '-v' in sys.argv:
    verbose = True

# load all morphologies in the definitions repo
sb = morphlib.sysbranchdir.open_from_within('.')
loader = morphlib.morphloader.MorphologyLoader()
morphs = [m for m in sb.load_all_morphologies(loader)]
print 'Found %d morphologies in total' % len(morphs)


# Harvest all the morphologies on the directory
morphologies = { 'chunk': '', 'stratum': '', 'system' : '', 'cluster': '' } 

for key in morphologies.iterkeys():
    morphologies[key] = [m for m in morphs if m['kind'] == key]
    #print 'There are: %d %s' %(len(morphologies[key]), key)

# look for a chunk morph in the repo
# NOTE: The following reimplements part of morphlib's remote repo cache stuff
def parse_repo_alias(repo):
    if verbose:
        print 'Parsing repo-alias %s' % repo
    domain, path = repo.split(':')
    if domain == 'baserock':
        repo = 'ssh://git@git.baserock.org/baserock/%s' % path
    elif domain == 'upstream':
        repo = 'ssh://git@git.baserock.org/delta/%s' % path
    else:
        raise Exception("I don't know how to parse the repo-alias \"%s\"" % repo)
    return repo

def make_request(path):
    server_url = 'http://git.baserock.org:8080/'
    url = urlparse.urljoin(server_url, '/1.0/%s' % path)
    handle = urllib2.urlopen(url)
    return handle.read()

def quote(*args):
    return tuple(urllib.quote(string) for string in args)

def cat_file(repo, ref, filename):
    return make_request('files?repo=%s&ref=%s&filename=%s' %
                        quote(repo, ref, filename))

# organise the definitions repo
definitions_repo = sb.get_git_directory_name(sb.root_repository_url)
def move_morphs(morphs, kind):
    if kind == 'cluster':
         print 'Moving %s into subdirectory' % kind
         subdir = os.path.join(definitions_repo, 'clusters')
         subprocess.call(['mkdir', '-p', subdir])
         for morph in morphs:
             for m in morph['systems']:
	         m['morph'] = 'systems/' + m['name'] + '.morph'
	     loader.save_to_file(morph.filename, morph)
             new_location = os.path.join(subdir, morph.filename)
             #subprocess.call(['git', 'mv', morph.filename, new_location])
             #morph.filename = new_location
             #subprocess.call(['git', 'commit', '--quiet', '-m',
             #                 'Move %s into subdirectory' %kind])
    if kind == 'system':
         print 'Moving %s into subdirectory' % kind
         subdir = os.path.join(definitions_repo, 'systems')
         subprocess.call(['mkdir', '-p', subdir])
         for morph in morphs:
	     for stratum in morph['strata']:
	         stratum['morph'] = 'strata/' + stratum['name'] + '.morph'
	     loader.save_to_file(morph.filename,morph)
             new_location = os.path.join(subdir, morph.filename)
             #subprocess.call(['git', 'mv', morph.filename, new_location])
             #morph.filename = new_location
             #subprocess.call(['git', 'commit', '--quiet', '-m',
             #                 'Move %s into subdirectory' %kind])
    if kind == 'stratum':
        print 'Moving %s into subdirectory' % kind
        for morph in morphs:
	    # Add chunk path on the chunk's morph field per stratum
	    stratum_path = 'strata/' + morph['name']
            subdir = os.path.join(definitions_repo, stratum_path)
            subprocess.call(['mkdir', '-p', subdir])

	    # Download chunks morphologies defined on the stratum and
	    # add them to the directory tree.
            for chunk in morph['chunks']:
                name = morphlib.util.sanitise_morphology_path(chunk['morph'])
                chunk['morph'] = stratum_path + '/' \
	    		     + morphlib.util.sanitise_morphology_path(chunk['morph'])
                ref = chunk['ref']
                repo = parse_repo_alias(chunk['repo'])
                print "%s %s %s" %(name, ref, repo)
	        try:
                    chunk_morph = cat_file(repo, ref, name)
                    new_chunk = loader.load_from_string(chunk_morph)
                    loader.save_to_file(chunk['morph'], new_chunk)
                    print "Downloading %s from %s and placing in %s" %(name, repo, chunk['morph'])
	        except urllib2.HTTPError as err:
		    # If there si no morphology in the repository we assume that the morphology
		    # system will be autodetected, so we don't have to create a new one
		    # unless we shut down the autodetecting system (fallback system).
	    	    if err.code == 404:
                        print "%s not found in %s" %(name, repo)
		except morphlib.morphloader.InvalidFieldError as err:
                    print "ERROR: %s in chunk %s" % (err, chunk_morph)
		    if "comments" in err:
		        fixed_chunk = str(chunk_morph).replace("comments", "description")
			m_fixed_chunk = loader.load_from_string(chunk['morph'],fixed_chunk)
			loader.save_to_file(chunk['morph'], m_fixed_chunk)
                        print "Downloading %s from %s and placing in %s" %(name, repo, chunk['morph'])
		except morphlib.morphloader.MorphologyNotYamlError as err:
                    print "ERROR: %s in chunk %s" % (err, chunk_morph)
                    new_chunk = json.loads(chunk_morph)
		    print "%s" % new_chunk
		    sys.exit(0)
                    loader.save_to_file(chunk['morph'], new_chunk)
                    print "Downloading %s from %s and placing in %s" %(name, repo, chunk['morph'])

            # Add path to the build-depends morphologies
            for build_depends in morph['build-depends']:
                build_depends['morph'] = 'strata/' + build_depends['morph'] + '.morph'
	    loader.save_to_file(morph.filename,morph)
            new_location = os.path.join(subdir, morph.filename)
            #subprocess.call(['git', 'mv', morph.filename, new_location])
            #morph.filename = new_location
            #subprocess.call(['git', 'commit', '--quiet', '-m',
            #                 'Move %s into subdirectory' %kind])
	 
# Move the morphologies to it directory
for key in morphologies.iterkeys():
    print "Moving %s....\n" %key
    if key == 'stratum': move_morphs(morphologies[key], key)

#move_morphs(chunks, 'chunks')
#move_morphs(strata, 'strata')
#move_morphs(systems, 'systems')
#move_morphs(clusters, 'clusters')
sys.exit(0)

#def ls_tree(repo, ref):
#    info = yaml.load(make_request('trees?repo=%s&ref=%s' % quote(repo, ref)))
#    return info['tree'].keys()
#
## get a list of ALL the chunks
#chunk_map = {}
#for stratum in strata:
#    for spec in stratum['chunks']:
#        if spec['name'] not in chunk_map:
#            chunk_map[spec['name']] = (spec['repo'], spec['ref'])
#
#
#new_chunks = []
#i = 0
#n = len(chunk_map)
#for name, (repo, ref) in chunk_map.iteritems():
#    i += 1
#    if verbose:
#        print '[%d/%d] Checking chunk: %s' % (i, n, name)
#    repo = parse_repo_alias(repo)
#    morph = morphlib.util.sanitise_morphology_path(name)
#    if morph in ls_tree(repo, ref):
#        print '[%d/%d] Getting chunk morph: %s' % (i, n, name)
#        text = cat_file(repo, ref, morph)
#        # convert the morphology to yaml, because we should be using yaml now
#        try:
#            morph_dict = json.loads(text)
#        except ValueError:
#            morph_dict = yaml.load(text)
#        output = yaml.dump(morph_dict,
#                           Dumper=morphlib.morphloader.OrderedDumper,
#                           default_flow_style=False)
#        path = os.path.join(definitions_repo, morph)
#        with open(path, 'w') as f:
#            f.write(output)
#        new_chunks.append(name)
#
## move the new chunks
#for chunk in new_chunks:
#    morph = morphlib.util.sanitise_morphology_path(chunk)
#    path = os.path.join(definitions_repo, os.path.join('chunks', morph))
#    subprocess.call(['mv', morph, path])
#    #subprocess.call(['git', 'add', path])
##subprocess.call(['git', 'commit', '-m', 'Add chunk morphologies'])
#
## update the chunk specs in the strata
#for stratum in strata:
#    for spec in stratum['chunks']:
#        if 'morph' in spec:
#            del spec['morph']
#        if spec['name'] in new_chunks:
#            morph = morphlib.util.sanitise_morphology_path(spec['name'])
#            spec['morph'] = os.path.join('chunks', morph)
#    loader.save_to_file(stratum.filename, stratum)