CORD-1467: Surgical cleanup for 3.0.1 to enable service development without attics

Change-Id: I1121c499f8120c416fa20ec41fa735276815776b
diff --git a/xos/genx/targets/service.xtarget b/xos/genx/targets/service.xtarget
index 87a0654..87f7413 100644
--- a/xos/genx/targets/service.xtarget
+++ b/xos/genx/targets/service.xtarget
@@ -1,4 +1,12 @@
+{% if options.legacy =='"True"' -%}
+{% set legacy_tag = '_decl' %}
+{% set legacy = True %}
+from core.models.xosbase import *
+{% else %}
+{% set legacy = False %}
+{% set legacy_tag = '' %}
 from header import *
+{% endif %}
 
 {% for m in proto.messages %}
 {% if file_exists(m.name|lower+'_header.py') -%}from {{m.name|lower }}_header import *{% endif %}
@@ -18,8 +26,7 @@
 {% endfor %}
 
 {% for m in proto.messages %}
-
-class {{ m.name }}{{ xproto_base_def(m.name, m.bases) }}:
+class {{ m.name }}{{ legacy_tag }}{{ xproto_base_def(m.name, m.bases) }}:
 
   KIND = {{ xproto_first_non_empty([m.options.kind, options.kind, options.name, "Set a kind in your xproto!"]) }}
 
@@ -37,12 +44,13 @@
 
   # Relations
   {% for l in m.links %}{% set peer_name=l.peer.name %}
-  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if peer_name==m.name -%}'self'{%- else -%}{{ peer_name }} {%- endif -%}, {{ xproto_django_options_str(l, l.dst_port ) }} )
+  {% if legacy and peer_name in proto.message_names %}{% set peer_tag = legacy_tag %}{% else %}{% set peer_tag = '' %}{% endif -%}
+  {{ l.src_port }} = {{ xproto_django_link_type(l) }}( {%- if peer_name==m.name -%}'self'{%- else -%}{{ peer_name }}{{ peer_tag }} {%- endif -%}, {{ xproto_django_options_str(l, l.dst_port ) }} )
   {%- endfor %}
 
   {% if file_exists(m.name|lower + '_model.py') -%}{{ include_file(m.name|lower + '_model.py') | indent(width=2)}}{%- endif %}
   pass
 
-{% if file_exists(m.name|lower+'_bottom.py') -%}{{ include_file(m.name|lower+'_bottom.py') }}{% endif %}
+{% if file_exists(m.name|lower+'_bottom.py') -%}{{ include_file(m.name|lower+'_bottom.py') }}{% endif %} 
 {% endfor %}
-+++ models.py
++++ models{{ legacy_tag }}.py
diff --git a/xos/genx/tool/Makefile b/xos/genx/tool/Makefile
index 8f667ee..e3b28ab 100644
--- a/xos/genx/tool/Makefile
+++ b/xos/genx/tool/Makefile
@@ -27,7 +27,7 @@
 
 # Rule to produce json dependencies
 %.ok: %.py
-	PYTHONPATH=$(PYTHONPATH):$(PREFIX)/tool python -m $(subst /,.,$(subst .py,,$<))
+	PYTHONPATH=$(PYTHONPATH):$(PREFIX)/tool python -m $(subst /,.,$(subst .py,,$<)) && touch $@
 
 # List of xprotos 
 xprotos = $(wildcard *.xproto)
diff --git a/xos/genx/tool/Makefile.service b/xos/genx/tool/Makefile.service
index 9fb96f4..6bdb823 100644
--- a/xos/genx/tool/Makefile.service
+++ b/xos/genx/tool/Makefile.service
@@ -7,9 +7,7 @@
 
 xprotos = $(wildcard *.xproto)
 
-all: models.py
-
-models.py: $(xprotos)
+all: $(xprotos)
 	$(XOSGEN) --attic attic --target $(DJANGO_TARGET) --output $@ $<
 
 .PHONY: all
diff --git a/xos/tools/apigen/modelgen b/xos/tools/apigen/modelgen
index 215feb9..b3ea1d6 100755
--- a/xos/tools/apigen/modelgen
+++ b/xos/tools/apigen/modelgen
@@ -232,8 +232,13 @@
                                         pass
 				else:
                                         f.type = f.__class__.__name__
+
                                         if (type(f)==ForeignKey):
-                                            f.related.model.class_name = f.related.model.__name__
+                                            if isinstance(f.related.model, str):
+                                                f.related.model = {'class_name':f.related.model}
+                                            else:
+                                                f.related.model.class_name = f.related.model.__name__
+
                                         if (f.name not in base_props):
                                             obj.fields.append(f)
                                         obj.all_fields.append(f)
diff --git a/xos/tools/corebuilder/corebuilder.py b/xos/tools/corebuilder/corebuilder.py
index ac99b01..44a16b0 100644
--- a/xos/tools/corebuilder/corebuilder.py
+++ b/xos/tools/corebuilder/corebuilder.py
@@ -278,7 +278,7 @@
                 if (not os.path.isdir(build_dest_fn)):
                     shutil.copytree(src_fn, build_dest_fn, symlinks=True)
                 else:
-                    os.system('cp -R %s/*.xproto %s/attic %s/*header.py %s'%(src_fn, src_fn, src_fn, build_dest_fn))
+                    os.system('cp -R %(src_fn)s/*.xproto %(src_fn)s/attic %(src_fn)s/models.py %(src_fn)s/*header.py %(build_dst_fn)s 2> /dev/null || :'%{'src_fn':src_fn, 'build_dst_fn':build_dest_fn})
             else:
                 shutil.copyfile(src_fn, build_dest_fn)